What will be the output of the following program? int a = 5; cout << "FIRST "…

2023

What will be the output of the following program?
int a = 5;
cout << "FIRST " << a<<2 << "SECOND";

  1. A.

    FIRST 52 SECOND

  2. B.

    FIRST 20 SECOND

  3. C.

    SECOND 25 FIRST

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 213 students.

Show answer & explanation

Correct answer: E

Concept: In C++, the operator << is overloaded. When its left operand is a stream object such as cout, << means STREAM INSERTION — it prints the right operand as text and returns the stream so the next << can chain onto it. This is different from the BITWISE LEFT-SHIFT <<, which applies only when BOTH operands are plain integers on their own (for example, a standalone 5 << 2 equals 20).

Chained << calls associate left to right, and whitespace never changes tokenization, so a<<2 and a << 2 are exactly the same two tokens (<< then 2). Once the chain already starts from cout, every later << in that same chain is stream insertion, not bit-shift.

Application: Trace cout << "FIRST " << a << 2 << "SECOND"; left to right, with a = 5:

  1. cout << "FIRST " prints FIRST (with its trailing space) and returns cout.

  2. << a prints the value of a, i.e. 5 → output so far: FIRST 5

  3. << 2 is stream insertion (the left operand is the stream returned above, not a bare int), so it prints the literal 2, not a shifted value → output so far: FIRST 52

  4. << "SECOND" appends SECOND with no separating space → final output: "FIRST 52SECOND"

Cross-check against the options:

  • "FIRST 52 SECOND" inserts an extra space before SECOND that the code never produces.

  • "FIRST 20 SECOND" would only hold if a << 2 were read as the bitwise shift 5 << 2 = 20 — but the left operand here is a stream, not a bare int, so that reading does not apply.

  • "SECOND 25 FIRST" reverses both the print order and the digit order, contradicting left-to-right stream evaluation.

  • Since none of the three specific strings equal "FIRST 52SECOND", "more than one of the above" cannot hold either.

Correct Answer: None of the above

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs