1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Given const int n = 5; const int& r = n; what types are…

Added 30 Nov -0001

Given const int n = 5; const int& r = n; what types are deduced for auto a = r; and auto& b = r;?

  1. a is const int& and b is const int&, because auto copies the initialiser's type exactly
  2. a is int and b is int, because auto& also drops the const qualifier from the referent
  3. a is const int and b is const int&, because auto keeps const but removes the reference
  4. 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

MCQSEXAM

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

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs