1. What is the scope of a local variable in C++?
(A) Within the block or function where it is declared
(B) Whole program
(C) Between two functions
(D) Global scope
2. What is the output of the following code?
void fun() { int x = 10; cout << x; }
int main() { fun(); cout << x; }
(A) 10
(B) Error: x not declared in main()
(C) Garbage value
(D) 0
3. Variables declared inside a function are called:
(A) Global variables
(B) Static variables
(C) Local variables
(D) Dynamic variables
4. What is the lifetime of a local variable?
(A) Until program ends
(B) Until the function execution ends
(C) Until declared again
(D) Forever
5. What will be printed?
int x = 5;
void fun() { int x = 10; cout << x; }
int main() { fun(); }
(A) 5
(B) Undefined
(C) Error
(D) 10
6. Global variables are declared:
(A) Inside main()
(B) Outside all functions
(C) Inside any loop
(D) Inside class only
7. What will the code print?
int x = 5;
void fun() { cout << x; }
int main() { fun(); }
(A) 0
(B) 5
(C) Error
(D) Garbage value
8. Which keyword is used to declare a variable that retains its value between function calls?
(A) static
(B) const
(C) extern
(D) volatile
9. What is the output of the code?
void counter() { static int x = 0; x++; cout << x; }
int main() { counter(); counter(); }
(A) 1 1
(B) 2 2
(C) 1 2
(D) 0 1
10. What is the scope of a global variable?
(A) Within one function
(B) Throughout the program
(C) Only in main()
(D) Until declared again
11. What will happen if a local and global variable have the same name?
(A) Compiler error
(B) Both merge
(C) Global variable is given priority
(D) Global variable is hidden inside the function
12. What will this program output?
int x = 100;
void display() { int x = 50; cout << x; }
int main() { display(); cout << x; }
(A) 50 100
(B) 100 50
(C) 50 50
(D) Error
13. The lifetime of a global variable is:
(A) Until it goes out of scope
(B) Until the program terminates
(C) Until function ends
(D) One execution
14. Which keyword is used to access a global variable hidden by a local variable?
(A) static
(B) extern
(C) scope resolution (::)
(D) global
15. What is the output of this code?
int x = 10;
void fun() { int x = 20; cout << ::x; }
int main() { fun(); }
(A) 10
(B) 20
(C) Error
(D) Undefined
16. What will the following code output?
void test() { static int a = 5; a++; cout << a; }
int main() { test(); test(); test(); }
(A) 6 6 6
(B) 6 7 8
(C) 5 6 7
(D) Error
17. Where is the memory of global variables stored?
(A) Stack
(B) Heap
(C) Data segment
(D) CPU registers
18. What will be printed?
void show() { int a = 10; cout << a; }
int main() { show(); show(); }
(A) 10 10
(B) 10 11
(C) 10 9
(D) Error
19. What is the output?
void fun() { static int n = 1; cout << n << " "; n++; }
int main() { fun(); fun(); fun(); }
(A) 1 1 1
(B) 1 2 3
(C) 2 3 4
(D) Error
20. What happens to a local variable after the function ends?
(A) It is reset to zero
(B) It remains stored in memory
(C) It becomes global
(D) It is destroyed
21. Which variable type is visible to all functions in a file?
(A) local
(B) global
(C) static local
(D) none
22. What is the default value of a local variable if not initialized?
(A) 0
(B) Undefined (garbage)
(C) NULL
(D) Depends on compiler
23. What is the default value of a global variable if not initialized?
(A) 0
(B) Garbage value
(C) NULL
(D) Undefined
24. What will the code output?
int x;
void fun() { x = 10; }
int main() { fun(); cout << x; }
(A) 0
(B) Error
(C) 10
(D) Garbage
25. The keyword "extern" is used to:
(A) Declare a local variable
(B) Declare a variable defined elsewhere
(C) Make variable private
(D) Allocate memory
26. What is the output?
int x = 5;
void fun() { extern int x; cout << x; }
int main() { fun(); }
(A) 0
(B) Error
(C) 5
(D) Undefined
27. What will be printed?
void fun() { static int x = 1; cout << x; x++; }
int main() { fun(); fun(); fun(); }
(A) 1 2 3
(B) 1 1 1
(C) 2 3 4
(D) 0 1 2
28. What is the output of this code?
int a = 5;
void show() { int a = 10; cout << a; }
int main() { cout << a; show(); }
(A) 10 5
(B) 5 10
(C) 5 5
(D) Error
29. Which variable is destroyed only when the program ends?
(A) static variable
(B) local variable
(C) global variable
(D) Both A and C
30. What is the lifetime of a static local variable?
(A) Until function ends
(B) Until next function call
(C) Until program ends
(D) Forever in memory