Question

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?

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2024 answer key

Solution

Trace the integer division a / 12 inside the while (a < 10) loop.

Initially, a = 6 and b = 0. Since a < 10 is true:

  • In the first iteration, integer division 6 / 12 evaluates to 0, so a = 0 + 1 = 1, and a += b leaves a = 1.
  • In every subsequent iteration, a = 1 < 10, and 1 / 12 + 1 again evaluates to 0 + 1 = 1.

Because a remains 1 forever, the loop condition a < 10 never becomes false and the program gets stuck in an infinite loop.

Final answer

Option (C)

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2024 Computer Science and Information Technology - Set 1 (CS1)
Question
18 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.