Java Questions & Answers – Assignment Operators and Operator Precedence

This section of our 1000+ Java MCQs focuses on assignment operators and operator precedence in Java Programming Language.

1. Which of these have highest precedence?
a) ()
b) ++
c) *
d) >>
View Answer

Answer: a
Explanation: Order of precedence is (highest to lowest) a -> b -> c -> d.

2. What should be expression1 evaluate to in using ternary operator as in this line?

 expression1 ?  expression2  :  expression3

a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned
View Answer

Answer: c
Explanation: The controlling condition of ternary operator must evaluate to boolean.
advertisement
advertisement

3. What is the value stored in x in the following lines of Java code?

   int x, y, z;
    x = 0;
    y = 1;
    x = y = z = 8;

a) 0
b) 1
c) 9
d) 8
View Answer

Answer: d
Explanation: None.
advertisement

4. What is the order of precedence (highest to lowest) of following operators?

    1. &    
    2. ^
    3. ?:

a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1
View Answer

Answer: a
Explanation: None.
advertisement

5. Which of these statements are incorrect?
a) Equal to operator has least precedence
b) Brackets () have highest precedence
c) Division operator, /, has higher precedence than multiplication operator
d) Addition operator, +, and subtraction operator have equal precedence
View Answer

Answer: c
Explanation: Division operator, /, has equal precedence as of multiplication operator. In expression involving multiplication and division evaluation of expression will begin from the right side when no brackets are used.

6. What will be the output of the following Java code?

  1.     class operators 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int var1 = 5; 
  6.             int var2 = 6;
  7.             int var3;
  8.             var3 = ++ var2 * var1 / var2 + var2;
  9.             System.out.print(var3);
  10.         } 
  11.     }

a) 10
b) 11
c) 12
d) 56
View Answer

Answer: c
Explanation: Operator ++ has the highest precedence than / , * and +. var2 is incremented to 7 and then used in expression, var3 = 7 * 5 / 7 + 7, gives 12.
output:

$ javac operators.java
$ java operators
12

7. What will be the output of the following Java code?

  1.     class operators 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x = 8;
  6.              System.out.println(++x * 3 + " " + x);
  7.         } 
  8.     }

a) 24 8
b) 24 9
c) 27 8
d) 27 9
View Answer

Answer: d
Explanation: Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 than multiplied with 3 giving 27.
output:

$ javac operators.java
$ java operators
27 9

8. What will be the output of the following Java code?

  1. class Output 
  2. {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x=y=z=20;
  6.  
  7.         } 
  8. }

a) compile and runs fine
b) 20
c) run time error
d) compile time error
View Answer

Answer: d
Explanation: None.

9. Which of these lines of Java code will give better performance?

   1. a | 4 + c >> b & 7; 
   2. (a | ((( 4 * c ) >> b ) & 7 ))

a) 1 will give better performance as it has no parentheses
b) 2 will give better performance as it has parentheses
c) Both 1 & 2 will give equal performance
d) Dependent on the computer system
View Answer

Answer: c
Explanation: Parentheses do not degrade the performance of the program. Adding parentheses to reduce ambiguity does not negatively affect your system.

10. What will be the output of the following Java program?

  1. class Output 
  2. {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a,b,c,d;
  6.              a=b=c=d=20;
  7.             a+=b-=c*=d/=20;
  8.            System.out.println(a+" "+b+" "+c+" "+d);
  9.  
  10.         } 
  11. }

a) compile time error
b) runtime error
c) a=20 b=0 c=20 d=1
d) none of the mentioned
View Answer

Answer: c
Explanation: Expression will evaluate from right to left.
output:

$ javac Output.java
$ java Output
20 0 20 1

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.