ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int a = 1, b = 0, c = 1;
int result = a || b && c;
printf("%d", result);- A. 1, and && is evaluated first because it binds tighter than ||
- B. 0
- C. 1, but only because of left-to-right evaluation with no precedence involved
- D. Compile-time error: ambiguous expression, needs explicit parentheses
Correct answer: A. 1, and && is evaluated first because it binds tighter than ||
&& has higher precedence than ||, so a || b && c parses as a || (b && c). Since a is 1 (true), the || short-circuits immediately: the whole expression is true (1) without even needing to evaluate b && c at all, though if evaluated it would be (0 && 1) = 0 — either way the overall result is 1.
More C Programming questions
- What is printed? c int x = 5; int y = x++ + (x = 10); printf("%d", y);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5, b = 10, c = 15; int arr[3]; arr[0] = &c; arr[1] = &a; arr[2] = &b; int sum = 0…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 100; int p = &a; int q = p; p = NULL; printf("%d %d", (p == NULL), (q));
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void movePoint(struct Point p, int dx, int dy) { p->x += dx; p->y…
- Assume a 64-bit LP64 ABI. What happens? c int createArray(int n) { int arr[n]; for (int i = 0; i < n; i++) arr[i] = i i; return arr; } int…
- Assume a 64-bit LP64 ABI. What is printed? c int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int sum = 0; for (int i = 0; i < 3; i++) sum +=…