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. new
B. create
C. malloc
D. alloc

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

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

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

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

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

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

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

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

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

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

7 What is a memory leak?

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

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

A. void
B. Reference to the allocated type
C. int
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. Yes, it does nothing
C. No, it causes a compilation error
D. No, it causes a runtime crash

10 What is Compile-time Polymorphism also known as?

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

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

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

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

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

13 What is 'Early Binding'?

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

14 What is 'Late Binding'?

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

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

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

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

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

17 What is a Virtual Destructor used for?

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

18 What is a Pure Virtual Function?

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

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

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

20 Can you instantiate an object of an Abstract Class?

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

21 What is a Concrete Class?

A. A class that cannot be inherited from
B. A class made of stone
C. A class with no virtual functions
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 cannot be pointed to
D. A class that inherits from itself

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

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

24 What is a Dynamic Constructor?

A. A constructor that is virtual
B. A constructor that changes its name at runtime
C. A constructor that calls other constructors
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. A global variable
B. A static function
C. A destructor to free memory
D. An inline function

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

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

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

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

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

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

29 Function overriding requires which of the following?

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

30 Which operator cannot be overloaded?

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

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

A. At the start of the program (before main)
B. When the object is created
C. At compile time
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. Ignore it
D. printf error

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

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

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. Derived class show()
C. None
D. Both

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

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

36 What is the overhead of using virtual functions?

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

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

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

38 Can a static member function be virtual?

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

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

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

40 What is 'dangling pointer'?

A. A pointer initialized to 0
B. A pointer to a virtual function
C. A pointer pointing to NULL
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. Object Type
B. Execution history
C. Function Signature
D. Return Type

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

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

43 Deep copy is recommended over shallow copy when:

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

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

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

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

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

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. All destructors are called
B. Compilation error
C. The heap is cleared entirely
D. Only the first object's destructor is called

48 Can friend functions be virtual?

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

49 Dynamic binding typically reduces execution speed slightly because:

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

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

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