Object Oriented Programming using C++ Questions and Answers – New Operator

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “New Operator”.

1. What is the new operator?
a) Allocates memory for an object or array
b) Allocates memory for an object or array and returns a particular pointer
c) Used as return type when an object is created
d) Used to declare any new thing in a program
View Answer

Answer: b
Explanation: The new keyword is used to allocate memory of an object or array. The new object or array can be of any type. Then it returns a suitable non zero pointer to the object.

2. Microsoft C++ Components extensions support new keyword to _____________
a) Modify a vtable
b) Replace a vtable slot entry
c) Add new vtable slot entries
d) Rearrange vtable slot entries
View Answer

Answer: c
Explanation: The new keyword is used for adding new vtable slot entries. This is an additional feature in Microsoft C++. It can use predefined class object for this work.

3. What happens when new fails?
a) Returns zero always
b) Throws an exception always
c) Either throws an exception or returns zero
d) Terminates the program
View Answer

Answer: c
Explanation: While creating new objects, the new operator may fail because of memory errors or due to permissions. At that moment the new operator returns zero or it may throw an exception. The exception can be handled as usual.
advertisement
advertisement

4. If new throws an error, which function can be called to write a custom exception handler?
a) _set_handler
b) _new_handler
c) _handler_setter
d) _set_new_handler
View Answer

Answer: d
Explanation: If the default exception handler has to be replaced by a user defined handler, we can call _set_new_handler run-time library function with the function name as an argument. This lets the programmer to give a custom definition for handling new operator failure.

5. In C++, if new operator is used, when is the constructor called?
a) Before the allocation of memory
b) After the allocation of memory
c) Constructor is called to allocate memory
d) Depends on code
View Answer

Answer: b
Explanation: The constructor function is called after the allocation of memory. In C++ the feature works in a bit different way. The memory for all the data members is allocated first and then the constructor function is called to finalize the memory allocated.

6. Which among the following is correct syntax to declare a 2D array using new operator?
a) char (*pchar)[10] = new char[][10];
b) char (pchar) = new char[][10];
c) char (*char) = new char[10][];
d) char (*char)[][10]= new char;
View Answer

Answer: a
Explanation: The new operator usage to declare a 2D array requires a pointer and size of array to be declared. Data type and then the pointer with size of array. The left index can be left blank or any variable can be assigned to it.

7. For declaring data by using new operator ____________________
a) Type name can’t contain const
b) Type name can’t contain volatile
c) Type name can’t contain class declarations
d) Type name can’t contain const, volatile, class declaration or enumerations
View Answer

Answer: d
Explanation: The declaration of any data where we use new operator, any of the mentioned types are not allowed. This is because the new operator allocated memory based on the type of data which can be allocated dynamically.
advertisement

8. The new operator _____________
a) Can allocate reference types too
b) Doesn’t allocate reference types
c) Can allocate reference to objects
d) Doesn’t allocate any data
View Answer

Answer: b
Explanation: The new operator doesn’t allocate reference types. This is because the reference types are not objects. The new operator is used to allocate memory to the direct objects.

9. Which among the following is true?
a) New operator can’t allocate functions but pointer to functions can be allocated
b) New operator can allocate functions as well as pointer to functions
c) New operator can allocate any type of functions
d) New operator is not applicable with functions allocation
View Answer

Answer: a
Explanation: The new operator can’t allocate functions but can allocate pointer to the functions. This is a security feature as well as to reduce the ambiguity in code. The new keyword is not given functionality to directly allocate any function.
advertisement

10. Which among the following is added in grammar of new operator?
a) Finalize
b) Arg
c) Initializer
d) Allocator
View Answer

Answer: c
Explanation: The new operator grammar is added with an initializer field. This can be used to initialize an object with a user defined constructor. Hence can allocate memory as intended by the programmer.

11. Initializers __________________
a) Are used for specifying arrays
b) Are used to defined multidimensional arrays
c) Can’t be specified for arrays
d) Can’t be specified for any data
View Answer

Answer: c
Explanation: The initializers can’t be specified for arrays. The initializers can create arrays of object if and only if the class has a default constructor. That is a zero argument constructor so that it can be called without any argument.

12. The objects allocated using new operator ________________
a) Are destroyed when they go out of scope
b) Are not destroyed even if they go out of scope
c) Are destroyed anytime
d) Are not destroyed throughout the program execution
View Answer

Answer: b
Explanation: It is not necessary that the objects get destroyed when they go out of scope if allocated by using new operator. This is because new operator returns a pointer to object that it had allocated. A suitable pointer with proper scope should be defined by the programmer explicitly.

13. The new operator _________________
a) Invokes function operator new
b) Doesn’t invoke function operator new
c) Invokes function operator only if required
d) Can’t invoke function operator new implicitly
View Answer

Answer: a
Explanation: The new operator invokes function operator new. This is done to allocate the storage to an object. ::operator new is called for storage allocation implicitly.

14. If a new operator is defined for a class and still global new operator have to be used, which operator should be used with the keyword new?
a) Colon
b) Arrow
c) Dot
d) Scope resolution
View Answer

Answer: d
Explanation: As usual, scope resolution operator is used to get the scope of parent or the global entities. Hence we can use scope resolution operator with the new operator to call the global new operator even if new operator is defined for the class explicitly.

15. How does compiler convert “::operator new” implicitly?
a) ::operator new( sizeof( type ) )
b) ::operator new( sizeof( ) )
c) new operator :: type sizeof( type )
d) new sizeof( type ) operator
View Answer

Answer: a
Explanation: The compiler implicitly converts the syntax so that the instruction can be understood by the processor and proper machine code can be generated. The conversion is done implicitly and no explicit syntax is required.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.