0% found this document useful (0 votes)
12 views7 pages

OOP - DSWT - CPP

The document contains a series of multiple-choice questions, true/false questions, fill-in-the-blank questions, and coding exercises related to Object-Oriented Programming (OOP) concepts in C++. It covers topics such as constructors, encapsulation, inheritance, polymorphism, and method overloading in C++. Additionally, it includes code snippets that require debugging and output demonstration.

Uploaded by

islamabad.rally
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)
12 views7 pages

OOP - DSWT - CPP

The document contains a series of multiple-choice questions, true/false questions, fill-in-the-blank questions, and coding exercises related to Object-Oriented Programming (OOP) concepts in C++. It covers topics such as constructors, encapsulation, inheritance, polymorphism, and method overloading in C++. Additionally, it includes code snippets that require debugging and output demonstration.

Uploaded by

islamabad.rally
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/ 7

1/10/2025 OOP Practice

DSWT – 2 (C++)

Syed Muhammad Zafar Rizvi


HAMDARD UNIVERISTY
Multiple Choice Questions (MCQs)
1. In C++, what is the purpose of a constructor?
A) To define methods in a class B) To initialize an object’s attributes
C) To destroy objects D) To inherit methods from a parent class
Answer:
2. Which method is called automatically when an object is created in C++?
A) Both B and D B) Constructor C) All Methods D) Destructor
Answer:
3. Encapsulation in Object-Oriented Programming is achieved using:
A) Interfaces B) Member functions C) Access Specifiers D) Constructors
Answer:
4. Which of the bellow functions should always be Public?
A) Constructor B) Getter C) Add D) Abstract functions
Answer:
5. What type of function in C++ is declared with = 0 and has no body?
A) Getter Function B) Abstract Function (Pure Virtual Function)
C) Static Function D) Constructor
Answer:
6. Which OOP concept does this C++ code snippet demonstrate?
A) Inheritance
B) Abstraction
C) Method Over-riding
D) Method Over-loading
Answer:
7. What does the following C++ code snippet do? (Figure 1)
A) Prints "A" B) Prints "B"
C) Raises an error D) Prints both "A" and "B"
Answer:

8. Which OOP concept does this C++ code snippet demonstrate?


A) Method Overloading B) Inheritance
C) Method Overriding D) Constructor
Answer:

9. Which of the following statements is correct about the protected


member of class in C++?
A) It is used to initialize the child class constructor
B) It gives access to the parent class's methods and attributes
C) It can be accessed only with in the class
D) It can be accessed with in the class and its child classes
Answer:

10. Polymorphism allows:


A) Multiple objects to have the same name
B) One method to perform different tasks
C) Multiple methods to have the same name but different parameters
D) None of the above
Answer:
True/False Questions

11. Private members of a class can only be accessed within the same class.
Answer:

12. A constructor in c++ can return a value.


Answer:

13. Encapsulation ensures data hiding by making variables private.


Answer:

14. Inheritance is a feature of OOP, which is based on reusability of code.


Answer:

15. Class is an instance of an Object.


Answer:

16. Protected variables or functions can be accessed outside of the class.


Answer:

17. Private members of a class can be accessed by anyone outside the class, but not by other members of
the class.
Answer:

18. A child class in c++ can access private members of its parent class directly.
Answer:

19. A method defined in child class overrides the method of parent class if they have same name.
Answer:

20. Overloading methods is implemented by defining multiple methods with the same parameters.
Answer:
Fill in the Blanks

21. It is necessary for every function except ____________ to have a return type, if we don’t want to
return anything from a function it’s return type would be ___________.
Answer:

22. Function ____________ occurs when multiple methods with the same name but different parameters
are used in a class.
Answer:

23. In OOP, the concept of _____________ allows a class to define structure, without providing the
implementation of its functions.
Answer:

24. Method Over-Riding is also called __________________.


Answer:

25. We can access all _________ members (functions & variables) inside the class and it’s child classes.
Answer:

26. Changing a function in a derived class to provide a new function replacing the base class's function,
is called ________________.
Answer:

27. When creating an object of a class, the auto-call function used to initialize the object is called _____.
Answer:

28. Derived class is also known as ________ class.


Answer:

29. Method Over-Loading is also called ________________.


Answer:

30. There are ______ Pillars of OOP (Object Oriented Programming) _________, _________,
_________ and __________.
Answer:
Kill the Bug
Find and correct the errors also show the output.
31.
#include <iostream>
using namespace std;

class Car {
public:
string make;
string model;

void Car(string make, string model) {


this->make = make;
this->model = model;
}
};

int main() {
Car car1("Toyota", "Camry");
cout << car1.make << endl;
return 0;
}
Answer:
Output:
32.
#include <iostream>
using namespace std;

class Calculator {
public:
int add(int a, int b) {
return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
};

int main() {
cout << calc.add(2, 3) << endl;
cout << calc.add(2, 3, 4) << endl;
return 0;
}
Answer:
Output:
33.
#include <iostream>
using namespace std;

class Person {
public:
string name;
Constructor(string name) {
this->name = name;
}
};

int main() {
Person p("Alice");
cout << p.name << endl;
return 0;
}
Answer:
Output:
34.
#include <iostream>
using namespace std;

class Animal {
public:
string sound;
Animal(string sound) {
this->sound = sound;
}
virtual void speak() {
cout << sound << " makes a sound" << endl;
}
};

class Dog : public Animal {


public:
Dog(string sound) : Animal(sound) {}
void speak() override {
cout << sound << " barks" << endl;
}
};

int main() {
Dog dog("Woof");
dog.speak();
return 0;
}
Answer:
Output:
35.
#include <iostream>
using namespace std;

class Car {
public:
string make;
string model;
Car(string make, string model) {
this->make = make;
this->model = model;
}

string display() {
return "Car make: " + make + ", Model: " + model;
}
};

int main() {
Car car("Toyota");
cout << car.display() << endl;
return 0;
}
Answer:
Output:

You might also like