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

OOP 2 answers

The document contains a series of multiple-choice questions and programming exercises related to C++ concepts such as constructors, destructors, access specifiers, inheritance, and polymorphism. It includes specific questions about class member functions, object creation, and operator overloading. Additionally, it provides examples of class definitions and requires the reader to write function definitions based on given specifications.

Uploaded by

muhammedtarek160
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OOP 2 answers

The document contains a series of multiple-choice questions and programming exercises related to C++ concepts such as constructors, destructors, access specifiers, inheritance, and polymorphism. It includes specific questions about class member functions, object creation, and operator overloading. Additionally, it provides examples of class definitions and requires the reader to write function definitions based on given specifications.

Uploaded by

muhammedtarek160
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Constructors cannot have a(n) _________


a) arguments
b) return type
c) class name
2. The default access specification of class members is _________.
a) Protected
b) Private
c) Public
3. ________ is invoked to create an object.
a) A constructor
b) A destructor
c) A virtual method
d) A pure virtual function
4. The fields in a structure of a C++ program are by default
a) Protected
b) Private
c) Public
d) There’s no default
5. The default access specification of class members is
a) Protected
b) Private
c) Public
6. A ___________ pointer refers to an object that is currently invokes
a member function.
a) this
b) these
c) that
d) none of above
7. Destructor has the same name as the class and it is preceded by
a) !
b) ?
c) ~
d) $
1 A.T.G
8. A function with the same name as the class, but proceeded with a
tilde character (~) is called __________ of that class.
a) Constructor
b) Destructor
c) Function
d) Object
9. A constructor that accepts __________ parameters is called the
no-parameter constructor.
a) One
b) Two
c) Three
d) No
10 .Which of the following are NOT provided by the compiler by
default?
a) Zero-argument Constructor
b) Destructor
c) Copy Constructor
d) Copy Destructor
11. Abstract class cannot have_______________
a) Constructors.
b) Instances.
c) Member functions.
d) None of these option.
12. A pure virtual function is a function that:
a) has no body
b) can’t be overridden
c) must return void
d) takes no argument
13. ___________ means one name, multiple forms.
a) Polymorphism
b) Encapsulation
c) Data hiding
d) Operator overloading

2 A.T.G
14. If you want to write multiple functions in a class with same
name, then what C++ feature will you use?
a) Function overriding
b) Encapsulation
c) Function overloading
d) None
15. Which of the following is not a type of constructor?
a) Copy constructor
b) Friend constructor
c) Default constructor
d) Parameterized constructor
16. Which of the following is not the member of class?
a) Static function
b) Friend function
c) Const function
d) Virtual function
17. Which of the following cannot be friend?
a) Function
b) Class
c) Object
d) Operator function
18. Which constructor function is designed to copy objects?
a) Create constructor
b) Object constructor
c) Dynamic constructor
d) Copy constructor
19. Which of the following statement is correct?
a) Constructor has the same name as that of the class.
b) Destructor has the same name as that of the class with a tilde
symbol at the beginning.
c) Both A and B.
d) Destructor has the same name as the first member function of
the class.

3 A.T.G
20. Constructor __________ to allow different approaches of object
construction.
a) cannot overloaded
b) can be overloaded
c) can be inherited
d) can be nested
21. Which of the following is not an operator overloaded
a) pow
b) >>
c) +
d) <<
22. The correct function name for overloading the addition +
operator is
a) operator_+
b) operator:+
c) operator+
d) operator(+)
23. Which allows you to create a derived class that inherits properties
from more than one base class?
a) Multilevel inheritance
b) Multiple inheritance
c) Hybrid Inheritance
d) Hierarchical Inheritance
24. class derived: public base1, public base2 is an example of
a) Polymorphic inheritance
b) Multilevel inheritance
c) Hierarchical inheritance
d) Multiple inheritance
25. Concept of polymorphism is implemented using
a) Function overloading
b) Operator overloading
c) A and B

4 A.T.G
26. When we use the same function name in both the base and
derived classes, the function in base class should be declared as ___
a) Friend
b) Virtual
c) A and b
d) None of above

27. ________________ is a function declared in a base class that has


no definition relative to the base class.
a) Virtual function
b) Pure virtual function
c) Friend function
d) None of above
28. A virtual function, equated to zero is called_____________
a) Pure virtual function
b) Virtual function
c) Friend function
d) None of above
29. Object-oriented programming allows you to derive new classes
from existing classes. This is called ____________.
a) Encapsulation
b) Inheritance
c) Abstraction
30. __________ members of a base class are like private members,
except they may be accessed by derived classes.
a) Protected
b) Private
c) Public
31. A(n) __________ member function in a base class expects to be
overridden in a derived class.
a) Constructor
b) Virtual
c) Friend

5 A.T.G
32. A(n) __________ class cannot be instantiated.
a) Polymorphic class
b) Abstract base class
c) Friend class
33. What is the base class name? class Pet : public Dog
a) Dog
b) Pet
34. What is the derived class name? class Pet : public Dog
a) Dog
b) Pet
35. A derived class inherits the __________ of its base class.
a) Member functions
b) Data members
c) A and b
d) Friends
36. When overloading the __________ operator, its function must
have a dummy parameter.
a) Prefix
b) Postfix
c) Relational
37. A(n) __________ is a special constructor, called whenever a new
object is initialized with another object’s data.
a) Copy constructor
b) Friend constructor
c) Default constructor
d) Parameterized constructor
38. A(n) __________ function is not a member of a class, but has
access to the private members of the class.
a) Overloaded
b) Overridden
c) Friend

6 A.T.G
39. Which among the following is correct?
a) class student{ public: int student(){} };
b) class student{ public: void student (){} };
c) class student{ public: student{}{} };
d) class student{ public: student(){} };
40. In which access should a constructor be defined, so that object of
the class can be created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
41. Which object will be created first?
class student
{
int marks;
};
student ahmed, ali, mona;
a) ahmed then ali then mona
b) mona then ali then ahmed
c) ahmed then mona then ali
d) All are created at same time
42. Which object will be destructed first?
class student
{
int marks;
};
student ahmed, ali, mona;
a) ahmed then ali then mona
b) mona then ali then ahmed
c) ahmed then mona then ali
d) All are created at same time

7 A.T.G
43. For constructor overloading, each constructor must differ in
___________ or__________
a) Number of arguments or type of arguments
b) Number of arguments or return type
c) Return type or type of arguments
d) Return type or definition
44. Object declared in main() function:
a) Can be used by any other function
b) Can be used by main() function of any other program
c) Can’t be used by any other function
d) Can be accessed using scope resolution operator
45. A class declaration must be terminated by a _________
a) Colon
b) Semicolon
c) Dot
d) Scope resolution
46. A member function defined in a class's body is ____________
a) Friend function
b) Overloaded function
c) Inline function
d) Pure virtual function
47. Every class definition contains the keyword _________ followed
immediately by the class's name.
a) class
b) struct
c) friend
48. Return type __________ indicates that a function will perform a
task but will not return any information when he completes its task.
a) Int
b) Void
c) no_return
d) return 0
e)

8 A.T.G
49. Keyword public is an _________.
a) Data type
b) Access specifier
c) Friend method
50. When a member function is defined outside the class definition,
the function header must include the class name and the ___________,
followed by the function name to "tie" the member function to the class
definition.
a) Colon
b) Semicolon
c) Dot
d) Scope resolution
51. When the compiler selects the proper function to call by examining
the number, types, and order of the arguments in the call.
a) Function overriding
b) Function overloading
c) Friend functions
d) A and B
52. When you dereference an object pointer, use the _______
a) Colon
b) Arrow operator
c) Dot
d) Scope resolution operator
53. This is automatically called when an object is destroyed
a) Pure virtual function
b) Constructor function
c) Destructor function
d) Scope resolution operator

9 A.T.G
1. Consider the following declarations
class bagType
{
public:
void set(string, double, double, double, double);
void print();
string getStyle();
double getPrice();
void get(double, double, double, double);
bagType();
bagType(string, double, double, double, double);

private:
string style;
double l;
double w;
double h;
double price;
};
bagType newBag; // variable declaration

a) How many members does class bagType have? 12


b) How many private members does class bagType have? 5
c) How many private functions does class bagType have? 0
d) How many constructors does class bagType have? 2
e) How many members does class bagType have excluding
constructors functions? 10
f) How many functions does class bagType have including
constructors functions? 7
g) Which constructor is used to initialize the object newBag?
The no parameter constructor bagType()
h) How many access specifiers used? 2

i) Write a statement that declares the object tempBag, and


initialize the member variables "backPack", 15, 8, 20 and
49.99, respectively using set function
bagType tempBag;
tempBag.set("backPack", 15, 8, 20, 49.99);

10 A.T.G
j) Write the definition of the number function set so that private
members are set according to the parameters
void bagType::set(string style, double l, double w,
double h, double price)
{
this->style = style;
this->l = l;
this->w = w;
this->h = h;
this->price = price;
}

k) Write the definition of the member function print that prints the
values of the data members
void bagType::print()
{
cout << style << endl;
cout << l << endl;
cout << w << endl;
cout << h << endl;
cout << price << endl;
}

l) Write the definition of the default constructor of the class


bagType so that the private member variables are initialized to
"", 0.0, 0.0, 0.0, 0.0, respectively
bagType::bagType()
{
this->style = "";
this->l = 0.0;
this->w = 0.0;
this->h = 0.0;
this->price = 0.0;
}

m)Write a statement that prints the value of the object newBag.


newBag.print();

11 A.T.G
2. Consider the following declarations

class employee //Line 1


{ //Line 2
public: //Line 3
employee(); //Line 4
employee(string, int, double); //Line 5
employee(int, double); //Line 6
employee(string); //Line 7
void setData(string, int, double); //Line 8
void print(); //Line 9
void updateSalary(double x); //Line 10
int getNumOfServiceYears(); //Line 11
double getSalary(); //Line 12
private: //Line 13
string name; //Line 14
int numOfServiceYears; //Line 15
double salary; //Line 16
}; //Line 17

Give the line number containing the constructor that is executed in


each of the following declarations:
a) employee tempEmployee; Line 4
b) employee newEmployee("Harry Miller", 0, 25000); Line 5
c) employee oldEmployee("Bill Dunbar", 15, 55000); Line 5
d) employee emp("Bill"); Line 7
e) employee new_emp(10, 50000); Line 6

f) Write the definition of the constructor in Line 4 so that the


instance variables are initialized to "", 0, and 0.0, respectively.
employee::employee()
{
name = "";
numOfServiceYears = 0;
salary = 0.0;
}

12 A.T.G
g) Write the definition of the constructor in Line 5 so that the
instance variables are initialized according to the parameters.
employee::employee(string name, int years, double
salary)
{
this->name = name;
this->numOfServiceYears = years;
this->salary = salary;
}

h) Write the definition of the constructor in Line 6 so that the


variable name is initialized to the empty string and the remaining
variables are initialized according to the parameters.
employee::employee(int years, double salary)
{
this->name = "";
this->numOfServiceYears = years;
this->salary = salary;
}

i) Write the definition the function print to output the values of


the instance variable
void employee::print()
{
cout << name << endl;
cout << numOfServiceYears << endl;
cout << salary << endl;
}
j) Write the definition of the function updateSalary to update the
value of the variable salary by adding the value of the parameter.
void employee::updateSalary(double x)
{
salary += x;
}

13 A.T.G
3. Design and Implement a class called personType the following
properties:
The class personType has four member variables:
name (string), age (int), weight (double), height (double)

The class personType has the following member functions:


constructor— with default parameters: The default value of name is
the empty string "", and the default values of age, weight, and height
are 0.
Constructor— takes name, age, weight, and height as arguments.
print— outputs the data stored in the member variables.
setName— function to set the name
setAge— function to set the age
setWeight— function to set the weight
setHeight— function to set the height
getName— value- returning function to return the name
getAge— value- returning function to return the age
getWeight— value- returning function to return the weight
getHeight— value- returning function to return the height

14 A.T.G
#include<iostream>
#include<string>
using namespace std;

class personType
{
string name;
int age;
double weight;
double height;

public:
personType()
{
name = "";
age = 0;
weight = height = 0;
}

personType(string name, int age, double


weight, double height)
{
this->name = name;
this->age = age;
this->height = height;
this->weight = weight;
}

void print()
{
cout << "Name: " << name;
cout << "Age: " << age;
cout << "Height: " << height;
cout << "Weight: " << weight;
}

void setName(string name)


{
this->name = name;
}
15 A.T.G
void setAge(int age)
{
this->age = age;
}

void setHeight(double height)


{
this->height = height;
}

void setWeight(double weight)


{
this->weight = weight;
}

string getName()
{
return name;
}

int getAge()
{
return age;
}

double getHeight()
{
return height;
}

double getWeight()
{
return weight;
}
};

16 A.T.G
4. The following class declaration contain errors. Find as many as possible.
class Circle:
{
private
double centerX;
double centerY
double radius;
public;
setCenter(double, double);
setRadius(double);
}

class Circle:
Class declaration has no colon (:) after class name.

private
Access specifier must followed by a colon (:).
Correct: private:

double centerY
There’s a missed semicolon (;) at the end of the statement.
Correct: double center;

public;
Access specifier must followed by a colon (:) not a semicolon (;)
Correct: public:

setCenter(double, double);
setRadius(double);
Setter methods must have a return type.
void setCenter(double, double);
void setRadius(double);

Class declaration must be terminated by a


semicolon (;)
17 A.T.G
5. The following class declaration contain errors. Find as many as possible.
class Circle
{
protected;
double radius;
double PI;

void Circle(double radius)


{
this.radius = radius;
PI = 22/7;
}

void setRadius(double radius);


{
this->radius != radius;
}

getArea()
{
return PI * radius * radius
}

~Circle(double r)
{ }
}

Circle c;
c.getArea();

protected;
Access specifier must followed by a colon (:) not a semicolon (;)

void Circle(double radius)


Constructors has no return type not even void.

18 A.T.G
this.radius = radius;
this is a pointer so, it must be written this->radius = radius;
Or (*this).radius = radius;

PI = 22/7;
The result of division int / int = int so, the fraction will be ignored
and the value of PI will be 3. It should be writtern 22/7.0;
That’s a logical error not a syntax error.

void setRadius(double radius);


The semicolon (;) at the end of the statement indicates that the
function decleration will be written outside the class so, there’s no
need to the body.
getArea()
The function isn’t a constructor so, it must has a return type.
this->radius != radius;
This is a boolean expression which is either true or false not
assignment statement.

return PI * radius * radius


There’s a missed semicolon (;) at the end of the statement.

~Circle(double r)
A destructor has no parameters. ~Circle(void)

Class declaration must be terminated by a


semicolon (;)

Circle c;
There’s no default constructor for the class.
c.getArea();
getArea is a protected method that we can’t access it outside the
class.

19 A.T.G

You might also like