OOP 2 answers
OOP 2 answers
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
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
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;
}
11 A.T.G
2. Consider the following declarations
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;
}
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)
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;
}
void print()
{
cout << "Name: " << name;
cout << "Age: " << age;
cout << "Height: " << height;
cout << "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);
getArea()
{
return PI * radius * radius
}
~Circle(double r)
{ }
}
Circle c;
c.getArea();
protected;
Access specifier must followed by a colon (:) not a semicolon (;)
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.
~Circle(double r)
A destructor has no parameters. ~Circle(void)
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