Given const int n = 5; const int& r = n; what types are deduced for auto a = r; and auto& b = r;?
A
a is const int& and b is const int&, because auto copies the initialiser's type exactly
B
a is int and b is int, because auto& also drops the const qualifier from the referent
C
a is const int and b is const int&, because auto keeps const but removes the reference
D
a is int and b is const int&, because plain auto strips references and top-level const
Correct answer
Correct answer
D. a is int and b is const int&, because plain auto strips references and top-level const
14 views
⚔️
Challenge a friend on C++ and Object-Oriented Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
Explanation
Template argument deduction rules govern auto, so plain auto drops references and top-level const, giving int for a, while auto& keeps the reference and therefore the const, giving const int& for b. Use auto&& or decltype(auto) when the exact value category matters. Forgetting this silently turns intended aliases into expensive container copies.
Reference
Asked in: NTS, PPSC IT, Campus Interviews
Doubts & discussionNot sure why the answer is D? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ Base declares virtual void f(int x = 10); and Derived overrides it as void f(int x = 20);. Through a Base* b pointing at a Derived object, b->f() passes which argument? Next MCQ › A const std::string s is passed to a constructor call as std::move(s). Which constructor runs, and why?