What is the output of the following piece of code?

int x = 3, y = 5, z;
z = x + ++y;

printf(ā€œ%dā€,z);
  1. 8

  2. 9

  3. 10

  4. 11


Correct Option: B

AI Explanation

To determine the output of the given code, let's go through each line of code step by step:

  1. int x = 3, y = 5, z; - Here, three variables x, y, and z are declared and assigned the initial values of 3, 5, and garbage value respectively.

  2. z = x + ++y; - In this line, the value of y is incremented by 1 (++y) and then added to the value of x. So, y becomes 6 and x + y is 3 + 6 = 9. The result is stored in the variable z.

  3. printf("%d", z); - This line prints the value of z using the %d format specifier, which represents an integer.

Therefore, the output of the code will be 9.

Now, let's go through the options to determine the correct answer:

Option A) 8 - This option is incorrect because the output of the code is 9, not 8.

Option B) 9 - This option is correct. The output of the code is indeed 9.

Option C) 10 - This option is incorrect because the output of the code is not 10.

Option D) 11 - This option is incorrect because the output of the code is not 11.

Therefore, the correct answer is B) 9.

Find more quizzes: