Understanding Java Increment and Decrement Operators - Quiz

Total: 4 questions

1. 

What is the difference between prefix (++a) and postfix (a++) increment?

The prefix form ++a increments the variable first, then returns the new value. The postfix form a++ returns the current value first, then increments. The difference only matters when the operation is part of another expression.

2. 

What will b hold: int a = 1; int b = a++;?

b becomes 1 and a becomes 2. Postfix: the current value of a is assigned to b first, then a is incremented. With the prefix form ++a, both would become 2.

3. 

What happens when you increment a char variable?

The increment is applied to the character's Unicode code, giving the next character. For example, for char ch = X, after ch++ the variable holds Y.

4. 

Can you apply the increment operator to a final variable?

No. A final variable cannot be changed after initialization, so incrementing it causes a compilation error.

Page 1 of 1