Assignment Operators QuizS2C Home « Assignment Operators Quiz

Fundamentals Quiz 8

The quiz below tests your knowledge of the material learnt in Fundamentals - 9 - Assignment Operators.

Question 1 : What will be output from the following code?

boolean a = false;
boolean b = false;
if (a &= b) {System.out.println("a: " + a + " b: " + b);}
-The <code>&=</code> assignment operator checks both operands for <code>true</code> values and assigns <code>true</code> or <code>false</code> to the first operand dependant upon the outcome of the expression, so the contents of the <code>if</code> construct will never be executed and hence nothing is output.
Question 2 : Which of the following assignment operators is invalid?
- code>$=</code> is NOT a valid assignment operator.
Question 3 : Which of the following is an invalid automatic type conversion?
- code>byte = int</code> is NOT a valid automatic type conversion.
Question 4 : We can convert a boolean to an int using a cast?
- The boolean type is not only incompatible but also inconvertible with other types.
Question 5 : What will be output from the following code?

boolean a = true;
boolean b = true;
if (a &= b) {System.out.println("a: " + a + " b: " + b);}
-The <code>&=</code> assignment operator checks both operands for <code>true</code> values and assigns <code>true</code> or <code>false</code> to the first operand dependant upon the outcome of the expression, so the contents of the <code>if</code> construct will be executed as both are true and <code>a: true b: true</code> will be output.
Question 6 : Which code will work?
- C is the correct answer. We don't have to use the same type for the cast as long as the cast is not widening. We will just get back the first 8 bits of the value.
Quiz Progress Bar Please select an answer


What's Next?

The next quiz on Java is all about Bitwise Logical Operators.