What is the result?

Given the code fragment:

int a = 0;
a++;
System.out.println(a++);
System.out.println(a);

What is the result?

Given the code fragment:

int a = 0;
a++;
System.out.println(a++);
System.out.println(a);

What is the result?

A.
1
2

B.
2
2

C.
12

D.
1
1

Explanation:
The first println prints variable a with value 1 and then increases the variable to 2.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Abed Abu Alhalawa

Abed Abu Alhalawa

A

James

James

The Answer is A.

The postfix operation (a++) is executed immediately after the variable a is used in a statement. The prefix operation (a++) is executed immediate before the variable a is used in a statement.