0% found this document useful (0 votes)
2 views23 pages

PF Practice

The document contains multiple-choice questions (MCQs) covering key concepts in C++ programming, including constructors, destructors, pointers, function overloading, polymorphism, inheritance, and memory management. Each question is followed by the correct answer, providing a comprehensive review of object-oriented programming principles. Additionally, it includes dry run examples demonstrating the execution flow and behavior of C++ OOP concepts.

Uploaded by

ammadaziz12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

PF Practice

The document contains multiple-choice questions (MCQs) covering key concepts in C++ programming, including constructors, destructors, pointers, function overloading, polymorphism, inheritance, and memory management. Each question is followed by the correct answer, providing a comprehensive review of object-oriented programming principles. Additionally, it includes dry run examples demonstrating the execution flow and behavior of C++ OOP concepts.

Uploaded by

ammadaziz12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

MCQS

Constructors & Destructors

1. Which of the following is true about constructors?


a) They return void
b) They can be overloaded
c) They must be virtual
d) They are called manually

Answer: b) They can be overloaded

2. Which type of constructor takes no parameters?


a) Copy constructor
b) Default constructor
c) Parameterized constructor
d) Virtual constructor

Answer: b) Default constructor

3. What is the purpose of a destructor?


a) Initialize class data members
b) Release resources before object is destroyed
c) Overload a function
d) Handle inheritance

Answer: b) Release resources before object is destroyed

4. Which symbol precedes a destructor name?


a) *
b) &
c) ~
d) %
Answer: c) ~

💡 Pointers

5. What does a pointer store?


a) Value of a variable
b) Address of a variable
c) Class name
d) Data type

Answer: b) Address of a variable

6. What is the correct syntax to declare a pointer to an int?


a) int ptr;
b) int *ptr;
c) int& ptr;
d) int pointer;

Answer: b) int *ptr;

7. Which operator is used to access the value at the pointer address?


a) &
b) *
c) ->
d) %

Answer: b) *

8. Which operator gives the address of a variable?


a) *
b) &
c) ->
d) @
Answer: b) &

🧰 Function Overloading

9. Function overloading is an example of:


a) Runtime polymorphism
b) Static polymorphism
c) Virtual function
d) Operator overloading

Answer: b) Static polymorphism

10. Which is true about overloaded functions?


a) Must have different return types
b) Must have different names
c) Must differ in parameter list
d) Cannot exist in the same class

Answer: c) Must differ in parameter list

🌀 Polymorphism

11. Polymorphism allows:


a) Many functions to act as one
b) One function to act in many ways
c) A class to inherit from multiple base classes
d) Classes to have multiple constructors

Answer: b) One function to act in many ways

12. Which of the following supports runtime polymorphism?


a) Function overloading
b) Operator overloading
c) Virtual functions
d) Constructors

Answer: c) Virtual functions

13. Which keyword is used to achieve dynamic binding?


a) override
b) static
c) virtual
d) friend

Answer: c) virtual

14. What is the output of calling a virtual function through a base


class pointer?
a) Base class version always
b) Derived class version, if overridden
c) Compile error
d) No effect

Answer: b) Derived class version, if overridden

🧱 General OOP Concepts

15. Which of the following is not an OOP principle?


a) Encapsulation
b) Inheritance
c) Compilation
d) Polymorphism

Answer: c) Compilation
16. Which concept allows reusing code from existing classes?
a) Polymorphism
b) Inheritance
c) Encapsulation
d) Abstraction

Answer: b) Inheritance

17. Encapsulation in C++ is achieved using:


a) Classes and objects
b) Only functions
c) Arrays and pointers
d) Macros

Answer: a) Classes and objects

18. Which access specifier makes members accessible only


within the same class?
a) public
b) private
c) protected
d) global

Answer: b) private

19. Which class member can be accessed by derived classes but


not outside the class?
a) private
b) protected
c) public
d) static

Answer: b) protected
20. Which concept hides internal details and shows only essential
features?
a) Inheritance
b) Encapsulation
c) Abstraction
d) Polymorphism

Answer: c) Abstraction

Constructors & Destructors (Advanced)

1. Which constructor is invoked when an object is initialized with


another object of the same class?
a) Default constructor
b) Copy constructor
c) Move constructor
d) Destructor

Answer: b) Copy constructor

2. When is a destructor automatically called?


a) When the class is created
b) When the object goes out of scope
c) During constructor overloading
d) Only if manually called

Answer: b) When the object goes out of scope

3. Which constructor is used when an r-value (temporary object) is


passed?
a) Copy constructor
b) Move constructor
c) Default constructor
d) Parameterized constructor

Answer: b) Move constructor

🔗 Inheritance

4. Which inheritance type results in a "diamond problem"?


a) Single inheritance
b) Multilevel inheritance
c) Multiple inheritance
d) Hybrid inheritance

Answer: c) Multiple inheritance

5. Which keyword is used to prevent a class from being inherited?


a) final
b) sealed
c) private
d) static

Answer: a) final (in C++11 and above)

6. In case of private inheritance:


a) Public and protected members of the base class become private in
derived class
b) All members become protected
c) Private members become public
d) Nothing is inherited

Answer: a) Public and protected members of the base class become private
in derived class
🧬 Polymorphism

7. Polymorphism allows functions to behave:


a) Only at runtime
b) Only at compile-time
c) Differently based on parameters or object type
d) Like templates

Answer: c) Differently based on parameters or object type

8. Which is a requirement for a base class function to support


polymorphism?
a) It must be static
b) It must be inline
c) It must be virtual
d) It must be overloaded

Answer: c) It must be virtual

9. What is the keyword used in a derived class to override a virtual


function from a base class (C++11 and later)?
a) virtual
b) override
c) extends
d) overwrite

Answer: b) override

🛠 Function Overloading & Overriding

10. Which of the following is not valid for function overloading?


a) Different number of parameters
b) Different types of parameters
c) Different return type only
d) Default arguments

Answer: c) Different return type only

11. Which of the following best describes function overriding?


a) Functions in different scopes
b) Base class and derived class functions with same name and
signature
c) Constructor overloading
d) Static function redefinition

Answer: b) Base class and derived class functions with same name and
signature

🎯 Pointers and Memory Management

12. Which operator is used to dynamically allocate memory in


C++?
a) malloc()
b) new
c) alloc
d) calloc()

Answer: b) new

13. What does the delete operator do?


a) Erases object data
b) Removes pointer variable
c) Deallocates memory allocated by new
d) Replaces constructor

Answer: c) Deallocates memory allocated by new


14. Which of the following correctly creates a pointer to an
object?
a) MyClass object = new MyClass;
b) MyClass *ptr = new MyClass;
c) MyClass ptr = *new MyClass();
d) MyClass *ptr = MyClass();

Answer: b) MyClass *ptr = new MyClass;

📐 Abstract Classes & Interfaces

15. Which of the following makes a class abstract?


a) At least one static function
b) A virtual function
c) A pure virtual function
d) A friend function

Answer: c) A pure virtual function

16. What is the syntax of a pure virtual function?


a) virtual void func() = default;
b) virtual void func() = 0;
c) void func();
d) virtual func();

Answer: b) virtual void func() = 0;

17. Can you instantiate an abstract class directly?


a) Yes
b) No
c) Only in static context
d) Only if it has constructors

Answer: b) No
📦 Miscellaneous OOP

18. Which of the following allows multiple functions with the


same name but different signatures?
a) Inheritance
b) Encapsulation
c) Function Overloading
d) Operator Overloading

Answer: c) Function Overloading

19. Which feature hides internal implementation details?


a) Inheritance
b) Encapsulation
c) Abstraction
d) Polymorphism

Answer: c) Abstraction

20. What happens if a class does not define a constructor?


a) It cannot be instantiated
b) Compiler provides a default constructor
c) Program will crash
d) You must create one

Answer: b) Compiler provides a default constructor

DRY RUN practice


Example 1: Constructor and Destructor Execution Order

cpp
CopyEdit
#include <iostream>
using namespace std;

class Base {
public:
Base() { cout << "Base Constructor\n"; }
~Base() { cout << "Base Destructor\n"; }
};

class Derived : public Base {


public:
Derived() { cout << "Derived Constructor\n"; }
~Derived() { cout << "Derived Destructor\n"; }
};

int main() {
Derived d;
return 0;
}

Dry Run:

• Derived d; triggers:
o Base Constructor
o Derived Constructor
• Upon exiting main():
o Derived Destructor
o Base Destructor

Output:

Base Constructor
Derived Constructor
Derived Destructor
Base Destructor

✅ Example 2: Pointer to Base with Virtual Function

#include <iostream>
using namespace std;

class Animal {
public:
virtual void sound() { cout << "Animal
sound\n"; }
};

class Dog : public Animal {


public:
void sound() override { cout << "Dog barks\n"; }
};

int main() {
Animal *a;
Dog d;
a = &d;
a->sound();
return 0;
}

Dry Run:

• Animal* a can point to any derived object.


• a = &d; means a points to a Dog.
• Because sound() is virtual, Dog's version is called.

Output:

Dog barks

✅ Example 3: Deep Copy vs Shallow Copy

#include <iostream>
#include <cstring>
using namespace std;

class Student {
char *name;
public:
Student(const char *n) {
name = new char[strlen(n) + 1];
strcpy(name, n);
}

Student(const Student &s) {


name = new char[strlen(s.name) + 1];
strcpy(name, s.name);
}

void changeName(const char *newName) {


strcpy(name, newName);
}

void print() {
cout << name << endl;
}

~Student() {
delete[] name;
}
};

int main() {
Student s1("John");
Student s2 = s1; // Deep copy
s2.changeName("Alice");
s1.print();
s2.print();
return 0;
}
Dry Run:

• s1 is initialized with "John" — allocates memory.


• s2 = s1; — uses custom copy constructor → deep copy.
• s2.changeName("Alice"); modifies s2, not s1.

Output:

John
Alice

C++ OOP Concepts - Dry Run Worksheet (Part 2)

Instructions:

Perform a dry run for each code snippet. Clearly


indicate the flow of execution, object
creation/destruction, and function resolution.
Analyze how C++ OOP principles behave in each case.

1. Function Overloading vs Overriding

#include <iostream>
using namespace std;
class Base {
public:
void display(int x) { cout << "Base display: " <<
x << endl; }
};

class Derived : public Base {


public:
void display(double x) { cout << "Derived
display: " << x << endl; }
};

int main() {
Derived d;
d.display(5);
d.display(5.5);
return 0;
}

Q1: Which version of display() gets called and why?


Explain resolution rules.

Output:

Derived display: 5
Derived display: 5.5
2. Object Slicing in Assignment

#include <iostream>
using namespace std;

class Base {
public:
virtual void print() { cout << "Base print" <<
endl; }
};

class Derived : public Base {


public:
void print() override { cout << "Derived print"
<< endl; }
};

int main() {
Derived d;
Base b = d; // Object slicing
b.print();
return 0;
}

Q2: Why is Base::print() called despite Derived


overriding it? Explain object slicing.

Output:

Base print
3. Abstract Class and Pure Virtual Function

#include <iostream>
using namespace std;

class Shape {
public:
virtual void draw() = 0;
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};

int main() {
Shape* s = new Circle();
s->draw();
delete s;
return 0;
}

Q3: Explain how abstract classes and pure virtual


functions enforce interface behavior.

Output:
Drawing Circle

4. Copy Constructor vs Assignment Operator

#include <iostream>
#include <cstring>
using namespace std;

class Book {
char *title;
public:
Book(const char *t) {
title = new char[strlen(t) + 1];
strcpy(title, t);
}

Book(const Book &b) {


title = new char[strlen(b.title) + 1];
strcpy(title, b.title);
}

void operator=(const Book &b) {


if (this != &b) {
delete[] title;
title = new char[strlen(b.title) + 1];
strcpy(title, b.title);
}
}
void show() { cout << title << endl; }

~Book() {
delete[] title;
}
};

int main() {
Book b1("C++ Programming");
Book b2 = b1; // Copy constructor
Book b3("Temp");
b3 = b1; // Assignment operator
b1.show(); b2.show(); b3.show();
return 0;
}

Q4: Distinguish between copy constructor and


assignment operator. Track object states.

Output:

C++ Programming
C++ Programming
C++ Programming

5. Static Members and Functions

#include <iostream>
using namespace std;
class Counter {
private:
static int count;
public:
Counter() { count++; }
static void showCount() {
cout << "Count: " << count << endl;
}
};

int Counter::count = 0;

int main() {
Counter c1, c2, c3;
Counter::showCount();
return 0;
}

Q5: How many times is count incremented and why?


Explain static member behavior.

Output:

Count: 3

You might also like