This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “New Operator”.
1. What is 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
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 return 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
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
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.
4. If new throws an error, which function can be called to write a custom exception handler?
Explanation: If the default exception handler have 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
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) = new char;
b) char (pchar) = new char;
c) char (*char) = new char;
d) char (*char)= new char;
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
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.
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
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
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.
10. Which among the following is added in grammar of new operator?
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
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
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
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 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?
d) Scope resolution
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
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), here is complete set of 1000+ Multiple Choice Questions and Answers.