This set of Object Oriented Programming using C++ test focuses on “Memory Allocation of Object”.
1. What does memory allocation for objects mean?
a) Actual creation and memory allocation for object members
b) Creation of member functions
c) Creation of data members for a class
d) Actual creation and data declaration for object members
View Answer
Explanation: The memory allocated for the object members indicates actual creation of the object members. This is known as memory allocation for object.
2. Where is the memory allocated for the objects?
a) HDD
b) Cache
c) RAM
d) ROM
View Answer
Explanation: The memory for the objects or any other data is allocated in RAM initially. This is while we run a program all the memory allocation takes place in some RAM segments. Arrays in heap and local members in stack etc.
3. 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
View Answer
Explanation: 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.
4. 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
View Answer
Explanation: 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.
5. Which of the following function can be used for dynamic memory allocation of objects?
a) malloc()
b) calloc()
c) create()
d) malloc() and calloc()
View Answer
Explanation: 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.
6. How much memory will be allocated for an object of class given below?
class Test { int mark1; int mark2; float avg; char name[10]; };
a) 22 Bytes
b) 24 Bytes
c) 20 Bytes
d) 18 Bytes
View Answer
Explanation: The size of an object of the class given in question will be of size 22 bytes. This is because the size of an object is always equal to the sum of sizes of the data members of the class, except static members.
7. Which keyword among the following can be used to declare an array of objects in java?
a) new
b) create
c) allocate
d) arr
View Answer
Explanation: 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.
8. 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
View Answer
Explanation: 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.
9. 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
View Answer
Explanation: 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.
10. Which function is called whenever an object goes out of scope?
a) Destructor function
b) Constructor function
c) Delete function
d) Free function
View Answer
Explanation: 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.
11. Which operator can be used to check the size of an object?
a) sizeof(objectName)
b) size(objectName)
c) sizeofobject(objectName)
d) sizedobject(objectName)
View Answer
Explanation: 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.
12. 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
View Answer
Explanation: 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.
13. If an object is declared in a user defined function __________________
a) Its memory is allocated in stack
b) Its memory is allocated in heap
c) Its memory is allocated in HDD
d) Its memory is allocated in cache
View Answer
Explanation: The memory for any data or object that are used in a user defined function are always allocated in the stack. This is to ensure that the object is destroyed as soon as the function is returned. Also this ensures that the correct memory allocation and destruction is performed.
14. In java, ____________________ takes care of managing memory for objects dynamically.
a) Free collector
b) Dust collector
c) Memory manager
d) Garbage collector
View Answer
Explanation: The garbage collector in java takes care of the memory allocations and their deletions dynamically. When an object is no more required then the garbage collector deletes the object and free up all the resources that were held by that object.
15. Which operator can be used to free the memory allocated for an object in C++?
a) Free()
b) delete
c) Unallocate
d) Collect
View Answer
Explanation: 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++.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming using C++ for tests, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice MCA MCQs
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Check Object Oriented Programming Books