OOP - DSWT - CPP (Solved)
OOP - DSWT - CPP (Solved)
DSWT – 2 (C++)
11. Private members of a class can only be accessed within the same class.
Answer: True
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
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
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;
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 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;
}
};
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’