CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304...

83
1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing Specialization Extension 2. Abstraction is a feature of OOP which enables an object to -------------. Increase Complexity Decrease Complexity (pg 21) Increase Modularity Decrease Modularity 3. In a class, objects cannot- have common __________. Behaviour Attributes States Names 4. Which of the following is an example of multiple inheritances? Student Woman Mermaid Train 5. A C++ class is similar to a _________. Structure (pg65) Header File Library File Function 6. Which of the following is used to free the memory space allocated as a result of object declaration? Constructor New operator

Transcript of CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304...

Page 1: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

1

CS304 Object Oriented Programming Midterm

Paper

CS304 Object Oriented Programming

1. Which of the following is the way to extract common behaviour and attributes from

the given classes and make a separate class of those common behaviours and

attributes?

Generalization (pg 29)

Sub-typing

Specialization

Extension

2. Abstraction is a feature of OOP which enables an object to -------------.

Increase Complexity

Decrease Complexity (pg 21)

Increase Modularity

Decrease Modularity

3. In a class, objects cannot- have common __________.

Behaviour

Attributes

States

Names

4. Which of the following is an example of multiple inheritances?

Student

Woman

Mermaid

Train

5. A C++ class is similar to a _________.

Structure (pg65)

Header File

Library File

Function

6. Which of the following is used to free the memory space allocated as a result of object

declaration?

Constructor

New operator

Page 2: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

2

CS304 Object Oriented Programming Midterm

Paper

Destructor (pg 81)

Accessor Function

7.________ and ______ methods may not be declared abstract.

Private,Static

Private,Public

Static,Public

Static, Protected

8. If a static variable is not explicitly initialized at the time of its definition then,

The compiler will give an error message

The compiler will give a warning message

The compiler will initialize it with value Zero. (pg 107 )

The program terminates

9. Object cannot be declared constant if it is required to change the state of its _________.

Constant data members

Non-constant data members

Constant member functions

Static data members

10. A friend function can access a class’s private data without being a ____________.

Static member of the class

None member of the class

Member of the class (pg 135)

Constant member of the class

11. Identify which of the following overloaded operator function’s declaration is

appropriate for the given call?

Rational_number_1 + 2.325

Hint: Where Rational_number_1 is an object of user defined class Rational_number.

Rational_number operator+( Rational_number & obj);

Rational_number operator+(double& obj); (pg 145)

Rational_number operator+(Rational_number &obj, double& num);

operator+(double& obj);

12. A class can be identified from a statement by -------------.

Noun (pg 58)

Pronoun

Verb

Adverb

13. The return type of a constructor is of -------------.

Integer

Character

Double

No type (pg74)

14. Suppose obj1 and obj2 are two objects of a user defined class A. An + operator is

overloaded to add obj1 and obj2 using the function call obj1+obj2.

Identify the correct function prototype against the given call?

A operator + ( A &obj);

int + operator();

int operator (plus) (); (pg 143)

Page 3: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

3

CS304 Object Oriented Programming Midterm

Paper

A operator(A &obj3);

15. Abstract classes have ……..object/s, while concrete classes have …………..object/s.

One, One

More than one, more than one

No, One or more

No, One

16. If MyClass has a destructor its name will be,

MyClass

~MyClass (reference 232)

My~Class

MyClass~

17. A static member function can be called, even when an object is not __________.

Excecute

Define

Instantiated

Mention

18. Consider the following code segment:

void UserInfo (Complex & C1){

C1=C1;

}

It is an example of:

Assignment

Self assignment

Compound assignment

Multiple assignments

19. While overloading the stream insertion operator, the return type must be _________.

Ostream (pg 156)

Istream

Instream

Iostream

20. To overload subscript operator, it requires one _________ type parameter.

Int (pg 160)

Float

Char

String

CS304 Object Oriented Programming

1. When the base class cannot be replaced by its derived class then it is called,

Sub-typing (pg 31)

Super-typing

Generalization

Specialization

2. Suppose that the Test class does not have an overloaded assignment operator. What

happens when an assignment a=b; is given for two Test objects a and b?

The automatic assignment operator is used

Page 4: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

4

CS304 Object Oriented Programming Midterm

Paper

The copy constructor is used

Compiler error

Run-time error

3. By default, which access level is assigned to members of a class?

Public

Private

Local

Protected

4. In OOP's, advantage of inheritance include all of the given below except,

Provide a useful conceptual framework

Reusing the code

Dealing with real world entities

Decrease maintainability

5. Main problem(s) with multiple inheritances includes _________.

Increased Complexity, Reduced Understanding, Less Reusability

Increased Complexity, Decreased Code Redundancy, Less Reusability

Increased Complexity, Reduced understanding, Duplicate Features (pg45)

Increased Complexity, Less Reusability, Duplicate Features

6. Which is true about sub-typing in case of inheritance?

In sub-typing derived class shows some extended behavior of its parent. (Pg. )

In sub-typing derived class shows some restricted behavior of its parent class.

In sub-typing derived class should be abstract.

Sub-typing has no relation with inheritance

7. The ______ keyword tells the compiler to substitute the code within the function

definition for every instance of a function call.

Virtual

Inline (Pg. 72)

Instance

Static

8. The function that is automatically called when an object of a class is created is named

as _________.

Constructor (Pg. 74)

Destructor

Member Functions

Inline Functions

9. Which will be the good example of a method that is shared by all instances of a class?

Virtual method

Overloaded method

Constant method

Static method (Pg. 108)

10. If you do not initialize static variable of int type then it is automatically

initialized with _________.

0 (Pg. 107)

1

-1

Page 5: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

5

CS304 Object Oriented Programming Midterm

Paper

Null

11. Which one is not keyword in C++?

operator

B_op

const

using

12. We capture the object attributes and behavior in Object Oriented programming

using---------------.

Class (pg 15)

Function

Data Members

Instances

13. The return type of a constructor is of -------------.

Integer

Character

Double

No type (pg 74)

14. For more than one member in initializes list, then we use ________ to separate them.

Dot (.)

Comma (,) (pg 104)

Colon (:)

Semicolon(;)

15. Assume a class C with objects obj1, obj2, and obj3. For the statement obj3 = obj1 -

obj2 to work correctly, if the overloaded - operator must

Take two arguments.

Return a value.

Create a named temporary object.

Take four arguments.

16. To convert from a user-defined class to a basic type, you would most likely use

a conversion operator that’s a member of the class. (pg168)

a built-in conversion operator.

a one-argument constructor.

an overloaded = operator.

17. A generalization-specialization relation between classes are implemented using

Data hiding

Friend classes

Encapsulation

Inheritance (pg 49)

18. Operator overloading is:

Making C++ operators able to work with objects.

Giving C++ operators more than they can handle.

Giving new meanings to existing Class members.

Making new C++ operators.

19. The life of sub object is dependant on the life of master class in _____________.

Separation

Composition (Pg. 120)

Aggregation

Association

Page 6: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

6

CS304 Object Oriented Programming Midterm

Paper

20. __________ operator will take only one operand.

New

Const

Int

This

CS304 Object Oriented Programming

1. Which one of the following features of OOP is used to deal with only relevant details?

Abstraction (pg 23)

Information hiding

Object

Inheritance

2. Inheritance is a way to

make general classes into more specific classes. (pg 27)

pass arguments to objects of classes.

improve data hiding and encapsulation.

providing class growth through natural selection.

3. Consider the code below,

class Fred {

public:

Fred();

...

};

int main()

{

Fred a[10];

Fred* p = new Fred[10];

...

}

Select the best option,

A) Fred a[10]; calls the default constructor 09 times

Fred* p = new Fred[10]; calls the default constructor 10 times

B) Produce an error

C) Fred a[10]; calls the default constructor 11 times

Fred* p = new Fred[10]; calls the default constructor 11 times (Pg. 175)

D) Fred a[10]; calls the default constructor 10 times

Fred* p = new Fred[10]; calls the default constructor 10 times

4. Which operator can not be overloaded?

The relation operator ( >= )

Assignment operator ( = )

Script operator ( [] )

Conditional operator (? : ) (pg 141)

5. Abstraction is a feature of OOP which enables an object to -------------.

Increase Complexity

Decrease Complexity

Increase Modularity

Decrease Modularity

6. Composition is an example of _________ relationship.

Page 7: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

7

CS304 Object Oriented Programming Midterm

Paper

Simple

Complex

Strong (Pg. 53)

Weak

7. Consider a class having name MyClass, what will be the name of this class constructor

?

MyClass

Myclass

myClass

myclass

8. The function that is automatically called when an object of a class is created is named

as _________.

Constructor (Pg. 74)

Destructor

Member Functions

Inline Functions

9. The const member functions uses following specifers __________.

Only access

Only read (Pg. 100)

Access and read only

Access, read, and modify

10. The life of sub object is not dependant on the life of master class in ___________.

Composition

Aggregation (pg 134)

Separation

Abstraction

11. Friend functions are ____________ functions of a class.

Object member

Member

Non-member (pg 136)

Static member

12. Suppose obj1 and obj2 are two objects of a user defined class A. An + operator is

overloaded to add obj1 and obj2 using the function call obj1+obj2.

Identify the correct function prototype against the given call?

A operator + ( A &obj);

int + operator();

int operator (plus) ();

A operator(A &obj3);

13. A generalization-specialization relation between classes are implemented using

Data hiding

Friend classes

Encapsulation

Inheritance (pg 49)

14. A real world object’s attributes are represented through,

Member function

Data members(Pg. 14)

Global variables

Page 8: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

8

CS304 Object Oriented Programming Midterm

Paper

Const data members

15. Why we should avoid the use of friend function/class?

Friend function/class minimizes encapsulation (Pg. 153)

Difficult to use

Decreases complexity

Increase program code size

16. Which of the following is NOT an advantage of information hiding?

Simplifies the model

Restriction to change

Prevents damage from errant external code

Provide direct access to data members

17. Assume that a user wants to count, how many objects are created for a particular

class, which one will be most suitable to perform this task:

Const data member

Non static data member

Static data member

Global data member

18. ___________, which means if A declares B as its friend it does NOT mean that A can

access private data of B. It only means that B can access all data of A.

Friendship is one way only (Pg.292 )

Friendship is two way only

NO Friendship between classes

Any kind of friendship

19. Consider the following code segment:

void UserInfo (Complex & C1){

C1=C1;

}

It is an example of:

Assignment

Self assignment (Pg. 151)

Compound assignment

Multiple assignment

20. Subscript operator must be ________ of the class when overloaded.

Static function

Virtual function

Member function (pg 160)

Non member function

CS304 Object Oriented Programming

1. What is a class?

A class is a section of computer memory containing objects.

A class is a section of the hard disk reserved for object oriented programs

Page 9: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

9

CS304 Object Oriented Programming Midterm

Paper

A class is the part of an object that contains the variables.

A class is a description of a kind of object.

2. Suppose that the Test class does not have an overloaded assignment operator. What

happens when an assignment a=b; is given for two Test objects a and b?

The automatic assignment operator is used

The copy constructor is used

Compiler error

Run-time error

3. Consider the following statement. "Cupboard has books"

What is the relationship between Cupboard and books?

Composition

Aggregation (Pg. 54)

Inheritance

Association

4. Which of the following is directly related to polymorphism?

Const members

Overriding (pg )

Static members

Const members functions

5. The constructor of a class is called when --------------.

An object is change its state

An object is executed

An object is created (pg 74)

An object is destroyed

6. Which will be the good example of a method that is shared by all instances of a

class?

Virtual method

Overloaded method

Constant method

Static method

7. The const member functions uses following specifers __________.

Only access

Only read (Pg. 100)

Access and read only

Access, read, and modify

8. Friend functions are ____________ functions of a class.

Object member

Member

Non-member (pg 136)

Page 10: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

10

CS304 Object Oriented Programming Midterm

Paper

Static member

9. The return type of a constructor is of -------------.

Integer

Character

Double

No type (Pg. 74)

10. For more than one member in initializer list, then we use ________ to separate them.

Dot (.)

Comma (,) (Pg. 104)

Colon (:)

Semicolon(;)

11. Given the following class

class Base{

int Age=33;

}

How you can improve above class with respect to accessing the field Age?

Define the variable Age as private

Define the variable Age as protected

Define the variable Age as private and create a get method that returns it and a set

method that updates it

Define the variable Age as protected and create a set method that returns it and a get

method that updates it

12. A normal C++ operator that acts in special ways on newly defined data types is said

to be

Glorified.

Encapsulated.

Classified.

Overloaded.

13. To convert from a user-defined class to a basic type, you would most likely use

a conversion operator that’s a member of the class.

a built-in conversion operator.

a one-argument constructor.

an overloaded = operator.

14. A generalization-specialization relation between classes are implemented using

Data hiding

Friend classes

Encapsulation

Inheritance (Pg. 49)

15. Abstract classes have……..object/s, while concrete classes have …………..object/s.

One, One

More than one, more than one

No, One or more (Pg. 229)

No, One

Page 11: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

11

CS304 Object Oriented Programming Midterm

Paper

16. Hiding details of an object from the other parts of a program is done by which

principle of OOP?

Obfustication

Data Mining

Compilation

Encapsulation (pg 16)

17. Graphical representation of the classes and objects is called object model it

shows -------.

Class name, Attributes, States, Operations

Class name, Attributes, Operations, Methods

Class name, Attributes, Operations, Behaviour

Class name, Attributes, Operations, Relationship

18. Consider the following function:

void multiplication (int x, int y);

If we overloaded the above function, then it will require ………. operands.

One

Two

Three

Four

19. Consider the following code segment:

void UserInfo (Complex & C1){

C1=C1;

}

It is an example of:

Multiple assignment

Assignment

Self assignment (Pg. 151)

Compound assignment

20. While overloading the stream extraction operator, the return type must be _________.

Ostream

Iostream

Istream

Instream

CS304 Object Oriented Programming

1. Which one of the following is not a major element of an Object Model?

Abstraction

Encapsulation

Persistence

Hierarchy

2. Encapsulation is a feature of Object Oriented Programming which means,

Keep data separated from code

Keep data and code in a single place (Pg. )

Keep data separated from code and also keep data and code in a single place

Reusing code by creating new classes from previously created classes

3. Reusability can be achieved through __________.

Inheritance

Page 12: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

12

CS304 Object Oriented Programming Midterm

Paper

Aggregation

Association

Abstraction

4. Which of the following is directly related to polymorphism?

Const members

Overriding

Static members

Const members functions

5. Which of the class’s members are available to every one?

Public

Private

Protected

Internal

6. A member function having the same name as the class name preceded by a tilde (~)

sign is called _______.

Constructor

Getter

Setter

Destructor (pg 92)

7. Which of the following is used to free the memory space allocated as a result of object

declaration?

Constructor

New operator

Destructor (Pg. 81)

Accessor Function

8. __________ provide the facility to access the data member.

Accesser function (pg 68)

Private function

Inline function

Constructor

9. What will be the value of the variable data and data2 respectively, If the function

below is called 4 times?

void func()

{

static int data = 4;

int data2 = 4;

++data;

++data2;

}

8, 5

5, 8

5, 5

7, 5

Page 13: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

13

CS304 Object Oriented Programming Midterm

Paper

10. In ____________, a pointer or reference to an object is created inside a class.

Aggregation (Pg. 134)

Composition

Separation

Association

11. Identify which of the following overloaded operator function’s declaration is

appropriate for the given call?

Rational_number_1 + 2.325

Hint: Where Rational_number_1 is an object of user defined class Rational_number.

Rational_number operator+( Rational_number & obj);

Rational_number operator+(double& obj); (Pg. 145)

Rational_number operator+(Rational_number &obj, double& num);

operator+(double& obj);

12. We capture the object attributes and behavior in Object Oriented programming

using---------------.

Instances

Class (pg 15)

Function

Data Members

13. A normal C++ operator that acts in special ways on newly defined data types is said

to be

Glorified.

Encapsulated.

Classified.

Overloaded.

14. Suppose obj1 and obj2 are two objects of a user defined class A. An + operator is

overloaded to add obj1 and obj2 using the function call obj1+obj2.

Identify the correct function prototype against the given call?

A operator + ( A &obj);

int + operator();

int operator (plus) (); (pg 143)

A operator(A &obj3);

15. A generalization-specialization relation between classes are implemented using

Data hiding

Friend classes

Encapsulation

Inheritance (pg 49)

16. The technique in which we visualize our programming problems according to real

life’s problems is called_________.

Structured programming

Object oriented programming (pg 9)

Procedural programming

Sequential programming

17. How many objects for a class can be constructed in an application?

Only one per constructor.

As many as the application asks for.

Page 14: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

14

CS304 Object Oriented Programming Midterm

Paper

Only one per class.

One object per variable.

18. We ………… change the evaluation precedence of operators through operator

overloading.

Can

Can not

Only

Just

19. Subscript operator must be ________ of the class when overloaded.

Static function

Virtual function

Member function (Pg. 160)

Non member function

20. friend Complex & operator -(Complex & t);

It is the declaration of:

Unary minus member function

Unary minus non member function

Unary minus static member function

Unary minus non static member function

CS304 Object Oriented Programming

1. Inheritance is a way to

organize data.

pass arguments to objects of classes.

add features to existing classes without rewriting them. (pg 27)

improve data-hiding and encapsulation.

2. The ability to derive a class from more than one classes is called,

Single inheritance

Encapsulation

Multiple inheritance

Polymorphism

3. For classes with common behavior, you can save effort by placing the common

behavior in a __________.

Derived Class

Base class (pg 29)

Deprived Class

Named class

4. If Rollno is a const data member of a class student then which definition is correct

for the member function show ()

int show() const { return Rollno; }

void show() const { return Rollno; }

int show() const { return Rollno + 50; }

int show(num) const { Rollno = num; }

5. An overloaded operator always requires __________ argument than its number of

operands.

One less (pg 896)

One most

Page 15: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

15

CS304 Object Oriented Programming Midterm

Paper

Two less

Three most

6. Which of the following is a weak relationship between two objects?

Inheritance

Composition

Aggregation

Association

7.The constructor of a class is called when --------------.

An object is change its state

An object is executed

An object is created

An object is destroyed

8. The function that is automatically called when an object of a class is created is named

as _________.

Constructor (Pg. 74)

Destructor

Member Functions

Inline Functions

9. Which of the following is used to free the memory space allocated as a result of object

declaration?

Constructor

New operator

Destructor (Pg. 92)

Accessor Function

10. Separation of implementation from interface provides the facility to restrict the

_________ from direct access.

Data member

Data function

Class

Object (Pg. 19)

11. "This pointer" does not pass implicitly to __________ functions.

Static member (Pg. 114)

Non-static member

Instance member

Constant member

12. In ____________, a pointer or reference to an object is created inside a class.

Aggregation (Pg. 133)

Composition

Separation

Association

13. A class can be identified from a statement by -------------.

Noun

Pronoun

Verb

Adverb

14. By default all members of a class are,

Page 16: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

16

CS304 Object Oriented Programming Midterm

Paper

Public

Protected

Private

Public & protected

15. Graphical representation of the classes and objects is called object model it shows ---

----.

Class name, Attributes, States, Operations

Class name, Attributes, Operations, Methods

Class name, Attributes, Operations, Behaviour

Class name, Attributes, Operations, Relationship

16. Why we should avoid the use of friend function/class?

Friend function/class minimizes encapsulation

Difficult to use

Decreases complexity

Increase program code size

17. Operator overloading is:

Making C++ operators able to work with objects.

Giving C++ operators more than they can handle.

Giving new meanings to existing Class members.

Making new C++ operators.

18. Which of the following operator can not be overloaded?

Scope resolution operator ( :: ) (pg 141)

Insertion operator ( << )

Extraction operator ( >> )

The relation operator ( > )

19 Consider the following code segment:

void UserInfo (Complex & C1){

C1=C1;

}

It is an example of:

Assignment

Self assignment

Compound assignment

Multiple assignment

20.Subscript operator must be ________ of the class when overloaded.

Static function

Virtual function

Member function

Non member function

CS304 Object Oriented Programming

1. Which one of the following features of OOP is used to derive a class from another?

Encapsulation

Polymorphism

Data hiding

Inheritance (pg 25)

2. Which sentence clearly defines an object?

one instance of a class.

another word for a class.

Page 17: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

17

CS304 Object Oriented Programming Midterm

Paper

a class with static methods.

a method that accesses class attributes.

3. If Rollno is a const data member of a class student then which definition is correct

for the member function show ()

int show() const { return Rollno; }

void show() const { return Rollno; }

int show() const { return Rollno + 50; }

int show(num) const { Rollno = num; }

4. Suppose that the Test class does not have an overloaded assignment operator. What

happens when an assignment a=b; is given for two Test objects a and b?

The automatic assignment operator is used

The copy constructor is used

Compiler error

Run-time error

5. Abstract class has _______.

Zero Instances (there is no object)

One Instance

Two Instances

Many Instances

6. Consider a class having name MyClass, what will be the name of this class constructor

?

MyClass

Myclass

myClass

myclass

7. Which of the following is used to free the memory space allocated as a result of object

declaration?

Constructor

New operator

Destructor

Accessor Function

8. The const member functions uses following specifers __________.

Only access

Only read

Access and read only (Pg. 106)

Access, read, and modify

9. What will be the value of the variable data and data2 respectively, If the function

below is called 4 times?

void func()

{

static int data = 4;

int data2 = 4;

++data;

++data2;

}

8, 5

5, 8

5, 5

Page 18: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

18

CS304 Object Oriented Programming Midterm

Paper

7, 5

10. Object cannot be declared constant if it is required to change the state of its _________.

Constant data members

Non-constant data members

Constant member functions

Static data members

11. In order to free the memory occupied by the object, we use -------------.

Constructor

Destructor

Shallow Copy

Deep Copy

12. The property in which existing class is modified to form a new class is

called………..

Polymorphism

Inheritance

Encapsulation

Information hiding

13. In inheritance hierarchy which one is on the top?

Subtyping

Specialization

Extension

Generalization

14. Graphical representation of the classes and objects is called object model it

shows -------.

Class name, Attributes, States, Operations

Class name, Attributes, Operations, Methods

Class name, Attributes, Operations, Behaviour

Class name, Attributes, Operations, Relationship

15. Why we should avoid the use of friend function/class?

Friend function/class minimizes encapsulation

Difficult to use

Decreases complexity

Increase program code size

16. In the following operators, the _____ operator can be overloaded.

%

##

?:

.

Page 19: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

19

CS304 Object Oriented Programming Midterm

Paper

17. C2+C1 is equivalent to ___________, when plus operator is overloaded, and C2, C1

are objects.

C1.operator + (C2)

C1.operator + (C1)

C2.operator + (C1)

C2.operator + (C2)

18.Consider the following code segment:

void UserInfo (Complex & C1){

C1=C1;

}

It is an example of:

Assignment

Self assignment

Compound assignment

Multiple assignment

19. To overload subscript operator, it requires one _________ type parameter.

Int

Float

Char

String

20. friend Complex & operator -(Complex & t);

It is the declaration of:

Unary minus member function

Unary minus non member function (Pg. 162)

Unary minus static member function

Unary minus non static member function

MCQz (Set-1)

1. We can get only one unique value which can be used by all the objects of that

class by

The use of,

Static variables

Dynamic variables

Instance variables

Data members

2. A member function having the same name as that of a class and a ~ sign with it

is called,

Constructor

Getter

Setter

Destructor

Page 20: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

20

CS304 Object Oriented Programming Midterm

Paper

3. Using encapsulation we can achieve

Information hiding

Least interdependencies among modules

Implementation independence

All of given options

4. Inheritance is a way to make general classes into more specific classes.

Pass arguments to objects of classes.

Improve data hiding and encapsulation.

Providing class growth through natural selection.

5. Static variable can be initialized more than once.

True

False

6. for classes with common behavior, you can save effort by placing the common

behavior in a __________.

Derived Class

Base class

Deprived Class

Named class

7. Which of the following are advantages of OOP?

OOP makes it easy to re-use the code

It provides an ability to create one user defined data type by extending the other

It provides the facility of defining Abstract data types through which real world entities

can be defined better

All of the given options

8. The >= operator can be overloaded.

True

False

9. A static member function cannot be declared.

Static

Implicit

Explicit

Virtual

10. Static variables act like a global variable in the context or scope of the class.

True

Page 21: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

21

CS304 Object Oriented Programming Midterm

Paper

False

11. The compiler won‘t object if you overload the * operator to perform division.

True

False

12. We can use "this" pointer in the constructor in the body and even in the

initialization list of any class if we are careful,

TRUE

False

13. A C++ class is similar to --------------------

Structure

Header File

Library File

None of the given

14. Which operator can not be overloaded?

The relation operator ( >= )

Assignment operator ( = )

Script operator ( [] )

Conditional operator (? : ), Ternary Operator

15. An overloaded operator always requires one less argument than its number of

operands.

True

False

16. A generalization-specialization relation between classes are implemented

using

Data hiding

Friend classes

Encapsulation

Inheritance

16. In OOP a class is an example of _____

Data Type

Abstract Type

User Defined Type

None of the given

Page 22: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

22

CS304 Object Oriented Programming Midterm

Paper

17. Identify which of the following overloaded operator function‘s declaration is

appropriate for the given call?

Rational_number_1 + 2.325

Where Rational_number_1 is an object of user defined class Rational_number.

Rational_number operator+( Rational_number & obj);

Rational_number operator+(double& obj);

18. Rational_number operator+(Rational_number &obj, double& num);

operator+(double& obj);

A class can be identified from a statement by -------------

Noun

Pronoun

Verb

Adverb

19. The members of a class that can be accessed without creating the object of the

class is called

Private member

Data Member

Public Member

Static

MCQz (Set-2)

Question No: 1 (Marks: 1) - Please choose one

Suppose there is an object of type Person, which of the following can be considered as

one of its attributes ► Name

► Age

► Work()

► Both Name and Age

Question No: 2 ( Marks: 1 ) - Please choose one

What a derived class can add? ► New data members

► New member functions and New friend functions

► New constructors and destructor

► All of given

Question No: 3 ( Marks: 1 ) - Please choose one

________ is/are used to access information hidden within an object?

► Interface ► Private data members

► Private member functions

► Both public and private members

Page 23: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

23

CS304 Object Oriented Programming Midterm

Paper

Question No: 4 ( Marks: 1 ) - Please choose one

this pointers are not accessible for static member functions.

► True ► False

Question No: 5 ( Marks: 1 ) - Please choose one

A static member function cannot be declared.

► Static

► Implicit ► Explicit

► Virtual

Question No: 6 ( Marks: 1 ) - Please choose one

C++ compiler does not allow to dynamically allocate memory for objects

► False ► True

Question No: 7 ( Marks: 1 ) - Please choose one

Given the following class

class Base{

int Age=33;

}

How you can improve above class with respect to accessing the field Age? ► Define the variable Age as private

► Define the variable Age as protected

► Define the variable Age as private and create a get method that returns it and a

set method that updates it ► Define the variable Age as protected and create a set method that returns it and a get

method that updates it

Question No: 8 ( Marks: 1 ) - Please choose one

Friend class and friend function can be used as an alternate to each other

► True

► False

Question No: 9 ( Marks: 1 ) - Please choose one

Which of the following operators always takes no argument if overloaded? ► /

► -

► +

► ++

Question No: 10 ( Marks: 1 ) - Please choose one

Suppose that the Test class does not have an overloaded assignment operator. What

happens when an assignment a=b; is given for two Test objects a and b?

► The automatic assignment operator is used

► The copy constructor is used

► Compiler error ► Run-time error

Page 24: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

24

CS304 Object Oriented Programming Midterm

Paper

Question No: 11 ( Marks: 1 ) - Please choose one

Assume a class C with objects obj1, obj2, and obj3. For the statement obj3 = obj1 - obj2

to work correctly, if the overloaded - operator must

► take two arguments. ► return a value.

► create a named temporary object.

► take four arguments

Question No: 12 ( Marks: 1 ) - Please choose one

Which operator can not be overloaded?

► The relation operator ( >= )

► Assignment operator ( = )

► Script operator ( [] )

► Conditional operator (? : )

Question No: 13 ( Marks: 1 ) - Please choose one

We achieve independence of internal implementation from its external interface

through--

---------

► Encapsulation

► Information Hiding ► Abstraction

► both encapsulation and information hiding

Question No: 14 ( Marks: 1 ) - Please choose one

Which one of the following is not an object association?

► Simple Association

► Inheritance ► Aggregation

► Composition

Question No: 15 (Marks: 1) - Please choose one

We capture the object attributes and behavior in Object Oriented programming using----

-

----------

► Class ► Function

► Data Members

► Instances

Question No: 16 (Marks: 1) - Please choose one

The return type of a constructor is of ------------- ► Integer

► Character

► Double

► No type pg 74

Page 25: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

25

CS304 Object Oriented Programming Midterm

Paper

In constant member function the type of this pointer is:

Select correct option:

1. Constant pointer

Constant pointer to object

2. Constant pointer to class

3. Constant pointer to constant object

Which of the following is the way to extract common behavior and attributes from

the given classes and make a separate class of those common behaviors and

attributes?

Generalization

1. Sub-typing

2. Specialization

3. Extension

The ability to derive a class from more than one class is called

1. Single inheritance

2. Encapsulation

Multiple inheritances

3. Polymorphism

If MyClass has a destructor what is the destructor named?

1. MyClass

~MyClass

2. My~Class

3. MyClass~

Class abc{ ----- }; Is a valid class declaration?

Yes

No

Without using Deep copy constructor, A ____________ problem can occur

1. System crash

2. Memory Leakage

3. Dangling pointer

All of the given

If only one behaviors of a derived class is incompatible with base class, then it is:

1. Generalization

Specialization

2. Extension

3. Inheritance

Which of the following may not be an integral part of an object?

1. State

2. Behavior

Protected data members

3. All of given

Page 26: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

26

CS304 Object Oriented Programming Midterm

Paper

Only tangible things can be chosen as an object.

1. True

False

MCQz (Set-5)

Question # 1 of 10 ( Start time: 09:26:23 PM ) Total Marks: 1

Consider the code below, class class1{ public: void func1(); }; class class2 : protected

class1 { }; Function func1 of class1 is ______ in class2,

Select correct option:

public

protected

private

none of the given options

Question # 2 of 10 ( Start time: 09:27:52 PM ) Total Marks: 1

In case of dynamic memory allocation in a class we should use,

Select correct option:

User defined default constructor

User defined copy constructor

Both of these

None of these

Question # 3 of 10 ( Start time: 09:29:20 PM ) Total Marks: 1

Consider the code below, class class1{ protected: int i; }; class class2 : public class1 { };

Then int member i of class1 is ______ in class2,

Select correct option:

public

protected

private

none of the given options

Question # 4 of 10 ( Start time: 09:30:13 PM ) Total Marks: 1

In private inheritance derived class pointer can be assigned to base class pointer in,

Select correct option:

Main function

In derived class member and friend functions

In base class member and friend functions

None of the given options

Question # 5 of 10 ( Start time: 09:31:42 PM ) Total Marks: 1

Child class can call constructor of its,

Select correct option:

Direct base class pg 209

Indirect base class

Both direct and indirect base classes

None of these

Question # 6 of 10 ( Start time: 09:31:59 PM ) Total Marks: 1

Consider the following two lines of code written for a class Student, 1. Student

Page 27: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

27

CS304 Object Oriented Programming Midterm

Paper

sobj1,sobj2; 2. sobj2 = sobj1; In line No.2 what constructor of Student class will be

called,

Select correct option:

Default constructor of Student class.

Copy constructor of student class

Both default and copy constructer of Student class

No constructor will be called.

Question # 7 of 10 (Start time: 09:33:17 PM) Total Marks: 1

Consider the code below, class class1{ public: int i; }; class class2 : public class1 { }; Then

int member i of class1 is ______ in class2,

Select correct option:

Public

Protected

Private

None of the given options

Question # 8 of 10 ( Start time: 09:33:54 PM ) Total Marks: 1

We can call base class constructor from derived class constructor,

Select correct option:

From derived class constructor body

From the initialized list of derived class constructor

From any member function of derived class

We can not call the base class constructor

Question # 9 of 10 ( Start time: 09:35:20 PM ) Total Marks: 1

Adding a derived class to a base class requires fundamental changes to the base class.

Select correct option:

True

False

Question # 10 of 10 ( Start time: 09:36:44 PM ) Total Marks: 1

Public Inheritance represents,

Select correct option:

“IS A” relationship

―Has A‖ relationship

―IS Special Kind of‖ relationship

None of these options

MCQz (Set-9)

Question No: 1 (Marks: 1) - Please choose one

A template provides a convenient way to make a family of

► Variables and data members ► Functions and classes

► Classes and exceptions

► Programs and algorithms

Question No: 2 ( Marks: 1 ) - Please choose one

Page 28: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

28

CS304 Object Oriented Programming Midterm

Paper

Which one of the following terms must relate to polymorphism? ► Static allocation

► Static typing

► Dynamic binding ► Dynamic allocation

Question No: 3 ( Marks: 1 ) - Please choose one

What is true about function templates? ► The compiler generates only one copy of the function template

► The compiler generates a copy of function respective to each type of data ► The compiler can only generate copy for the int type data

► None of the given.

Question No: 4 ( Marks: 1 ) - Please choose one

Which of the following is the best approach if it is required to have more than

onefunctions having exactly same functionality and implemented on different data

types?

► Templates ► Overloading

► Data hiding

► Encapsulation

Question No: 5 ( Marks: 1 ) - Please choose one

template <>

class Vector<char*> { }

This is an example of partial specialization. ► True

► False

Question No: 6 ( Marks: 1 ) - Please choose one

Classes like TwoDimensionalShape and ThreeDimensionalShape would normally be

concrete, while classes like Sphere and Cube would normally be abstract.

► True ► False

Question No: 7 ( Marks: 1 ) - Please choose one

A non-virtual member function is defined in a base class and overridden in a derived

class; if that function is called through a base-class pointer to a derived class object, the

derived-class version is used.

► True ► False

Question No: 8 ( Marks: 1 ) - Please choose one

Assume a class Derv that is privately derived from class Base. An object of class Derv

located in main() can access ► public members of Derv.

► protected members of Derv. ► private members of Derv.

► protected members of Base.

Page 29: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

29

CS304 Object Oriented Programming Midterm

Paper

Question No: 9 ( Marks: 1 ) - Please choose one

In order to define a class template, the first line of definition must be:

► template <typename T> ► typename <template T>

► Template Class <ClassName>

► Class <Template T>

Question No: 10 ( Marks: 1 ) - Please choose one

If there is a pointer p to objects of a base class, and it contains the address of an object

of a derived class, and both classes contain a nonvirtual member function, ding(), then

the statement p->ding(); will cause the version of ding() in the _____ class to be executed. ► Base

► Derived

► Abstract

► virtual

Question No: 11 ( Marks: 1 ) - Please choose one

When the base class and the derived class have a member function with the same name,

you must be more specific which function you want to call (using ___________). ► scope resolution operator

► dot operator

► null operator ► Operator overloading

Question No: 12 ( Marks: 1 ) - Please choose one

Non Template Friend functions of a class are friends of ________instance/s of that class. ► All

► One specific ► All instances of one date type

► None of the given options

Question No: 13 ( Marks: 1 ) - Please choose one

The find() algorithm ► finds matching sequences of elements in two containers.

► finds a container that matches a specified container. ► takes iterators as its first two arguments.

► takes container elements as its first two arguments.

Question No: 14 ( Marks: 1 ) - Please choose one

If you define a vector v with the default constructor, and define another vector w with a

one-argument constructor to a size of 11, and insert 3 elements into each of these

vectors with push_back(), then the size() member function will return ______ for v and

_____ for w. ► 11 for v and 3 for w.

► 0 for v and 0 for w.

► 0 for v and 3 for w.

► 3 for v and 11 for w.

Question No: 15 ( Marks: 1 ) - Please choose one

Page 30: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

30

CS304 Object Oriented Programming Midterm

Paper

Which of the following may not be an integral part of an object? ► State

► Behavior

► Protected data members

► All of given

Question No: 16 ( Marks: 1 ) - Please choose one

Which is not the Advantage of inheritance? ► providing class growth through natural selection.

► facilitating class libraries. ► avoiding the rewriting of code.

► providing a useful conceptual framework.

Question No: 17 ( Marks: 1 ) - Please choose one

class DocElement

{

public:

virtual void Print() { cout << "Generic element"; }

};

class Heading : public DocElement

{

public:

void Print() { cout << "Heading element"; }

};

class Paragraph : public DocElement

{

public:

void Print() { cout << "Paragraph element"; }

};

void main()

{

DocElement * p = new Paragraph();

p->Print();

}

When you run this program, it will print out a single line to the console output.

What will be in that line?

Select one correct answer from the following list: ► Generic element

► Heading element

► Paragraph element ► Nothing will be printed.

Question No: 18 ( Marks: 1 ) - Please choose one

When a virtual function is called by referencing a specific object by name and using the

dot member selection operator (e.g., squareObject.draw()), the reference is resolved at

compile time.

► True ► False

Page 31: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

31

CS304 Object Oriented Programming Midterm

Paper

Question No: 19 ( Marks: 1 ) - Please choose one

In case of multiple inheritance a derived class inherits, ► Only the public member functions of its base classes

► Only the public data members of its base classes

► Both public data members and member functions of all its base classes ► Data members and member functions of any two base classes

Question No: 20 ( Marks: 1 ) - Please choose one

When we write a class template the first line must be: ► template < class class_name>

► template < class data_type>

► template < class T >

Here T can be replaced with any name but it is preferable. ► class class-name()

class template<class_name>

Question No: 21 ( Marks: 1 ) - Please choose one

Which of the following is incorrect line regarding function template? ► template<class T>

► template <typename U>

► Class<template T> ► template < class T, class U>

Question No: 22 ( Marks: 1 ) - Please choose one

An STL container can not be used to, ► hold objects of class employee.

► store elements in a way that makes them quickly accessible.

► compile c++ programs.

► organize the way objects are stored in memory

Question No: 23 ( Marks: 1 ) - Please choose one

Algorithms can only be implemented using STL containers.

► True ► False

Question No: 24 ( Marks: 1 ) - Please choose one

Consider a class named Vehicle, which of the following can be the instance of class

Vehicle?

1. Car

2. Computer

3. Desk

4. Ahmed

5. Bicycle

6. Truck ► 1, 4, 5

► 2, 5, 6

► 1, 2, 3, 6

► 1, 5, 6

Page 32: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

32

CS304 Object Oriented Programming Midterm

Paper

Question No: 25 ( Marks: 1 ) - Please choose one

Consider the code below,

class Fred {

public:

Fred();

...

};

int main()

{

Fred a[10];

Fred* p = new Fred[10];

...

}

Select the best option, ► Fred a[10]; calls the default constructor 09 times

Fred* p = new Fred[10]; calls the default constructor 10 times ► Produce an error

► Fred a[10]; calls the default constructor 11 times

Fred* p = new Fred[10]; calls the default constructor 11 times ► Fred a[10]; calls the default constructor 10 times

Fred* p = new Fred[10]; calls the default constructor 10 times

Question No: 26 ( Marks: 1 ) - Please choose one

When a variable is define as static in a class then all object of this class, ► Have different copies of this variable

► Have same copy of this variable ► Can not access this variable

► None of given

Question No: 27 ( Marks: 1 ) - Please choose one

The life of sub object is dependant on the life of master class in _____________. ► Separation

► Composition

► Aggregation

► None of the given

Question No: 28 ( Marks: 1 ) - Please choose one

___________, which means if A declares B as its friend it does NOT mean that A can

access private data of B. It only means that B can access all data of A. ► Friendship is one way only

► Friendship is two way only

► NO Friendship between classes

► Any kind of friendship

Question No: 29 ( Marks: 1 ) - Please choose one

Which of the following operators always takes no argument if overloaded? ► /

► -

► +

Page 33: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

33

CS304 Object Oriented Programming Midterm

Paper

► ++

Question No: 30 ( Marks: 1 ) - Please choose one

In Private -------------- only member functions and friend classes or functions of a

derived class can convert pointer or reference of derived object to that of parent object ► specialization

► inheritance

► abstraction

► composition

MCQz (Set-11)

Question # 1 of 10 ( Start time: 08:40:52 AM ) Total Marks: 1

Outside world can access only __________ members of a class using its object.

Select correct option:

Public

Private

Protected

No member is accessible.

Question # 2 of 10 ( Start time: 08:41:55 AM ) Total Marks: 1

Consider the code below, class class1{ private: int i; }; class class2 : protected class1 { };

Then int member i of class1 is ______ in class2,

Select correct option:

public

protected

private

none of the given options

Question # 4 of 10 ( Start time: 08:44:20 AM ) Total Marks: 1

In Public Inheritance the public members of base class become __________ in derived

class.

Select correct option:

Public

Private

Protected

None of the given options.

Question # 5 of 10 ( Start time: 08:45:35 AM ) Total Marks: 1

Consider the code below, class class1{ protected: int i; }; class class2 : public class1 { };

Then int member i of class1 is ______ in class2,

Select correct option:

public

protected

private

none of the given options

Question No: 1 ( Marks: 1 ) - Please choose one

Which one of the following terms must relate to polymorphism?

Page 34: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

34

CS304 Object Oriented Programming Midterm

Paper

► Static allocation

► Static typing

► Dynamic binding ► Dynamic allocation

Question No: 2 ( Marks: 1 ) - Please choose one

Multiple inheritance can be of type ► Public

► Private

► Protected

► All of the given

Question No: 3 ( Marks: 1 ) - Please choose one

When a subclass specifies an alternative definition for an attribute or method of its

superclass, it is _______ the definition in the superclass. ► overload

► overriding ► copy riding

► none of given

Question No: 4 ( Marks: 1 ) - Please choose one

Like template functions, a class template may not handle all the types successfully.

► True ► False

Question No: 5 ( Marks: 1 ) - Please choose one

It is sometimes useful to specify a class from which no objects will ever be created. ► True

► False

Question No: 6 ( Marks: 1 ) - Please choose one

Assume a class Derv that is privately derived from class Base. An object of class Derv

located in main() can access ► public members of Derv.

► protected members of Derv.

► private members of Derv.

► protected members of Base.

Question No: 7 ( Marks: 1 ) - Please choose one

A pointer to a base class can point to objects of a derived class.

► True ► False

Question No: 8 ( Marks: 1 ) - Please choose one

A copy constructor is invoked when ► a function do not returns by value.

► an argument is passed by value.

► a function returns by reference. ► an argument is passed by reference.

Page 35: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

35

CS304 Object Oriented Programming Midterm

Paper

Question No: 9 ( Marks: 1 ) - Please choose one

A function call is resolved at run-time in___________ ► non-virtual member function.

► virtual member function. ► Both non-virtual member and virtual member function.

► None of given

Question No: 10 ( Marks: 1 ) - Please choose one

When the base class and the derived class have a member function with the same name,

you must be more specific which function you want to call (using ___________).

► scope resolution operator ► dot operator

► null operator

► Operator overloading

Question No: 11 ( Marks: 1 ) - Please choose one

Each try block can have ______ no. of catch blocks. ► 1

► 2

► 3

► As many as necessary.

Question No: 12 ( Marks: 1 ) - Please choose one

Two important STL associative containers are _______ and _______. ► set,map

► sequence,mapping ► setmet,multipule

► sit,mat

Question No: 13 ( Marks: 1 ) - Please choose one

The mechanism of selecting function at run time according to the nature of calling object

is called, ► late binding

► static binding

► virtual binding

► None of the given options

Question No: 14 ( Marks: 1 ) - Please choose one

An abstract class is useful when, ► We do not derive any class from it.

► There are multiple paths from one derived class to another.

► We do not want to instantiate its object. ► You want to defer the declaration of the class.

Question No: 15 ( Marks: 1 ) - Please choose one

Which of the following is incorrect line regarding function template? ► template<class T>

► template <typename U>

Page 36: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

36

CS304 Object Oriented Programming Midterm

Paper

► Class<template T> ► template < class T, class U>

Question No: 16 ( Marks: 1 ) - Please choose one

Which of the following is/are advantage[s] of generic programming? ► Reusability

► Writability

► Maintainability

► All of given

Question No: 17 ( Marks: 1 ) - Please choose one

By default the vector data items are initialized to ____

► 0 ► 0.0

► 1

► null

Question No: 18 ( Marks: 1 ) - Please choose one

Which one of the following functions returns the total number of elements in a vector. ► length();

► size(); ► ele();

► veclen();

Question No: 19 ( Marks: 1 ) - Please choose one

Suppose you create an uninitialized vector as follows:

vector<int> evec;

After adding the statment,

evec.push_back(21);

what will happen? ► The following statement will add an element to the start (the back) of evec and will

initialize it with the value 21. ► The following statement will add an element to the center of evec and will reinitialize

it

with the value 21. ► The following statement will delete an element to the end (the back) of evec and will

reinitialize it with the value 21.

► The following statement will add an element to the end (the back) of evec and

initialize it with the value 21.

Question No: 20 ( Marks: 1 ) - Please choose one

An STL container can not be used to, ► hold objects of class employee.

► store elements in a way that makes them quickly accessible.

► compile c++ programs. ► organize the way objects are stored in memory

Question No: 21 ( Marks: 1 ) - Please choose one

Algorithms can only be implemented using STL containers.

Page 37: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

37

CS304 Object Oriented Programming Midterm

Paper

► True

► False

Question No: 22 ( Marks: 1 ) - Please choose one

The main function of scope resolution operator (::) is,

► To define an object ►To define a data member

►To link the definition of an identifier to its declaration

►To make a class private

Question No: 23 ( Marks: 1 ) - Please choose one

When is a constructor called? ►Each time the constructor identifier is used in a program statement

► During the instantiation of a new object ►During the construction of a new class

►At the beginning of any program execution

Question No: 24 ( Marks: 1 ) - Please choose one

Consider the code below,

class Fred {

public:

Fred();

...

};

int main()

{

Fred a[10];

Fred* p = new Fred[10];

...

}

Select the best option, ►Fred a[10]; calls the default constructor 09 times

Fred* p = new Fred[10]; calls the default constructor 10 times

► Produce an error ►Fred a[10]; calls the default constructor 11 times

Fred* p = new Fred[10]; calls the default constructor 11 times ►Fred a[10]; calls the default constructor 10 times

Fred* p = new Fred[10]; calls the default constructor 10 times

Question No: 25 ( Marks: 1 ) - Please choose one

Associatively can be changed in operator overloading. ►True

► False

Question No: 26 ( Marks: 1 ) - Please choose one

A normal C++ operator that acts in special ways on newly defined data types is said to

be ►glorified.

►encapsulated.

Page 38: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

38

CS304 Object Oriented Programming Midterm

Paper

► classified. ►overloaded.

Question No: 27 ( Marks: 1 ) - Please choose one

Which operator can not be overloaded? ►The relation operator ( >= )

►Assignment operator ( = )

►Script operator ( [] )

► Conditional operator (? : )

Question No: 28 ( Marks: 1 ) - Please choose one

Suppose obj1 and obj2 are two objects of a user defined class A. An + operator is

overloaded to add obj1 and obj2 using the function call obj1+obj2. Identify the correct

function prototype against the given call? ► A operator + ( A &obj);

► int + operator();

► int operator (plus) (); ► A operator(A &obj3);

Question No: 29 ( Marks: 1 ) - Please choose one

Default constructor is such constructor which either has no ---------or if it has some

parameters these have -------- values ► Parameter, temporary

► Null, Parameter

► Parameter, default ► non of the given

Question No: 30 ( Marks: 1 ) - Please choose one

Public methods of base class can --------- be accessed in its derived class ► directly

► inderectly ► simultaniously

► non of the given

Question No: 31 ( Marks: 1 )

Is Deque a Birectional Container?

Yes, deque behaves like queue (line) such that we can add elements on both sides of it.

Question No: 32 ( Marks: 1 )

What is meant by Generic Programming?

Generic programming refers to programs containing generic abstractions general code

that is same in logic for all data types like printArray function), then we instantiate that

generic program abstraction (function, class) for a particular data type, such

abstractions

can work with many different types of data.

Question # 1 of 10

Information hiding can be achieved through__________.

1.Encapsulation, Inheritance

2.Encapsulation, Polymorphism

Page 39: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

39

CS304 Object Oriented Programming Midterm

Paper

3.Encapsulation, Abstraction

4.Overloading

Question # 2 of 10 ( Start time: 01:11:21 AM ) Total Marks: 1

A good model is ................ related to a real life problem.

Select correct option:

1.Loosely

2.Openly

3.Closely

Question # 3 of 10 ( Start time: 01:12:33 AM ) Total Marks: 1

Which of the following features of OOP is used to derive a class from

another?

Select correct option:

1.Encapsulation

2.Polymorphism

3.Data hiding

4. Inheritance

Question # 4 of 10 ( Start time: 01:13:51 AM ) Total Marks: 1

Which of the following is a weak relationship between two objects?

Select correct option:

1. Inheritance

2.Composition

3. Aggregation

4. None of given

Question # 5 of 10 ( Start time: 01:14:56 AM ) Total Marks: 1

Data items in a class must be private.

Select correct option:

1. True

2. False

Question # 7 of 10 ( Start time: 01:16:55 AM ) Total Marks: 1

Suppose there is an object of type Person, which of the following can

be considered as one of its attributes

Select correct option:

1. Name

2. Age

3. Work ()

4. Both Name and Age

Question # 8 of 10 ( Start time: 01:17:52 AM ) Total Marks: 1

Which one is not an object association?

Select correct option:

1. Simple association

2. Inheritance

3. Aggregation

4. Association

Page 40: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

40

CS304 Object Oriented Programming Midterm

Paper

Question # 9 of 10 ( Start time: 01:18:50 AM ) Total Marks: 1

Using encapsulation we can achieve

Select correct option:

1. Information hiding

2. Least interdependencies among modules

3. Implementation independence

4. All of given options

Question # 10 of 10 ( Start time: 01:19:43 AM ) Total Marks: 1

In constant member function the type of this pointer is:

Select correct option:

1. Constant pointer

2. Constant pointer to object

3. Constant pointer to class

4. Constant pointer to constant object

Question # 1 of 10

Which of the following is the way to extract common behavior and attributes from the

given classes and make a separate class of those common behaviors and attributes?

1. Generalization

2. Sub-typing

3. Specialization

4. Extension

Question # 2 of 10

The ability to derive a class from more than one class is called

1. Single inheritance

2. Encapsulation

3. Multiple inheritances

4. Polymorphism

Question # 3 of 10:

If MyClass has a destructor what is the destructor named?

1. MyClass

2. ~MyClass

3. My~Class

4. MyClass~

Question # 4 of 10:

Class abc{ ----- }; Is a valid class declaration?

1.yes

2.no

Question # 5of 10:

Without using Deep copy constructor, A ____________ problem can

occur

1.System crash

2.Memory Leakage

3.Dangling pointer

Page 41: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

41

CS304 Object Oriented Programming Midterm

Paper

4.All of the given

Question # 6 of 10:

If only one behavior of a derived class is incompatible with base

Class, then it is:

1. Generalization

2. Specialization

3. Extension

4. Inheritance

Question # 7 of 10:

Which of the following may not be an integral part of an object?

1. State

2. Behavior

3. Protected data members

4. All of given

Question # 8 of 10:

Only tangible things can be chosen as an object.

1. True

2. False

Question # 1 of 10

Information hiding can be achieved through__________.

Encapsulation, Inheritance

Encapsulation, Polymorphism

Encapsulation, Abstraction

Overloading

Question # 2 of 10 (Start time: 01:11:21 AM) Total Marks: 1

A good model is ................ related to a real life problem.

Select correct option:

Loosely

Openly

Closely

Question # 3 of 10 ( Start time: 01:12:33 AM ) Total Marks: 1

Which of the following features of OOP is used to derive a class from another?

Select correct option:

Encapsulation

Polymorphism

Data hiding

Inheritance

Question # 4 of 10 ( Start time: 01:13:51 AM ) Total Marks: 1

Which of the following is a weak relationship between two objects?

Select correct option:

Inheritance

Composition

Page 42: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

42

CS304 Object Oriented Programming Midterm

Paper

Aggregation

None of given

Question # 5 of 10 ( Start time: 01:14:56 AM ) Total Marks: 1

Data items in a class must be private.

Select correct option:

True

False

Question # 7 of 10 ( Start time: 01:16:55 AM ) Total Marks: 1

Suppose there is an object of type Person, which of the following can be considered as

one of its attributes

Select correct option:

Name

Age

Work()

Both Name and Age

Question # 8 of 10 ( Start time: 01:17:52 AM ) Total Marks: 1

Which one is not an object association?

Select correct option:

Simple association

Inheritance

Aggregation

Association

Question # 9 of 10 ( Start time: 01:18:50 AM ) Total Marks: 1

Using encapsulation we can achieve

Select correct option:

Information hiding

Least interdependencies among modules

Implementation independence

All of given options

Question # 10 of 10 ( Start time: 01:19:43 AM ) Total Marks: 1

In constant member function the type of this pointer is:

Select correct option:

Constant pointer

Constant pointer to object

Constant pointer to class

Constant pointer to constant object

Question # 1 of 10

Which of the following is the way to extract common behavior and attributes from the

given classes and make a separate class of those common behaviors and attributes?

Generalization

Sub-typing

Specialization

Extension

Page 43: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

43

CS304 Object Oriented Programming Midterm

Paper

Question # 2 of 10

The ability to derive a class from more than one class is called

Single inheritance

Encapsulation

Multiple inheritance

Polymorphism

Question # 3 of 10:

If MyClass has a destructor what is the destructor named?

MyClass

~MyClass

My~Class

MyClass~

Question # 4 of 10:

Class abc{ ----- }; Is a valid class declaration?

yes

no

Question # 5of 10:

Without using Deep copy constructor, A ____________ problem can occur

System crash

Memory Leakage

Dangling pointer

All of the given

Question # 6 of 10:

If only one behaviour of a derived class is incompatible with base class, then it is:

Generalization

Specialization

Extension

Inheritance

Question # 7 of 10:

Which of the following may not be an integral part of an object?

state

behavior

Protected data members

All of given

Question # 8 of 10:

Only tangible things can be chosen as an object.

True

False

Class is not a mechanism to create objects and define user data types.

1. true

2. false

Page 44: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

44

CS304 Object Oriented Programming Midterm

Paper

=========================================================>

MCQz (Set-14)

Memory is allocated to non static members only, when:

1. Class is created

2. Object is defined

3. Object is initialized

4. Object is created

The sub-object‘s life is not dependent on the life of master class in ___________.

1. Composition

2. Aggregation

3. Separation

4. Non of the given

Unary operators and assignment operator are right associative.

1. True

2. False

The >= operator can't be overloaded.

1. True

2. False

_____ is creating objects of one class inside another class.

1. Association

2. Composition

3. Aggregation

4. Inheritance

If we are create array of objects through new operator, then

1. We can call overloaded constructor through new

2. We can‘t call overloaded constructor through new

3. We can call default constructor through new

4. None of the given

Object can be declared constant with the use of Constant keyword.

1. True

2. False

__________ Operator will take only one operand.

1. New

2. int

3. Object

4. None of the given

Which of the following operator(s) take(s) one or no argument if overloaded?

1. ++

2. *

3. %

Page 45: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

45

CS304 Object Oriented Programming Midterm

Paper

4. All of the given choices

This pointer does not pass implicitly to __________ functions.

1. Static Member

2. Non-Static Member

3. Instance Number

4. None of the given

Operator overloading is

1. Making C++ operators work with objects.

2. Giving C++ operators more than they can handle.

3. Giving new meanings to existing Class members.

4. Making new C++ operators

MCQz (Set-15)

Question # 1 of 10 ( Start time: 09:57:41 AM ) Total Marks: 1

Consider the code below, class class1 {public: void func1 () ;}; class class2: private

class1 { }; Function func1 of class1 is ______ in class2,

Select correct option:

Public

Protected

Private

None of the given options

Click here to Save Answer & Move to Next Question

Question # 2 of 10 ( Start time: 09:59:01 AM ) Total Marks: 1

User can make virtual table explicitly.

Select correct option:

True

False

Click here to Save Answer & Move to Next Question

Question # 3 of 10 ( Start time: 10:00:15 AM ) Total Marks: 1

In private inheritance derived class pointer can be assigned to base class pointer in,

Select correct option:

Main function

In derived class member and friend functions

In base class member and friend functions

None of the given options

Click here to Save Answer & Move to Next Question

Question # 4 of 10 ( Start time: 10:01:15 AM ) Total Marks: 1

In C++, we declare a function virtual by preceding the function header with keyword

―Inline‖

Select correct option:

True

False

Click here to Save Answer & Move to Next Question

Page 46: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

46

CS304 Object Oriented Programming Midterm

Paper

Question # 5 of 10 ( Start time: 10:02:45 AM ) Total Marks: 1

Outside world can access only __________ members of a class using its object.

Select correct option:

Public

Private

Protected

No member is accessible.

Click here to Save Answer & Move to Next Question

Question # 6 of 10 ( Start time: 10:03:10 AM ) Total Marks: 1

Friend Functions of a class are _________ members of that class.

Select correct option:

Public

Private

Protected

None of the given options.

Click here to Save Answer & Move to Next Question

Question # 7 of 10 ( Start time: 10:03:54 AM ) Total Marks: 1

Consider the following two lines of code written for a class Student, 1. Student

sobj1,sobj2; 2. sobj2 = sobj1; In line No.2 what constructor of Student class will be

called,

Select correct option:

Default constructor of Student class.

Copy constructor of student class

Both default and copy constructer of Student class

No constructor will be called.

Question # 10 of 10 ( Start time: 12:57:00 PM ) Total Marks: 1

If a class A inherits from class B, then class A is called.

Select correct option:

Child class

Derived class

Parent class

Child and derived class

Question # 1 of 10 ( Start time: 12:59:51 PM ) Total Marks: 1

If some of objects exhibit identical characteristics, then they belong to:

Select correct option:

Different classes

Multiple classes

Same class

None of the given

Question # 2 of 10 ( Start time: 01:00:41 PM ) Total Marks: 1

________ is automatically called when the object is created.

Select correct option:

member function

Page 47: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

47

CS304 Object Oriented Programming Midterm

Paper

object

constructor

None of the given

Question # 4 of 10 ( Start time: 01:04:28 PM ) Total Marks: 1

If a class involves dynamic memory allocation, then:

Select correct option:

Default copy constructor, shallow copy is implementing

User defined copy constructor, shallow copy is implement

Default copy constructor, deep copy is implemented

User defined copy constructor, deep copy is implement

Question # 5 of 10 ( Start time: 01:05:37 PM ) Total Marks: 1

Which one is a not class association

Select correct option:

Simple Association

Inheritance

Composition

Aggregation

Question # 6 of 10 ( Start time: 01:06:50 PM ) Total Marks: 1

Data items in a class must be private.

Select correct option:

True

False

Question # 7 of 10 ( Start time: 01:07:16 PM ) Total Marks: 1

Three main characteristics of "Object Oriented programming" are,

Select correct option:

Encapsulation, dynamic binding, polymorphisms

Polymorphism overloading, overriding

Encapsulation, inheritance, dynamic binding

Encapsulation, inheritance, polymorphism

Question # 8 of 10 ( Start time: 01:08:14 PM ) Total Marks: 1

Which of the following is the way to extract common behavior and attributes from the

given classes and make a separate class of those common behaviors and attributes?

Select correct option:

Generalization

Sub-typing

Specialization

Extension

Question # 9 of 10 ( Start time: 01:09:04 PM ) Total Marks: 1

The sentence ―Object Oriented Programming book in bookshelf‖ is an example of:

Select correct option:

Association

Multiple association

Aggregation

Page 48: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

48

CS304 Object Oriented Programming Midterm

Paper

Question # 10 of 10 ( Start time: 01:16:05 PM ) Total Marks: 1

Data members are the attributes of objects.

Select correct option:

True

False

Question # 1 of 10 ( Start time: 01:18:48 PM ) Total Marks: 1

Constructor have same name as the class name.

Select correct option:

True

False

Question # 2 of 10 ( Start time: 01:19:03 PM ) Total Marks: 1

Which of the following features of OOP is used to derive a class from another?

Select correct option:

Encapsulation

Polymorphism

Data hiding

Inheritance

Question # 3 of 10 ( Start time: 01:19:29 PM ) Total Marks: 1

Class abc{ ----- }; Is a valid class declaration?

Select correct option:

Yes

No

Question # 6 of 10 ( Start time: 01:22:47 PM ) Total Marks: 1

Which of the following is a weak relationship between two objects?

Select correct option:

Inheritance

Composition

Aggregation

None of given

Question # 4 of 10 ( Start time: 01:20:47 PM ) Total Marks: 1

Without using Deep copy constructor, A ____________ problem can occur

Select correct option:

System crash

Memory Leakage

Dangling pointer

All of the given

Question # 5 of 10 ( Start time: 01:21:20 PM ) Total Marks: 1

An abstract class shows ____________ behaviour.

Select correct option:

Overriding

Specific

General

Page 49: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

49

CS304 Object Oriented Programming Midterm

Paper

None of the given

Question # 8 of 10 ( Start time: 01:24:19 PM ) Total Marks: 1

If a class A inherits from class B, then class A is called.

Select correct option:

Child class

Derived class

Parent class

Child and derived class

Question # 9 of 10 ( Start time: 01:24:44 PM ) Total Marks: 1

Consider the statement ―room has chair‖ which of the following type of association

exists between room and chair?

Select correct option:

Inheritance

Composition

There is no association

Aggregation

Question # 10 of 10 ( Start time: 01:25:05 PM ) Total Marks: 1

The dot operator (or class member access operator) connects the following two entities

(reading from left to right):

Select correct option:

A class member and a class object

A class object and a class

A class and a member of that class

A class object and a member of that class

MCQz (Set-17)

Question No: 1 ( Marks: 1 ) – Please choose one

Which part of an object exhibits its state?

► Data ► Operations

► Any public part

► Any private part

Question No: 2 ( Marks: 1 ) – Please choose one

Inheritance is a way to ► organize data.

► pass arguments to objects of classes.

► add features to existing classes without rewriting them. ► improve data-hiding and encapsulation.

Question No: 3 (Marks: 1) – Please choose one

Suppose you have been given the following design, A person has a name, age, address

and —–. You are designing a class to represent a type of person called a patient. This

kind of person may be given a diagnosis, have a Spouse and may be alive‖.

Given that the person class has already been created, what of the following would be

Page 50: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

50

CS304 Object Oriented Programming Midterm

Paper

Appropriate to include when you design the patient class?

► Registration date and diagnosis ► Age and —–

► —– and diagnosis

► Diagnosis and age

Question No: 4 ( Marks: 1 ) – Please choose one

What problem(s) may occur when we copy objects without using deep copy

constructor? ► Dangling pointer

► Memory Leakage

► All of the given ► System crash

Question No: 5 ( Marks: 1 ) – Please choose one

These pointers are not accessible for static member functions.

► True ► False

Question No: 6 ( Marks: 1 ) – Please choose one

A static member function cannot be declared. ► Static

► Implicit ► Explicit

► Virtual

Question No: 7 ( Marks: 1 ) – Please choose one

_______ remain in memory even when all objects of a class have been destroyed.

► Static variables ► Instance variable

► Primitive variables

► None of given

Question No: 8 ( Marks: 1 ) – Please choose one

Friend functions are _____________ functions of a class. ► None of given

► object member

► non-member ► data member

Question No: 9 ( Marks: 1 ) – Please choose one

___________, which means if A declares B as its friend it does NOT mean that A can Access

private data of B. It only means that B can access all data of A.

► Friendship is one way only ► Friendship is two way only

► NO Friendship between classes

► Any kind of friendship

Question No: 10 ( Marks: 1 ) – Please choose one

Page 51: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

51

CS304 Object Oriented Programming Midterm

Paper

The statement objA=objB; will cause a compiler error if the objects are of different

Classes. ► True

► False

The members of a class that can be accessed without creating the object of the

class is

called

Private member

Data Member

Public Member

Static

‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

CS304 Object Oriented Programming Subjective

21. If, within a class, marks is a member variable, will the statement

this.marks=37; assign 37 to 'marks'? 02

Answer: no within a class, marks is a member variable, will the statement

this.marks=37; assign 37 to 'marks'.

22. Consider the following call of an overloaded “+” operator:

Rational_number_1 + Rational_number_2;

Identify which object will be passed as an argument to the overloaded function.

Justify your answer with at least one solid reason.

Hint: Rational_number_1 and Rational_number_2 are user defined objects. 02

Answer: Rational_number_2 will be passed

For example Complex t = c1 + c2 + c3;

The above statement is automatically converted by the compiler into appropriate

function calls:

(c1.operator +(c2)).operator +(c3); (Pg. 143)

23. Suppose we have a Base class A and a Derived class B, show by a diagram how

object of Derived class B will be represented in memory.3

Page 52: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

52

CS304 Object Oriented Programming Midterm

Paper

24. Can you justify that why it’s saying that “to keep all getter functions of a class as

constant” is a good programming practice? 03

Ans: Good Practice:

Never return a handle to a data member from getter function because you are

never sure that function accessing the reference will not change the value of the

variable.

25. Can you justify that why it’s saying that “to keep all getter functions of a class as

constant” is a good programming practice? 05

Ans: Good Practice:

Never return a handle to a data member from getter function because you are

never sure that function accessing the reference will not change the value of the

variable.

26. Suppose we have a Base class Woman and a Derived class Lady Doctor Describe

the case in which we can use derived class “Lady Doctor” Member Initialization List

to initialize the base class “Woman” data members. Give a practical example.

05marks

Page 53: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

53

CS304 Object Oriented Programming Midterm

Paper

Ans: Order of Initialization

• Data member are initialized in order they are declared in the class

• Order in member initializer list is not significant at all

Constructors

• The anonymous object of base class must be initialized using constructor of base

class

• When a derived class object is created the constructor of base class is executed

before the constructor of derived class

Example

class Woman{

public:

Woman(){ cout <<

“Woman Constructor...”;}

};

class Lady_doctor : public Woman{

public:

Lady_doctor(){ cout <<

“Lady_doctor Constructor...”;}

};

int main(){

Lady_doctor cobj;

return 0;

}

Output:

Woman Constructor...

Lady_doctor Constructor...

Page 54: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

54

CS304 Object Oriented Programming Midterm

Paper

21. Friend functions minimize "Encapsulation", what is your opinion? 02

Ans: It can be said that friend functions are against the principle of object

oriented programming because they violate the principle of encapsulation which

clearly says that each object methods and functions should be encapsulated in it.

But there we are making our private member accessible to other outside

functions.

22. Can you justify by giving an example/ sound reasoning that is it possible to

make an “Array of Objects”? 2

Ans: (Pg. 114&115)

Array of Objects

1. Array of objects can only be created if an object can be created without

supplying an explicit initializer

2.There must always be a default constructor if we want to create array of

objects

Example

class Test{

public:

};

int main(){

Test array[2]; // OK

}

class Test{

public:

Test();

};

int main(){

Test array[2]; // OK

}

Page 55: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

55

CS304 Object Oriented Programming Midterm

Paper

class Test{

public:

Test(int i);

};

int main(){

Test array[2]; // Error

}

class Test{

public:

Test(int i);

}

int main(){

Test array[2] = {Test(0),Test(0)};

}

class Test{

public:

Test(int i);

}

int main(){

Test a(1),b(2);

Test array[2] = {a,b};

}

23Suppose we have a parent class and a child class, give the order in which

constructors and destructors of these classes will be called when we will create an

object of child class. 3marks

Answer:

Sequence of Calls

Page 56: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

56

CS304 Object Oriented Programming Midterm

Paper

Constructors and destructors are called automatically

Constructors are called in the sequence in which object is declared

Destructors are called in reverse order

Example

Student::Student(char * aName){

cout << aName << “Constructor\n”;

}

Student::~Student(){

cout << name << “Destructor\n”;

}

};

Example

int main()

{ Student studentB(“Ali”);

Student studentA(“Ahmad”);

return 0;

}

Example

Output:

Ali Constructor

Ahmad Constructor

Ahmad Destructor

Ali Destructor

24. What will be output after executing following lines of code, (suppose there is no

error and code will be executed correctly), justify your answer as well,

Page 57: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

57

CS304 Object Oriented Programming Midterm

Paper

class Student{

static int noOfStudents;

int rollNo;

public:

Student::Student(){

noOfStudents++;

}

Student::~Student(){

noOfStudents--;

}

static int getTotalStudent(){

return noOfStudents;

}

};

int Student::noOfStudents = 0;

int main(){

Student obj1,obj2;

{

Student obj3;

}

cout<<Student::getTotalStudent();

} 3marks

Answer: The output will be 3

Page 58: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

58

CS304 Object Oriented Programming Midterm

Paper

25. How we resolve the following problems in overloading of assignment operator

in string class, (explain with the help of c++ code)

a. Self referencing

b. Assigning a string value to more than one strings in a single line like,

stringobject1 = string object2 = stringobject3 = stringobject4

5marks

Ans: (Pg. 148-9)

Page 59: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

59

CS304 Object Oriented Programming Midterm

Paper

Self Refrencing:

If we assign same string to itself as done in main function below our program will

produce unexpected results as source and destination operands for copying are

same, this is called self refrencing or self assignment.

int main(){

String str1("Fakhir");

str1 = str1; // Self Assignment problem…

return 0;

}

We can resolve this issue by adding a simple if condition to ensure that both

strings

are not same

String & String :: operator = (const String & rhs){

if(this != &rhs){

size = rhs.size;

delete [] bufferPtr; // deleting memory of left hand side operand

if(rhs.bufferPtr != NULL){

bufferPtr = new char[rhs.size+1];

strcpy(bufferPtr,rhs.bufferPtr);

// memory access violation or incorrect data copy

}

else bufferPtr = NULL;

}

return *this;

}

Now self-assignment is properly handled:

int main(){

Page 60: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

60

CS304 Object Oriented Programming Midterm

Paper

String str1("Fakhir");

str1 = str1;

return 0;

}

Part B: Assigning a string value to more than one strings in a single line like,

stringobject1 = string object2 = stringobject3 = stringobject4 5marks

Page 61: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

61

CS304 Object Oriented Programming Midterm

Paper

class String{

public:

String & operator = (const String &);

};

String & String :: operator = (const String & rhs){

size = rhs.size;

delete [] bufferPtr;

if(rhs.size != 0){

bufferPtr = new char[rhs.size+1];

strcpy(bufferPtr,rhs.bufferPtr);

}

else bufferPtr = NULL;

return *this;

}

int main(){

String str1(“ABC");

String str2(“DE”), str3(“FG”);

str1 = str2; // Valid…

str1 = str2 = str3; // Error…

return *this;

}

26. Non-static variables can be accessed through objects of a class. List any two

ways to access static variables of the class with C++ coding example.

5mark

Ans:

class Student{

Page 62: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

62

CS304 Object Oriented Programming Midterm

Paper

static int noOfStudents;

int rollNo;

public:

Student::Student(){

noOfStudents++;

}

Student::~Student(){

noOfStudents--;

}

static int getTotalStudent(){

return noOfStudents;

}

};

int Student::noOfStudents = 0;

int main(){

Student obj1,obj2;

{

Student obj3;

}

cout<<Student::getTotalStudent();

}

21. Can we use initialization list to set the value of any base class public data

member, justify your answer in either case as well.02

Ans:

Page 63: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

63

CS304 Object Oriented Programming Midterm

Paper

Yes we use initialization list to set the value of any base class public data

member. We can access through child class. also define with help of eg

22. Can we create an array of objects for a class having user defined constructor?

Justify your answer.2

Ans Yes we create an array of objects for a class having user defined constructor.

class Test{

public:

Test(int i);

}

int main(){

Test array[2] = {Test(0),Test(0)};

}

Page 64: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

64

CS304 Object Oriented Programming Midterm

Paper

23. Considering the following scenario:

A class X does not have any overloaded operators and has a member function

“Subtract” to subtract two objects of X, and then placing the result in Third object,

where Third object is the calling object.

Write a single statement that performs the above functionality. You can take any

name for the three objects of class X. 03

Ans:

class X{

private:

double real, img;

public:

X Subtract(const X &);

}

X X::Subtract(

const X & c1){

X t;

t.real = real - c1.real;

t.img = img - c1.img;

return t;

}

// Subtracts the contents of c2 to c1

// creating new object c3 and assigning it result of c1-c2

24. Suppose we have a Base class Vehicle and a Derived Van. Describe the

accessibility of public, protected and private members of base class “Vehicle” in

derived class “Van” in case of public inheritance. 3

Ans:

Page 65: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

65

CS304 Object Oriented Programming Midterm

Paper

1. ‘public’ is used to tell that member can be accessed whenever you have access

to the object.

2. ‘private’ is used to tell that member can only be accessed from a member

function

3. ‘protected’ to be discussed when we cover inheritance

1. class vehicle ( )

{

Public:

Int weight;

};

Class van: public vehicle

{

};

Void main( )

{

Van obj1

Obj1.weight=10’;

}

2. class vehicle ( )

{

Private:

Int weight;

};

Class van: private vehicle

{

};

Void main( )

{

Page 66: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

66

CS304 Object Oriented Programming Midterm

Paper

Van obj1

Obj1.weight=10’;

}

3. class vehicle ( )

{

Protected:

Int weight;

};

Class van: protected vehicle

{

};

Void main( )

{

Van obj1

Obj1.weight=10’;

}

Page 67: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

67

CS304 Object Oriented Programming Midterm

Paper

25. Let’s suppose there is an integer array of size 10. One way to initialize array is

A[10]= { 1,2,3,4,5,6,7,8,9};

Is there any way that class of array of objects can also be initialized in the same

way? Give a C++ coding example. 05

Ans: Yes for Example

class Test{

public:

Test(int i);

}

int main(){

Test array[2] = {Test(0),Test(0)};

}

26. Give the c++ code to overload post and pre decrement operators for "Complex

Number" class.05

Ans: Post-increment ++:

Post-increment operator ++ increments the current value and then returns the

previous value

previous value

Post-decrement --: Works exactly like post ++

Example:

int x = 1, y = 2;

cout << y++ << endl;

cout << y;

Output:

2

3

Example:

Page 68: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

68

CS304 Object Oriented Programming Midterm

Paper

int y = 2;

y++++; // Error

y++ = x; // Error

Pre-increment ++:

Pre-increment operator ++ increments the current value and then returns it’s

reference

Pre-decrement --:

Works exactly like Pre-increment ++

Example:

int y = 2;

cout << ++y << endl;

cout << y << endl;

Output:

3

3

Example:

int x = 2, y = 2;

++++y;

cout << y;

++y = x;

cout << y;

Output:

4

2

(Pg. 164 Lecture No.21) for more reference

21. Identify binary and unary operators from the following:

sizeof, %, --, *, /, ++, =, &&, & 02

Page 69: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

69

CS304 Object Oriented Programming Midterm

Paper

Ans:

22. Write a statement that a member function can use to return the entire object of

which it is a member, without creating any temporary objects. 02

Ans: In the statements the given below return statement is returning the whole

object of class complex with its function operator + ( )

Complex Complex: operator +( const Complex & rhs){

Complex t;

t.real = real + rhs.real;

t.img = img + rhs.img;

return t;

}

Page 70: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

70

CS304 Object Oriented Programming Midterm

Paper

23. Give example c++ code to overload unary ‘ ++ ‘ operator for complex numbers

class.3

Ans: class Complex{

double real, img;

public:

...

Complex & operator ++ ();

// friend Complex & operator ++(Complex &);

}

Complex & Complex::operator++(){ // member function

real = real + 1;

return * this;

}

Complex & operator ++ (Complex & h){ // non member function

h.real += 1;

return h;

}

24. Consider the class given below explain the order in which variables x,y and z will

be initialized after creating object of this class,

class ABC{

int x;

int y;

int z;

public:

ABC();

};

ABC::ABC():z(10),x(20),y(30)

Page 71: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

71

CS304 Object Oriented Programming Midterm

Paper

{

} 03mrks

Ans: Data member are initialized in order they are declared in the class

Order in member initializer list is not significant at all

class ABC{

int x;

int y;

int z;

public:

ABC();

};

ABC::ABC():y(10),x(y),z(y)

{

}

/* x = Junk value

y = 10

z = 10 */ (Pg. 105)

25. How we can use the concept of overloading to convert a type according to our

own requirements? Give one example. 05

Ans:

Page 72: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

72

CS304 Object Oriented Programming Midterm

Paper

Yes we can use the concept of overloading to convert any class type according to

our own requirements For example

class Complex{

...

public:

void operator+(

const Complex & rhs);

};

void Complex::operator+(const Complex & rhs){

real = real + rhs.real;

img = img + rhs.img;

};

26. It is said that the default values of parameters given in constructor should be

invalid in normal course of action. Do you agree or not? Justify with solid reasons by

giving C++ coding example.05

Ans:

21. Can we use initialization list to set the value of any base class public data

member, justify your answer in either case as well.02

Ans:

1. Derived class can only initialize members of base class using overloaded

constructors

2.Derived class can not initialize the public data member of base class using

member initialization list

Page 73: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

73

CS304 Object Oriented Programming Midterm

Paper

22. Can constant object access the non constant member functions of the class?02

Ans: const objects can access only const member functions so chances of change of

state of

const objects once they are created are eliminated.

We make getRollNo function constant so that we can access it using constant

objects,

Example

class Student{

int rollNo;

public:

int getRollNo()const{

return rollNo;

}

};

int main(){

const Student aStudent;

int a = aStudent.getRollNo();

} (Pg. 107)

Page 74: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

74

CS304 Object Oriented Programming Midterm

Paper

23. Consider the code given below explain what kind of association exists between

class A and class B. Justify your answer as well.

class A{

private:

int a,b,c;

public:

….

};

class B{

private:

int d,e,f;

A * obj1;

public:

….

}; 03

Ans: It is a kind of Object Association called aggregation. In this relationship two

classes get services of each other but can exist independently.

24.Tell the two ways to indicate the compiler that we are overloading post

increment operator. 03

Page 75: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

75

CS304 Object Oriented Programming Midterm

Paper

25. Suppose we have a Base class Woman and a Derived class Lady Doctor Describe

the case in which we can use derived class “Lady Doctor” Member Initialization List

to initialize the base class “Woman” data members. Give a practical example. 05

Ans: class Woman{

public:

Woman(){ cout <<

“Woman Constructor...”;}

};

class Lady_doctor : public Woman{

public:

Lady_doctor(){ cout <<

“Lady_doctor Constructor...”;}

};

int main(){

Lady_doctor cobj;

return 0;

}

Output:

Woman Constructor...

Lady_doctor Constructor...

26. Consider the following scenario,

In a University a Course consists of 45 Lectures, 6 Assignments, 1 Quiz and 1 GMDB,

similarly each Lecture has corresponding 1 general MDB;

Describe how following associations between different types of objects are

implemented in c++ (you don’t need to write whole classes only write relevant one

or two lines for each type of association)

A Simple association

B Composition 05

21. Identify binary and unary operators from the following:

Page 76: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

76

CS304 Object Oriented Programming Midterm

Paper

Size of, %, --, *, /, ++, =, &&, & 02

22. Write the basic logic/check for the “ set Day (….)” function whose basic purpose

is to check appropriate value given by user. 02

Ans:

Date::Date(int aDay, int aMonth,

int aYear) {

if(aDay==0) {

this->day = defaultDate.day;

}

else{

setDay(aDay);

}

if(day == 29 && month == 2

&& !leapyear(year)){

day = 1;

if(d >= 0 && d <= 31){

day = d;

} (Pg. 118)

23. Consider the following code segment:

class User{

private:

int x, y;

public:

User operator - ();

};

Overload the unary minus operator (-) for “User” class, when it is a member

function of class “User”.03

Ans:

Page 77: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

77

CS304 Object Oriented Programming Midterm

Paper

User User::operator -(){

User temp;

temp.x = -x;

temp.y = -y;

return temp;

} (Pg. 163 )

24. Write C/C++ code to overload an assignment operator for complex class. 03

Ans:

class Complex{

private:

double real, img;

public:

Complex operator =(const Complex & rhs);

};

Complex Complex::operator =( const Complex & rhs){

Complex t;

t.real = rhs.real;

t.img = rhs.img;

return t;

}

25. Write C++ code of overloading ^ operator in complex numbers class.

A

ns: class Complex{

private:

double real, img;

public:

Page 78: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

78

CS304 Object Oriented Programming Midterm

Paper

Complex operator ^(const Complex & rhs);

};

Complex Complex::operator ^( const Complex & rhs){

Complex t;

t.real = real ^ rhs.real;

t.img = img ^ rhs.img;

return t;

}If we have two objects of complex number class as follows,

Complex obj1,obj2;

and we write statement,

Complex obj3 = obj1 ^ obj2;

obj3 real and imaginary parts will be,

obj3.real = (obj1.real) obj2.real and obj3.img = (obj1.img) obj2.img

Hint: You can use C++ built in function power(x,y) that returns the result of x y.

05

Ans: class Complex{

private:

double real, img;

public:

Complex operator ^(const Complex & rhs);

};

Complex Complex::operator ^( const Complex & rhs){

Complex t;

t.real = real ^ rhs.real;

t.img = img ^ rhs.img;

Page 79: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

79

CS304 Object Oriented Programming Midterm

Paper

return t;

}

26. Briefly give benefits of the use of static variable noOfStudents along with static

method by considering following coded example:

class Student {

static int noOfStudents;

int rollNo;

public:

int getrollNo(){ //Non static member function

return roollNo;

}

static int getTotalStudent(){ // Static member function

return noOfStudents;

}

}; 05

Ans: static member initialize automatically with zero and retain its value and it

can be incremented or decremented at the end we can find final value. By using it

we can calculate no of student’s objects present in the memory.

Nonstatic member dont initialize automatically with zero and can not retain its

value and we can find any arithmetic result.

21. Friend functions increase ‘Programming bugs’. What is your opinion?02

Ans:

The functions which are not member functions of the class yet they can access all

private members of the class are called friend functions.

>> this is against encapsulation and data hiding

It can be said that friend functions are against the principle of object oriented

programming because they violate the principle of encapsulation which clearly

says that each object methods and functions should be encapsulated in it. But

there we are making our private member accessible to other outside functions.

22.

Page 80: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

80

CS304 Object Oriented Programming Midterm

Paper

Can constant object access the non constant member functions of the class?02

Ans:

const objects can access only const member functions so chances of change of

state of

const objects once they are created are eliminated.

We make getRollNo function constant so that we can access it using constant

objects,

23. What is meant by anonymous base class object explain with an example.03

Ans:

Every object of derived class has an anonymous object of base class Constructors

• The anonymous object of base class must be initialized using constructor of base

class

• When a derived class object is created the constructor of base class is executed

before the constructor of derived class

Base class constructor initializes the anonymous (base class) object

Derived class constructor initializes the derived class object

Example

class Parent{

public:

Parent(){ cout <<

“Parent Constructor...”;}

};

class Child : public Parent{

public:

Child(){ cout <<

“Child Constructor...”;}

};

int main(){

Child cobj;

Page 81: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

81

CS304 Object Oriented Programming Midterm

Paper

return 0;

}

Output:

Parent Constructor...

Child Constructor...

24. What will be output after executing following lines of code, (suppose there is no

error and code will be executed correctly), justify your answer as well,

class Student{

static int noOfStudents;

int rollNo;

public:

Student::Student(){

noOfStudents++;

}

Student::~Student(){

noOfStudents--;

}

static int getTotalStudent(){

return noOfStudents;

}

};

int Student::noOfStudents = 0;

int main(){

Student obj1,obj2;

{

Page 82: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

82

CS304 Object Oriented Programming Midterm

Paper

Student obj3;

}

cout<<Student::getTotalStudent();

}

3marks

Ans: The output will be 3

25. Write c++ code to overload subscript[] operator for String class. 05

Ans:

class String

{

private:

int m_anList[10];

public:

int& operator[] (const int nIndex);

};

int& String::operator[] (const int nIndex)

{

return m_anList[nIndex];

}

26. Consider the following diagram:

Page 83: CS304 Object Oriented Programming Paper · 1 CS304 Object Oriented Programming Midterm Paper CS304 Object Oriented Programming 1. Which of the following is the way to …

83

CS304 Object Oriented Programming Midterm

Paper

Lesson

ID

Title

Add

Remove

Select

Topic

ID

Title

Add

Remove

Select

Which type of relationship exists between the given classes?

Implement data members and member functions in C++ with appropriate data

types (just write declaration).

Implement the relationship between the two classes in C++. 05

Ans:

This is an example of Aggregation