Unit 5: Dynamic Memory Management and Polymorphism - Practice Quiz

CSE202 — Object Oriented Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which operator is used in C++ to allocate memory dynamically at runtime?

A. create
B. alloc
C. new
D. malloc

2 Which operator is used to release memory previously allocated by 'new'?

A. remove
B. free
C. delete
D. dealloc

3 What happens if the 'new' operator fails to allocate memory?

A. It throws a std::bad_alloc exception
B. It terminates the program immediately
C. It returns NULL by default
D. It returns a void pointer

4 Which syntax is correct for dynamically allocating an array of 10 integers?

A. int *arr = new int[10];
B. int arr[10] = new int;
C. int arr = new int(10);
D. int *arr = new int(10);

5 How do you correctly deallocate an array allocated with 'new int[10]'?

A. remove arr;
B. delete[] arr;
C. delete arr;
D. delete[10] arr;

6 Where is memory allocated when using the 'new' operator?

A. Heap (Free Store)
B. Stack
C. Data Segment
D. Code Segment

7 What is a memory leak?

A. When a program uses too much RAM
B. When dynamically allocated memory is not freed
C. When the stack overflows
D. When a pointer points to NULL

8 What is the return type of the 'new' operator?

A. void
B. int
C. Reference to the allocated type
D. Pointer to the allocated type

9 Is it safe to use 'delete' on a NULL pointer?

A. Yes, but it prints a warning
B. No, it causes a compilation error
C. No, it causes a runtime crash
D. Yes, it does nothing

10 What is Compile-time Polymorphism also known as?

A. Static Binding
B. Dynamic Binding
C. Late Binding
D. Virtual Binding

11 Which of the following is an example of Compile-time Polymorphism?

A. Dynamic Casting
B. Abstract Classes
C. Function Overloading
D. Virtual Functions

12 Which keyword is used to enable Run-time Polymorphism in C++?

A. friend
B. static
C. virtual
D. inline

13 What is 'Early Binding'?

A. The function call is resolved at compile time
B. The memory is allocated at startup
C. The function call is ignored
D. The function call is resolved at runtime

14 What is 'Late Binding'?

A. Associating a function call to a function definition at runtime
B. Binding variables to types
C. Linking object files late in the build process
D. Declaring variables at the end of a function

15 Which mechanism does C++ use to implement Late Binding?

A. Template instantiation
B. Macro expansion
C. Virtual Table (vtable)
D. Stack unwinding

16 Can a C++ constructor be declared 'virtual'?

A. No
B. Yes
C. Only in abstract classes
D. Only if the destructor is also virtual

17 What is a Virtual Destructor used for?

A. To ensure the derived class destructor is called when deleting via a base pointer
B. To prevent memory leaks in stack memory
C. To delete static variables
D. To faster delete objects

18 What is a Pure Virtual Function?

A. A virtual function initialized to 0 with no body in the base class
B. A function that must be defined in the base class
C. A function that returns void
D. A function with no return type

19 A class containing at least one pure virtual function is called:

A. Static Class
B. Concrete Class
C. Abstract Class
D. Virtual Class

20 Can you instantiate an object of an Abstract Class?

A. Only if it has a constructor
B. Only using pointers
C. Yes
D. No

21 What is a Concrete Class?

A. A class that cannot be inherited from
B. A class with no virtual functions
C. A class made of stone
D. A class that implements all inherited pure virtual functions

22 What is a Self-Referential Class?

A. A class that calls its own main function
B. A class containing a pointer to an object of the same class type
C. A class that inherits from itself
D. A class that cannot be pointed to

23 Why can't a class contain an object of itself as a member variable?

A. It would cause infinite size/recursion definition
B. It violates encapsulation
C. Compiler cannot name it
D. It is allowed

24 What is a Dynamic Constructor?

A. A constructor that changes its name at runtime
B. A constructor that calls other constructors
C. A constructor that is virtual
D. A constructor that allocates memory using 'new'

25 If a class uses dynamic memory allocation in its constructor, what should usually be implemented?

A. An inline function
B. A global variable
C. A destructor to free memory
D. A static function

26 What is the syntax to declare a pure virtual function?

A. virtual void display() {};
B. virtual void display() null;
C. virtual void display() = 0;
D. void display() = 0;

27 If a derived class does not override a pure virtual function from the base class:

A. The program crashes at runtime
B. The compiler generates a default implementation
C. It is treated as a normal virtual function
D. The derived class also becomes an abstract class

28 Which pointer is used implicitly to access the object's members inside a member function?

A. object
B. self
C. this
D. me

29 Function overriding requires which of the following?

A. Static keyword
B. Different function names
C. Different parameters
D. Same function name and same signature in base and derived class

30 Which operator cannot be overloaded?

A. ==
B. new
C. sizeof
D. +

31 When is the memory allocated for a static data member?

A. When the object is created
B. At compile time
C. At the start of the program (before main)
D. When the class is defined

32 Which of the following best describes a memory allocation failure handling method?

A. return 0
B. std::nothrow
C. printf error
D. Ignore it

33 In C++, polymorphism is mainly divided into:

A. Single and Multiple
B. Public and Private
C. Internal and External
D. Compile-time and Run-time

34 If you have a pointer 'Base *b = new Derived();', which function is called for 'b->show()' if show is NOT virtual?

A. Base class show()
B. Both
C. None
D. Derived class show()

35 If you have a pointer 'Base *b = new Derived();', which function is called for 'b->show()' if show IS virtual?

A. Random
B. Derived class show()
C. Compiler Error
D. Base class show()

36 What is the overhead of using virtual functions?

A. Increased memory for vptr and vtable lookup time
B. Cannot use recursion
C. None
D. Compilation takes longer only

37 Which data structure is typically used to implement a Self-Referential class?

A. Stack (Static)
B. Linked List
C. Array
D. Queue (Static)

38 Can a static member function be virtual?

A. Sometimes
B. If it returns void
C. No
D. Yes

39 Which of the following creates an object in the stack?

A. new MyClass;
B. MyClass obj;
C. MyClass *obj = new MyClass();
D. malloc(sizeof(MyClass));

40 What is 'dangling pointer'?

A. A pointer initialized to 0
B. A pointer pointing to NULL
C. A pointer to a virtual function
D. A pointer pointing to a memory location that has been deleted

41 The mechanism of selecting the appropriate function at compile time is based on:

A. Execution history
B. Function Signature
C. Return Type
D. Object Type

42 What is the role of the vptr (virtual pointer)?

A. Points to the heap memory
B. Points to the virtual table of the class
C. Points to the parent class
D. Points to the stack memory

43 Deep copy is recommended over shallow copy when:

A. The class has no pointers
B. The class uses dynamic memory allocation
C. The class is small
D. The class is static

44 In C++, an interface is best simulated by:

A. A class with no functions
B. A class with only pure virtual functions
C. A concrete class
D. A class with only static members

45 If a destructor is virtual in the base class, is it automatically virtual in the derived class?

A. Only if explicitly declared
B. Yes
C. Only in multiple inheritance
D. No

46 Which operator is used to access members of a class using a pointer to an object?

A. .
B. ->
C. :
D. ::

47 What happens if you forget [] when deleting an array of objects?

A. Only the first object's destructor is called
B. All destructors are called
C. Compilation error
D. The heap is cleared entirely

48 Can friend functions be virtual?

A. No
B. Only if they are static
C. Only in protected mode
D. Yes

49 Dynamic binding typically reduces execution speed slightly because:

A. The code is interpreted
B. Pointer dereferencing takes time
C. Memory is fragmented
D. The compiler sleeps

50 To prevent a class from being inherited, C++11 introduced which keyword?

A. sealed
B. stop
C. final
D. end