What is the output of the following code? #include <iostream> using namespace…

2021

What is the output of the following code?

#include <iostream>
using namespace std;

int main()
{
    int a = 3, b, *p, **q;

    p = &a;
    q = &p;

    b = 2 * (**q + *p);

    cout << b;
}

  1. A.

    3

  2. B.

    12

  3. C.

    18

  4. D.

    24

Attempted by 81 students.

Show answer & explanation

Correct answer: B

Step-by-step execution:

  1. a = 3

  2. p = &a
    p stores the address of a.

  3. q = &p
    q stores the address of p.

  4. *p gives the value of a3

  5. *q gives the value stored in p → address of a
    **q gives the value of a3

Conceptually, the intention is:

  • *p = 3

  • **q = 3

So:

b = 2 * (**q + *p)
  = 2 * (3 + 3)
  = 2 * 6
  = 12

Hence, the output is 12.

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs