ProgrammingHardGATE CS · Campus placements
static int total = 0; int add(int n) { static int t = 0; t += n; return t; } Calls add(3), add(4) and add(5) in that order. What does the last call return?
- A. 8
- B. 15
- C. 12
- D. 5
Correct answer: C. 12
The static local t keeps its value across calls, so the running totals are 3, 7 and 12. The file-scope total is unused and does not affect the result.
More C Programming questions
- int r = 0; switch (2) { case 1: r += 1; case 2: r += 10; case 3: r += 100; break; default: r += 1000; } What is r?
- for (int i = 0, j = 10; i < j; i += 3, j -= 3) count++; How many times does the body run?
- int n = 5; int r = n++ 2; What are the final values of r and n?
- Adding two non-negative ints a and b without +, a loop repeats a = a ^ b and b = (old a & old b) << 1 until b is 0. Starting with a = 5…
- uint8t sum = (uint8t)(200 + 100); if (sum < 50) printf("yes"); else printf("no"); What is printed?
- char msg[] = "abc"; printf("%c", msg[msg[0] - 'a' + 1]); What is printed?