Which of the following statements is true about new and malloc? I. The new is a type of operator while malloc is a kind of function. II. The new invokes a constructor, whereas malloc does not invoke the constructor. III. The malloc returns void pointer and also needed to typecast whereas new returns required pointer.

Options

  • A. Only I
  • B. Both I and II
  • C. Both II and III
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

D. More than one of the above

Detailed Explanation

Answer: (D) More than one of the above

All three statements are correct.

Statement I: new is a C++ operator, whereas malloc() is a library function used for dynamic memory allocation.

Statement II: When new is used to create an object, the appropriate constructor is called. malloc() only allocates raw memory and does not call a C++ constructor.

Statement III: In C++, malloc() returns a void*, which traditionally requires conversion to the required pointer type. The new expression returns a pointer of the appropriate type.

Therefore, statements I, II and III are all true. Since the options do not provide an option saying exactly “I, II and III”, the intended answer is (D) More than one of the above.