1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What does the C++ expression new int(5) produce?

Added 30 Nov -0001

What does the C++ expression new int(5) produce?

  1. A reference to a temporary int value
  2. A compile error, since new cannot take arguments
  3. A pointer to a heap int initialised to 5 Correct answer
  4. An int object with automatic storage duration

Correct answer

C. A pointer to a heap int initialised to 5

14 views

MCQSEXAM

Explanation

new allocates storage from the free store, constructs the object there, and yields a pointer, so new int(5) initialises the int to 5 and must later be released with delete. In contrast, new int[5] allocates an array of five ints and requires delete[] rather than delete. Mixing the scalar and array forms is undefined behaviour, which is one reason modern C++ prefers std::vector and smart pointers.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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