Free PYQ practice · GATE CS
Scope traps: 10 GATE CS PYQs
A name that hides another variable, a static that never resets, a parameter change the caller never sees. GATE has tested scope and storage classes like this ten times since 2005. Answer each question the way you would in the exam, then see why the tempting answer is wrong.
- 10 real PYQs
- 2005–2026
- No sign-in to start
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.
Liked these? There are hundreds more.
Hundreds more GATE CS PYQs, with hints and worked solutions, in your free Outlyn account. Or send this set to a friend who is preparing too.
All 10 questions in one listRead the whole set at once. Answers and hints are in the quiz above.
- Q1
Variable shadowing inside block scope
Solve it in the quizConsider 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. - Q2
Compilation error check for C programs
Consider the following three ANSI – C programs. P1, P2, and P3
P1 P2 P3 #include <stdio.h>int a = 5;int main(){int a = 7;return(0);}#include <stdio.h>int main(){int a=5;int a=7;return(0);}#include <stdio.h>int main(){int a=5;float a=7;return(0);}Which one of the following statement is true?
- AOnly P1 will compile without any error
- BOnly P2 will compile without any error
- COnly P3 will compile without any error
- DAll three programs P1, P2, and P3 will compile without any error
- Q3
Static Variable Value Across Function Calls
Solve it in the quizThe integer value printed by the ANSI-C program given below is ______
#include <stdio.h> int funcp() { static int x = 1; x++; return x; } int main() { int x, y; x = funcp(); y = funcp() + x; printf("%d", (x + y)); return 0; } - Q4
Interaction of static and global variables across multiple functions
Solve it in the quizConsider the following C program. The output of the program is __________.
# include <stdio.h> int f1(void); int f2(void); int f3(void); int x = 10; int main() { int x = 1; x += f1() + f2() + f3() + f2(); printf("%d", x); return 0; } int f1() { int x = 25; x++; return x; } int f2( ) { static int x = 50; x++; return x; } int f3( ) { x *= 10; return x; } - Q5
Nested static variables in functions
Solve it in the quizConsider the following C functions.
int fun1(int n) { static int i = 0; if (n > 0) { ++i; fun1(n - 1); } return (i); } int fun2(int n) { static int i = 0; if (n > 0) { i = i + fun1(n); fun2(n - 1); } return (i); }The return value of fun2(5) is ____
- Q6
Static counter in bitwise set-bit counting
Solve it in the quizThe output after executing the following program is ___
#include <stdio.h> int total(int v) { static int count = 0; while (v) { count += v & 1; v >>= 1; } return count; } void main() { static int x = 0; int i = 5; for (; i > 0; i--) { x = x + total(i); } printf("%d\n", x); } - Q7
Execution trace with static variable in recursion
What will be the output of the following C program?
void count(int n){ static int d = 1; printf("%d ", n); printf("%d ", d); d++; if(n > 1) count(n - 1); printf("%d ", d); } void main(){ count(3); }- A312213444
- B312111222
- C3122134
- D3121112
- Q8
Static variable persistence across function calls
Consider the following C code segment.
int a, b, c = 0; void prtFun (void); main () { static int a = 1; prtFun (); a += 1; prtFun (); printf("\n %d %d ", a, b); } void prtFun (void) { static int a = 2; int b = 1; a += ++b; printf (" \n %d %d ", a, b); }What output will be generated by the given code segment?
- A3 1
4 1
4 2 - B4 2
6 1
6 1 - C4 2
6 2
2 0 - D4 2
4 2
2 0
- A3 1
- Q9
Output of C code with auto and register storage classes (common data)
Common data (Q48–Q49). Consider the following C code segment:
int a, b, c = 0; void prtFun(void); main() { static int a = 1; /* Line 1 */ prtFun(); a += 1; prtFun(); printf(" %d %d ", a, b); } void prtFun(void) { static int a = 2; /* Line 2 */ int b = 1; a += ++b; printf(" %d %d ", a, b); }What output will be generated by the given code segment if:
Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;- A3 1
4 1
4 2 - B4 2
6 1
6 1 - C4 2
6 2
2 0 - D4 2
4 2
2 0
- A3 1
- Q10
Recursion and Pass-by-Value Output Trace
Consider the following C-program:
void foo(int n, int sum) { int k = 0, j = 0; if (n == 0) return; k = n % 10; j = n / 10; sum = sum + k; foo(j, sum); printf("%d,", k); } int main() { int a = 2048, sum = 0; foo(a, sum); printf("%d\n", sum); getchar(); }What does the above program print?
- A8, 4, 0, 2, 14
- B8, 4, 0, 2, 0
- C2, 0, 4, 8, 14
- D2, 0, 4, 8, 0