ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int x = 5;
int getX(void) {
return x;
}
int main(void) {
int x = 20;
printf("%d %d", x, getX());
}- A. 20 20
- B. 5 5
- C. 20 5
- D. 5 20
Correct answer: C. 20 5
main's local x=20 only shadows the global within main's own scope; printf's first argument, x, refers to that local, 20. getX() is a separate function with no access to main's local x at all — it only ever sees the file-scope global x=5, which it returns unchanged.
More C Programming questions
- What is printed? c static int privateCounter = 0; void bump(void) { privateCounter++; } int getCounter(void) { return privateCounter; }…
- What is printed? c int value = 100; void modify(int value) { value = 200; } int main(void) { modify(value); printf("%d", value); }
- What is printed? c int a = 1; int b = a++ + a++; int c = ++a + ++a; printf("UB UB");
- What is printed? c int a = 5; int b = 10; int result = a > b ? a : b > 0 ? b : 0; printf("%d", result);
- What is printed? c int x = 5; int y = (x = 10) + (x = 20); printf("%d", y);
- What is printed? c int a = 1, b = 2; int result = !a == !b; printf("%d", result);