ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int a = 0;
for (int i = 0; i < 4; i++) {
    a |= (1 << i);
}
printf("%d", a);
  1. A. 8
  2. B. 16
  3. C. 4
  4. D. 15

Correct answer: D. 15

The loop sets bits 0,1,2,3 one at a time: after all 4 iterations a = 0b1111 = 15.

More C Programming questions

Exams that ask this topic