Test Series - oops

Test Number 52/78

Q: When is the memory allocated for an object?
A. At declaration of object
B. At compile time
C. When object constructor is called
D. When object is initialized to another object
Solution: The object memory allocation takes place when the object constructor is called. Declaration of an object doesn’t mean that memory is allocated for its members. If object is initialized with another object, it may just get a reference to the previously created object.
Q: Using new is type safe as _______________________
A. It require to be specified with type of data
B. It doesn’t require to be specified with type of data
C. It requires the name of data
D. It allocated memory for the data
Solution: The new is type safe because we don’t have to specify the type of data that have to be allocated with memory. We can directly use it with data name. Name of the data doesn’t matter though for type of memory allocation though.
Q: Which of the following function can be used for dynamic memory allocation of objects?
A. malloc()
B. calloc()
C. create()
D. both malloc() and calloc()
Solution: The malloc() function can be used to allocate dynamic memory for objects. Function calloc() can also be use. These functions differ in the way they allocate memory for objects.
Q: Which keyword among the following can be used to declare an array of objects in java?
A. new
B. create
C. allocate
D. arr
Solution: The keyword new can be used to declare an array of objects in java. The syntax must be specified with an object pointer which is assigned with a memory space containing the required number of object space. Even initialization can be done directly.
Q: When is the memory allocated for an object gets free?
A. At termination of program
B. When object goes out of scope
C. When main function ends
D. When system restarts
Solution: Whenever an object goes out of scope, the deletion of allocation memory takes place. Actually the data is not deleted, instead the memory space is flagged to be free for further use. Hence whenever an object goes out of scope the object members become useless and hence memory is set free.
Q: Which among the following keyword can be used to free the allocated memory for an object?
A. delete
B. free
C. either delete or free
D. only delete
Solution: The memory allocated for an object is usually automatically made free. But if explicitly memory has to be made free then we can use either free or delete keywords depending on programming languages.
Q: Which function is called whenever an object goes out of scope?
A. Destructor function
B. Constructor function
C. Delete function
D. Free function
Solution: The destructor function of the class is called whenever an object goes out of scope. This is because the destructor set all the resources, acquired by the object, free. This is an implicit work of compiler.
Q: Which operator can be used to check the size of an object?
A. sizeof(objectName)
B. size(objectName)
C. sizeofobject(objectName)
D. sizedobject(objectName)
Solution: The sizeof operator is used to get the size of an already created object. This operator must constail keyword sizeof(objectName). The output will give the number of bytes acquired by a single object of some class.
Q: The memory allocated for an object ____________________
A. Can be only dynamic
B. Can be only static
C. Can be static or dynamic
D. Can’t be done using dynamic functions
Solution: The memory allocation for an object can be static or dynamic. The static memory allocation is when an object is declared directly without using any function usually. And dynamic allocation is when we use some dynamic allocation function to allocate memory for data member of an object.
Q: Which operator can be used to free the memory allocated for an object in C++?
A. Free()
B. delete
C. Unallocate
D. Collect
Solution: The delete operator in C++ can be used to free the memory and resources held by an object. The function can be called explicitly whenever required. In C++ memory management must be done by the programmer. There is no automatic memory management in C++.

You Have Score    /10