oops qbank kalai

28
SRI SAI RAM ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK Subject Name / Code : Object Oriented Programming / CS2203 Year/ Sem: II(2010) / III UNIT - I 2 MARKS 1. Defie Inline Functions You can declare functions in a way that allows the compiler to expand them inline rather than calling them through the usual function call mechanism. 2. What is inline function?? The __inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution occurs only at the compiler's discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline. 3. What is "this" pointer? The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call myDate.setMonth( 3 ); can be interpreted this way: setMonth( &myDate, 3 ); The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class. 4. What is name mangling in C++? The process of encoding the parameter types with the function/method name into a unique name is called name mangling. The inverse process is called de-mangling. For example Foo::bar(int, long) const is mangled as `bar__C3Fooil'. For a constructor, the method name is left out. That is Foo::Foo(int, long) const is mangled as `__C3Fooil'. 1

Upload: pkalaimathisrm

Post on 07-Apr-2015

138 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: OOPs QBank Kalai

SRI SAI RAM ENGINEERING COLLEGEDEPARTMENT OF INFORMATION TECHNOLOGY

QUESTION BANK

Subject Name / Code : Object Oriented Programming / CS2203

Year/ Sem: II(2010) / III

UNIT - I 2 MARKS1. Defie Inline Functions

You can declare functions in a way that allows the compiler to expand them inline rather than calling them through the usual function call mechanism.

2. What is inline function??The __inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution occurs only at the compiler's discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline.3. What is "this" pointer?

The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function callmyDate.setMonth( 3 ); can be interpreted this way: setMonth( &myDate, 3 );The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.

4. What is name mangling in C++?The process of encoding the parameter types with the function/method name into a unique name is called name mangling. The inverse process is called de-mangling.For example Foo::bar(int, long) const is mangled as `bar__C3Fooil'. For a constructor, the method name is left out. That is Foo::Foo(int, long) const is mangled as `__C3Fooil'.

5. What is the difference between a pointer and a reference?A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized.

6. What is diff between malloc()/free() and new/delete?malloc allocates memory for object in heap but doesn't invoke object's constructor to initiallize the object.new allocates memory and also invokes constructor to initialize the object.malloc() and free() do not support object semantics Does not construct and destruct objects string * ptr = (string *)(malloc (sizeof(string)))Are not safe Does not calculate the size of the objects that it construct Returns a pointer to void int *p = (int *) (malloc(sizeof(int)));int *p = new int;

1

Page 2: OOPs QBank Kalai

Are not extensible new and delete can be overloaded in a class "delete" first calls the object's termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty []:-Int_t *my_ints = new Int_t[10];...delete []my_ints;

7. what is the diff between "new" and "operator new" ?"operator new" works like malloc.

14. What is difference between template and macro??There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.If macro parameter has a postincremented variable ( like c++ ), the increment is performed two times.Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.for example:Macro:#define min(i, j) (i < j ? i : j)template:template<class T> T min (T i, T j) { return i < j ? i : j;}

8. What are C++ storage classes?Auto, register, static and externauto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that blockregister: a type of auto variable. a suggestion to the compiler to use a CPU register for performancestatic: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins executionextern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.

9. What are storage qualifiers in C++ ?They are Const, volatile, mutableConst keyword indicates that memory once initialized, should not be altered by a program.volatile keyword indicates that the value in the memory location can be altered even though nothing in the programcode modifies the contents. for example if you have a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.   mutable keyword indicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.struct data{

2

Page 3: OOPs QBank Kalai

char name[80];mutable double salary;}const data MyStruct = { "Satish Shetty", 1000 }; //initlized by complierstrcpy ( MyStruct.name, "Shilpa Shetty"); // compiler errorMyStruct.salaray = 2000 ; // complier is happy allowed

10. What is reference ??reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object.prepending variable with "&" symbol makes it as reference.for example:int a;int &b = a;  

11. What is passing by reference?Method of passing arguments to a function which takes parameter of type reference.for example:void swap( int & x, int & y ){ int temp = x; x = y; y = temp; }int a=2, b=3;swap( a, b );Basically, inside the function there won't be any copy of the arguments "x" and "y" instead they refer to original variables a and b. so no extra memory needed to pass arguments and it is more efficient.  

19. When do use "const" reference arguments in function?a) Using const protects you against programming errors that inadvertently alter data.b) Using const allows function to process both const and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments.c) Using a const reference allows the function to generate and use a temporary variable appropriately.

12. When are temporary variables created by C++ compiler?Provided that function parameter is a "const reference", compiler generates temporary variable in following 2 ways.a) The actual argument is the correct type, but it isn't Lvaluedouble Cube(const double & num){  num = num * num * num;  return num;}double temp = 2.0;double value = cube(3.0 + temp); // argument is a expression and not a Lvalue;b) The actual argument is of the wrong type, but of a type that can be converted to the correct typelong temp = 3L;double value = cuberoot ( temp); // long to double conversion 

13. What is a dangling pointer?A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.

3

Page 4: OOPs QBank Kalai

14. What do you mean by Stack unwinding?It is a process during exception handling when the destructor is called for all local objects in the stack between the place where the exception was thrown and where it is caught.

15. How do you access the static member of a class?<ClassName>::<StaticMemberName>

16. What are the access privileges in C++? What is the default access level?The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.

17. What is a nested class? Why can it be useful?A nested class is a class enclosed within the scope of another class. For example:  //  Example 1: Nested class  class OuterClass  {    class NestedClass    {      // ...    };  };Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in Example 1, if NestedClass is public then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are therefore made private; in Example 1, if NestedClass is private, then only OuterClass's members and friends can use NestedClass.When you instantiate as outer class, it won't instantiate inside class.

18. What is a local class? Why can it be useful?local class is a class defined within the scope of a function -- any function, whether a member function or a free function. For example:  //  Example 2: Local class  //  int f()  {    class LocalClass    {      // ...    };    // ...  };Like nested classes, local classes can be a useful tool for managing code dependencies. 

19. Can a copy constructor accept an object of the same class as parameter, instead of reference of the object? 

No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

20. Write any four features of OOPS.Class is an user defined data type, in which we can combine various kinds of data and functions, by which we can create a number of instances.

21. What are the Basic concepts of OOS?

4

Page 5: OOPs QBank Kalai

Classes, objects, abstraction, encapsulation, inheritance, polymorphism, data hiding, message passing, and dynamic binding.

22. What is a scope resolution operator?It is a operator used to make a global identification either to a member variable or a member function. Some other usage such as to define static variable, to avail original global new and delete operator, to redefine the data access specifiers are given by this operator.

23. is a default argument?The function assigns a default value to the parameter which does not have a matching argument in the function call. They are useful in situations where some arguments always have the same value.e.g., float amt (float P, float n, float r = 0.15);

24. What are the properties of a static data member?The properties of a static data member are,

It is initialized to zero when the first object is created and no other initialization is permitted. Only one copy of that member is created and shared by all the objects of that class. It is visible only within the class, but the life time is the entire program.

25. What are the properties of a static member function? A static member function can access only other static members declared in the same class. It can be called using the class name instead of objects as follows,

class_name :: function_name;26. How can objects be used as function arguments?

An object can be used as a function argument in two ways, A copy of the entire object is passed to the function. (Pass by value) Only the address of the object is transferred to the function. (Pass by reference)

27. Explain typecasting.It is possible to convert from one data type to another data type in C++ either implicitly or explicitly. Explicit data type conversion is called typecasting. For example convert from an object to another object or to built in data type etc.

28. What are the operators available in C++? All operators in C are also used in C++. In addition to insertion operator &lt;&lt; and extraction operator &gt;&gt; the other new operators in C++ are, : Scope resolution operator : : * Pointer-to-member declarator -&gt;* Pointer-to-member operator .* Pointer-to-member operator delete Memory release operator endl Line feed operator new Memory allocation operator setw Field width operator

29. Define void pointer using C++.Void pointer is a pointer is used to define a pointer with out the specification of the data type of the pointer. By using this pointer we can assign any kind of address to this pointer by using casting operation.

PART-B (16 MARKS)

5

Page 6: OOPs QBank Kalai

1) Explain with the Basic Concepts of object oriented programming.Classes and Objects (2)Encapsulation and Abstraction (3)Polymorphism (5)Message passing (3)Dynamic programming (3)

2) Explain about call-by-reference and return by reference.i. Definition of call by reference and return by reference (6)ii. Example program for the above two (10)

3) What is friend function? What is the use of using friend functions in c++? Explain with a program.

i. Friend class - Definition, syntax and example (5)ii. Friend member function - Definition, syntax and example (6)iii. Friend non-member function - Definition, syntax and example (5)

4) What are the advantages of using default arguments? Explain with an example program.i. Default arguments – definition (2)ii. Example program for the above (6)

5) Write a program to demonstrate how a static data is accessed by a static member function.i. Static variable, member function – definition(6)

ii. Example program using the abobve (10)6) Write a program to get the student details and print the same using

pointers to objects and pointers to members of a class. Create a class student. And use appropriate functions and data members.

i. Definition for classes and objects (4)ii. Student class and its objects creation (6)

iii. Member functions and its usage in main() (6)

UNIT-II2 - MARKS

1. Define 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( );//constructor 

6

Page 7: OOPs QBank Kalai

……… } 

2. Define default constructor.The constructor with no arguments 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 

3. Define 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. Define default argument constructor.The constructor with default arguments are called default argument constructor Eg: 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,

7

Page 8: OOPs QBank Kalai

it cause ambiguity for a statement such as A a; the ambiguity is whether to call A::A () or A::A (int i=0) 

6. Define 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 dynamic constructor.Allocation of memory to objects at time of their construction is known as dynamic constructor. The memory is allocated with the help of the NEW operator Eg: Class string { char *name; int length; public: string( ) { length=0; name=new char[ length +1]; } void main( ) { string name1(“Louis”),name3(Lagrange); } 

8. Define 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 (~) Eg; ~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. 

9. Define multiple constructors.The class that has different types of constructor is called multiple constructors Eg: #include<iostream. h> #include<conio.h> class integer { int m,n; public: 

8

Page 9: OOPs QBank Kalai

integer( ) //default constructor { m=0;n=0; } 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 } 

10. Write some special characteristics of constructor.• T hey should be declared in the public section • They are invoked automatically when the objects are created • They do not have return types, not even void and therefore, and they cannot return values • They cannot be inherited, though a derived class can call the base class • They can have default arguments • Constructors cannot be virtual f unction 

11. List out the operators that cannot be overloaded.• Class member access operator (. , .*) • Scope resolution operator (::) • Size operator ( sizeof ) • Conditional operator (?:) 

12. 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. 

13. Write at least four rules for Operator overloading.• Only the existing operators can be overloaded. • The overloaded operator must have at least one operand that is of user defined data type. • The basic meaning of the operator should not be changed. • Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. 

14. How will you overload Unary & Binary operator using member functions?When unary operators are overloaded using member functions it takes no explicit arguments and return no explicit values. When binary operators are overloaded using member functions, it takes one explicit argument. Also the left hand side operand must be an object of the relevant class. 

15. How an overloaded operator can be invoked using Friend functions?

9

Page 10: OOPs QBank Kalai

In case of Unary operators, overloaded operator can be invoked as op object_name or object_name op In case of binary operators, it would be invoked as Object . Operator op(y) where op is the overloaded operator and y is the argument. 

16. List out the operators that cannot be overloaded using Friend function.• Assignment operator =  • Function call operator ( ) • Subscripting operator [ ] • Class member access operator 

17. how an overloaded operator can be invoked using member functions?When unary operators are overloaded using friend function, it takes one reference argument (object of the relevant class) When binary operators are overloaded using friend function, it takes two explicit arguments. 

18. What is meant by casting operator and write the general form of overloaded casting operator?A casting operator is a function that satisfies the following conditions • It must be a class member. • It must not specify a return type. • It must not have any arguments. The general form of overloaded casting operator is operator type name ( ) { ……….. // function statements } It is also known as conversion function. 

PART-B (16 MARKS)

1. Explain copy constructor and destructor with suitable C++ coding.

a. Definition for constructor and Destructor with syntax (6)b. Example program for the above (Matrix Multiplication) (10)

2. Explain about Unary Operator and Binary Operator Overloading with program.

a. Unary Operator Overload – definition, syntax, and example (4)b. Binary Operator Overload – definition, syntax, and example (4)c. Unary Operator Overload using friend function, and example (4)d. Binary Operator Overload using friend function, and example (4)

3. List out the rules for overloading operators with example. Define a supplier class. Assume that the items supplied by any given supplier are different and varying in number. Use dynamic memory allocation in the constructor function to achieve the solution.

a. Rules for Operator Overloading (3)b. Class Definition for supplier class (3)c. Program using assignment operator overloading (10)

4. Consider an example of book shop which sells book and video tapes. These two classes are inherited from the base class called media. The media class has common data members such as title and publication. The book class has data members for storing number of pages in a book and the tape class has the playing time in a tape. Each class will have member function such as read() and show() in the base class, these members have to be defined as virtual

10

Page 11: OOPs QBank Kalai

functions, write a program which models the class hierarchy for book shop and process objects of these classes using pointers to the base class.

i. Class media definition with the following members (3)

1. string title,publication;2. virtual void read()=0;3. virtual void show()=0;

ii. Derived class book from media with its overridden functions (3)

iii. Derived class tape from media with its overridden functions(3)

iv. Main function with Base pointer with RTTI access(6)

UNIT-III2 - MARKS

1. What is a template?Template is a generic function or class by which we can create a common function or class which can apply to all kinds of data.

2. What is R T T I ?Run Time Type Information/Identifier, used in polymorphism, to identify the kinds of data or object during run time.

3. What is namespace? Namespace is a qualifier to qualify data or function or library for the purpose of usage in all programs. Deferent kinds of libraries are possible to utilize by using namespace.

4. What are the different mechanisms of traditional error handling? What is the problem with them?

Compilation and error checking. The runtime error cannot identify. Exception provide this facility.

5. What is the role of terminate() function in exception handling? Why doesn’t the exception handling mechanism not call abort() directly?

If there is no proper exception handling mechanism provided, an abnormal program termination occur if any runtime error occurs. By this time the run time system calls the terminate() or unexpected() functions. To avoid this we can provide our own set_terminate() or set_unexpected() functions, by which we can provide the facility to close all resources that are allocated to a program.

6. What is an exception? What are the steps involved in exception handling?An exception is a condition that is caused by a run time error in aprogram. The steps involved in exception handling are,Hit the exceptionThrow the exceptionCatch the exceptionHandle the exception

6. Give the syntax of exception handling code.The syntax of exception handling code is given by,

try{statements

11

Page 12: OOPs QBank Kalai

}catch ( Exception-type e){statements}

7. What is the difference between throwing exceptions inside the function and outside the function?Inside function means we should provide one more throw statement we need to complete them.

8. When do we need multiple catch blocks for a single try block? Give an example.try{

----------

}catch(int e){}catch(float){}

9. What are exception specifications? In which case are they needed?We can restrict the catch data by specify the kinds of data to be caught. It is useful in some expected kinds of exceptions.

10. What is rethrow()? What is its use?If a throw statement written in side a function that will be executed by other function which is going to call the function. So the calling function again provides one more throw statement. That is possible by rethrow mechanism.

PART-B (16 MARKS)

1) What is the need of Templates? Explain.

i. Purpose for templates (2)ii. Types of templates (2)iii. Syntax and example for function template (5)iv. Syntax and example for class template (7)

2) Implement selection sort as a generic function.

i. Template function for selection sort (5)ii. Main function to call the template function (5)iii. Array of inputs and proper outputs (6)

3) Write a program for generic queue class with two member functions, insert and delete. Use the array to implement the queue.

i. Template class for Queue with insert and delete functions(5)

12

Page 13: OOPs QBank Kalai

ii. Define template member functions of insert and delete functions (5)

iii. Main function to call the insert and delete member template function (6)

4) Define a stack. The class should throw an exception when the stack underflow and overflow takes place.

i. Class definition for Stack with Exception for underflow and overflow (5)

ii. Define member functions of push and pop functions (5)

iii. Main function to call push and pop member function (6)

5) What are the use of terminate() and Unexpected functions? Explain with a program.

i. Definition of uncaught exception (2)ii. Types of uncaught exception and its solution

Terminate() – set_terminate() (4) Unexpected() – set_unexpected() (4)

iii. Example program for the above (6)

Unit - 4

PART-A (2MARKS)

1. Define basic to class type conversion with an example.Conversion from basic data type to class type can be done in destination class. Using constructors does it. Constructor takes a single argument whose type is to be converted.Eg: Converting int type to class typeclass time{int hrs,mins;public:………….Time ( int t) //constructor{hours= t/60 ; //t in minutesmins =t % 60;}};Constructor will be called automatically while creating objects so that this conversion is done automatically.

13

Page 14: OOPs QBank Kalai

2. Define one class to another class conversion with an example.Conversion from one class type to another is the combination of class to basic and basic to class type conversion. Here constructor is used in destination class and casting operator function is used in source class. Eg: objX = objY objX is the object of class X and objY is an object of class Y. The class Y type data is converted into class X type data and the converted value is assigned to the obj X. Here class Y is the source class and class X is the destination class. 

3. 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.

4. What is meant by Abstract base class?A class that serves only as a base class from which derived classes are derived. Noobjects of an abstract base class are created. A base class that contains pure virtualfunction is an abstract base class.

5. Write short notes on virtual base class.A base class that is qualified as virtual in the inheritance definition. In case ofmultiple inheritance, if the base class is not virtual the derived class will inherit more thanone copy of members of the base class. For a virtual base class only one copy of memberswill be inherited regardless of number of inheritance paths between base class andderived class.Eg: Processing of students’ results. Assume that class sports derive the rollnumber from class student. Class test is derived from class Student. Class result isderived from class Test and sports.As a virtual base class As a virtual base class

6. What is polymorphism? Explain with an example.Polymorphism is the feature that allows one interface to be used for a generalclass of actions.(ie) “one interface multiple methods”.This means that it is possible to design a generic interface to a group of relatedactivites.This helps reduce complexity by allowing the same interface to be used tospecify a general class of action.

7. What are Friend functions? 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 friend data_type function_name( ); Friend function is preceded by the keyword ‘friend’. 

8. Write some properties of friend functions.• 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. • Usually it has object as arguments. • It can be declared either in the public or private part of a class. • It cannot access member names directly. It has to use an object name and dot membership operator with each member name. eg: ( A . x ) 

9. What are virtual functions? A function qualified by the ‘virtual’ keyword is called virtual function. When a virtual function is called through a pointer, class of the object pointed to determine which function definition will be used. 

14

Page 15: OOPs QBank Kalai

10. Write some of the basic rules for virtual functions.• Virtual f unctions must be member of some class. • They cannot be static members and they are accessed by using object pointers • Virtual f unction in a base class must be defined. • Prototypes of base class version of a virtual function and all the derived class versions must be identical. • If a virtual function is defined in the base class, it need not be redefined in the derived class. 

11. What are pure virtual functions? Write the syntax. A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases, the compiler requires each derived class to either define the function or redeclare it as a pure virtual function. A class containing pure virtual functions cannot be used to declare any object of its own. It is also known as “donothing” function. The “do-nothing” function is defined as follows: virtual void display ( ) =0; 

12. 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 • Function overloading • Operator overloading 

13. 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. )

70) What is operator overloading?C++ has the ability to provide the operators with a special meaning for a data type. 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. 

14. List out the operators that cannot be overloaded.• Class member access operator (. , .*) • Scope resolution operator (::) • Size operator ( sizeof ) • Conditional operator (?:) 

15. 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 } 

15

Page 16: OOPs QBank Kalai

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. 

16. Write at least four rules for Operator overloading. • Only the existing operators can be overloaded. • The overloaded operator must have at least one operand that is of user defined data type. • The basic meaning of the operator should not be changed. • Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. 

17. will you overload Unary & Binary operator using member functions? When unary operators are overloaded using member functions it takes no explicit

arguments and return no explicit values. When binary operators are overloaded using member functions, it takes one explicit argument. Also the left hand side operand must be an object of the relevant class. 

18. How will you overload Unary and Binary operator using Friend functions? When unary operators are overloaded using friend function, it takes one reference argument (object of the relevant class) When binary operators are overloaded using friend function, it takes two explicit arguments. 

19. How an overloaded operator can be invoked using member functions? In case of Unary operators, overloaded operator can be invoked as op object_name or object_name op  In case of binary operators, it would be invoked as Object . Operator op(y)  where op is the overloaded operator and y is the argument. 

20. How an overloaded operator can be invoked using Friend functions? In case of unary operators, overloaded operator can be invoked as Operator op (x); In case of binary operators, overloaded operator can be invoked as Operator op (x , y) 

PART-B (16 MARKS)

1) What are the different forms of inheritance supported in c++? Discuss on the visibility of base class members in privately and publicly inherited classes.

i. Types of inheritance (4)ii. Visibility modes and its access modes (6)iii. How to access base class members by derived class – example

(6)

2) Discuss about Run Time Type Identifications with example.

i. RTTI – Definition (3)ii. Typeid and type info (3)iii. Casting with usage of typeid (3)iv. Example program to explain the above (7)

3) Define a student class. Inherit that into MCAStudent class and NonMCAStudent. MCAStudents inherits into GLSSTudents and NonGLSStudents. A function ShowPracticalHours can only be applied to MCAStudents. We have a base class Student pointer to a GLSStudent

16

Page 17: OOPs QBank Kalai

object. Use dynamic_cast to check that NonMCAStudents do not ShowPracticalHours.

i. Student class with members GLSSTudents, NonGLSStudents.(3)

ii. MCAStudent class – function ShowPracticalHours(3)

iii. NonMCAStudent class (3)iv. Main program with Student pointer as base pointer

(3)v. Use Student pointer with dynamic_cast for derived class

(4)

UNIT-V

PART-A(2MARKS )

1. What are streams? Why they are useful?Stream is a hierarchy of classes used to handed IO operations in C++.

2. Briefly describe the class hierarchy provided by c++ for stream handling.

3. What is the difference between a text file and a binary file?Text Files: It contains alphanumeric and graphic data input using text-editor program. All information stored in the disk will be displayed in the same format as it would be displayed on the screen. For example the ‘A’ is written as A onto the file, and the number 336 will be written as the string “336”.

Binary Files: It allows writing numeric equivalent data on the disk file using less number of bytes than text file. For example the character ‘A’ is written as an ASCII representation with two bytes of spaces. So these files require less storage. But special command are needed to read them as discussed later.

4. What is a file pointer?

17

Page 18: OOPs QBank Kalai

It is used to point the cursor pointer of a specified file by file operation.5. Write the syntax and use of getline () and write () functions.

It allows extraction of characters(text data) from a stream until a delimiter (by default ' \n ') is encountered, possibly with a limit to the number of characters to be extracted.The get() and getline() functions behave similarly, except that getline() extracts the final delimiter and get() does not.Syntax: cin.getline(string variable, Max. size of string variable);

6. What is meant by object serialization? Serializing is a process of storing and retrieving objects on and from auxiliary devices. The objects which can be stored and retrieved back later as it is are known as persistent objects. In C++ it is complex to perform object serializing, because any object doesn’t contain its member

function. All are available as static global one. But we can write the objects into disk as writing object in binary file.

7. What is name conflict problem? How can it be solved using namespaces?When a programmer uses multiple libraries like standard library and third-party libraries where both libraries define some function with the same name, the compiler doesn’t understand that the function belongs to which library. This problem is called name conflict problem. The namespace technique solves this problem. For example, a programmer uses two kinds of abs() function in his program. One is to find out absolute value of the given number, provided by Math namespace and another is to find number of absentees in a class, provided by third party library let us Academic namespace. So the compiler gets conflict on those two abs() functions. The above problem could be avoided by implementing the above two function as below:

Math::abs(); and Academic::abs():8. How can we define our own namespace?

namespace NamespaceName{

//Data members//Function memebers

}9. How can we define our functions inside the namespace and use them outside?

By using name-of-the-namespace :: function-name{}10. What is unnamed namespace? What is the use of unnamed namespace?

It is possible to have the namespace without the name. Such namespaces are called unnamed namespaces. The variables of unnamed namespaces are used like normal variables without qualifying with the namespace name because we do not have name for our namespace. So we can use the variables of unnamed namespaces similar to static global variable. But it cannot access with in other programs than the program in which it is defined.

11. What is STL? How it is different from C++ standard library?STL is a collection of generic software components (containers), algorithms, linked by objects called iterators. The three components of STL work with conjunction with one another to provide support to a variety of programming solutions. STL components are now part of the ANSI C++ Libraries which are defined in the namespace std. We must therefore use the directive using namespace.

12. List the three types of containers.Sequence containers: Vector, Deque and ListAssociative containers: Set, MultiSet, Map, MultiMapDerived Containers: Stack, Queue, Priority_Queue

13. What is an iterator? What are the its characteristics?

18

Page 19: OOPs QBank Kalai

It is an pointer like object that points to and element in a container. We can use iterators to move the contents through the containers. Iterators handled just like pointers. We can increment and decrement the iterators to manipulate the data stored in the containers.

14. How are the STL algorithms implemented?It is a procedure that is used to process the data contained in the containers. The STL includes many different kinds of algorithm to provide support to a variety of containers to perform some common tasks such as initialize the object, searching, sorting, and merging and so on. Algorithms are implemented by template-functions.

15. Define File modes.A sequence of flag bits are attached with open() to specify the purpose of opening file such as input mode or output mode. They are in, out, app, ate(to anywhere of the file )

PART-B(16 MARKS)

1. Discuss about Streams and stream classesistream (2)ostream (2)streambuf (2)iostream (2)Ifstream (2)Fstream (2)Ofstream (2)Fstreambase (2)filebuf (2)

2. Write notes on Formatted and Unformatted Console I/O Operations.scanf (2)printf (2)getchar() (2)getch() (2)putchar() (2)putch() (2)read() (2)write() (2)

3. Explain about File Pointers and Manipulations with example.Seekg and seekp (2)Arguments of seekg and seekp: (2)tellg and tellp (2)updating task (2)example (8)

4. Explain in detail about STL.Definition (2)Containers (3)

19

Page 20: OOPs QBank Kalai

Algorithm (3)Iterators (3)Simple Example (5)

5. Data file “DATA” contains the name and marks of a set of students. Write a C++ program that reads the contents of this file into an object, sorts the data in descending order of marks and writes the result to an output file “OUTPUT”.

Class Definition (2)Read student data (4)Display student data (4)Sort student data (6)

6. Consider an example of book shop which sells book and video tapes. These two classes are inherited from the base class called media. The media class has common data members such as title and publication. The book class has data members for storing number of pages in a book and the tape class has the playing time in a tape. Each class will have member function such as read() and show() in the base class, these members have to be defined as virtual functions, write a program which models the class hierarchy for book shop and process objects of these classes using pointers to the base class.

Media, Book and video Class Definition (4)Member data definition (4)Member Function Definition (4)Main function with base Pointer (4)

20