QUESTION 37 What will be the output of the program given below?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What will be the output of the program given below?
#include <stdio.h>
int a = 40;
int main()
{
int a, b = 5;
a = b + 0;
printf("%d", a);
return 0;
}
Show answer & explanation
Answer: D. 5
The local variable a declared inside main() shadows the global variable a = 40. The local a is initialized with garbage (uninitialized), but b is explicitly initialized to 5. The assignment a = b + 0 sets local a to 5. The printf outputs the local a, which is 5, not the global value.
Step-by-step Derivation:
- Global scope:
int a = 40;declares and initializes a global variable. - In main():
int a, b = 5;declares two local variables. Localais uninitialized (has garbage value initially),b = 5. - Local variable
ashadows the globalawithin main(). a = b + 0;assigns5 + 0 = 5to locala.printf("%d", a);prints the locala, which is 5.- Output:
5