Function Overloading in C++
15 questions · 12 min · Medium
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.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
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.
C. int& ref = x;
The ampersand in int& declares a reference to an integer, and the reference must be initialized with a suitable object.
A. It becomes an alias for that variable
A reference refers to the object used to initialize it rather than creating an independent object.
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.
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.
C. 25
The reference r aliases x, so assigning 25 to r changes the value of x to 25.
A. const int& ref = x;
A const int& reference allows the referenced integer to be read through that reference but not modified through it.
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.
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.
D. void update(int& x)
An int& parameter refers to the caller's integer, allowing the function to modify the original value.
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.
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.
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.
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.
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.