ProgrammingHardTCS NQT · Infosys pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int x = 7;
int y = x > 5 && x < 10 ? x * 2 : x + 1;
printf("%d", y);- A. 8
- B. 14
- C. Compile-time error: condition needs extra parentheses
- D. 7
Correct answer: B. 14
&& binds tighter than ?:, so the condition is (x > 5 && x < 10), i.e. (7>5) && (7<10) = true && true = true. The ternary then picks the true-branch, x * 2 = 14.
More C Programming questions
- What happens when this runs? c int p = malloc(10 sizeof(int)); for (int i = 0; i <= 10; i++) { p[i] = i; } free(p);
- Assume 32-bit int. What is printed? c int a = 6; int b = a++ 2; printf("%d %d", a, b);
- What is printed? c #define MAXSIZE 100 #if MAXSIZE > 50 const char label = "large"; #else const char label = "small"; #endif printf("%s"…
- Assume 32-bit int. What is printed? c int x = 10; int p = &x; p += 5; p = 2; printf("%d", x);
- What happens when this runs? c int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } int main(void) {…
- Assume 32-bit int. What is printed? c int arr[3] = {1, 2, 3}; int total = 0; int i; for (i = 0; i < 3; i++) total = total + arr[i]…