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?
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 / 12evaluates to0, soa = 0 + 1 = 1, anda += bleavesa = 1. - In every subsequent iteration,
a = 1 < 10, and1 / 12 + 1again evaluates to0 + 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.