Test Series - java

Test Number 15/64

Q: An Arithmetic expression in Java involves which Operators or Operations?
A.  Addition (+), Subtraction (-)
B. Multiplication (*), Division (/)
C. Modulo Division (%), Increment/Decrement (++/--), Unary Minus (-), Unary Plus (+)
D. All the above
Solution: no solution
Q: Choose the Compound Assignment Arithmetic Operators in Java below.
A. +=, -=
B. *=, /=
C. %=
D. All the above
Solution: no solution
Q: What is the output of the below Java code snippet?
int a = 2 - - 7;
System.out.println(a);
A. -5
B. 10
C. 9
D. Compiler Error
Solution: Minus of Minus is Plus. So 2 - - 7 becomes 2+7.
Q: What is the output of Java code snippet below?
short p = 1;
short k = p + 2;
System.out.println(k);
A. 1
B. 2
C. 3
D. Compiler error
Solution: Numbers are treated as int type by default. So an int value cannot be assigned to a short variable. You have to type cast the whole expression.


short k = (short)(p + 2);
Q: What is the output of Java code snippet?
short k=1;
k += 2;
System.out.println(k);
A. 1
B. 2
C. 3
D. Compiler error about Type Casting
Solution: Compound assignment operators automatically convert the expression value to the left-hand side data type.


k = k + 1; //Error
k += 1; //Works
k++; //Works
Q: What is the output of the Java code snippet?
int a=5, b=10, c=15;
a -= 3;
b *= 2;
c /= 5;
System.out.println(a +" " + b + " " + c);
A. 2 20 3
B. 2 20 5
C. 2 10 5
D. -2 20 3
Solution: a = a - 3;
b = b*2;
c = c/5;
Q: How do you rewrite the below Java code snippet?
int p=10;
p = p%3;
A. p=%3;
B. p%=3;
C. p=3%;
D. None of the above
Solution: no solution
Q: Which is the arithmetic operator in Java that gives the Remainder of Division?
A.  /
B. @
C. %
D. &
Solution: //Modulo Division operator
// or simply Modulus Operator
int a = 14%5;
//a holds 4

5)14(2
 -10
------
   4
Q: Arithmetic operators +, -, /, *  and % have which Associativity?
A. Right to Left
B. Left to Right
C. Right to Right
D. Left to Left
Solution: no solution
Q: Between Postfix and Prefix arithmetic operators in Java, which operators have more priority?
A. Postfix operators have more priority than Prefix operators
B. Prefix operators have more priority than Postfix operators
C. Both Prefix and Postfix operators have equal priority
D. None of the above
Solution: op++, op-- have more priority than --op, ++op.

You Have Score    /10