Test Series - java

Test Number 16/64

Q: Among Postfix Decrement and Prefix Increment operators in Java, which operator has less priority?
A. Postfix Decrement has less priority than Prefix Increment
B. Prefix Increment has less priority than Postfix Decrement
C. Both operators have same priority
D. None of the above
Solution: a++ > ++b
Q: Increment and Decrement arithmetic operators in Java has which Associativity?
A. Left to Right
B. Right to Left
C. Left to Left
D. Right to Right
Solution: no solution
Q: Choose the correct statement about Java Operators +, -, *, / and %.
A. + and - have equal priority
B. * and / have equal priority
C. / and % have equal priority
D. All the above
Solution: All are having equal priority.
Q: Among the operator groups (++, --) and (+, -, *, /, %) in Java, which group has higher priority? 
A. (++, --) group has higher priority than (+, -, *, /, %) group
B. (++, --) group has lower priority than (+, -, *, /, %) group
C. (++, --) group and (+, -, *, /, %) group have equal priority
D.  None of the above
Solution: Again between Prefix and Post operators, Postfix operators have higher priority.
Q: What is the output of the Java code snippet?
int a=10, b=6;
int c = a+b*5;
System.out.println(c);
A. 40
B. 50
C. 80
D. Compiler error
Solution: * has higher priority than +. So, Multiplication operation is performed first.


a+(b*5)
10 + (6*5)
10 + 30
40
Q: What is the output of the Java code snippet?
int a=10, b=5, c=3;
int d = a+c/2*b;
System.out.println(d);
A. 17.5
B. 32.5
C. 15
D. 30
Solution: / and * have equal priority. So associativity of Left to Right is used. Remember that 3/2 is 1 not 1.5 as both operands are integers.


a+c/2*b
a+(c/2*b)
a + ( (c/2) * b)
a + ( 3/2 * b)
a + ( 1 * 5)
10 + 5
15
Q: What is the output of the Java code snippet?
int a=5, b=6;
if(a++ == --b)
{
  System.out.println("5=5");
}
else
{
  System.out.println("NONE");
}
A. NONE
B. 5=5
C. Compiler error
D. None of the above
Solution: At time of evaluating a++ == --b, a(5)is compared with --b(6-1). So, "if" condition passes. If you check a value after the ELSE block, it will be a+1 i.e 6.
Q: What is the output of the Java code snippet?
int a=6, b=5;
if(++b == a--)
{
  System.out.println("RABBIT");
}
else
{
  System.out.println("BUNNY");
}
A. RABBIT
B. BUNNY
C. Compiler error
D. None of the above
Solution: After the ELSE block, b will be b+1 i.e 6


++b == a--
++b == (a-1)
b == (a-1)
5 ==5
Q: What is the output of the Java code snippet?
int a=10, b=20;
int c = a++*2;
int d = --b*2;
System.out.println(c +"," + d);
A. 20,40
B. 22,40
C. 20,38
D. 22,38
Solution: The prefix is incremented or decremented immediately. Postfix incremented or decremented on the next line/statement.


1)
a++*2
a*2

2)
--b*2
(b-1)*2
Q: Choose the correct statement about Java Prefix and Postfix operations.
A. Prefix operation is carried out immediately and the variable value will be incremented or decremented accordingly
B. Postfix operation is carried out on the next line or statement. So the variable value will not change.
C. A and B
D. None of the above
Solution: no solution

You Have Score    /10