Unit 5 - Practice Quiz

CSE202

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

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

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

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

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

A. It returns NULL by default
B. It throws a std::bad_alloc exception
C. It terminates the program immediately
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 = 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. 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. Pointer to the allocated type
C. Reference to the allocated type
D. int

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

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

10 What is Compile-time Polymorphism also known as?

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

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

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

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. Associating a function call to a function definition at runtime
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. Stack unwinding
B. Virtual Table (vtable)
C. Macro expansion
D. Template instantiation

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

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

17 What is a Virtual Destructor used for?

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

18 What is a Pure Virtual Function?

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

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

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

20 Can you instantiate an object of an Abstract Class?

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

21 What is a Concrete Class?

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

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 is allowed
B. It would cause infinite size/recursion definition
C. Compiler cannot name it
D. It violates encapsulation

24 What is a Dynamic Constructor?

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

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

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

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

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

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

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

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

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

29 Function overriding requires which of the following?

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

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. When the class is defined
C. At compile time
D. At the start of the program (before main)

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

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

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

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

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. Both
D. None

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. Compiler Error
D. Random

36 What is the overhead of using virtual functions?

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

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

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

38 Can a static member function be virtual?

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

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

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

40 What is 'dangling pointer'?

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

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

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

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

A. Points to the parent class
B. Points to the virtual table of the class
C. Points to the heap memory
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 only static members
B. A class with only pure virtual functions
C. A class with no functions
D. A concrete class

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

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

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. Yes
B. No
C. Only if they are static
D. Only in protected mode

49 Dynamic binding typically reduces execution speed slightly because:

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

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

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