0% found this document useful (0 votes)
26 views

OOP - DSWT - CPP (Solved)

The document contains multiple choice questions, true/false questions, fill-in-the-blank questions, and code snippets related to Object-Oriented Programming (OOP) concepts in C++. It covers topics such as constructors, encapsulation, inheritance, polymorphism, and method overloading/overriding. Additionally, it includes examples of code errors and their corrections, along with expected outputs.

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)
26 views

OOP - DSWT - CPP (Solved)

The document contains multiple choice questions, true/false questions, fill-in-the-blank questions, and code snippets related to Object-Oriented Programming (OOP) concepts in C++. It covers topics such as constructors, encapsulation, inheritance, polymorphism, and method overloading/overriding. Additionally, it includes examples of code errors and their corrections, along with expected outputs.

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: B
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: B
3. Encapsulation in Object-Oriented Programming is achieved using:
A) Interfaces B) Member functions C) Access Specifiers D) Constructors
Answer: C
4. Which of the bellow functions should always be Public?
A) Constructor B) Getter C) Add D) Abstract functions
Answer: B
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: B
6. Which OOP concept does this C++ code snippet demonstrate?
A) Inheritance
B) Abstraction
C) Method Over-riding
D) Method Over-loading
Answer: A
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: D

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


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

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: D

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: B
True/False Questions

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

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


Answer: False
(True Answer: A constructor in c++ cannot return a value; it is only for initializing an object.
Constructor doesn’t have a return type)

13. Encapsulation ensures data hiding by making variables private.


Answer: True

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


Answer: True

15. Class is an instance of an Object.


Answer: False
(True Answer: An Object is an instance of a Class.)

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


Answer: False
(True Answer: Protected variables or functions are intended to be accessed only within the class and
its subclasses.)

17. Private members of a class can be accessed by anyone outside the class, but not by other members of
the class.
Answer: False
(True Answer: Private members of a class cannot be accessed directly outside the class but can be
accessed within the class and its methods. They can also be accessed outside the class indirectly
through public getter or setter methods.).

18. A child class in c++ can access private members of its parent class directly.
Answer: False
(True Answer: A child class in c++ can access only public members of its parent class directly,
private members can be accessed only with in the class.)

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

20. Overloading methods is implemented by defining multiple methods with the same parameters.
Answer: False
(True Answer: Overloading methods is implemented by defining multiple methods with the same
name but different parameters.)
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: constructor, void

22. Function ____________ occurs when multiple methods with the same name but different parameters
are used in a class.
Answer: Function Over-Loading / Compile-time Polymorphism

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

24. Method Over-Riding is also called __________________.


Answer: Run-Time Polymorphism

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

26. Changing a function in a derived class to provide a new function replacing the base class's function,
is called ________________.
Answer: Function Over-Riding / Run-time Polymorphism

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

28. Derived class is also known as ________ class.


Answer: child

29. Method Over-Loading is also called ________________.


Answer: Compile-Time Polymorphism

30. There are ______ Pillars of OOP (Object Oriented Programming) _________, _________,
_________ and __________.
Answer: 4, Polymorphism, Inheritance, Abstraction and Encapsulation.
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: Constructor can’t have a return type.
Output: >>> “Toyota”
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: Object of class Calculator is not created but are trying to use. Calculator calc
Output:
>>> 5
>>> 9
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: Constructor of a class should always be same name as the class. Hence, here it should be
Person.
Output: ‘Alice’
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: No error
Output: ‘Woof Barks’
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: Second argument in Constructor call (Object) missing.
Output: ‘Car make: Toyota, Model: Camry’

You might also like