All mock tests are FREE till 31 December — register to save your progress

Consider the two C++ programs P1 and P2 shown below and two statements about them:

S1: P1 prints out 3

S2: P2 prints out 4:2 What can you say about S1 and S2?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: A. Only S1 is true.

Explanation

P1: f(i, &i, i) copies a = 0 → 1 locally, *b = 2 sets i = 2, and c is a reference to i, so c = 3 leaves i = 3: S1 is true. P2: f(a) binds d to a and sets a = 4, then returns a reference to b, which is assigned 5, so it prints 4:5: S2 is false.

More Programming Languages and Computer Graphics questions