Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

# Pre and Post Increament Practice Problems

## 1. What is the value of “x” after the following code snippet executes?

``````int x = 5;
x++;
``````

Explanation: The initial value of “x” is 5. The post-increment operator (x++) is applied to “x”, which increments the value of “x” by 1. After the operation, the value of “x” becomes 6.

## 2. What is the value of “y” after the following code snippet executes?

``````int y = 3;
int x = y++;``````

Explanation: The initial value of “y” is 3. The post-increment operator (y++) is applied to “y” during the assignment to “x”. Therefore, the original value of “y” (3) is assigned to “x”, and then “y” is incremented by 1. After the operation, the value of “y” becomes 4.

## 3. What is the value of “x” after the following code snippet executes?

``````int x = 2;
int y = x++ + ++x;
``````

Explanation: The initial value of “x” is 2. In the expression x++ + ++x, the post-increment operator (x++) is applied to the first occurrence of “x” and the pre-increment operator (++x) is applied to the second occurrence of “x”. The post-increment operator returns the original value of “x” (2) and then increments “x” by 1. The pre-increment operator increments “x” by 1 and returns the updated value (4). Therefore, the expression evaluates to 2 + 4, resulting in 6. After the operation, the value of “x” becomes 4.

## 4. What is the output of the following code snippet?

``````int x = 2;
System.out.println(x++);
System.out.println(x);
``````

Explanation: The initial value of “x” is 2. In the first println statement, the post-increment operator (x++) is applied to “x”. The original value of “x” (2) is printed, and then “x” is incremented by 1. Therefore, the output is 2. In the second println statement, the updated value of “x” (3) is printed. Therefore, the output is 3.

## 5. What is the value of “y” after the following code snippet executes?

``````int y = 5;
int x = ++y + y++;
``````

Explanation: The initial value of “y” is 5. In the expression ++y + y++, the pre-increment operator (++y) is applied to the first occurrence of “y” and the post-increment operator (y++) is applied to the second occurrence of “y”. The pre-increment operator increments “y” by 1 and returns the updated value (6). The post-increment operator returns the original value of “y” (6) and then increments “y” by 1. Therefore, the expression evaluates to 6 + 6, resulting in 12. After the operation, the value of “y” becomes 7.

## 6. What is the value of “x” after the following code snippet executes?

``````int x = 3;
x = ++x + x++;
``````

Explanation: The initial value of “x” is 3. In the expression ++x + x++, the pre-increment operator (++x) is applied to the first occurrence of “x” and the post-increment operator (x++) is applied to the second occurrence of “x”. The pre-increment operator increments “x” by 1 and returns the updated value (4). The post-increment operator returns the original value of “x” (4) and then increments “x” by 1. Therefore, the expression evaluates to 4 + 4, resulting in 8. After the operation, the value of “x” becomes 4.

## 7. What is the output of the following code snippet?

``````int x = 2;
System.out.println(++x + x++);
``````

Explanation: The initial value of “x” is 2. In the println statement, the pre-increment operator (++x) is applied to the first occurrence of “x” and the post-increment operator (x++) is applied to the second occurrence of “x”. The pre-increment operator increments “x” by 1 and returns the updated value (3). The post-increment operator returns the original value of “x” (3) and then increments “x” by 1. Therefore, the expression evaluates to 3 + 3, resulting in 6. The output will be 6.

## 8. What is the value of “y” after the following code snippet executes?

``````int x = 4;
int y = x++ + ++x;
``````

Explanation: The initial value of “x” is 4. In the expression x++ + ++x, the post-increment operator (x++) is applied to the first occurrence of “x” and the pre-increment operator (++x) is applied to the second occurrence of “x”. The post-increment operator returns the original value of “x” (4) and then increments “x” by 1. The pre-increment operator increments “x” by 1 and returns the updated value (6). Therefore, the expression evaluates to 4 + 6, resulting in 10. After the operation, the value of “y” becomes 10.

## 9. What is the value of “x” after the following code snippet executes?

``````int x = 10;
int y = x-- - --x;
``````

Explanation: The initial value of “x” is 10. In the expression x– – –x, the post-decrement operator (x–) is applied to the first occurrence of “x” and the pre-decrement operator (–x) is applied to the second occurrence of “x”. The post-decrement operator returns the original value of “x” (10) and then decrements “x” by 1. The pre-decrement operator decrements “x” by 1 and returns the updated value (8). Therefore, the expression evaluates to 10 – 8, resulting in 2. After the operation, the value of “x” becomes 8.

## 10. What is the output of the following code snippet?

``````int x = 5;
System.out.println(x--);
System.out.println(x);
``````

Explanation: The initial value of “x” is 5. In the first println statement, the post-decrement operator (x–) is applied to “x”. The original value of “x” (5) is printed, and then “x” is decremented by 1. Therefore, the output is 5. In the second println statement, the updated value of “x” (4) is printed. Therefore, the output is 4.

## 11. What is the result of the expression

``x = 5; y = ++x;``

a) x = 6, y = 6
b) x = 6, y = 5
c) x = 5, y = 6
d) x = 5, y = 5

## 12. What is the value of “x” after the following code snippet executes ?

``````int x = 3;
int y = x++;``````

a) 3
b) 4
c) 5
d) 6

## 13. What is the value of “y” after the following code snippet executes?

``````int x = 2;
int y = x++ + ++x;``````

a) 4
b) 5
c) 6
d) 7

## 14. What is the output of the following code snippet?

``````int x = 5;
System.out.println(x++);
System.out.println(x);``````

a) 5, 6
b) 6, 6
c) 6, 5
d) 5, 5

## 15. What is the value of “y” after the following code snippet executes?

``````int x = 2;
int y = x++ * ++x;``````

a) 4
b) 6
c) 8
d) 10

## 16. What is the value of “x” after the following code snippet executes?

``````int x = 4;
x = x++ + ++x;``````

a) 7
b) 8
c) 9
d) 10

## 17. What is the output of the following code snippet?

``````int x = 1;
System.out.println(++x + x++);``````

a) 4
b) 5
c) 6
d) 7

## 18. What is the value of “y” after the following code snippet executes?

``````int x = 3;
int y = ++x + x++;``````

a) 7
b) 8
c) 9
d) 10

## 19. What is the value of “x” after the following code snippet executes?

``````int x = 10;
int y = x-- - --x;``````

a) 8
b) 9
c) 10
d) 11

## 20. What is the output of the following code snippet?

``````int x = 5;
System.out.println(x--);
System.out.println(x);``````

a) 5, 4
b) 5, 5
c) 4, 4
d) 4, 5