Operators And Assignments MCQ Questions
21. What is the output of relational operators?
A. Integer
B. Boolean
C. Characters
D. Double
View Answer
Ans : B
Explanation: None.
22. Which of these is returned by "greater than", "less than" and "equal to" operators?
A. Integers
B. Floating - point numbers
C. Boolean
D. None of the mentioned
View Answer
Ans : C
Explanation: All relational operators return a boolean value ie. true and false.
23. Which of the following operators can operate on a boolean variable?
A. &&
B. ==
C. ?:
D. +=
View Answer
Ans : D
Explanation: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values.
24. Which of these operators can skip evaluating right hand operand?
A. !
B. |
C. &
D. &&
View Answer
Ans : D
Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone.
25. Which of these statements is correct?
A. true and false are numeric values 1 and 0
B. true and false are numeric values 0 and 1
C. true is any non zero value and false is 0
D. true and false are non numeric values
View Answer
Ans : D
Explanation: True and false are keywords, they are non numeric values which do not relate to zero or non zero numbers. true and false are boolean values.
26. What is the output of this program?
class Relational_operator
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
System.out.print(var1 > var2);
}
}
A. 1
B. 0
C. TRUE
D. FALSE
View Answer
Ans : D
Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned.
output:
$ javac Relational_operator.java
$ java Relational_operator
false
27. What is the output of this program?
class ternary_operator
{
public static void main(String args[])
{
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
System.out.print(z);
}
}
A. 0
B. 1
C. 3
D. -4
View Answer
Ans : C
Explanation: None.
output:
$ javac ternary_operator.java
$ java ternary_operator
3
28. What is the output of this program?
class Output
{
public static void main(String args[])
{
int x , y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
A. 1
B. 2
C. Runtime error owing to division by zero in if condition
D. Unpredictable behavior of program
View Answer
Ans : B
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2
29. What is the output of this program?
class Output
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
boolean c = a ^ b;
System.out.println(!c);
}
}
A. 0
B. 1
C. FALSE
D. TRUE
View Answer
Ans : C
Explanation: None.
output:
$ javac Output.java
$ java Output
false
30. Which of these have highest precedence?
A. ()
B. ++
C. *
D. >>
View Answer
Ans : A
Explanation: Order of precedence is (highest to lowest) a -> b -> c -> d.
Also check :
Discussion