Unit 5 - Practice Quiz

CSE202 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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

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

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

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

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

A. It throws a std::bad_alloc exception
B. It returns NULL by default
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[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[10] arr;
D. delete[] arr;

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

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

7 What is a memory leak?

A. When the stack overflows
B. When a program uses too much RAM
C. When dynamically allocated memory is not freed
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. No, it causes a runtime crash
B. No, it causes a compilation error
C. Yes, it does nothing
D. Yes, but it prints a warning

10 What is Compile-time Polymorphism also known as?

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

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

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

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

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

13 What is 'Early Binding'?

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

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. Virtual Table (vtable)
B. Macro expansion
C. Stack unwinding
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 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 that must be defined in the base class
B. A function with no return type
C. A function that returns void
D. A virtual function initialized to 0 with no body in the base class

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

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

20 Can you instantiate an object of an Abstract Class?

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

21 What is a Concrete Class?

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

22 What is a Self-Referential Class?

A. A class that cannot be pointed to
B. A class containing a pointer to an object of the same class type
C. A class that calls its own main function
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 violates encapsulation
C. It is allowed
D. It would cause infinite size/recursion definition

24 What is a Dynamic Constructor?

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

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

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

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

A. virtual void display() null;
B. virtual void display() {};
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 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. object
C. me
D. this

29 Function overriding requires which of the following?

A. Same function name and same signature in base and derived class
B. Different parameters
C. Different function names
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. At compile time
B. At the start of the program (before main)
C. When the object is created
D. When the class is defined

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

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

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

A. Compile-time and Run-time
B. Internal and External
C. Single and Multiple
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. None
B. Derived class show()
C. Both
D. Base 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. Base class show()
D. Compiler Error

36 What is the overhead of using virtual functions?

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

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

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

38 Can a static member function be virtual?

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

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

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

40 What is 'dangling pointer'?

A. A pointer pointing to a memory location that has been deleted
B. A pointer pointing to NULL
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. Return Type
B. Execution history
C. Function Signature
D. Object Type

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 stack memory
D. Points to the heap memory

43 Deep copy is recommended over shallow copy when:

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

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

A. A concrete class
B. A class with no functions
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. Only if explicitly declared
B. No
C. Yes
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. The heap is cleared entirely
B. All destructors are called
C. Compilation error
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. Yes
D. No

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. sealed
B. end
C. final
D. stop