JAVA : What is the output of the following Java code snippet?

preview_player
Показать описание
JAVA : What is the output of the following Java code snippet?

SDET Automation Testing Interview Questions & Answers

We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.

JAVA : What is the output of the following Java code snippet?

int i = 5;
Options:
A) 11
B) 12
C) 13
D) 14

Answer: Option D) 14

Explanation:

In the given code snippet, the variable i is initialized with a value of 5. In the println() statement, the post-increment operator i++ is used to add the value of i (which is 5) to the output, and then increment i by 1.

The resulting value is then added to the pre-incremented value of i, which is incremented by 1 before it is used in the addition operation. Therefore, the second operand is 7 (because the value of i is now 7).

So, the expression i++ + ++i becomes 5 + 7, which equals to 12. However, the println() statement uses the post-increment operator i++ again to increment i by 1 after the addition operation. Therefore, the final value of i is 6.

As a result, the output of this code will be: 14. Therefore, the correct answer is D) 14.
Рекомендации по теме
Комментарии
Автор

JAVA : What is the output of the following Java code snippet?

int i = 5;
System.out.println(i++ + ++i);
Options:
A) 11
B) 12
C) 13
D) 14

Answer: Option D) 14

Explanation:

In the given code snippet, the variable i is initialized with a value of 5. In the println() statement, the post-increment operator i++ is used to add the value of i (which is 5) to the output, and then increment i by 1.

The resulting value is then added to the pre-incremented value of i, which is incremented by 1 before it is used in the addition operation. Therefore, the second operand is 7 (because the value of i is now 7).

So, the expression i++ + ++i becomes 5 + 7, which equals to 12. However, the println() statement uses the post-increment operator i++ again to increment i by 1 after the addition operation. Therefore, the final value of i is 6.

As a result, the output of this code will be: 14. Therefore, the correct answer is D) 14.

sdet_automation_testing
visit shbcf.ru