What is the output of the following code snippet? int num = 10; System.out.println(num++); System.out.println(++num);
Question
What is the output of the following code snippet?
int num = 10;
System.out.println(num++);
System.out.println(++num);
Solution
The output of the code snippet would be:
10 12
Explanation:
In the first print statement, num++ is used. This is a post-increment operation, which means the current value of num (which is 10) is printed first, then it is incremented by 1. So, the output of the first print statement is 10.
After the first print statement, the value of num is now 11.
In the second print statement, ++num is used. This is a pre-increment operation, which means the value of num is incremented by 1 first, then it is printed. So, the value of num becomes 12, and then it is printed. Hence, the output of the second print statement is 12.
Similar Questions
What is the output of the following code snippet?int x = 5;int y = 2;int result = x % y;System.out.println(result);Question 2Answera.3b.2c.1d.0
What is the output of the following code snippet? int x = 5; int y = 2; System.out.println(x / y);
What is the output of the following code snippet? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);
What is the output of the following code snippet?int x = 8;int y = 4;System.out.println(x & y);Question 16Answera.8b.0c.1d.4
What will the result of num variable after execution of the following statements?int num = 58;num % = 11;ans.1112313
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.