Question
Consider the following program in C:
#include <stdio.h>
void func(int i, int j) {
if(i < j) {
int i = 0;
while (i < 10) {
j += 2;
i++;
}
}
printf("%d", i);
}
int main() {
int i = 9, j = 10;
func(i, j);
return 0;
}
The output of the program is _________. (answer in integer)
Note: Assume that the program compiles and runs successfully.
Official answer and solution
- Official answer
- 9
- From
- Official GATE 2026 answer key
Solution
Track the block scope of the inner variable int i = 0; declared inside the if statement versus the formal parameter i of func.
When func(9, 10) is called, the formal parameters are i = 9 and j = 10. Since 9 < 10, the if block is entered. Inside the if block, int i = 0; declares a new local variable i whose scope is restricted to the if block, shadowing the parameter i. The while loop increments this inner i from 0 to 10. Once the if block ends, the inner i goes out of scope, and printf("%d", i); prints the formal parameter i, which still has its original value 9.
9
Printing 10 by overlooking the int declaration in int i = 0; inside the if block.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2026 Computer Science and Information Technology - Session 1 (CS1)
- Question
- 34 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.