LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 7 of 27

References: lvalue and rvalue

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

References: lvalue and rvalue · Study material

C++ References – Interview Questions

1. What is a reference in C++?

An alias – another name – for an existing object. It must be initialized and always refers to the same object.

2. What are the differences between a reference and a pointer?

References cannot be null, cannot be re-seated, need no dereference syntax and do not support arithmetic; pointers can be null, can point elsewhere, use * and -> and support arithmetic.

3. Can a reference be reassigned?

No. r = y assigns y's value to the object r refers to; it does not make r refer to y.

4. Why can a const reference bind to a temporary but a non-const one cannot?

Modifying a temporary that disappears immediately is almost always a bug, so the language allows only read-only (const&) or rvalue (&&) references to bind to it. Binding a const& directly extends the temporary's lifetime.

5. What is a dangling reference?

A reference to an object whose lifetime has ended, e.g. a returned reference to a local variable. Using it is undefined behaviour.

6. What is an lvalue and an rvalue?

An lvalue designates an object with identity (a named variable, *p, a[i]); an rvalue is a temporary value without identity (literals, x + y, function results returned by value).

7. What is an rvalue reference?

A reference declared with && that binds only to rvalues. It lets functions detect temporaries and move from them.

8. What does std::move do?

It casts its argument to an rvalue reference (static_cast<T&&>). The actual moving happens in the move constructor or move assignment operator chosen as a result.

9. What is the state of an object after it has been moved from?

Valid but unspecified: it can be destroyed or assigned a new value, but its contents should not be relied on.

10. What is a forwarding (universal) reference?

A parameter of the form T&& where T is a deduced template parameter (or auto&&). It binds to lvalues and rvalues.

11. What does std::forward do?

Casts a forwarding reference back to its original value category, so lvalues stay lvalues and rvalues stay rvalues when passed on.

12. Explain reference collapsing.

When references to references arise in templates, & &, & && and && & collapse to &; only && && collapses to &&.

13. Is a named rvalue reference an lvalue or an rvalue?

An lvalue – anything with a name is an lvalue. That is why std::move or std::forward is needed to pass it on as an rvalue.

14. Can you have an array or vector of references?

No, references are not objects. Use pointers or std::reference_wrapper.

15. What is std::reference_wrapper?

A copyable, assignable object holding a reference, created with std::ref/std::cref; used in containers and to pass references through std::thread, std::bind and algorithms.

16. Why return a const reference from a getter?

To give read access to a member without copying it, while preventing callers from modifying it.

17. Does sizeof(T&) give the size of a reference?

No, it gives sizeof(T) – the reference behaves like the object.

18. Can a function return an rvalue reference?

Yes (std::move itself does), but returning T&& to a local variable dangles. Functions normally return by value and rely on moves or copy elision.

19. What happens when you std::move a const object?

The result is const T&&, which cannot bind to a move constructor's T&&, so the copy constructor is used.

20. What is the lifetime of a temporary bound to a const reference?

It is extended to the lifetime of the reference – as long as the reference is bound directly to the temporary, not through a function return.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links