Print the output of this program main() { int k = 5; if (++k < 5 && k++/5 || ++k <= 8); printf("%d\n", k); }

  1. 5

  2. 7

  3. 8

  4. garbage value


Correct Option: B
Explanation:

To solve this question, we need to understand the order of operations and logical operators.

The given program uses the if statement to check some conditions. If the conditions are met, it increases the value of k. Finally, it prints the value of k using printf.

The given expression can be broken down into three parts:

  1. ++k < 5
  2. k++/5
  3. ++k <= 8

Let's evaluate each part:

  1. ++k < 5: The value of k is first incremented to 6. Then, this expression checks if 6 is less than 5. This is false, so the expression evaluates to false.

  2. k++/5: The value of k is still 6. This expression divides 6 by 5 (integer division), which evaluates to 1. However, this expression is not used in the if statement because of short-circuiting.

  3. ++k <= 8: The value of k is now 7 (due to the previous increment). This expression checks if 7 is less than or equal to 8. This is true, so the expression evaluates to true.

Since the expression in the if statement evaluates to true, the code inside the if statement is executed. This code does nothing, so k remains 7. Finally, the value of k is printed using printf.

Therefore, the output of this program is:

7

The answer is: B. 7

Find more quizzes: