Consider the following three ANSI-C programs, P1, P2, and P3. Which one of the following statements is true?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2026 Set 2 · Q19
Time

Select one answerSingle correct
Solution
Official answer: A) Only P1 will compile without any error
Programming in CLet us analyze each program:
Program P1:
int a = 5; is declared at file (global) scope.
int a = 7; is declared inside the block scope of main().
In ANSI-C, a local variable can shadow/hide a global variable with the same name. Hence, P1 compiles without any error.
Program P2:
Both int a = 5; and int a = 7; are declared within the same block scope of main().
Redeclaring an identifier in the same scope is illegal and causes a compilation error (redefinition of variable a).
Program P3:
Both int a = 5; and float a = 7; are declared within the same block scope of main().
Redeclaring an identifier with conflicting types in the same scope causes a compilation error.
Thus, only P1 will compile without any error. Hence, option (A) is correct.
Try the question first, then check your work.
Examiner trap
Check the scope of each declaration of variable a. A local variable can shadow an outer/global variable, but two declarations of the same identifier cannot exist in the exact same block scope.
This Programming in C previous year question is Q19 of the GATE CSE 2026 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 20 of them are 1-mark MCQs like this one, and GATE CSE 2026 Set 2 has 3.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.