1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. int* p = new int[10]; delete p; is executed. What is the…

Added 30 Nov -0001

int* p = new int[10]; delete p; is executed. What is the defect?

  1. It fails to compile, because delete cannot be applied to an array new expression
  2. The form of delete does not match the form of new, so the behaviour is undefined Correct answer
  3. Only the first element is destroyed and the remaining nine are leaked silently
  4. The delete is acceptable, but the ten ints stay allocated until the program exits

Correct answer

B. The form of delete does not match the form of new, so the behaviour is undefined

15 views

MCQSEXAM

Explanation

Array new records the element count in a cookie that delete[] uses to run every destructor, so pairing new[] with scalar delete is undefined behaviour and frequently corrupts the allocator. With int the destructors are trivial, so the corruption may stay hidden in testing. The reverse mismatch, delete[] applied to a scalar new, is equally fatal.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

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