Test Series - java

Test Number 22/64

Q: What is the output of the Java code snippet?
int a=25, b=30;
boolean c = a>25 | b<40;
if(c)
{
  System.out.println("RABBIT");
}
else
{
  System.out.println("GOOSE");
}
A. RABBIT
B. GOOSE
C. Compiler error
D. None of the above
Solution: false | true is true only.
Q: What is the output of the Java code snippet?
int a=25, b=30;
boolean c = a>25 & b<40;
if(c)
{
  System.out.println("RABBIT");
}
else
{
  System.out.println("GOOSE");
}
A. RABBIT
B. GOOSE
C. Compiler error
D. None of the above
Solution: false & true is false only.
Q: What is the output of the Java code snippet?
int a=3, b=8;
boolean c = a>5 && ++b>6;
System.out.println(b);
A. 8
B. 9
C. 6
D. Compiler error
Solution: ++b>6 is not evaluated as the first operand itself is false. Short Circuit AND operator skips the second expression.
Q: What is the output of the Java code snippet?
int a=5, b=9;
boolean c = a>1 || b++<10;
System.out.println(b);
A. 9
B. 10
C. 8
D. Compiler error
Solution: b++<10 is not evaluated by the Short Circuit OR operator as the first operand is already true. There is no need to check the second expression.
Q: What is the output of the Java code snippet?
int a=4, b=6, c=8;
boolean d = a>5 && b>5 & c++<10;
System.out.println(c);
A. 8
B. 9
C. 10
D. Compiler error
Solution: Grouping or association of operands is done first based on the priority.


a>5 && (b>5 & c++<10)
false && (anything)
false
Q: What is the output of the Java code snippet?
int a=4, b=8;
boolean c = a>1 ^ b<10;
if(c)
{
  System.out.println("TREE");
}
else
{
  System.out.println("BIRD");
}
A. TREE
B. BIRD
C. Compiler error
D. None of the above
Solution: The exclusive operator (^) gives an output of TRUE only if both the operands are different.
Q: What is the output of the Java code snippet?
int a=5;
boolean b = a>1 || false;
b ^= false;
System.out.println(b);
A. false
B. true
C. Compiler error
D. None of the above
Solution: b ^= false;
b = b^false;
b = true ^ false;
b = true;
Q: What is the output of the Java code snippet?
int a=4, b=8;
boolean c = a>2 ^ b<10 & false;
System.out.println(c);
A. false
B. true
C. Compiler error
D. None of the above
Solution: AND has a higher priority than Exclusive OR.


a>2 ^ b<10 & false
a>2 ^ (b<10 & false)
a>2 ^ (true & false)
a>2 ^ (false)
true ^ false
true
Q: What is the output of the Java code snippet?
int a=3, b=1;
int c = a & b;
System.out.println("CAT " + c);
A. CAT true
B. CAT 1
C. Compiler error
D. None of the above
Solution: Here AND & operator is used with numbers. So, it is a Bitwise operator, not a Logical operator.
Q: If an AND (&) operator is applied with two integers, what is this operator?
A. Boolean operator
B. Bitwise operator
C. Logical operator
D. Arithmetic operator
Solution: no solution

You Have Score    /10