ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int n = 0;
int t(void) { n++; return 1; }
int f(void) { n++; return 0; }
int main(void) {
int r = f() || t() && f() || t();
printf("%d %d", r, n);
}- A. 1 4
- B. 1 3
- C. 1 2
- D. 0 3
Correct answer: A. 1 4
&& binds tighter, so this is f() || (t() && f()) || t(). f() gives 0, so the && runs: t() is 1 and f() is 0, giving 0. The last t() then gives 1. All four calls run, and r = 1. '1 2' wrongly short-circuits after t().
More C Programming questions
- What is printed? c int a[] = {3, 5, 0, 7}, i = 0, s = 0, v; while ((v = a[i++])) s += v; printf("%d %d", s, i);
- What is printed? c int cnt[26] = {0}; for (const char p = "banana"; p; p++) cnt[p - 'a']++; int best = 0; for (int i = 1; i < 26; i++) if…
- In a binary search over int indices, why is mid = lo + (hi - lo) / 2 preferred to mid = (lo + hi) / 2?
- What is printed? c int A[2][2] = {{1, 2}, {3, 4}}, B[2][2] = {{0, 1}, {1, 0}}, C[2][2] = {0}; for (int i = 0; i < 2; i++) for (int j = 0…
- When should a variable be declared volatile?
- What is printed? c void f(int a, int n) { if (n <= 1) return; a[n - 2] += a[n - 1]; f(a, n - 1); } int main(void) { int a[] = {1, 2, 3…