Skip to content
QuiznExam

References in C++ Quiz

Test your knowledge of references in C++, including reference declaration, initialization, pass-by-reference, reference parameters, const references, reference returns, and the differences between references and pointers.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is a reference in C++?

    • A. A separate copy of an existing variable
    • B. An alternative name for an existing object or variable
    • C. A function that returns an integer
    • D. A pointer that must be dynamically allocated
    Show answer

    B. An alternative name for an existing object or variable

    A reference acts as another name for an existing object and can be used to access or modify that object, depending on the reference type.

  2. 2. Which syntax correctly declares an integer reference named ref to an integer variable x?

    • A. int ref = &x;
    • B. reference int ref = x;
    • C. int& ref = x;
    • D. int* ref = x;
    Show answer

    C. int& ref = x;

    The ampersand in int& declares a reference to an integer, and the reference must be initialized with a suitable object.

  3. 3. What happens when a reference is initialized with a variable?

    • A. It becomes an alias for that variable
    • B. It automatically creates a pointer
    • C. It copies the variable into a separate memory location
    • D. It destroys the original variable
    Show answer

    A. It becomes an alias for that variable

    A reference refers to the object used to initialize it rather than creating an independent object.

  4. 4. Which statement about ordinary lvalue references in C++ is correct?

    • A. They can never refer to variables
    • B. They must always be initialized with nullptr
    • C. They can be declared without an initializer and assigned later
    • D. They must be initialized when declared
    Show answer

    D. They must be initialized when declared

    An ordinary lvalue reference must be initialized at declaration. It cannot later be reseated to refer to a different object.

  5. 5. What is the main advantage of passing an argument to a function by reference?

    • A. It always creates a new copy of the argument
    • B. It can avoid copying and allow the function to modify the original object
    • C. It prevents the function from accessing the argument
    • D. It automatically allocates memory on the heap
    Show answer

    B. It can avoid copying and allow the function to modify the original object

    Passing by reference gives the function access to the original object without requiring a separate parameter copy.

  6. 6. What will this code print? int x = 10; int& r = x; r = 25; cout << x;

    • A. 10
    • B. 0
    • C. 25
    • D. Compilation error
    Show answer

    C. 25

    The reference r aliases x, so assigning 25 to r changes the value of x to 25.

  7. 7. Which declaration creates a const reference to an integer?

    • A. const int& ref = x;
    • B. int const ref = &x;
    • C. int& const ref = x;
    • D. const int* ref = x;
    Show answer

    A. const int& ref = x;

    A const int& reference allows the referenced integer to be read through that reference but not modified through it.

  8. 8. Can a const reference bind to a temporary value of a compatible type?

    • A. No, references can only bind to named variables
    • B. Only if the temporary is allocated using new
    • C. Yes, in applicable cases, the temporary's lifetime is extended
    • D. Only when the reference is a pointer
    Show answer

    C. Yes, in applicable cases, the temporary's lifetime is extended

    A const lvalue reference can bind to a compatible temporary, and direct binding in a local declaration generally extends the temporary's lifetime to that of the reference.

  9. 9. What is the main difference between a reference and a pointer in C++?

    • A. References must always be dynamically allocated
    • B. A reference cannot be reseated to refer to another object, while a pointer can be reassigned
    • C. Pointers cannot store memory addresses
    • D. References must always be compared using nullptr
    Show answer

    B. A reference cannot be reseated to refer to another object, while a pointer can be reassigned

    A reference remains bound to its referred object, whereas a pointer variable can be changed to hold the address of another object.

  10. 10. Which function parameter declaration allows a function to modify the caller's integer variable?

    • A. void update(int x)
    • B. void update(const int& x)
    • C. void update(int x const)
    • D. void update(int& x)
    Show answer

    D. void update(int& x)

    An int& parameter refers to the caller's integer, allowing the function to modify the original value.

  11. 11. What does returning an int& from a function allow?

    • A. The function to return a reference to an integer object
    • B. The function to return only a copied integer
    • C. The function to create a reference to a local variable safely in every case
    • D. The function to return a null reference
    Show answer

    A. The function to return a reference to an integer object

    A function returning int& returns a reference to an integer object. The referred object must remain alive and valid when the reference is used.

  12. 12. Why is returning a reference to a non-static local variable dangerous?

    • A. It automatically converts the variable to a pointer
    • B. It makes the function execute twice
    • C. The local variable's lifetime ends when the function returns, leaving a dangling reference
    • D. It prevents the function from compiling in all cases
    Show answer

    C. The local variable's lifetime ends when the function returns, leaving a dangling reference

    A non-static local variable is destroyed when its function returns. A reference to it becomes invalid and using that reference results in undefined behavior.

  13. 13. What is an rvalue reference in C++?

    • A. A reference declared only with the const keyword
    • B. A reference declared using &&
    • C. A pointer declared using **
    • D. A reference that can only refer to global variables
    Show answer

    B. A reference declared using &&

    An rvalue reference is declared using && and can bind to suitable rvalues. It is commonly used in move semantics and perfect forwarding.

  14. 14. Which statement about references to arrays in C++ is correct?

    • A. An array reference must always point to dynamically allocated memory
    • B. C++ does not allow references to arrays
    • C. An array reference automatically converts the array into a pointer
    • D. A reference can refer to an entire array while preserving its size in the type
    Show answer

    D. A reference can refer to an entire array while preserving its size in the type

    A reference can bind to an entire array, preserving the array's type and size, such as int (&ref)[3] for an array of three integers.

  15. 15. What happens when a reference variable is used in an expression?

    • A. It generally behaves as an expression referring to the original object
    • B. It always returns the memory address of the object
    • C. It creates a new object each time it is accessed
    • D. It automatically becomes a constant pointer
    Show answer

    A. It generally behaves as an expression referring to the original object

    Using a reference accesses the object it refers to. Unlike a pointer, a reference is not dereferenced with the * operator to access that object.