GATE CS 2024 Set 1 | Question 18

MCQ · 1 mark · −0.33

Consider the following C program:

#include <stdio.h>
int main(){
   int a = 6;
   int b = 0;
   while(a < 10) {
      a = a / 12 + 1;
      a += b;}
   printf("%d", a);
   return 0;}

Which one of the following statements is CORRECT?

  1. Option A: The program prints 9 as output
  2. Option B: The program prints 10 as output
  3. Option C: The program gets stuck in an infinite loop (correct)
  4. Option D: The program prints 6 as output

Answer

(C)

Explanation

Both operands of a / 12 are integers, so the division truncates. Starting from a = 6, the first pass gives \(6/12 = 0\), so a becomes \(0 + 1 = 1\), and a += b adds 0.

On every later pass a is 1: the test \(1 < 10\) holds, \(1/12 = 0\), and a is set to 1 again. Since b stays 0, nothing ever raises a to 10, so the condition never fails, the loop never exits and printf is never reached.

Options A, B and D all assume that the loop ends and something is printed.

Answer: C