33
CS2203 Object Oriented Programming CS2203 OBJECT ORIENTED PROGRAMMING TWO MARK QUESTION AND ANSWER UNIT I 1. Give some characteristics of procedure-oriented language. The characteristics of procedure-oriented language are: i. Emphasis is on doing things (algorithms). ii. Larger programs are divided into smaller programs known as functions. iii. Most of the functions share global data. iv. Data move openly around the system from function to function. v. It employs top-down approach in program design. 2. What are the basic concepts of OOPS? The basic concepts of OOPS are: i) Objects ii) Classes iii) Data abstraction and Encapsulation iv) Inheritance v) Polymorphism vi) Dynamic binding vii) Message passing 3. What is an object? An object is basic run-time entity in an object- oriented system. They may represent a person, a place, 1

Cs2203 Oops Two Marks

Embed Size (px)

Citation preview

Page 1: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

CS2203 OBJECT ORIENTED PROGRAMMING

TWO MARK QUESTION AND ANSWER

UNIT I

1. Give some characteristics of procedure-oriented language.

The characteristics of procedure-oriented language are:

i. Emphasis is on doing things (algorithms).

ii. Larger programs are divided into smaller programs known as

functions.

iii. Most of the functions share global data.

iv. Data move openly around the system from function to function.

v. It employs top-down approach in program design.

2. What are the basic concepts of OOPS?

The basic concepts of OOPS are:

i) Objects

ii) Classes

iii) Data abstraction and Encapsulation

iv) Inheritance

v) Polymorphism

vi) Dynamic binding

vii) Message passing

3. What is an object?An object is basic run-time entity in an object-oriented system. They

may represent a person, a place, a bank account, a table of data or any

item that the program has to handle. Each object has the data and code to

manipulate the data and theses objects interact with each other.

4. What is a class?

A class is a Collection of objects of similar type. Once a class has

been defined, we can create any number of objects belonging to the class.

Class is a user-defined data type and behaves like built-in types of the

programming language.

1

Page 2: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

5. What is an encapsulation?

Wrapping up of data and function within the structure is called as

encapsulation. The insulation of data from direct access by the program is

called as data hiding or information binding. The data is not accessible to

the outside world and only those functions, which are wrapped in the

class, can access it.

6. What is meant by dynamic binding or late binding?

Dynamic binding means that the code associated with a given

procedure call is not known until the time of the call at the run-time.

7. Write the process of programming in an object-oriented language?

The process of programming in an object-oriented language are:

i) Create classes that define objects and their behavior.

ii) Creating objects from class definition.

iii) Establishing communication among objects.

8. List any four advantages of OOPS.

The advantages of OOPS are:

i) The principle of data hiding helps the programmer to build

secure programs that cannot be invaded by code in other parts of

the program.

ii) It is possible to have multiple instances of an object to co-exist

without any interference.

iii) Object oriented programming can be easily upgraded from small

to large systems.

iv) Software complexity can be easily managed.

9. What are the features required for object-based programming

Language?

The features required for object-based programming are:

i) Data encapsulation.

ii) Data hiding and access mechanisms.

iii) Automatic initialization and clear up of objects.

iv) Operator overloading.

2

Page 3: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

10. Give any four applications of the OOPS. The applications of OOPS are:

i) Real-time systems.

ii) Simulation and modeling.

iii) Object-oriented databases.

iv) AI and expert systems.

11. What are the operators available in C++? The operators available in C++ are:

i) :: - Scope resolution operator

ii) :: * - Pointer-to-member declarator

iii) ->* - Pointer-to-member operator

iv) .* - Pointer-to-member operator

v) delete - Memory release operator

vi) endl - Line feed operator

vii) new - Memory allocation operator

viii) setw - Field width operator

12. What is a scope resolution operator?

Scope resolution operator is used to uncover the hidden variables.

It also allows access to global version of variables.

Example: #include<iostream. h> int m=10; // global variable m void main ( ) { int m=20; // local variable m cout<<”m=”<<m<<”\n”; cout<<”: : m=”<<: : m<<”\n”; }

Output: 20 10 (: : m access global m)

Scope resolution operator is used to define the function outside the class.

3

Page 4: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

Syntax: return type <class name> : : <function name>

Example: void x : : getdata()

13.What is a default argument?

Default argument assigns a default value to the parameter, which

does not have matching argument in the function call. Default values are

specified when the function is declared.

Example: float amount(float principle,int period, float rate=0. 15){

}

Function call is:

value=amount(5000,7);

Here it takes principle=5000& period=7

And default value for rate=0.15

Value=amount(5000,7,0.34)

Passes an explicit value 0f 0.34 to rate

We must add default value from right to left

14. What is constant argument?

Keyword is const. The qualifier const tells the compiler that the

function should not modify the argument. The compiler will generate an

error when this condition is violated. This type of declaration is significant

only when we pass arguments by reference or pointers

Example: int strlen (const char *p);

15. How the class is specified? Generally class specification has two parts

1. Class declaration It describes the type and scope of its member

2. Class function definition It describes how the class functions are implemented

4

Page 5: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

The general form is: class class_name {

private: variable declarations; function declaration;

public: variable declaration;

function declaration;};

16. How do you create an object?

Once the class has been declared, we can create variables of that

type by using the class name

Example: classname x; //memory for x is created

17. How do you access a class member?

We can access the member function by using the following syntax,

Object-name. Function-name (actual arguments);

eg:x.getdata(100,75.5);

18. How is the member functions defined?

Member functions can be defined in two ways:1. Outside the class definition

Member function can be defined by using scope resolution operator (::).General format is

return type class_ name:: function-name (argument declaration) { }

2. Inside the class definition

This method of defining member function is to replace the function

declaration by the actual function definition inside the class. It is

treated as inline function

Example: class item {

int a, b;

5

Page 6: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

void getdata (int x, int y) {

a=x; b=y;

}};

19. What is a static data member?

Static variable are normally used to maintain values common to the

entire class.

Features: i) It is initialized to zero when the first object is created. No other

initialization is permitted ii) only one copy of that member is created for the entire class and is shared by all the objects iii) It is only visible within the class, but its life time is the entire class type and scope of each static member variable must be defined outside the class iv) It is stored separately rather than objects

Example: static int count; /*count is initialized to zero when an object is

created. */int classname::count; //definition of static data member

20. What is static member function?

A member function that is declared as static has the following properties:

i) A static function can have access to only other static member declared

in the same class

ii) A static member function can be called using the classname as follows

class name :: function_name;

21. How the objects are used as function argument? This can be done in two ways, i) A copy of the entire object is passed to the argument ii) Only address of the objects is transferred to the f unction

22. What is Friend function? Write the syntax.

A function that has access to the private member of the class but is not

itself a member of the class is called friend functions.

The general form is:

6

Page 7: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

friend datatype function name (object dec);

Friend function is preceded by the keyword ‘friend’.

23. Write some properties of friend function. The properties of friend function are:

1. Friend function is not in the scope of the class to which it has been

declared as friend. Hence it cannot be called using the object of that

class.

2. Usually it has object as arguments.

3. It can be declared either in the public or private part of a class.

4. It cannot access member names directly. It has to use an object name

and dot membership operator with each member name. eg: ( A . x )

24. What is function overloading? Give an example. Function overloading means we can use the same function name to

create functions that perform a variety of different tasks.

Eg: An overloaded add( ) function handles different data types as shown

below.

// Declarations

i. int add( int a, int b); //add function with 2 arguments of same type

ii. int add( int a, int b, int c); //add function with 3 arguments of same type

iii. double add( int p, double q); //add function with 2 arguments of

different type

//Function calls

add (3 , 4); //uses prototype ( i. )

add (3, 4, 5); //uses prototype ( ii. ) add (3 , 10.0); //uses prototype ( iii. )

25. What is constant function?When we define the function as constant we should not modify the

objects. If the functions are modified to change the values, the error can

be caught.

Syntax: < Return type> <Function name> ( ArgList ) const { funtion body; }

7

Page 8: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

26. What is constant object?Const object is object that is not modifiable. Only functions that are

defined as Const can be accessed by const object. Even public variables

of the object are not modifiable.

Syntax:

const class_name obj_name;

27. Define the nested class.We have two classes. Outside class is a nesting class and contains

the entire body of the inside class. The inside class, which is defined

inside the outside class is called nested class.

28. What is the syntax, if we defined the nested class as public?

class outerClass{ public: class innerClass {

------------------

};----------------

};

29. What is the syntax, if we defined the nested class as private?

class outerClass{ private: class innerClass; public:

------------------

};

class outerClass :: innerClass{ Definition of innerClass; -----------------

-----------------}

8

Page 9: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

30. What is polymorphism? What are its types? Polymorphism is the ability to take more than one form. An

operation may exhibit different behaviors in different. The behavior

depends upon the type of data used.

Polymorphism is of two types. They are 1. Function overloading 2. Operator overloading

9

Page 10: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

UNIT – II

1. Define the term constructor.A constructor is a special member function whose task is to

initialize the objects of its class. It is special because its name is same as

class name. The constructor is invoked whenever an object of its

associated class is created. It is called constructor because it constructs

the values of data members of the class

Eg:

integer Class

{

……

public:

integer( );//constructo r

………

}

2. What is meant by default constructor?

The constructor with no argument is called default constructor.Eg: Class integer { int m,n; Public: Integer( ); ……. }; integer::integer( )//default constructor { m=0;n=0; } the statement integer a; invokes the default constructor

10

Page 11: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

3. What is meant by parameterized constructor?

Constructor with arguments is called parameterized constructor.Eg; class integer {

int m,n; public: integer(int x,int y) {

m=x;n=y; }

};

To invoke parameterized constructor we must pass the initial

values as arguments to the constructor function when an object is

declared. This is done in two ways:

1.By calling the constructor explicitly eg: integer int1=integer(10,10); 2.By calling the constructor implicitly eg: Integer int1(10,10);

4. What is a default argument constructor?

The constructor with default arguments are called default argument

constructor.

Example:

Complex(float real,float imag=0);

The default value of the argument imag is 0

The statement complex a(6.0) assign real=6.0 and imag=0

the statement complex a(2.3,9.0) assign real=2.3 and imag=9.0

5. What is the ambiguity between default constructor and default argument constructor?

The default argument constructor can be called with either one

argument or no arguments. When called with no arguments, it becomes a

default constructor. When both these forms are used in a class, it cause

ambiguity for a statement such as A a;

The ambiguity is whether to call A::A() or A::A(int i=0)

11

Page 12: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

6. Define the term copy constructor.A copy constructor is used to declare and initialize an object from another

object. It takes a reference to an object of the same class as an argument

Eg: integer i2(i1);

would define the object i2 at the same time initialize it to the values of i1.

Another form of this statement is

Eg: integer i2=i1;

The process of initializing through a copy constructor is known as copy

initialization.

7. Define the term destructor.

It is used to destroy the objects that have been created by

constructor. Destructor name is same as class name preceded by tilde

symbol(~).

Example:

~integer()

{

}

A destructor never takes any arguments nor it does it return any

value. The compiler upon exit from the program will invoke it. new

Whenever operator is used to allocate memory in the constructor, we

should use delete to free that memory.

8. What is meant by constructor overloading?

The class that has different types of constructor is called multiple

constructors.

Example:#include<iostream. h> #include<conio.h> class integer {

int m,n; public: integer( ) //default constructor {

m=0;n=0; }

12

Page 13: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

integer(int a,int b) //parameterized constructor {

m=a; n=b; } integer(&i) //copy constructor {

m=i. m; n=i.n;

}}; void main() {

integer i1; //invokes default constructor integer i2(45,67);//invokes parameterized constructor integer i3(i2); //invokes copy constructor

}

9. Write some special characteristics of constructor.

Special characteristics of constructor are:

1. They should be declared in the public section

2. They are invoked automatically when the objects are created

3. They do not have return types, not even void and therefore, and they

cannot return values

4.They cannot be inherited, though a derived class can call the base class

5. They can have default arguments

6. Constructors cannot be virtual f unction

10. How the objects are initialized dynamically? To call parameterized constructor we should the pass values to the

object ie,for the constructor integer(int a,int b) it is invoked by integer

a(10,18). This value can be get during run time. i.e., f or above constructor

int p,q;

cin>>p>>q;

integer a(p,q);

11. What is meant by operator overloading?

13

Page 14: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

This mechanism of giving such special meanings to an operator is

known as Operator overloading. It provides a flexible option for the

creation of new definitions for C++ operators.

12. List out the operators that cannot be overloaded. The operators that cannot be overloaded are:1. Class member access operator (., .*)

2. Scope resolution operator (::)

3. Size operator (sizeof)

4. Conditional operator (? :)

13. List out the operators that cannot be overload as friend functions.

The operators that cannot be overload as friend function are:a. Assignment operator =

b. Function call operator ( )

c. Array subscript operator [ ]

d. Access to class member using pointer to object operator ->

14. What is the purpose of using operator function? Write its syntax. To define an additional task to an operator, we must specify what it means

in relation to the class to which the operator is applied. This is done by

Operator function , which describes the task. Operator functions are either

member functions or friend functions. The general form is

return type classname :: operator op(arglist )

{

function body

}

where return type is the type of value returned by specified operation.

Op- operator being overloaded. The op is preceded by a keyword

operator. operator op is the function name.

15. Write at least four rules for Operator overloading. Rules for Operator overloading are:

1.Only the existing operators can be overloaded.

2. The overloaded operator must have at least one operand that is of user

14

Page 15: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

defined data type.

3. The basic meaning of the operator should not be changed.

4. Overloaded operators follow the syntax rules of the original operators.

5. They cannot be overridden.

16.What are the different types of conversion?

The different types of conversions are:1. Basic type to Class type.

2. Class type to Basic type.

3. Class type to Class type.

4. Basic type to Basic type

17.Write the syntax to convert from class to basic type.

operator typename( ){

…………Function statements;…………

}

18.What are the conditions to satisfy the type casting function?

The conditions to satisfy the type casting function are:1. it must be a class member.

2. it must not specify a return type.

3. it must not have any arguments.

15

Page 16: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

UNIT III

TEMPLATES AND EXCEPTION HANDLINGS

1.What is the use of typename?

The keyword typename indicates that the expression following the

keyword is the name of a type. If we write

typename identifier;

then that identifier is treated as a generic type in that function.

2. What is template instantiation?

The process of generating a data type specific instance of a template is known as instantiation.

3. What is template argument deduction?

The process of determining the types and values of template arguments

from the type of function argument is called template argument deduction.

4. What are the different types of inclusion compilation model?

The two models differ in how the definitions from the source files are made

available to the compiler. The models are

i. Inclusion compilation model

ii. Separate compilation model

5. What is use of export keyword?

export is a keyword, required for implementing the separate compilation model.

6. What are the various traditional error handling methods?

16

Page 17: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

The various traditional error handling methods are:

i. Returning error number

ii. Global flag manipulation

iii. Abnormal termination

17

Page 18: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

7. What is the importance of exceptional handling?

The importance of exceptional handling is:

i. Divide the error handling

ii. To provide unconditional termination and programmer preferred

termination

iii. For separating error reporting and error handling

iv. To solve the object destroy problem

8. What are the three keywords used for exception handling mechanism?

The three keywords used for exception handling mechanism are:

i. try for indicating program area where the exception can be thrown

ii. throw for throwing an exception

iii. catch for taking an action for specific exception

9. What are the challenges in the exception handling mechanism?

The challenges in the exception handling mechanism are:

i. Finding proper catch block

ii. Finding proper catch block for polymorphic object

iii. Backtracking till the beginning of the try block

10. What is meant by exception specification?

It is possible to specify what kind of exceptions can be thrown by functions,

using a specific syntax. We can append the functions definition header with throw

keyword and possible type of expressions to be thrown in the parenthesis. It is

known as exception specification.

11. What is significance of terminate function?

When the exception handling mechanism does not get an proper catch

block for a thrown exception, the terminate() function is called to terminate the

program execution. It is a built in function, when it is invoked, internally it calls the

abort() function call to cancel the program execution tin the event of run time

error related to exceptions.

18

Page 19: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

12. What is the use of unexpected function?

The unexpected function is called when a function throws an exception not

listed in its exception specification. Internally, it calls the terminate function to

terminate the program execution. The function set_unexpected () is used to call

our own unexpected function in place of the built-in unexpected function.

19

Page 20: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

UNIT IVINHERITANCE

1. What is an access control?The member functions of the class, member functions of the derived class,

friends and object can access different parts of the class. Access control

describes who can access what and in which form.

2. What is meant by access declaration?Using access declaration, we can proved public access to some of the

base class members even after deriving them as private.

3. What is an abstract class?An abstract class itself contains some other objects as members. Such

objects are known as composite objects or container objects.

4. What is meant by composite object?Sometime object itself contains some other objects as members. Such

objects are known as composite objects or container objects.

5. What is meant by sub object?

Whenever a base class is inherited, the derived class contains the base

class sub object. In case of multiple inheritance, the derived class contains

multiple sub objects. in case of inheritance with more than one level, we have n

sub objects in the class derived at n+1 level.

6. What is a polymorphic class?

A class that declares or inherits a virtual function is called a polymorphic

class.

7. What is a pure virtual function?

A pure virtual function is a function declared in a base class that has no

definition relative to the base class. The pure virtual function is defined as

follows: virtual void display()=0;

8. What is meant by static invocation of virtual function?

It is possible to call virtual function using as object of the class or using a

scope resolution operator. In that case, virtual function is invoked statically. This

is known as static invocation of the virtual function. The syntax for static

invocation of virtual function is:

pointer-object classname::virtual-function();

20

Page 21: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

9. What is meant by inheritance? Inheritance is the process by which objects of one class acquire the properties of another class. It supports the concept of hierarchical classification. It provides the idea of reusability. We can add additional features to an existing class without modifying it by deriving a new class from it.

10. What is meant by visibility mode? Mention the visibility modes available. Visibility mode specifies whether the features of the base class are privately derived or publically derived. There are 3 visibility modes. They are

Private Public Protected

11. What is meant by single inheritance? If a single class is derived from a single base class is called single inheritance. Eg: Base class

Derived class

Here class A is the base class from which the class D is derived. Class D is the public derivation of class B hence it inherits all the public members of B. But D cannot access private members of B.

12. What is multiple inheritance? If a class is derived from more than one base class, it is called multiple inheritance.

Eg: Base classes

Derived class Here class C is derived from two base classes A & B.

13. What is hierarchical inheritance? If a number of classes are derived from a single base class then it is called hierarchical inheritance.

Eg : Hierarchical classification of students in University

21

A

B

A

C

B

Page 22: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

14. What is multilevel inheritance? If a class is derived from a class, which in turn is derived from another class, is called multilevel inheritance. This process can be extended to any number of levels.Eg: Base class Grand father

Intermediate Base class Father

Derived class Child

15. What is hybrid inheritance?It is the combination of one or more types of inheritance.

Multilevel inheritance

Multiple

inheritance

22

Student

Arts Engineering

Medical

CSE ECECivil

A

B

C

Student

Test

Result

Sports

Page 23: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

The class result will have both the multilevel and multiple inheritances.

16. What is meant by Abstract base class? A class that serves only as a base class from which derived classes are derived. No objects of an abstract base class are created. A base class that contains pure virtual function is an abstract base class.

17. Write short notes on virtual base class. A base class that is qualified as virtual in the inheritance definition. In case of multiple inheritance, if the base class is not virtual the derived class will inherit more than one copy of members of the base class. For a virtual base class only one copy of members will be inherited regardless of number of inheritance paths between base class and derived class.

Eg: Processing of students’ results. Assume that class sports derive the roll number from class student. Class test is derived from class Student. Class result is derived from class Test and sports.

As a virtual base class As a virtual base class

23

Student

Test

Result

Sports

Page 24: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

UNIT VRTTI

1. What is Real Time Type Information (RTTI)?

RTTI is a mechanism by which we can find the type of an object at

runtime.

2. What are the operators provided by RTTI?

The operators provided by RTTI are:

(i) typeid

(ii) dynamic_cast

3. Why do need RTTI?

RTTI can be used to find out the type of polymorphic object at run time.

Also it is used to find out the type of the element in the body of a template

function or class.

4. List the different types of casting operators.

(i) reinterpret_cast

(ii) dynamic_cast

(iii) static_cast

(iv) const_cast

5. What are the issues related with RTTI?

Some of the RTTI related issues are follows:

(i) Backward compatibility with C and Casting

(ii) Efficiency

(iii) Using virtual functions in place of RTTI

6. What is cross casting?

In multiple inheritance when a derived class object is pointed by one of its

base class pointer object, casting from one base class pointer into another base

class pointer is known as cross casting.

7. What is down casting?

24

Page 25: Cs2203 Oops Two Marks

CS2203 Object Oriented Programming

Casting from base class pointer to the derived class pointer is known as

down casting.

8. What is polymorphic object?

An object which can be manipulated by using virtual function is called

polymorphic object

25