References Questions with Answers

18 practice questions on References (C++) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

Which of these declarations does NOT compile?

int a = 10;
const int& r1 = a;      // (A)
const int& r2 = 5;      // (B)
int& r3 = a;            // (C)
int& r4 = 5;            // (D)
  1. (A)
  2. (C)
  3. (B)
  4. (D)

Answer: D. (D)

Explanation

A non-const lvalue reference cannot bind to a literal (an rvalue). A const reference can bind to a temporary, which is why (B) is fine.

Step-by-step solution

  1. Const int& r1 = a; binds a const reference to a variable: valid.
  2. Int& r3 = a; binds a non-const reference to a variable: valid.
  3. Const int& r2 = 5; binds a const reference to a temporary: valid.
  4. Int& r4 = 5; binds a non-const reference to a literal: invalid, so option D is the answer.

Why the other options are wrong

  • A. (A) binds a const reference to a variable, which compiles.
  • B. (C) binds a non-const reference to a variable, which compiles.
  • C. (B) binds a const reference to a temporary, which compiles.

Tip: A non-const lvalue reference needs an lvalue; a const reference can bind to a temporary.

Question 2Easy

What is the output?

#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int& r = a;
    r++;
    cout << a << " " << r;
    return 0;
}
  1. 6 5
  2. 5 6
  3. 5 5
  4. 6 6

Answer: D. 6 6

Explanation

A reference is another name for the same object, so incrementing r increments a.

Step-by-step solution

  1. r is a reference, another name for a.
  2. r++ increments a itself.
  3. Both print 6.

Why the other options are wrong

  • A. r is not a copy, so it cannot stay at 5.
  • B. a is changed too, so it is not 5.
  • C. Neither stays at 5 after the increment.

Tip: Reference = nickname for the same variable.

Question 3Easy

What is the output?

#include <iostream>
using namespace std;

void swapV(int x, int y) { int t = x; x = y; y = t; }
void swapR(int& x, int& y) { int t = x; x = y; y = t; }

int main() {
    int a = 1, b = 2;
    swapV(a, b);
    cout << a << b << " ";
    swapR(a, b);
    cout << a << b;
    return 0;
}
  1. 21 21
  2. 12 12
  3. 12 21
  4. 21 12

Answer: C. 12 21

Explanation

swapV works on copies, so a and b are unchanged; swapR receives references and swaps the originals.

Step-by-step solution

  1. SwapV gets copies, so a and b stay 1 and 2, and 12 is printed.
  2. SwapR takes references, so a becomes 2 and b becomes 1.
  3. Printing gives 21.
  4. The output is 12 21: option C.

Why the other options are wrong

  • A. SwapV does not change a or b, so the first print is 12, not 21.
  • B. SwapR does swap the values, so the second print is 21, not 12.
  • D. The first print uses the unchanged values, so it is 12, not 21.

Tip: Pass by value works on copies; pass by reference changes the caller's variables.

Question 4Easy

Which statement about C++ references is true?

  1. Arrays of references such as int& arr[3] are allowed
  2. A reference variable can be set to nullptr
  3. A reference can be declared without an initialiser and bound later
  4. Once bound, a reference cannot be made to refer to a different object

Answer: D. Once bound, a reference cannot be made to refer to a different object

Explanation

References are bound once at initialisation and cannot be reseated; there are no null references and no arrays of references.

Step-by-step solution

  1. A reference is bound to its object once, when it is declared.
  2. It cannot be made to refer to a different object later.
  3. So the true statement is option D.

Why the other options are wrong

  • A. Arrays of references are not allowed in C++.
  • B. References cannot be null, so nullptr cannot be bound to a reference.
  • C. A reference must be initialised when declared; it cannot be bound later.

Tip: References must be initialised, cannot be null, cannot be reseated and cannot form arrays.

Question 5Medium

What is the output?

#include <iostream>
using namespace std;

int main() {
    int x = 1;
    auto byVal = [x] { return x; };
    auto byRef = [&x] { return x; };
    x = 5;
    cout << byVal() << byRef();
    return 0;
}
  1. 11
  2. 51
  3. 15
  4. 55

Answer: C. 15

Explanation

A by-value capture copies x when the lambda is created (1); a by-reference capture reads the current value (5).

Step-by-step solution

  1. ByVal copies x when the lambda is created, while x is 1.
  2. ByRef refers to x itself, so it reads x when it is called.
  3. X becomes 5 before the calls, so byRef() returns 5.
  4. The output is 15: option C.

Why the other options are wrong

  • A. ByRef reads x after it became 5, so it is not 1.
  • B. ByVal kept the copy 1 from when the lambda was created, so it is not 5.
  • D. ByVal holds the old copy 1, not 5.

Tip: A by-value lambda capture keeps the value from creation; a by-reference capture sees later changes.

Question 6Medium

What is the output?

#include <iostream>
using namespace std;

int main() {
    int a = 1, b = 2;
    int& r = a;
    r = b;
    b = 5;
    cout << a << " " << r << " " << b;
    return 0;
}
  1. 2 2 5
  2. 2 5 5
  3. 5 5 5
  4. 1 5 5

Answer: A. 2 2 5

Explanation

A reference cannot be reseated: r = b copies the value 2 into a. Changing b later does not affect a or r.

Step-by-step solution

  1. Int& r = a; binds r to a.
  2. r = b; assigns b's value 2 to a, so a and r are both 2.
  3. b = 5; changes only b, which becomes 5.
  4. The output is 2 2 5: option A.

Why the other options are wrong

  • B. A is 2 after r = b, not 5; b = 5 does not change a.
  • C. A stayed 2; only b became 5.
  • D. A was changed from 1 to 2 by r = b.

Tip: Assigning to a reference changes the object it refers to; a reference cannot be rebound.

Question 7Medium

What is the output?

#include <iostream>
using namespace std;

int main() {
    double d = 2.5;
    double& r = d;
    cout << (sizeof(r) == sizeof(double));
    return 0;
}
  1. Compilation error
  2. 0
  3. 8
  4. 1

Answer: D. 1

Explanation

sizeof applied to a reference gives the size of the referred type, not the size of an address.

Step-by-step solution

  1. R refers to d, and sizeof on a reference gives the size of the object it refers to.
  2. That object is a double, which is 8 bytes on typical compilers.
  3. So sizeof(r) equals sizeof(double) and the comparison is true.
  4. Cout prints 1: option D.

Why the other options are wrong

  • A. The code compiles; sizeof on a reference is valid.
  • B. The comparison is true, so the output is 1, not 0.
  • C. 8 would be a size, but the output is the bool result 1.

Tip: Sizeof on a reference gives the size of the referred object, not the size of a pointer.

Question 8Medium

What is the output?

#include <iostream>
using namespace std;

int main() {
    int a = 4, b = 9;
    (a > b ? a : b) = 0;
    cout << a << " " << b;
    return 0;
}
  1. 0 9
  2. 4 9
  3. Compilation error
  4. 4 0

Answer: D. 4 0

Explanation

In C++, when both branches are lvalues of the same type the conditional operator yields an lvalue, so the larger variable b is set to 0.

Step-by-step solution

  1. The condition a > b is 4 > 9, which is false, so the conditional picks b.
  2. Both branches are int lvalues, so the result is the lvalue b.
  3. Assigning 0 sets b to 0; a stays 4.
  4. The output is 4 0: option D.

Why the other options are wrong

  • A. A is 4, not 0; only b is set to 0.
  • B. B was set to 0, so it is not 9.
  • C. Assigning to a conditional is valid when both branches are lvalues of the same type.

Tip: When both branches of ?: are lvalues of the same type, the result is an lvalue that can be assigned to.

Question 9Hard

What is the output?

#include <iostream>
using namespace std;

#include <string>

int main() {
    const string& s = string("ab") + "c";
    cout << s << " " << s.size();
    return 0;
}
  1. ab 2
  2. abc 3
  3. Undefined output (dangling reference)
  4. Compilation error

Answer: B. abc 3

Explanation

Binding a temporary directly to a const reference extends the temporary's lifetime to that of the reference, so s stays valid.

Step-by-step solution

  1. The expression string(ab) + c creates a temporary string holding abc.
  2. Binding that temporary to a const reference extends its lifetime to that of s.
  3. So s stays valid, holds abc and has size 3.
  4. The output is abc 3: option B.

Why the other options are wrong

  • A. The concatenation adds c, so s holds abc, not ab.
  • C. The temporary's lifetime is extended, so the reference does not dangle.
  • D. This is valid C++ and compiles.

Tip: A temporary bound directly to a const reference lives as long as that reference.

Question 10Hard

What is the output?

#include <iostream>
using namespace std;

int main() {
    int a = 1;
    int& r = a;
    auto b = r;
    b = 10;
    cout << a << " " << b << " ";
    auto& c = r;
    c = 20;
    cout << a;
    return 0;
}
  1. 1 10 20
  2. 10 10 10
  3. 1 10 1
  4. 10 10 20

Answer: A. 1 10 20

Explanation

auto drops the reference, so b is an independent int copy; auto& keeps it, so c refers to a.

Step-by-step solution

  1. Auto b = r; drops the reference, so b is a new int copy of a, which is 1.
  2. b = 10 changes only b, so a stays 1; the first part prints 1 10.
  3. Auto& c = r; makes c a reference to a, so c = 20 sets a to 20.
  4. Printing a gives 20: the output is 1 10 20, option A.

Why the other options are wrong

  • B. A stays 1 after b changes, because b is a separate copy.
  • C. A was changed to 20 through c, so the last value printed is 20, not 1.
  • D. The first printed value of a is 1, because b is a separate copy.

Tip: Auto drops references and makes a copy; auto& keeps the reference to the original.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics