1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. double d = 3.9; int x{d}; What does a conforming C++11…

Added 30 Nov -0001

double d = 3.9; int x{d}; What does a conforming C++11 compiler do with this declaration?

  1. Compiles and sets x to 3, since braced init truncates like a cast does
  2. Compiles and sets x to 4, since braced init rounds to the nearest integer
  3. Compiles with a warning, since the value is implementation-defined here
  4. Rejects it, because narrowing conversions are ill-formed in braced init Correct answer

Correct answer

D. Rejects it, because narrowing conversions are ill-formed in braced init

19 views

MCQSEXAM

Explanation

Braced initialisation forbids narrowing conversions, so converting a double to int inside int x{d} is ill-formed; the copy form int x = d; is merely a warning-level narrowing. The rule exists to stop silent loss of precision and also rejects long to int and int to char when the value cannot be represented. Compilers must diagnose it, though the diagnostic is often an error rather than a warning.

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