The GATE Grind

GATE 2024 CS (CS1) – Question 18

Programming and Data Structures · Programming in C · 1 mark · Multiple choice

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. The program prints 9 as output
  2. The program prints 10 as output
  3. The program gets stuck in an infinite loop
  4. The program prints 6 as output

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (C) The program gets stuck in an infinite loop

Explanation

With a<10, a/12 is 0 so a becomes 1, then a+=0 keeps a=1. a stays below 10 forever, so the loop never terminates.