You are on page 1of 9

CS 2203 Object Oriented Programming 2 Marks Questions & Answers UNIT I 1. List out the characteristics of FOP. 1.

Large programs are divided into smaller programs known as functions. 2. Most of the functions share global data 3. Functions transform data from one form to another. 4. It employs top-down approach. 2. List out the characteristics of OOP. 1. Programs are divided into objects. 2. Data is hidden. 3. Objects communicate with each other, by sending messages and receiving responses. 4. It follows bottom-up approach. 3. List down the basic concepts of OOP. 1. Objects 2. Classes 3. Data abstraction and encapsulation 4. Inheritance 5. Polymorphism 6. Dynamic binding 7. Message passing 4. Define an object. Objects are the basic run time entities in an object oriented system. They may represent a person, a place or any item that a program has to handle. 5. Define Object Oriented Programming. OOP is a method of implementation in which programs are organized as co-operative collection of objects, each of which represents an instance of some class and whose classes are all members of a hierarchy of classes united through the property called Inheritance. 6. Define a class. A class is a collection of objects with similar attributes and operations, it defines the structure and characteristics of objects. . Eg. Class – Account It will create an object savings_account belonging to the class Account. 7. Define data member and member function? The attributes which are holding the information is known as data members. The functions that operate on data member are sometimes called as member function. 8. List down the benefits of OOP? 1. The principle of data hiding helps to build secure programs. 2. Through inheritance, redundant code is eliminated. 3. Software complexity can be easily managed. 9. List down the applications of OOP. 1. Real time system 2. Simulation and modeling 3. AI and expert system 4. Neural network programming.

5. CAD/CAM systems. 10. What is Implicit Type Conversion? When an expression consists of data items of different types, the compiler performs type conversions automatically. This is referred as Implicit Type Conversion. 11. List the different types of parameter passing techniques. (i) Pass by value (ii) Pass by Address (iii) Pass by Reference 12. What is Dynamic memory allocation? Allocation of memory space for any data structure during the course of the program execution is called as Dynamic memory allocation. 13. How memory management is performed dynamically in C++? Two operators are available for dynamic memory management. (i) new – for dynamic memory allocation (ii) delete – for dynamic memory deallocation 14. Define class? A class is a way to bind data and its associated functions together. It allows the data to be hidden if necessary. 15. Define nesting of member function. A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions. 16. List the properties of static members. A data member of a class can be qualified as static properties. 1. It is always initialized to zero when the first object of its class is created. 2. It is visible only within the class. 17. Write the properties of static member function. 1. A static function can have access to only other static members declared in the same class. 2. A static member function can be called using the class name (Instead of objects) as follows: class name:: function name; 18 . What are the features of object-oriented programming languages? Programs are divided into objects Data structures designed such that they characterize the objects. Functions that operate on the data of an object are tied together in the data structure Data is hidden and cannot be accessed by external functions Objects may communicate with each other through functions New data and functions can be easily added whenever necessary IT follows bottom-up approach in program design Concentration is on data rather than procedure 19. Describe I/O operator. Input Operator The statement cin>> num; is an input statement and causes the program to wait for the user to type in a number. The operator >> is known as extraction or get from operator. It takes the value from the keyboard and assigns it to

the variable on its right. Output Operator The statement Cout<< “ the numbers”; uses the cout identifier that represents the standard output stream ( screen) in C++. The operator < 20. Describe with example the uses of enumeration data type It provides a way for attaching names to numbers. The enum keyword gives the list of words by assigning them values 0,1,2 and so on. The Syntax is enum identifier { enumerated constants }; Ex: enum shape{ circle, square, triangle}; enum color { red, blue, green }; colur background; /* background is of type color */ Here each enumerated data type retains its own separate type. Ex: color background = blue; color background = ( color) 7; By default, the enumerators are assigned integer values starting with 0 for the first enumerator. But this can be over-ride. For ex, enum color { red, blue=4, green =8}; [ Write one example c++ program ] 21. Explain Symbolic Constants There are two ways of creating symbolic constants Using the qualifier Constants Any value declared as const can’t be changed. ex: const int size =10; char name[ size]; Defining a set of integer constants using enum keyword For ex, enum { X, Y, Z]; This defines X, Y,Z as integer constants with values 0,1,2 respectively. This can be also assigned explicitly. For ex, enum { X=100, Y=200, Z=300 }; 22. What do u mean by dynamic initialization of a variables? Initialization of variable at run time is known as dynamic initialization of a variables. That is the variables can be initialized at run time using expression at the place of declaration. Ex: ……… ……… int n = strlen (string); …….. for( int i=0;i<5;i++) { …… …… }

By using this, we can create exactly the type of object needed, using the information that is known only at the run time.

23. Explain about Type Casting Operator This is used for conversion of variables or expression explicitly Syntax: (type – name) expression or Type –name ( expression) Ex: avg= sum/ float(i); Alternatively, we can use typedef to create an identifier of the required type and use it in the functional notation typedef int * int_pt; p= int_pt(q); Bitwise expression 24. What are the advantages of new operator It automatically computes the size of the data object. So there is no need to use sizeof operator It automatically returns the correct pointer type, so that here is no need to use a type cast. It is possible to initialize the object while creating the memory space Like any other operator, operator new and delete can be overloaded. 25. What is the difference between structure and a class ? The only difference between a structure and a class in C++ is that , by default , the members of a class are private, while , by default the members of a structure are public. 26. When a function is defined inside a class ? it is treated as a inline function only small functions are defined inside the class definition 27. What is nesting of member functions ? A member function can be called by using its name inside another member function of the same class, is known as member function 28. How the space is allocated for the objects? The memory space is allocated when they are declared . space for the member variables is allocated separately for each object, but no separate space is allocated for the member functions 29. When do we declare a member of a class static ? When it is used to maintain values common to the entire class. The static member variables defined outside the class

30. What is a friend function? The functions that are declared with the keyword friend are known as friend functions. A function can be declared as a friend in any number of classes, it has full access rights to the private members of the class. 31. What are the special characteristics of friend function ? Can be invoked like a normal function, with the help of the object It has the objects as arguments It is not in the scope of the class to which is has been declared has friend 32. What is const member function ? If a member function does not alter any data in the class, then we declare it as const member function. The keyword const is appended to the function prototype.

Unit II 1. Define constructor? A constructor is a special member function whose task is to initialize the object of its class. It is called constructor because it constructs the value of data members of the class. 2. What are the characteristics of constructor? 1. Constructors should be declared in public section. 2. They are involved automatically when the objects are created. 3. They do not have return types. 4. They can not be inherited. 3. How do we invoke constructor function? It is invoked whenever an object of an associated class is created. It is called constructor because it constructs the values of data members of the class. 4. List some special properties of constructor functions.  They should be declared in the public section  They are invoked automatically when the objects are created  They do not have return types, therefore they cannot return values  They cannot be inherited  They can have default arguments  Cannot refer to addresses 5. Define parameterized constructor. Arguments can be passed to the constructor function when the objects are created. The constructors that can take arguments are called as parameterized constructor.

6. What is an implicit constructor? C++ compiler has an implicit constructor which creates objects even though it was not defined in the class. 7. What is the use of copy constructor? Copy constructor is used to declare and initialize an object from another object. E.g. Class name object2 (object1); Will define the object2 and at the same time initialize it the values of object1. 8. What do you mean by dynamic construction? Allocation of memory to objects at the time of their construction known as dynamic contraction of objects. The memory is allocated with the help of new operator 9. What is dynamic initialization of objects ? The initial value of an object provided at the run time. The advantage is that we can provide various initialization formats ,using overloaded constructors. 10. What is the use of destructor? It is used to destroy the objects that have been created by a constructor. It releases the memory space for future use. 11. What are the characteristics of destructor? 1. A destructor is a member function whose name is the same as the class name but it is preceded by a tilde. 2. It neither takes any argument nor returns any value. 3. It will be invoked implicitly by the compiler to cleanup the storage. 12. Define operator overloading? The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. 13. Define function overloading? Performing different types of task using single function name is referred as function overloading.

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 } 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. 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. 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) 21. List out the operators that cannot be overloaded using Friend function. • Assignment operator = • Function call operator ( ) • Subscripting operator [ ] • Class member access operator 22. 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. 23. 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; } }; Constructor will be called automatically while creating objects so that this conversion is done automatically. 24. 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. 25. 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.