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;
}- A.
3
- B.
12
- C.
18
- D.
24
Attempted by 81 students.
Show answer & explanation
Correct answer: B
Step-by-step execution:
a = 3p = &apstores the address ofa.q = &pqstores the address ofp.*pgives the value ofa→3*qgives the value stored inp→ address ofa**qgives the value ofa→3
Conceptually, the intention is:
*p = 3**q = 3
So:
b = 2 * (**q + *p)
= 2 * (3 + 3)
= 2 * 6
= 12Hence, the output is 12.