Which of the following statements about new and malloc are true? I. 'new' is…

2023

Which of the following statements about new and malloc are true? I. 'new' is an operator while 'malloc' is a function. II. 'new' invokes a constructor, whereas 'malloc' does not invoke a constructor. III. 'malloc' returns a void pointer and needs to be typecast, whereas 'new' returns a pointer of the required type.

  1. A.

    Only I

  2. B.

    Both I and II

  3. C.

    Both II and III

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 190 students.

Show answer & explanation

Correct answer: D

Statement I is correct: 'new' is a C++ operator used for dynamic memory allocation, while 'malloc' is a C standard-library function.

Statement II is correct: 'new' invokes the constructor of the object it creates, whereas 'malloc' only allocates raw memory and does not call any constructor.

Statement III is correct: 'malloc' returns a void pointer (void*) which must be explicitly typecast to the desired type, while 'new' returns a pointer already of the required type.

Since all three statements I, II and III are true, more than one of the listed statements is correct, so the answer is 'More than one of the above'.

हिन्दी:

कथन I सही है: 'new' डायनामिक मेमोरी आवंटन के लिए एक C++ ऑपरेटर है, जबकि 'malloc' एक C स्टैंडर्ड-लाइब्रेरी फ़ंक्शन है।

कथन II सही है: 'new' ऑब्जेक्ट के कंस्ट्रक्टर को कॉल करता है, जबकि 'malloc' केवल कच्ची मेमोरी आवंटित करता है और किसी कंस्ट्रक्टर को कॉल नहीं करता।

कथन III सही है: 'malloc' एक void पॉइंटर (void*) लौटाता है जिसे एक्सप्लिसिट टाइपकास्ट करना पड़ता है, जबकि 'new' पहले से आवश्यक प्रकार का पॉइंटर लौटाता है।

चूँकि तीनों कथन I, II और III सही हैं, सूची में से एक से अधिक कथन सही हैं, इसलिए उत्तर 'उपर्युक्त में से एक से अधिक' है।

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs