Oops 2marks

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 61

R.M.D.

Engineering College
CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

UNIT-I
Object oriented programming concepts objects-classes- methods and messagesabstraction and encapsulation-inheritanceabstract classespolymorphism. Introduction to C++- objects-classes-constructors and destructors.
Possible Part-B questions: 1. i) Define OOP.Explain object-oriented programming Features & Concepts in detail. (12) i) Write the Characteristics & Drawbacks of procedure oriented language POP. (4) 2. i) Define polymorphism. Explain the types of polymorphism with example. (8) ii) Write the Characteristics & Drawbacks of procedure oriented language POP. (4) iii)Write the difference between procedure oriented language (POP)& object oriented Language(OOP) (4) iii) State the merits and demerits of object oriented methodology. (6) 3. i) How object are used in C++ program? Write a C++ program for the employee class. ii) Write a C++ program to explain the concept of array of objects. Use student class. 4. i) Define inline function. List the situation where the inline function may not work? (4) ii) Write a C++ program to explain the concept of inline function. (4) iii) What is function overloading? & Explain with a suitable C++ program.(8) 5. i) Define & explain the following with a suitable C++ program. a. Local class(4) b. Nested class(4) ii) Define friend function. Explain with a suitable C++ program. (8) 6. i) What is static data member? Write a C++ program to explain the concept of static data member.(8) ii) ) What is static member function? a C++ program to explain the concept of static member function .(8) 7. i) Define Parameterized constructor .Explain the Parameterized constructor concepts with suitable C++ program. ii) Define Copy constructor Explain the Copy constructor concepts with suitable C++ program. 8. i) Define Multiple constructor Explain the Multiple constructor concepts with suitable C++ program. ii) How the Constructor with dynamic allocation is occur in C++? Explain with suitable example program. 9. i)What is Destructor? Write the difference between destructor & constructor. Explain the order of destructor to be called with suitable C++ program. ii) How the constructors can be overloaded? Illustrate with an example. 10. What is the role of new & delete in C++ program? Write a C++ program for String concatenation using dynamic memory allocation concept.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

UNIT-II
Operator overloading - friend functions- type conversions- templates - Inheritance virtual functions- runtime polymorphism. Possible Part-B questions: 1. i) Write a C++ to overlaod the Preincrement(++)& Post Decrement (--) unary operator using member function. ii Write a C++ to overlaod the Preincrement(++)& Post Decrement (--) unary operator using friend function. 2. i) ) Write a C++ to overlaod the for arithmetic operators (+,-) for two complex numeber using member function. ii) Write a C++ to overlaod the for arithmetic operators (*,/) for two complex numeber using friend function. 3. Write a program to overload function. the istream operator (>>)&ostream operator (<< )using friend

4. What is type conversion? Explain the various types of type conversion with suitable C++ program. 5. i) Define function template Implement a generic bubble sort template function for the following inputs. A. 9,2,7,4,5,6,8,3,1 B. a,s,d,t,f,e,g,c,b C. 7.7,1.1,4.4,3.3,8.8,2.2,6.6,9.9,5.5 ii) Write a function template to swap a two interger,float,double datas 6. i) How the function templates can be overloaded. Write a C++ program.

ii) Define class template. Write a C++ program.


7. Define Inheritance. Explain the various types of Inheritance in detail with suitable example. 8. How to resolve ambiguity problem in inheritance? 9. i) Define virtual base class. Explain the concept of virtual base class with a suitable C++ program. ii) Define virtual function. List the rules to define a virtual function. And Write a C++ program for virtual function 10. Explain how the constructor are defined in derived class. Illustrate with an example. 11. i) Define pure virtual function. List the rules to define a pure virtual function. And Write a C++ program for pure virtual function. 12. What is run time polymorphism? Compare it with compile time polymorphism. Write a C ++ program to explain the run time polymorphism in detail

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

UNIT-III
Exception handling - Streams and formatted I/O file handling namespaces String Objects - standard template library
Possible Part-B questions: 1. What is exception handling? Explain the components of exception handling with neat sketch

of an exception handling model.


2. i) When do we use multiple catch? Explain with an example.(8)

ii) Rethrowing an exception& Exception specification with an example program.


3. i)Explain the IOS stream class hierarchy in detail.(6)

ii) Explain the different types of streams & various formatted I/O on C++. (10)
4. i) What are manipulators? Explain the various manipulators with example. (8)

ii) How can you design an own manipulators? Write a program. (8)
5. Explain the file handling techniques in C++ programming. 6. Using tellp (), tellg ( ) &seekg (), seekp () write a C++ program to access the data from a file

by randomly.
7. Define namespace. Write a program for the following:

a. How the namespace can be used in functions? Write a C++ program. b. How the namespace can be used in Class? Write a C++ program.

8. Explain the various functions supported by the String class in C++. Explain with suitable C+

+ programs.

9. Discuss in detail about the components of Standard template library.(STL).

10. Explain the applications of STL with an example.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

UNIT-IV
Introduction to JAVA , bytecode, virtual machines objects classes Javadoc packages Arrays - Strings
Possible Part-B questions: 1. i)What is JVM? Explain the JVM process in detail with neat sketch. ii) Explain the java features in detail (or) Explain the java BUZZ words in details. 2. i) Write a java program to implement the addition of matrix using single dimensional array. ii) Write a java program to implement the multiplication of matrix using two dimensional arrays 3. write a java program for the following : i. ii. iii. Find the maximum of given array. (Or) Find the minimum of given array. Sort the given array elements in ascending order (Or) Sort the given array elements in descending order. To Copy one array elements to another array.

4. i) Write short notes on the various string methods in Java ii) Write a java program to perform all string operations using String class. 5. i) Define package. What are the types of packages? Write the steps to create a package in java. ii) Write a program to define a user defined package in java.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

UNIT-V
Inheritance interfaces and inner classes - exception handling threads - Streams and I/O
Possible Part-B questions: 1. i) Explain the various types of inheritance in java ii) Write a java program for single inheritance. (Example1: Room->Bedroom, Example2: Box->Box3D, Example3: Student -> Calculate ) 2. i) Why multiple inheritance is not possible in java? Justify it. How it is possible to create multiple inheritance in java. (8) ii) Write a java program to explain the concept of interface in detail. 3. Explain about exception handling in java with an example& List the different types of java exception. 4. i) Write a java to program to divide 2 numbers & raise exception when dividing by zero. (By Predefined Exception) ii) Write a java to program to add 2 integers & raise exception when any other character except no (0-9) is given as input. (By User-defined Exception) 5. i) Differentiate thread from process. Explain the thread life cycle in detail with neat sketch ii) Write a java program to explain the concept of multithreading by Using the various thread methods. 6. i) Short notes on various I/O streams in java. ii) Short notes on inner class. Write a simple java program for inner class.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

UNIT- I PART- A
1. Define OOP. OOP is a method of implementation in which programs are organized as co-operative collections of objects, each of which represents an instance of some class and whose classes are all members of hierarchy of classes united through the property called inheritance. 2. What are the characteristics of procedure oriented language? The following are the characteristics of procedure-oriented language Emphasis is on doing things (algorithms). large programs are divided into smaller program known as functions most of the functions share the global data Data move openly around the system from function to function. Functions transform data from one form to another. It employs TOP-DOWN approach in a program design.

3. List the drawbacks of procedure-oriented language? The following are the drawbacks of procedure-oriented language, More concentration on function not for data. In large program its difficult to find what data is used by which program It does not meet the real world problems well. Less data security. In case the need arises to modify the format of the data, all the functions access The data also need to be modified. This makes over head. 4. What the characteristics are of object oriented language?

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Emphasis is on data rather than procedure. Programs are divided into what are known as objects. Data structures are designed such a way that they characterize the objects. Functions that are operating on the data of an object are tied together in the data structure. Data is hidden and cannot be accessed by others. Kavaraipettai -601 206

5. List the object oriented programming concepts. The following are the object oriented programming concepts CLASS: The collection of similar object is known as class. The entire set of data and code of an object can be made a user-defined data type with the help of a class. Once a class has been defined, we can create any number of objects belonging to the classes. Classes are user-defined data types. Example: Fruit mango; OBJECT: Objects are basic run-time entities 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. Fruit mango; ENCAPSULATION: The wrapping up of data and functions into a single unit is known as encapsulation. Example: Medical capsule. DATA ABSTRACTION: It refers to the act of representing the essential features without including the background details. The insulation of data from direct access by the program is called as data hiding or information // here mango is the object of the fruit class 7. Define the following with an example. a) ENCAPSULATION b) DATA ABSTRACTION. // here fruit is the class Objects Classes Data abstraction Encapsulation Inheritance Polymorphism Dynamic binding Message passing

6. Define the following with an example. a) CLASS b) OBJECT.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
binding. The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it. Example: TV remote. 8. Define the following with an example. a) INHERTITANCE b) POLYMORPHISM. INHERTITANCE: The process of creating a new class from an already existing class is known as inheritance. The new class contains all attributes of the old class in addition to some of its own attributes. It can also override some of the features of the old class. Class A Base Class (Or) Super Class (Or) Parent Class Derived Class (Or) Sub Class (Or) Child Class Class B The following are the types of inheritance. i. Single inheritance ii. Multiple inheritance iii. Multi level inheritance iv. Multipath inheritance v. Hierarchical inheritance vi. Hybrid inheritance b) POLYMORPHISM: Polymorphism means one name, multiple forms It is the property of the same object to behave differently in different contexts given the same message. Example: Kavaraipettai -601 206

cout<< welcome to oop; int a=2; int c=a<<2; Here with respect to cout << acts as an insertion operator with respect to a<<2 << acts as a left shift operator.
The types of polymorphism:

Polymorphism

Compile time polymorphism

Run time polymorphism

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank Function Overloading 9. Operator Overloading Virtual function
Kavaraipettai -601 206

10. Define dynamic binding. Binding refers to the linking of a procedure to the code to be executed in response to the call. 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 11. List the applications of OOP. The following are the applications: Real-time systems. Simulation and modeling. Object-oriented databases. AI and expert systems. 12. What are the merits of OOP? The following are the merits of OOP: Code reusability reduced maintenance. Improved reliability & flexibility. Real world modeling. 13. What are the demerits of OOP? The following are the demerits of OOP: The runtime cost of dynamic binding is the major disadvantage of OOP. reduced maintenance. Improved reliability & flexibility.

14. What do you mean by object-based programming? Object based programming is the style of programming that primarily supports encapsulation and object identity. Features supported: Data encapsulation Automatic initialization and clear up of objects Data hiding and access methods. 15. How does object oriented programming differs from object based programming? Object based programming is the style of programming that primarily supports encapsulation and object identity .whereas

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank Object oriented programming = object based + inheritance + dynamic binding (Eg) Java, Eiffel, Simula, Small talk.
16. Sketch the structure of C++ program. Include files Class declaration Member function definition Main function program Kavaraipettai -601 206

17. Define token. What are the types of token? The smallest individual units in a program are known as tokens. C++ has the following tokens, Keyword Identifiers Constants Strings Operator

18. State the use of void in C++.

The two normal uses of void are To specify the return type of the function when it is not returning a value To indicate an empty argument list to a function 19. Define an enumeration data type. An enumerated data type is a user defined data type that provides a way for attaching names to numbers thereby increasing comprehensibility of the code. The enum keyword is used which automatically enumerates a list of words by assigning them values 0, 1, 2 E.g.) enum weeks {mon, tues, wed,thus,fri,sat,sun};
20. Define constant pointer and pointer to a constant.

The constant pointer concept does not allow us to modify the value initialized to the pointer. e.g.) char * const ptr = GOOD; The pointer to a constant concept doesnt allow us to modify the address of the pointer.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank E.g.) int const * ptr = &n;
21. List the rules to define the identifiers. Only alphabetic characters, digits and underscore are permitted. The name cannot start with a digit. The upper case and lower case letters are distinct. A declared keyword cannot be used as a variable name What is reference variable? Give an example. Kavaraipettai -601 206

Reference is a mechanism to make an alias of a variable. When a referencing variable is made to refer to a referred variable, both variables becomes alias of each other. Changing the value of either of them, changes the values of the both. Syntax : datatype &reference_variable_name = variable_name; Example: int y; int &x = y; Now the value of x and y will be the same and also if any one of the value is changed the change will be reflected in the other variable. y =10; Now x is also 10. x = 20; Now y is also 20. y = x + 10; Now x and y is 30 22. What are Data types available in C++?

C++ DATA TYPES

User Defined Data Type


structure union

Built-in Data Type


Integral Floating type type void

Derived Data Type


Array function

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
class int char double 23. Define operator. List the C++ operators. enumeration Types: 1. 2. 3. 4. 5. 6. 7. 8. 9. Assignment operator Increment/ decrement operator Conditional or Ternary operator Logical operator Relational operator Casting operator Arithmetic operator Bit wise operator Special operator float pointer reference Kavaraipettai -601 206

Operators are the symbols which represent various computation performed on various data item.

24. What is the use of Scope Resolution operator? The scope resolution operator: : is used to access global variable in the inner block. When global and local variable are identical name the scope resolution operator will help to access the global variable in the inner block. This cannot be done in C programming. This operator will read only the global variable. Example: int x=10; int main() { int x=100; { int x=1000; cout<<"\n"<<x; // This statement will display 1000 cout<<"\n"<<::x; // This statement will display 10 } cout<<"\n"<<x; //This statement will display 100 cout<<"\n"<<::x; //This statement will display 10 return 0; } 25. List the C++ special operators. :: ::* Scope resolution operator Pointer-to-member declaration operator

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
->* .* delete new Pointer-to-member operator Pointer-to-member operator Memory release operator Memory allocation operator Kavaraipettai -601 206

26. What are the memory management operators in C++? new : It is used to allocate the memory. Also known as Memory allocation operator. Syntax: pointer-variable = new data type; delete : It is used to de allocate the memory. Also known as Memory release operator. Syntax: delete pointer-variable; Program: void main() { int *a; a= new int(21); cout<<the value of *a = <<*a; delete a; } 27. Write the difference between structure & class ; Structure Strucutre Class

It is a collection of disimilar data types. The default access modifer for members (data and methods ) is public. Example:
struct stud { Public: int a; int b; };

It is a collection of similar type of object. The default access modifer for members (data and methods ) is private. Example:
class stud { Private: };

int a; int b;

28. Write the difference and Advantages of new versus malloc (). Syntax of new is similar than malloc (). No size, no void pointer & no casting are required for new operator.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
new is an operator. , malloc () is function. Generally operator execute faster than function. new has various forms. malloc ( ) does not have those facilities. New is class-aware. It is possible to change the meaning of new to suit the need of a userdefined class. This is known as operator overloading. 29. Define manipulator. Give an example. Manipulators are operators that are used to format the display. Setw, endl are known as manipulators. The endl manipulator when used in an output statement causes a linefeed to be inserted and its effect is similar to that of the newline character\n. Example: Cout<<setw(5)<<sum<<endl; 30. What is function prototyping? The function prototype describes function interface to the compiler by giving details such as number, type of arguments and type of return values. Function prototype is a declaration statement in the calling program. Syntax: return -type function_name(argument list); Example : float volume(int x,float y); 31. Define inline function. Give an example. An inline function is a function that is expanded in line when it is invoked. That is compiler replaces the function call with the corresponding function code. Syntax: inline function-header { function body } } for functions returning values, if loop, a switch, or a goto exists for functions not returning values ,if a return statement exists if function contain static variables if inline functions are recursive. 32. List the situation when the inline function may not work? Example: inline float mean( float x,float y) { return(x*y); Kavaraipettai -601 206

33. What is a default argument? Default arguments assign 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 :

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
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 34. What are constant arguments? Const keyword is. 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 getdetails(const char *p); 35. Define function overloading. 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 volume( int a, int b); ii. int volume ( int a, int b, int c); iii. double volume ( int p, double q); //Function calls : volume (3 , 4); volume (3, 4, 5); volume (3 , 10.0); //uses prototype ( i. ) //uses prototype ( ii. ) //uses prototype ( iii. ) // function with 2 arguments of same type // function with 3 arguments of same type // function with 2 arguments of different type Kavaraipettai -601 206

36. Define friend function. Give an example. A function which is not member function of a class but is given special permission to access the private variable of the class is known as friend function. Friend function is preceded by the keyword friend. Syntax: friend return- type function_name(list of arguments ); Example: friend float mean( sample s); 37. Define friend class. Give an example. A class that is made a friend of another class is known as friend class. Example: Class x { Private: int fun(); }; Class y { friend int x :: fun ( );

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
}; 38. Define nested class. Give an example. The class which is defined inside the another class is said to be nested class. Example: Class outerclass { Class innnerclass { .. }; }; 39. Define local class. Give an example. A class which is defined inside the function is known as local class. It can use global variables and static variables declared inside the function but cannot use automatic local variables. Example: void test(int a) { class student { }; student s1(a); } } 40. What is pointer? Define pointer, void pointer & Constant pointer. Give an example. Pointer: It refers to Address of the another variable. Void pointer: A void pointer points to values which does not have a type. This means that the values pointed to by the void pointer has an undetermined length and dereferencing properties. Example: Void* ptr2; Constant pointer: A constant pointer is one cannot point to anything other than what it is pointing to at the time of definition. Example: int* const ptr3; Kavaraipettai -601 206

41. Define pointers to member. It is possible to take the address of a member of a class and assign it to a pointer. The address of a member can be obtained by applying the operator &to a fully qualified class member name. A class member pointer can be declared using the operator::*with the class name. Eg:

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
class A { int m; public: void show( ); }; pointer to member m is defined as int A::*ip=&A::m; A::*->pointer to member of A class &A::m->address of the m member of A class 42. What is static data member? A static data member is defined with a static keyword preceding it. It must have a definition outside the body of the class. It is shared between all the objects of the class. Example: Static int x; 43. What is static member function? A member function that is declared as static keyword preceding it is known as static member function The properties of a static member variable are It is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created. It is visible only within the class, but its lifetime is the entire program. Type and scope of each static member variable must be defined outside the class. It is stored separately rather than objects The volatile keyword is a type qualifier used to declare that an object can be modified in the program by the external parameters which are not under the control of the program. A member function can be declared as volatile if it is invoked by a volatile object. 45. Define constructor. Give an example. A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values f data members of the class. 46. How will you declare and define a constructor? Constructor declaration : Class integer { int m,n; Kavaraipettai -601 206

44. What is volatile function?

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
public : integer ( void ); } constructor definition : integer :: integer (void ) { m=0; n=0; } 47. What is the need of object initialization? When the object is declared, it may need to initialize the individual data members to some specific default values. For Example; integers are initialized to zero, pointer variables to null. Kavaraipettai -601 206

Dynamic allocation may be required when an object is defined. Object may need to be set to special values. When inheritance is in effect, if a base class has a constructor, a derived class must also have one to initialize itself. 48. List the rules for defining constructor. (Or) what are the characteristics of a constructor? A constructor must have the same name as the class itself. A Constructor has no return type, even void. Constructor cannot be a virtual function. Constructor should be defined in Public only. Constructor can be overloaded. An Address of a constructor cannot be obtained; hence we cannot have a function pointer, pointing to a constructor. We can define constructors as private, but that would disable us from defining objects using those constructors. Therefore, unless normal definition of the object is not restricted, the constructors should be defined public. 49. How the constructor function is invoked? (Or) what are the 2 methods are used to invoke the constructor function. There are 2 methods are used to invoke the constructor function. By calling the constructor function Explicitly. By calling the constructor function Implicitly.

Explicit Call: test t1=test(0,100);

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
The explicit call is invoked; the object referenced by the t1 is initialized by temporary object created by the compiler. The temporary object will be created first and then its data members are initialized with their respective values and then the entire object is copied to the actual object t1. Implicit Call: test t1 (0,100); The implicit call is initializing the test object with integers values. t1 is an object of a test class. And o, 100 are the values of the object with integer type 50. What are the types of constructor? The following are the types of constructors

Kavaraipettai -601 206

Default constructor Parameterized constructor. Copy constructor Multiple constructors. Explicit Constructor

51. Define default constructor. Give an example. A default constructor is a constructor with no argument or with all default argument. It is also called a 0-argument constructor. There are two types of constructor. 1. Complier Defined Default Constructor 2. User Defined Default Constructor EXAMPLE: class Sample { public: Sample () { } }; 52. What is parameterized constructor? Give an example. constructor with arguments is called parameterized constructor Eg; Class integer { int m,n; public: integer(int x,int y) { m=x; n=y; }

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
}; 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); 53. 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 54. 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) 55. Define multiple constructor. Give an example. The class that has different types of constructor is called multiple constructors Eg: class integer { int m,n; public: integer( ) //default constructor { m=0;n=0; } Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
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 } 56. How can we allocate the memory dynamically using constructor. . The allocation of memory to objects at the time of their construction is known as Dynamic Allocation. The allocated memory can be released when the object is no longer needed at the run time is known as Dynamic Destruction. The key words new and delete are used to allocate and release the memory. Eg: Class employee { char *name; int length; public: employee ( ) { length=0; name=new char[length +1]; } }; void main( ) { string name1(Louis),name3(Lagrange); } Kavaraipettai -601 206

57.

Define dynamic constructor.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
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); } 58. What is copy constructor? Give an example. A copy constructor is a function that is called whenever a copy of an existing instance needed to be created. Classname ( classname &reference object) { } Example: class Test { private: ------------------public: Test(Test &t); //copy constructor, reference object as parameter -------}; Void main() { Test T1; //object T1 created, default constructor Test T2=T1; //copy constructor Test T2(T1); //Alternative } 59. Define private copy constructor. Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
A constructor which is defined in the private section is called private copy constructor. .If we define copy constructor in a private section, the object of the class cannot call it. Also the program will not compile because the copy constructor body are not executed. 60. Define destructor. Give an example. The member function which is used to destroy the objects that have been created by constructor is known as destructor. Destructors are opposite of constructor. .The name of the destructor is the name of the class proceeded by a tilde symbol (~). Like a constructor. a destructor cannot have a return type. However, unlike a constructor, the argument list of a destructor must be empty. This means that we cannot overload a destructor. A class can have one and only destructor. Example; ~ Student ( ) { // } Kavaraipettai -601 206

61. Write the difference between constructor and destructor. CONSTRUCTOR Its used to construct the object It can be called automatically when an object comes into existence The constructor function is called every time an object is created. It is the function invoked first before calling any function It can be o overloaded It takes any number of argument A class can have multiple constructor DESTRUCTOR Its used to destroy the object It is called when an object goes out of scope. The destructor function is called every time the program exits a block. It is the function called at last It cannot be overloaded It takes no arguments. Only one destructor function for a class is possible.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
The constructor name and class name should be same Eg: for the student class, the constructor is , student() { } It behaves like a new operator. { } It behaves like a delete operator. The name of the destructor is the name of the class proceeded by a tilde symbol (~). Eg: for the student class, the destructor is , ~student() Kavaraipettai -601 206

UNIT -II PART- A


1. Define 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. 2. What are the types of operators? Operators can be broadly classified into: 1. Unary Operators 2. Binary Operators Unary Operators: As the name implies takes operate on only one operand. Some unary operators are namely (++) Increment operator (--) Decrement Operator(! ) Not operator (-)unary minus (+)unary minus. Binary Operators: The arithmetic operators, comparison operators, and arithmetic assignment

operators come under this category.


3. List the restrictions under operator overloading. 1. Operators do not have lost their original meanings instead they have an additional meaning when overloaded. One cannot change the original meaning of an operator. 2. New operator cannot be devised. Available operators with given restrictions can only be overloaded. 3. Operators will have additional meaning, but will not have additional precedence. The precedence remains the same to that of the original meaning. 4. Operator cannot change number of arguments, which were available in the original form. Hence + in binary form can only have two arguments. 5. Operators can only be overloaded for user-defined types. All overloaded operators must have at least one argument as user defined type. 6. Except ( ), no other operator can have a default arguments. 7. Some of the operators cannot be simply overloaded.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
4. What are the operators cannot be overloading using member function? 1. The dot operator for member access (.) 2. The dereference member to class operator (.*) 3. Scope resolution operator( :: ) 4. Size of operator ( sizeof ) 5. Conditional ternary operator ( ?:) 6. Casting operators static_const< >,dynamic_cast < >, const_cast < > 7. # and ## tokens for macro preprocessors. 5. What are the operators cannot be overloading using friend function? 1. Assignment operator ( =) 2. Function call operator ( ( ) ) 3. Array subscript operator ( [ ] ) 4. Access to class member using pointer to object operator (-> ) 6. Write the rules to overload binary, unary operators through member& friend functions. Operator function must be either member function or friend function. The difference between member function and friend function is: Member function has no argument for unary operator & one argument for binary operator. Friend function will have only one argument for unary operator & two arguments for binary operator. Note: argument may be passed either by value or reference. Kavaraipettai -601 206

7. Write the syntax to define the operator function. return_type classname :: operator operator symbol(argument) { .. statements; } return_type is the data type returned by the function class name - is the name of the class operator is the keyword operator symbol is the symbol of the operator which is being overloaded or defined for new functionality :: - is the scope resolution operator which is used to use the function definition outside the class.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
8. What is explicit constructor? C++ constructors that have just one parameter automatically perform implicit type conversion. For example, if you pass an int when the constructor expects a string pointer parameter, the compiler will add the code it must have to convert the int to a string pointer. However, you might not always want this automatic behavior. You can add explicit to the constructor declaration to prevent implicit conversions. This forces the code to either use a parameter of the correct type, or cast the parameter to the correct type. That is, if the cast is not visibly expressed in code, an error will result. Thus, an explicit constructor creates a nonconverting constructor Example: class STUDENT { explicit STUDENT (float x); // Explicit constructor { } }; 9. Define data conversion. What are the types of data conversion? The process of converting one data type into another is known as data conversion. The following are the types of data conversion. 1. Conversion between basic data types. 2. Conversion between objects and basic data types. A) Basic data types to user-defined data types. B) User defined data types to basic types. 3. Conversion between objects of different classes. Kavaraipettai -601 206

10. Explain 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 type class time { int hrs,mins; public: . Time ( int t) //constructor { hours= t/60 ; //t in minutes mins =t % 60;

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
} }; Constructor will be called automatically while creating objects so that this conversion is done automatically. 11. Explain class to basic type conversion with an example. Using Type Casting operator, conversion from class to basic type conversion can be done. It is done in the source class itself. Eg: vector : : operator double( ) { double sum=0; for(int I=0;I<size;I++) sum=sum+v[ i ] *u[ i ] ; return sqrt ( sum ) ; } This function converts a vector to the corresponding scalar magnitude. 11. Explain 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. Kavaraipettai -601 206

12. What is the need of template functions in C++? What are their advantages? Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. This is effectively a Turing-complete language. Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Library provides many useful functions within a framework of connected templates. 13. What is generic programming? Generic programming is an approach where generic types are used as parameters in algorithms so that they work for a variety of suitable data types and data structures. 14. Define template.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
C++ templates enable you to define a family of functions or classes that can operate on different types of information. It supports generic programming which allows developing a reusable software component such as functions and classes etc., supporting different data types in a single framework. There are two types of templates. They are 1. Function templates 2. Class templates 15. Compare polymorphism and template. Kavaraipettai -601 206

Polymorphism Polymorphism is achieved by function overloading and operator overloading. Polymorphism means use the same function or operator for different purpose without changing their basic meaning. Depending upon the data type passed to them they perform different operations, where function or operator should be defined for each data type. Polymorphism is used to reduce software complexity.

Template Template is achieved by function template and class template. Template means use the same function or class for different purpose without changing their basic meaning where function or class should be defined only once.

Templates are used to achieve reusability which eliminates redundant code.

16. Define function template. Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. Syntax: template<typename template datatype> Returntype function name(list of arguments) { .//.} 17. Define class template. A class is defined also to be declared to operate on different data type such classes are called class templates. Syntax: template<typename template datatype> class classname { } Syntax for object creation using class template: Classname<datatype> objectname1;

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
18. List the friend of a class template. A friend of a class template can be one of the following: i. Class template. ii. Function template. iii. Non- template function. iv. Non- template class. v. A specialization of class template. vi. A specialization of function template. 19. What are the uses of template functions deduction? The compiler while encountering call to a function template tries to find out the actual type for the generic type specified in the definition of function template this process is known as template arguments deduction. Its only possible in case of function templates but not in class template. 20. What are drawbacks of macros? i) Macros are not visible to the compiler .They are substituted by their body by a compiler. If there is some error in the body of the macro, the error is represented in a non user friendly form. This is more annoying for a user the macro from a library is not aware of variable used in the body. ii) The type related is information is lost in the macros. Moreover it is not possible to have any type related validations in the macros. iii) Macros are evaluated twice. Once when they are copied and next time when they are executed. So templates are preferred. 21. Write the syntax for single & multiple argument function template. Function template with single parameters: template< class T1> returntype function name(arguments of types T1 { . //body of the function . } Function template with multiple parameters: Template< class T1, class T2..> R.T function name(arguments of types T1,T2..) Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
{ . //body of the function . } 22. What is the difference between generic and non-generic (type and non-type) arguments to template function? Generic arguments Non- Generic arguments Run time values may be allowed as Generic arguments Generic arguments may be : Types Integral values Static/global address Template names Values known at the compile time is known as Non- Generic argument Non- Generic arguments may be : Integral constants Pointer or reference to a global function Pointer or reference to a non-local object Kavaraipettai -601 206

23. What is the difference between template function and template class? Template function Template class Data type is automatically deduced from the function call. No need to pass the type of data separately Only the actual function is generated Need to pass the type of data for the creation of object. Actual class and its object are generated simultaneously.

24. Define inheritance. List the types of inheritance. The process of creating a new class from an already existing class is known as inheritance. The new class contains all attributes of the old class in addition to some of its own attributes. It can also override some of the features of the old class. Class A Base Class (Or) Super Class (Or) Parent Class Derived Class (Or) Sub Class (Or) Child Class Class B The following are the types of inheritance. vii. Single inheritance

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
viii. ix. x. xi. xii. Multiple inheritance Multi level inheritance Multipath inheritance Hierarchical inheritance Hybrid inheritance Kavaraipettai -601 206

25. What are the advantages of inheritance? The following are the advantages of inheritance: i. We can extend the functionality of an existing class. ii. It offers reusability of code. iii. No need to create objects from scratch. iv. C++ provides us mapping of real world hierarchy in our program. v. Reusable software components can be developed. vi. It reduces software development time and cost considerably. 26. What is Is-a relationship and has a relationship? Is-a relationship: If class B is derived from the class A, then we can say that class B is a kind of A. Since class B has all the properties of A in addition to the features of its own. Example: CAR sports car is a type of car

has-a relationship:

SPORTS CAR If a class contains another class then the relationship is a part of relationship. The Part of relationship is also called as has a relationship. Example: CAR Engine is a part of car

Engine

27. What are the access speicifiers? Write syntax to derive a class. The following are the type of access speicifiers in C++: a) Public: b) Private: c) Protected: Syntax for Defining a Derived Class:

Class derived_class_name : visibility_mode base_class_name { ----- --- ----- -------- --- ----- ---}

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Example: Class B: public A { ---------}; 28. Define single inheritance. Give an example. Definition: The process of deriving a single class from a single base class is known as single inheritance . Base Class A Kavaraipettai -601 206

Derived Class

The class A is base class to the class B (i.e.,) class B is derived from class A. Syntax: class Base_class_name { //Data members //Member functions }; class Derived_class_name:visibility_mode base_class_name { //Data members //Member functions Example: Class Person { -------}; Class Student: public Person { ---------};

};

};

29. Define multiple inheritances. Give an example. Definition: The process of deriving a class from several base classes is known as multiple inheritance. Base Class 1 A B Base Class 2

Derived Class

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Class C is derived from two base classes (i.e.,) class A and class B. Its designed to have all (or) partial properties of classes A and B. in addition, the class C is permitted to have its own properties. Syntax: class Base_class1_name { //Data members //Member functions } class Base_class2_name { //Data members //Member functions } class Derived_class_name : visibility_mode base_class1_name, visibility_mode base_class2_name { //Body of derived class }; Example: Class InternalExam { -------}; Class ExternalExam { -------}; Class Result: public InternalExam, public ExternalExam { -------}; Kavaraipettai -601 206

};

36. Define multilevel inheritances. Give an example. The derivation of a class from another derived class is known as multilevel inheritance. Base Class A Derived Class (Intermediate Base Class)

C Syntax: class Base_class_name { //Body of base class } class Derived_class1_name:visibility_mode base_class_name { //Body of derived class1 }; class Derived_class2_name:visibility_mode derived_class1_name { //Body of derived class2 };

Derived Class Example: Class Person { -------}; Class Student: public Person { -------}; Class Exam: public Student

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
{ -------- }; \ 37.Define hierarchical inheritance Definition: The derivation of several classes from single base class is known as hierarchical inheritance. Kavaraipettai -601 206

B
Syntax:

C
Example: Class Car { -------}; Class SportsCar: public Car { -------}; Class PassenegerCar: public Car { --------};

class Base_class_name { //Body of base class } class Derived_class1_name:visibility_mode base_class_name { //Body of derived class1 }; class Derived_class2_name:visibility_mode base_class_name { //Body of derived class2 }; 30. Define hybrid inheritance. Give an example. Definition:

38. Define hybrid inheritance. The derivation of a class involving more than one form of inheritance is known as hybrid inheritance.

B B D

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

Syntax: class Base_class1_name { ---------------} class Derived_class1_name:visibility_mode base_class_name1 { ------------------}; class Base_class2_name { --------------------} class Derived_class2_name:visibility_mode derived_class1_name, visibility_mode Base_class2_name { -----------------------};

Example: Class Person { -------}; Class Student: public Person { -------}; Class Exam: public Student { -------}; Class Sports { -------}; Class Result: public Exam, public Sports { -------};

31. Define multi path inheritance. Give an example. Derivation of class from other derived classes, which are derived from the same base class is called multipath inheritance. A class Base_class_name { ---------------C B } Syntax: class Derived_class1_name:visibility_mode base_class_name D { ------------------}; class Derived_class2_name:visibility_mode base_class_name { -----------------------}; class Derived_class3_name:visibility_mode Derived_class1_name, visibility_mode Derived_class2_name, { -----------------------};

Example: Class A { -------}; Class B: public A { -------}; Class C: public A

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
{ -------}; Class D: public B, public C { -------}; Kavaraipettai -601 206

32. What is the public derivation? List the constraints of the public derivation. Syntax: class Base { //Body of the base class } class Derived : public Base { //Body of Derived class }; This derivation is known as public derivation. The member of base class are now treated as, 1. The public member of base class is treated as public member of the derived class. 2. The private member of base class is not inherited. 3. The protected member of base class is protected member of the derived class. 33. What is the private derivation? List the constraints of the private derivation. The member of base class are now treated as, 1. The public member of base class is treated as private member of the derived class. 2. The private member of base class is not inherited. 3. The protected member of base class is private member in the derived class. Syntax: class Base { //Body of the base class } class Derived : private { //Body of Derived class };

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
This derivation is known as private derivation. 34. What is the protected derivation List the constraints of the protected derivation. Syntax: Kavaraipettai -601 206

class Base { //Body of the base class } class Derived : protected Base { //Body of Derived class }; This derivation is known as protected derivation. The base class members are treated as follows: 1. The public member of base class is treated as protected member of the derived class. 2. The private member of base class is not inherited. 3. The protected member of base class is treated as protected member in the derived class. 35. What is virtual base class? Give an example. A class which is defined as virtual at the time of inheriting it. The compiler takes a note of it and when its inherited further using multiple inheritances it ensures that only one copy of the sub object of the virtual inherited class exists in the derived class. 36. Define abstract class. Give an example. The classes without any objects are known as abstract class or abstract base class. It contains at least one pure virtual function. Example: class sample { Public: void virtual show( )=0; }; 37. Compare compile time polymorphism with run time polymorphism? Compile time polymorphism: The compiler while compiling the program resolves the function call or the operator call. This is known as compile time or static polymorphism. It is achieved by operator & function overloading. Run time polymorphism: Resolving function call at run time is known as run time or dynamic binding. Its achieved by virtual functions. 38. What is this pointer?

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
This pointer is used to allow you to access the current object only. Its also used to return the address of the current object. This pointer is accessible only within the non static members of a class or struct, union type.st6atic member function do not have a this pointer. 39. What is virtual function? Give an example. A function defined with virtual keyword in the base class is known as virtual function, compiler will decide the exact class pointed to by the class pointer and call the respective function of that class if the function is defined as virtual. Virtual function supports dynamic binding Virtual function does not to be redefined in the derived class. Syntax: virtual return_type function_name( argument list) { -------------} 40. List the rules to define the virtual function. They cannot be static members. They are access by using object pointers. The constructor function cannot be a virtual function. A virtual function can be a friend of another class. The virtual function must be preceded by virtual keyword in the base class. The function derived in the derived class must be same name as of the virtual function defined in the base class and the same prototype. vii. The function derived in the derived class need not be preceded by virtual keyword. 41. What is pure virtual function? Give an example. A virtual function that has no definition within the base class is known as pure virtual function. In such cases the compiler requires derived class to either define function or redeclare it as a pure virtual function. Syntax: virtual return_type function_name( argument list) =0; Example: Class shape { Public: virtual void draw( )=0; }; Kavaraipettai -601 206

i. ii. iii. iv. v. vi.

UNIT-III PART- A
1. What are Exceptions? What are the two types of exceptions? Exceptions, which occur during the program execution, due to some fault in the input data. Exceptions are classifieds into

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
a) Synchronous exception:- The exception which occur during the program execution ,due to some fault in the input data within the program is known as Synchronous exception. b) Asynchronous exception:- The exceptions caused by events or faults unrelated to the program and beyond the control of program are called asynchronous exception 2. What is exception handling? Exception Handling is a mechanism to handle exception. Exceptions are error like situations. It is difficult to decide when an error becomes an exception. Exception refers to unexpected condition in a program. The unusual conditions could be faults causing an error which in then causes the program to fail. It is a new approach to error handling. 3. What are the components of exception handling mechanism? The Exception Handling has three building blocks 1. Try-for indicating program area where exception can be thrown. 2.Throw-for throwing an exception 3. Catch-for actually taking an action for the specific exception. 4. What are the challenges in exception handling? The new approach using exception handling when throwing an exception we also need to take care of all subsequent effects of throwing exception. 1. Finding proper catch block 2. Finding proper catch block for polymorphic object. 3. Backtracking till the beginning of try block. 5. What are the different mechanisms of traditional error handling? Traditional Error Handling is done in three ways Returning error number Global flag manipulation Abnormal termination Kavaraipettai -601 206

6. What is object destroy problem? How can exception handling help them? A destructor is needed for an object which is created with dynamic constructor to smoothly return the resources consumed by it and to close connections. The exit or abort functions do not call the destructor of an object so the objects are not destroyed properly. This is called object destroy problem. Exception handling mechanism may be used here to call the destructor automatically. 7. What is the importance of throwing objects rather than Built-in type values?

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Object may have a member function, which tells the user that do in that case and it helps to provide some alternate solution. With the object it is possible to provide some Generic error handling logic irrespective of the situation in which error has occurred using the member function of the object. This is not possible when we use built -in type values for throwing. 8. What is exception specification? It is possible to specify what kind of exception can be thrown by the functions using a specific syntax. It is known as exception specification. Example: void area(int a )throw (int, char) { } 9. List out the drawbacks of exception handling. i. ii. iii. iv. Exception Handling mechanism has a major drawback of uncertain termination:. The Another disadvantage of Exception Handling is overhead mobile device programmers also do not use Exception Handling All the user must be aware of the exceptions thrown by functions. It is not possible the Exception Handling approach will increase (or) not decrease the problems Kavaraipettai -601 206

Its best for developing a library for a large project. Its important that the library functions throwing exceptions must be standardized. 10. List out some of the error handling functions eof()-Returns true if end-of-file is encountered fail()-Returns true when an input or output operation has failed bad()-Returns true if invalid operation is attempted by or any uncoverable error has occurred good()-Returns true if no error has occurred 11. List out the tasks to be performed for error handling code 1) Detect the problem causing exception (Hit the exception) 2) Inform that an error has occurred (Throw the exception) 3) Receive the error information (Catch the exception) 4) Take corrective actions (Handle the exceptions) 12.Draw the Exception handling model? Try block

Perform operation which may throw or invoke external function if needed

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Kavaraipettai -601 206

Invoke function having throw block Throw block If(failure) exception Throw object; exception

Catch block Catches all exceptions thrown from within try block or by function invoked within a try block

The mechanism suggests that Exception Handling code must perform the following 1. Detect the problem causing exception (hit exception) 2. Inform that an error has occurred (thrown an exception) 3. Receive the error into (catch the exception) 4. Take corrective actions (Handle the exception) 13. Write the syntax of try construct The try keyword defines a boundary within which an exception can occur.A block of code in which an exception can occur must be prefixed by the keyword try.Following the try keyword is a block of code enclosed by braces.This indicates that the program is prepared to test for the existence of exceptions.

Keyword try

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
{ //code raising exception or referring to a function raising exception } catch(type_id1) { //actions for handling an exception } catch(type_idn) { //actions for handling an exception } 14. Write the syntax of catch construct The exception handler is indicated by the catch keyword.It must be used immediately after the statements marked by the try keyword.The catch handler can also occur immediately after another catch.Each handler will only evaluate an exception that matches,or can be covered to the type specified in its argument list. Kavaraipettai -601 206

Keyword

object name or nameless object

Catch(T) { // } actions for handling an exception

15.Write the syntax of throw construct

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
The keyword throw is used to raise an exception when an error is generated in the computation.The throw expression initializes a temporary object of the type T(to match the type of argyment arg)used in throw(T arg) Named object, nameless object Kavaraipettai -601 206

throw T; 16. Write the steps to be performed when an exception is raised 1) The program searches for a matching handler 2) If a handler is found, the stack is unwound to that point 3) Program control is transferred to the handler 4) If no handler is found, the program will invoke the terminate () function. If no exceptions are thrown, the program executes in the normal fashion 17. Write the syntax of specifying a list of exceptions

Function definition

List of exceptions that can be raised

FunctionSpecification throw(type id1,type id2,) { //Function body raising exceptions if error occurs } 18. Write the syntax of catch all construct Catch() { //actions for handling an exception } 19. Write the functions to handle uncaught exceptions 1) terminate () - It is invoked when an exception is raised and the handler is not found.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
2) set_terminate () - allow the user to install a function that defines the programs action to be taken to terminate the program when the handler of the exception cannot be found. 3) unexpected () - this function is called when a function throws an exception not listed in its exception specification. 4) set_unexpected() It allow the user to install a function that defines the programs action to be taken when a function throws an exception not listed in its exception specification. 20.What are the two types of errors or bugs? Logical errors-errors occur due to poor understanding of the problem and solution procedure Syntatic errors-errors that arise due to poor understanding of the language itself. 21. What are streams? A Stream is a sequence of bytes. It can either as a source from which the input data can be obtained or as a destination to which the output data can be sent The stream source that provides data to the program is called the input stream and the destination stream that receives output from the program is called output stream 22. What are the stream classes for console operations? a)ios b)istrean c)ostream d)iostream e)streambuf 23) List out some of the unformatted I/O operators. a)put() b)get() c)getline() d)write() Kavaraipettai -601 206

24.Draw the console stream classes for hierarchy

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
ios Streambuf *bp=&strstreamb ase::bug ostream iostream

Kavaraipettai -601 206

istream

streambuf

istream_withass ign

iostream_withassig n

ostream_withas sign

25) List out some of the formatted I/O operations C++ supports a number of feature that could be used for formatting the output.These features include a)ios stream class member functions and flags b)Standard manipulators c)User defined manipulators 26) List some of the ios functions a)width() b)precision() c)fill() d)setf() e)unsetf() 27) List some of the non-parameterized manipulators a)dec b)hex c)oct d)es e)endl f)ends g)flush 28) What is custom/user-defined manipulators The users can design their own manipulators to control the appearance of the output 29)Write the syntax of creating a custom manipulator. Ostream & manipulator(ostream & output, arguments_if_any) { (manipulator code)

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
Return output; } 30) List out some of the manipulators. a)setw(int width) b)setprecision(int prec) c)setfill(int fchar) d)setbase(int base) e)setiosflags(long flags) f)resetioflags(long flags) 31) What are the flags that do not have bit fields? a)ios::showbase-Use base indicator on output b)ios::showpos-Print + before positive integers c)ios::showpoint-Show trailing decimal point and zeros d)ios::uppercase-Use uppercase letters for hex output e)ios::unitbuf-Flush all streams after insertion g)ios::stdio-Flush stdout and stderr after insertion 32) What are the two types of data communication? a) Data transfer between the console unit and the program b) Data transfer between the program and disk file 33) Draw the stream classes for hierarchy Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
ios Kavaraipettai -601 206

istream

streambuf

ostream

iostream

filebuf

ifstream

fstream

ofstream

fstreambas e 34) What are the functions that the file stream class provides? a)seekg()-Moves get pointer to a specified location b)seekp()-Moves put pointer to a specified location c)tellg()-Gives the current position of the get pointer d)tellp()-Gives the current position of the put pointer 35) What is a file? A file is a collection of related information defined by its creator. Files represent programs (both source and object forms) and data.Data may be numeric,alphabetic, or alphanumeric.Files may be freeform,such as text files,or may be rigidly formatted. 36) List some of the file modes 1)ios::in 2)ios::out 3)ios::ate 4)ios::app 5)ios::trunk 6)ios::nocreate 7)ios::noreplace 8)ios::binary 37) What is meant by namespace?

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
ANSI C++ Standard has added a new keyword namespace to define a scope that could hold global identifiers.The best example of namespace scope is the C++ Standard Library.All classes,functions and templates are declared within the namespace named std. Using namespace std; The using namespace statement specifies that the members defined in std namespace will be used frequently throughout the program 38) Write the syntax for defining a namespace namespace namespace_name { //Declaration of //variables,functions,classes,etc. } 39) What is unnamed namespaces? An unnamed namespace is one that does not have a name. Unnamed namespace members occupy global scope and are accessible in all scopes following the declaration in the file 40) What is Standard Template Library? The collection of these generic classes and functions is called the Standard Template Library 41) List the components of STL The STL contains several components. They are 1)Containers 2)Algorithms 3)Iterators 42) What is container? A container is an object that actually stores data.It is a way data is organized in memory. The STL containers are implemented by template classes and therefore can be easily customized to hold different types of data 43) What is an algorithm? An algorithm is a procedure that is used to process the data contained in the containers. The STL includes many different kinds of algorithms to provide support to tasks such as initializing, searching, copying, and sorting and merging 44) What is an iterator? An iterator is an object that points to an element in a container. We can use iterators to move through the contents of containers.Iterators are handled just like pointers 45) What are the three types of containers? The STL contains three types of containers 1) Sequence containers 2) Associative containers Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
3) Derived containers 46) What is meant by sequence containers? Sequence containers store elements in a linear sequence, like a line. Each element is related to other elements by its position along the line. They all expand themselves to allow insertion of elements and all of them support a number of operations on them Kavaraipettai -601 206

Element0

Element1

Element2

.. Last Element

47) What are the types of sequence containers? The STL provides three types of sequence containers 1) Vector 2) List 3) Deque 48) What is meant by Associative Containers? Associative containers are designed to support direct access to elements using keys. 49) What are the types of Associative containers? The STL provides three types of associative containers 1) Set 2) Multiset 3) Map 4) Multimap 50) What is meant by Derived Containers? The derived containers do not support iterators and therefore we cannot use them for data manipulation. They support two member functions pop() and push() for implementing deleting and inserting operations. 51) What are the types of derived containers The STL provides three types of derived containers 1) Stack 2) Queue 3) Priority queue 52) What is meant by vector? The vector stores elements in contiguous memory locations and enables direct access to any element using the subscript operator[].A vector can change its size dynamically and therefore allocates memory as needed at run time.

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
53) What is meant by lists? The list is another container that is popularly used. It supports a bidirectional, linear list and provides an efficient implementation for deletion and insertion operations. 54) What is meant by maps? A map is a sequence of pairs where a single value is associated with each unique key. Retrieval of values is based on the key and is very fast. We should specify the key to obtain the associated value. Kavaraipettai -601 206

Key 1

Value 1

Key 2

Value 2

Key N

Value N

55) Write the member functions of the map class 1) begin () 2) clear() 3) empty() 4) end() 5) erase() 6) find() 7) insert() 8) size() 9) swap()

UNIT-IV PART- A
1. What is Java? Java is a high-level, third generation programming language, like C, FORTRAN, Smalltalk, Perl, and many others. You can use Java to write computer applications that crunch

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank numbers, process words, play games, store data or do any of the thousands of other things computer software can do. 2. What is meant by Platform? Platform is the hardware or system software environment in which your program runs. Most platforms are described as a combination of hardware and operating system. 3. Java is platform independent language. Justify. Platform is the hardware or system software environment in which your program runs. Moreover java language run by any operating system, thats why java is called platform independent languages. 4. What is meant by Java Application? An Application is a program that runs on your computer, under the operating system of that system. 5. What is meant by Java Applet? An Applet is an application designed to be transmitted over the internet and executed by a Java-Compatible web browser. 6. Define JDK. JDK(Java Development Kit). JDK is a software package from Sun Microsystems. Latest version of JDK is 1.5. This software package contains tools. 7. What are the JDK Tools 1. Javac- Java Compiler 2. Java Java interpreter 3. Jdb Java Debugger 4. Javap- Java Disassembler 5. Javadoc- Java Documentation 6. Javah - Java Header file Generator 7. appletviewer-The Appletviewer 8. What are the features of Java? The features of Java are, i. Simple. ii. Object Oriented. iii. Platform Independent. iv. Robust. v. Multithreaded. vi. Secure. 9. What are the various applications of Java? The various applications of Java are, i. Applets ii. Networking
Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank iii. Internationalization iv. Security v. Object serialization vi. Java Database Connectivity (JDBC) 10. What are features does not supported by java? 1. Goto statement 2. Multiple inheritance 3. Operator overloading 4. Structures and Unions and Pointers 11. What are features supported by java 1. Automatic memory management 2. Multithreaded programs 12. Define Java Character Set. 1. Alphabets 2. Digits 3. Special characters Java uses Unicode character set. 13. What is meant by Java Class Defintion? A java program contains 2 parts. They are (i)A class definition that encloses the entire program (ii)A main() method that contains the body. Every java program should contain atleast one class. 14. What is meant by Java virtual machine(JVM)? A Java virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets compiled Java binary code (called bytecode) for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions 15. What are the two components of Java platform? The two components of Java platform are, i. The Java Virtual Machine ii. The Java Application Programming Interface (API) 16. What is meant by bytecode in java? Java compiler after compiling the program creates a new file referred as the class file, which contains a special code referred as the bytecode. It is similar to machine language, but unlike machine language, java byte code is exactly the same on every platform. 17. Define API. Java API are libraries of compiled code that you can use in your programs. They let you add readymade and customizable functionality to save your programming time. 18. Define Garbage Collection in Java? Garbage Collection also referred as automatic memory management. Periodically frees the memory used by objects that are no longer needed. The garbage collector in java scans dynamic memory areas for objects and marks those that are referenced. After all possible paths to objects are investigated the unreferenced objects are freed. 19. How multiple inheritance is achieved in java?
Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank Java does not support multiple base classes. So multiple inheritances is not possible in java. But it is achieved by the use interface. 20. State the use of super keyword in java The super keyword is used to invoke the constructor method of the super class in subclass. The following are the conditions: Super may only be used within a subclass constructor method. The call to super constructor must be appear as the first statement within the subclass constructor. The parameter in the super call must be match the order and type of the instance variable declaration in the super class 21. What is an Object and how do you allocate memory to it? Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it. For example. class student { int Rno; String name; float total; } Object t creation: Student s= new student(10,Anitha, 98.5); 22. What are different types of access modifiers? The different types of access modifiers are, i. public: Any thing declared as public can be accessed from anywhere. ii.private: Any thing declared as private cant be seen outside of its class. iii.protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages. iv. default modifier: Can be accessed only to classes in the same package. 23. What is method overloading and method overriding? Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading. Method overriding: When a method in a class having the same method name with same arguments is said to be method overriding. 24. List out the primitive types in Java. The seven primitive types are listed in the following table:
Type byte short int Definition one-byte signed two's complement integer two-byte signed two's complement integer 4-byte signed two's complement integer
Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank long 8-byte signed two's complement integer float 4-byte IEEE 754 single-precision float double 8-byte IEEE 754 double-precision float char 2-byte unsigned Unicode character 25. What is String? A String is a class used to store a sequence of characters in Java. Strings are constant. Their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
Example: String str = "abc"; 26. What is an array? An array is a special object containing a group of contiguous memory locations that have the same name and the same type and a separate variable containing an integer constant equal to the number of array elements. The elements of Java arrays are numbered starting from 0. Example: double x []; // create an array reference x = new double [5]; // create array object 27. What are the methods used in String class? The methods used in String class are, i. charAt(int index) ii. compareTo(String anotherString) iii. concat(String str) iv. copyValueOf(char[] data) v. equals(Object anObject)
Kavaraipettai -601 206

28. List the Classes that represent strings in java and differentiate them 1. String 2. StringBuffer String class String objects are immutable( not subjected to change) Creates strings of fixed length 9. What is package? Package is a collection of related classes and interfaces. It is also defined as putting classes together. 30. What are the package of Java? 1. Java API Packages 2. User Defined Packages 31. Write about API Packages? StringBuffer class StringBuffer objects are mutable(subjected to change) Creates string of flexible length

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank Java API provides a large number of classes grouped into different packages according to functionality. 1. Language Package(java.lang) 2. Utilities Package(java.util) 3. I/O Package(java.io) 4. Networking Package(java.net) 5. Applet Package(java.applet) 6. AWT Package(java.awt) 32.Java does not support multiple inheritance. Why? Classes in java cannot have more than one superclass.
For example Class A extends B extends C { ----} is not permitted in java. Kavaraipettai -601 206

33. Why Java use Unicode? Java use Unicode to represent a character. Unicode defines a fully international character set that can represent all of the characters found in all human languages. 34. What is Classpath? The Classpath is an argument we can set either on the command-line, or through an environment variable that tells the Java Virtual Machine where to look for user defined classes and packages when running Java programs. 35. What is the Significance of class path. Java interpreter uses an environment variable classpath, to determine the path to look for user defined classes. 36. What are Nested classes? A nested class is a class defined as a member of another class. There are two types of nested class. 1. Static nested class. 2. Non static nested class ( inner class) The scope of nested class is bounded by the scope of its enclosing class. The nested class has access to the members of its enclosing class including private members. Example:
Class outer { Static Class inner

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank
{ .. } } Kavaraipettai -601 206

37. What is meant by inner class? A class which is defined within another class without static keyword is called inner class. There are two types of inner class. 1. Local class. 2. anonymous class .
Example: Class outer { Class inner { .. } }

38. What is meant by Wrapper Classes? Corresponding to all the primitive data types java defines a set of classes referred as wrapper classes, which serves as class versions of the fundamental data types and named similar to the data types. 39. Define Instance of Operator in java? The instance of operator is a keyword, which is used to find out whether the object belongs to which class instance or not. Note that the instance of operator will cause a compile time error if it is used to check a class when there is no inheritance. 40. Mention some of the important classes present in java.net package. 1. The inetaddress class 2. URL class 3. Socket Class 4. The contenthandler class

UNIT-V PART- A
1. What do you mean by inheritance?

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these members as is, or it can hide the member variables or override the methods. Syntax for inheritance in java: class baseclassname { } class derivedclassname extends A { } 2. What are the advantages of inheritance? The advantages of inheritance are, i. It permits code reusability. ii. Reusability saves time in program development. iii. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional. 3. Derived class is usually larger than the base class. Why? A derived class usually larger than base class, since is normally adds data members and memory function in addition to what it inherits from the base class. 4. Define Sub classing. Sub classing is the creation of new class that inherits from an existing class in the class hierarchy. 5. What Members Does a Subclass Inherit? A subclass inherits all of the members in its superclass that are accessible to that subclass unless the subclass explicitly hides a member variable or overrides a method. Note that constructors are not members and are not inherited by subclasses. 6.What are Encapsulation, Inheritance and Polymorphism? i. Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse. ii. Inheritance is the process by which one object acquires the properties of another object. iii. Polymorphism is the feature that allows one interface to be used for general class actions. 7. What is the use of super keyword? A Java(TM) programming language keyword used to access members of a class inherited by the class in which it appears. 8. What do mean by overriding methods? The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass whose behavior is "close enough" and then supplement or modify the behavior of that superclass. 9. List out the various types of inheritance. The various types of inheritance are, i. Simple inheritance - One base class and one derived class ii. Multilevel inheritance - a sub class derived from another sub class iii. Hierarchical inheritance - two or more sub class have the same base class
Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank 10. Define Interface. An Interface is basically a kind of class. Like classes, interfaces contain methods and variables with difference. That is interface do not specify any code to implement these methods . All the methods declared in the interface are abstract methods by default. And all the data members are static final members. Syntax: interface InterfaceName { //final variable declarations //abstract method declarations } 11. How multiple inheritance is implemented in java? A class can implement more than one interface (the Java platform supports multiple inheritance for interfaces), so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. Syntax: interface inf1 { //final variable declarations //abstract method declarations } interface inf2 { //final variable declarations //abstract method declarations } class sample implements inf1, inf2 { } 12. What is meant by Exception? An Exception is an condition that is caused by a runtime error in the program. An error may produce an incorrect output or may terminate the execution of the program. 13. How an Exception is handled in java? A program can catch exceptions by using a combination of the try, catch, and finally statements. The try statement identifies a block of code in which an exception can occur. The catch statement identifies a block of code, known as an exception handler that can handle a particular type of exception. 14. What is the use of finally block?
The finally statement identifies a block of code that cleans up regardless of whether an exception occurred within the try block. A try statement must be accompanied by at least one catch statement or a finally statement and may have multiple catch statements.
Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank 15. Define Multithreading? Multitasking means executing several programs at a time, in system terminology, it is called multithreading. Thread is single line of execution with in the program. 16. What is life cycle of thread? 1. New born state 2. Runnable state 3. Running state 4. Blocked state 5. Dead state
Kavaraipettai -601 206

17. Define Dead Lock. A Dead Lock is defined as a special type of error that relates specifically to multitasking is dead lock, which occurs when two threads have a circular dependency on a pair of synchronized objects(i.e.,Deadlock occurs when 2 or more threads are waiting for resources that they cant get.). 18. How Threads are created in Java? Theads are created in two ways, i. extending the Thread class ii. implementing Runnable interface. 19. What is Thread pool? A Thread pool is a managed collection of threads that are available to perform tasks. Thread pools usually provide, i. Improved performance when executing large numbers of tasks due to reduced per-task invocation overhead. ii. A means of bounding the resources, including threads, consumed when executing a collection of tasks. 20. What do you mean by Thread Scheduling?

Execution of multiple threads on a single CPU in some order is called Thread scheduling. The Java runtime environment supports a very simple, deterministic scheduling algorithm called fixed-priority scheduling. This algorithm schedules threads on the basis of their priority relative to other Runnable threads. 21. What is the use of Data input and output streams? Data input and output Streams are used to read or write primitive data types in a machineindependent format. 22. What is the use of Buffered Streams?

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank Buffered streams, buffer data while reading or writing, thereby reducing the number of accesses required on the original data source. Buffered streams are typically more efficient than similar nonbuffered streams and are often used with other streams. 23 What is filtered Stream? A filtered stream filters data as its being read from or written to the stream. The java.io package provides a set of abstract classes that define and partially implement filter streams. 24. What is final variable, method and class in Java final: final is a keyword. The variable decleared as final should be initialized only once and cannot be changed. Java classes declared as final cannot be extended. Methods declared as final cannot be overridden. Syntax: (i) For variable final int a=100; (ii) For method public final void add() { //body of Method } (iii) For class public final class sample { //body of class } 25. Define finally block in exception handling finally: finally is a block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling - it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. Syntax: try { }
Kavaraipettai -601 206

R.M.D. Engineering College


CS 2311- OBJECT ORIENTED PROGRAMMING -Question Bank cath(Exception_type e) { } finally { } 26. Describe the use of finalize method finalize: finalize is a method. Before an object is garbage collected, the runtime system calls its finalize() method. You can write system resources release code in finalize() method before getting garbage collected. Syntax: public void finalize() { }
Kavaraipettai -601 206

You might also like