module wise format oops questions

15
OBJECT ORIENTED PROGRAMMING USING C++ Module-I Short Question 2012 1. Differentiate between [2][Pointer] int *F( ); int (*F)( ); 2. What is the output of the following segment of code in C++: [2][Basics of C++] int a=10, b; b=a++ + a--; cout<<b; 2011 1. Write the output of the following sets of C++ statements [2][Basics of C++] void main( ) { unsigned i=1; signed j=1; if(i<j) cout<<” less”; else if(i>j) cout<<” greater”; elseif(i==j) cout<<”equal”; } 2. How do you declare a multidimensional array and initialize its members? How many elements are there in that array? [2][Array] 3. What is the difference between structure and union?[2][Basics of C++] 4. What is the difference between the address stored in a pointer and value stored at that address? [2][Pointer] 2010 1. What is an iterator in c++? [2][Iterators] 2. What will be the output and why? [2][Pointer] #include<iostream.h> int main( ) { const int a=20; const int *ptr=&a;

Upload: santosh-rath

Post on 06-May-2015

332 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

Module-I

Short Question 2012 1. Differentiate between [2][Pointer]

int *F( ); int (*F)( );

2. What is the output of the following segment of code in C++: [2][Basics of C++] int a=10, b; b=a++ + a--; cout<<b;

2011 1. Write the output of the following sets of C++ statements [2][Basics of C++]

void main( ) {

unsigned i=1; signed j=1; if(i<j) cout<<” less”; else if(i>j)

cout<<” greater”; elseif(i==j) cout<<”equal”;

} 2. How do you declare a multidimensional array and initialize its members? How many elements

are there in that array? [2][Array] 3. What is the difference between structure and union?[2][Basics of C++] 4. What is the difference between the address stored in a pointer and value stored at that address?

[2][Pointer]

2010

1. What is an iterator in c++? [2][Iterators] 2. What will be the output and why? [2][Pointer]

#include<iostream.h> int main( ) { const int a=20;

const int *ptr=&a;

Page 2: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

cout<< *ptr<<endl; (*ptr)++; cout<<a<<endl; return 0; }

2009 1. What do you mean by Free store? What is its use in C++ programming? [2][Basics of C++] 2. What is the application of scope resolution operator in C++? [2][Operators in C++] 3. How do the following statements differ? [2][Pointers]

char *const p; char const *p;

4. Give the syntax of declaring a pointer to a function? [2][Pointers]

2008 1. Which library file is included in each C++ program? Write the #include statement for that file

and explain why that files must be included? [2][Basics of C++] 2. How to read and print one character (including spaces) at a time from keyboard? [2][Basics of

C++] 3. List the three ways of passing arguments in or return value in C++. [2][Functions in C++] 4. What is the difference between cin.get and cin.getline? Give an example of each.[2][IO

operations] 5. What is the application of scope resolution :: operator in C++? [2][Operators in C++] 6. Re-write the following code segment using while loop; [2][control satement] 7. What is the output of the following segment of C++ code? [2.5][I/O operations]

int x=5, y=7, z=1; cout<<z<<##<<++x<<##<<y++;

Long Question 2012 1. Write a program in C++ that prompts the user for an integer and then prints out all prime

numbers up to that integer. [5][Basics of C++] 2. What do you mean by pointer initialization? [5][Pointers]

State the Differences among; char const *p; char *const p; const char *const p;

2011 1. Write down at least six properties of object oriented concepts that are not under concepts of

structural orientation. Explain using examples. [6][Introduction to C++] 2. Write a program in C++ using while loop to find the sum of first 100 even positive integers.

[5][Control statements]

Page 3: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

3. Write a program in C++ to find the results of multiplication of two numbers of two dimensional matrices. [5][Basics of C++]

2010 No Questions.

2009 1. Is it possible to return more than one value from a function? Justify with an example.

[5][Functions in C++] 2. Differentiate between the following: [2.5*2]

i. Implicit conversion and Explicit conversion. [Type conversion] ii. Passing argument by value and passing by reference. [Function]

2008 1. Write a C++ function which takes as input a even positive integer n less than 100 and returns

the sum; 2+4+6+………….+n. [5][Functions] 2. Write a C++ program, which accepts from the keyboard a name, a date of birth (of the user),

check with the user if these data were accepted correctly, then display personal greetings to this user, and display the date when user was exactly one year and one month old.[5][Basics of C++]

3. Write a C++ program that contains a main ( ) function and two additional functions to compute the area and perimeter of a square. The program should accept a number corresponding to the side length of a square and display the area and perimeter of the square on the screen? [5][Functions]

Module-II

Short Question

2012 1. Define ‘abstract class’. What is its use? [2][Polymorphism] 2. State differences between protected member and private member of a class.[2][Classes and

objects] 3. What is the use of this( )? [2][Polymorphism] 4. Whether a destructor can be virtual? Justify your answer. [2][Polymorphism] 5. Classify inheritance in C++. State its use. [2][Inheritance] 6. What is containership in C++? [2][Inheritance] 7. Define object slicing. [2][Classes and object] 8. What do you mean by static binding and dynamic binding? [2][Polymorphism]

Page 4: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

2011 1. How many copies of a static member are shared between objects of the class? [2][Classes and

objects] 2. What are the benefits of using ADTs? [2][Introduction to C++] 3. What is the wrong in the following set of statements? [2][Classes and objects]

#include<iostream.h> class my_class { int l; public: … … …

}; int main( ) { my_class obj; obj.l=15;

…. …. }

4. Find out the error in the following set of statements: [2][Constructor ] class my_class { double a, b, c; public: double my_class ( ); };

5. Differentiate between friend and inheritance. [2][Classes and object, Inheritance] 6. ____________ keyword supports dynamic method resolution. [2][Basics of C++] 7. What is the implicit pointer that is passed as the first argument for non-static member

functions? [2][Classes and objects] 8. ____________ is the most general exception handler that catches exception of any type.

[2][Exception handling] 9. What are the differences between private, public and protected classes? [2][Classes and

objects] 10. What is a friend function? Explain using an example. [2][Functions] 11. What are the differences between Static binding and Dynamic binding? [2][Polymorphism] 12. How abstract data types and pure virtual functions are linked? Explain with an example.

[2][Polymorphism] 13. What is the difference between constructor and destructor? Explain using example.

[2][Constructor and destructor] 14. What are the virtual and pure virtual functions? Use this concept to draw a square and

rhombus. [2][polymorphism]

Page 5: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

15. Differentiate between function overloading and operator overloading by giving example. [2][Polymorphism]

2010

1. For the statement: [2][Polymorphism] A ob;

ob = 20; Which mechanism should be carried out for successful execution of the above statements?

2. What is the work of set terminate ( ) function? [2][Exception handling] 3. For the following function definition: [2][Functions]

void abc(int a, int b=10, int c=20) { cout<<a+b+c; }

What will be the output of the following calls: abc(5,6);

abc(50); 4. What is the work of catch(…)? [2][exception handling] 5. What is pure virtual function? Why it is used? [2][Polymorphism] 6. Describe call by reference.[2][Functions] 7. Define inline function. [2][Functions] 8. For the following declaration: [2][Functions]

#define sqr(x) {return x*x;} What will be the result of the function call: sqr(4+4);

9. State at least two differences between an inline function and a macro substitution. [2][Functions]

10. What will be the output and why? [2][Classes and objects] #include <iostream.h> class X { int i; public:

static void f1( ) { i=20; i++; } void f2( ) {cout<< “i=”<<i<<endl; }

}; int main( ) {

X ob1,ob2;

Page 6: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

ob1.f1( ); ob2.f2( ); return 0;

} 11. Explain the two primary difference between a pointer variable and a reference variable.

[2][Basics of C++] 12. What will be the output and why? [2][Inheritance]

#include<iostream.h> class X {

public: int p;

}; class Y: protected X {

public: int q;};

int main( ) {

Y yob; yob.p=10; yob.q=20; cout<<yob.p<<” “<<yob.q<<endl; return 0;

} 13. What will be the output and why? [2][Polymorphism]

#include<iostream.h> class X {

public: void f1( ) {cout<<”X-men”<<endl;}

}; class Y:public X {

public: void f1( ) {cout<<”Y-men”<<endl;}

}; int main( ) {

X *xp; Y yob; xp=&yob;

Page 7: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

xp->f1( ); return 0;

} 14. What will be the output and why? [2][Classes and objects]

#include<iostream.h> class X {

int i; public: static void f( int i) {this->i=i; }

}; int main ( ) {

X xob; xob.f(20); return 0;

} 15. What will be the output and why? [2][Constructor and destructor]

#include<iostream.h> class X {

const int i; public: X(int p) { i=p;} void f( ) { cout<<”i=”<<i<<endl;}

}; int main( ) {

X ob(100); ob.f(); return 0;

} 16. What will be the output and why? [2][Exception handling]

#include<iostream.h> int main( ) {

cout<<”inside main”<<endl; try {

cout<<”inside try block”<<endl; throw 22;

Page 8: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

cout<<”exception thrown”<<endl; } catch(double d) {

cout<<”caught an exception =”<<d; } cout<<”end”<<endl; return 0; }

2009 1. How is memory allocated to Classes and Objects? [2][Classes and objects] 2. What is containership? How it is different from inheritance? [2][Inheritance] 3. Which library function is called when an exception is thrown which is not mentioned in throw

clause of function definition? [2][Exception handling] 4. What will be the output of the following program; [2][Classes and objects]

#include<iostream.h> using namespace std; class X {

static int i; public: void f1( ) { i=20;

i++; } void f2( ) { cout<<”i=”<<i<<endl }

}; int main( ) {

X ob1, ob2; ob1.f1( ); ob2.f2( ); return 0;

}

2008 1. What is an abstract class? Explain the use of abstract base class in C++. [2][Polymorphism] 2. What do you mean by dynamic initialization of object?[2][Dynamic memory]

Page 9: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

Long Question 2012 1. Define a class MONEY, which maintains two integer data fields, dollar and cents. Overload the +,

- operators to add, subtract and input and output operators for your class. Can you overload the * and / operators? If yes, what argument types should they accept? Overload the % operator so that if n is a floating point value, n % m yields n percent of the money amount m. [10][polymorphism]

2. Implement a class ACCOUNT. An account has a balance, functions to add and withdraw money and a function to query the current balance. Charge a $5 penalty if an attempt is made to withdraw more money than available in the account. [5][Classes and objects]

3. Derive a class MANAGER from EMPLOYEE. Add a data field, named department, of type string. Supply a function print that prints the manager’s name, department and salary. Derive a class EXECUTIVE from MANAGER. Supply a function print that prints the string Executive, followed by the information stored in the MANAGER base object.[5][Inheritance]

4. Create a base class called SHAPE. Use this class to store two double type values that could be used to compute the area of figures. Derive two special classes called TRIANGLE and RECTANGLE from the base class. Add to the base class, a member function get_data( ) to initialize base class data members and another member function display_area( ) to compute and display the area figures. Make display_area( ) as a virtual function and redefine this function in the derived classes to suit their requirement. Using these 3 classes, design a program that will accept dimensions of a rectangle or a triangle interactively and display the area.The two values given as input will be treated as length of two sides n case of rectangles and a base and height in the case of triangles. [10][Polymorphism]

5. Distinguish between the following two statements: time T2(T1); [4][constructors] time T2=T1;

T1 and T2 are objects of time class. 6. Implement a 2× 2 MATRIX class that holds four floating point numbers, and a VECTOR class that

holds two floating point numbers. Supply constructors and print operators, but do not expose the values of either class. Overload the multiplication operator so that a MATRIX can be multiplied by a VECTOR object. To do this will you need to make VECTOR a friend of MATRIX. [6][Polymorphism]

7. Write short notes on : [5*3] i. Exception handling [exception handling]

ii. Aggregation [Inheritance] iii. Virtual function vs Pure virtual function [Polymorphism]

2011 1. Write a C++ program using class and object with constructor to convert the Fahrenheit to

Celsius and vice versa. [5][Constructor] 2. Discuss the different forms of constructors with examples. [5][Constructor] 3. State any conflict that may rise due to multiple inheritances. Justify your answer.

[5][Inheritance]

Page 10: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

4. Write short notes on try, throw and catch block in C++? [5][Exception handling] 5. What is pure virtual function? When do we make a virtual function pure? What is the

implication of making a pure virtual function? Explain with suitable examples. [5][Polymorphism]

6. Write a program using pure virtual function to find out the area of circle, triangle, and square. [5][Polymorphism]

7. Write a program to read two double type numbers from keyboard and a function to calculate the division of these two numbers. A try block to throw an exception when a wrong type of data is entered and another try block to throw an exception if the condition” division occurs”. Appropriate catch block to handle the exception thrown. [5][Exception handling]

8. Write a function using reference variables as arguments to swap the values of pair of integers.[4][Functions]

9. What are different types of inheritance? Explain with example. [5][Inheritance] 10. Define a class rectangle by inheriting from class point. The point should indicate the upper left

corner of the rectangle. What are the class attributes? What additional methods do you introduce? [5][Inheritance]

11. Define a class to represent a bank account with following data members and member functions. Data members: [6][Classes and objects]

a) Name of the customer b) Account number c) Type of account d) Amount in account

Member function a) To assign initial values b) To deposit an amount c) To withdraw an amount d) To display name and balance

Write a main program to use the member functions. 12. Design the constructors for the classes mentioned in previous question. [4][Constructors] 13. Create a class FLOAT that contains one float data member. Overload all four arithmetic

operators so that they operate on the object of FLOAT. [5][Polymorphism] 14. Write a program that throws an exception when a wrong type of data is keyed in.

[5][Exception handling] 15. Write short notes on: [5*3]

a) Encapsulation [Introduction to C++] b) Inline function [Functions] c) Virtual destructor[Polymorphism]

2010 1. What is inheritance? Describe the various types of inheritances with examples for each one.

[10][Inheritance] 2. What is function overriding? How can we eliminate function overriding, describe with suitable

example.[10][Polymorphism] 3. Consider the following hierarchy:

Page 11: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

Here A is the topmost parent class, B is its child, C is the child of B, and D is the child of C. A is an abstract class containing member function get ( ) to read any value, which is implemented in B and C for reading value i.e. get( ) in B reads value x as member and get( ) in C reads value y as member, class D contains a member function add( ) to add x and y.

Write C++ program for the above. [10][Inheritance]

4. What is an exception; describe the mechanism of exception handling with suitable example?[10][Exception handling]

5. What is a constructor; explain its types with suitable examples for each.[10][Constructors and destructors]

6. Create a class complex with real and imaginary parts as member variables, member functions get ( ) and display( ) to input and display the complex number respectively. Write a program using above class to overload + and – operators for performing addition and subtraction of two complex numbers. [10][Polymorphism]

7. Create two classes Meter and Centimeter to represent meter and centimeters respectively. Use appropriate conversion routines to convert, [10][Classes and objects]

a) Meters to centimeters b) Centimeters to meter

8. Create a class called Student which contains protected attributes such as stud_name, stud_roll and stud_branch. Provide an appropriate method to take user input to initializes these attributes and display the details regarding 50 students of a class. [5][Classes and objects]

9. Create a class called Area which contains method called “find_area”. Write down appropriate code to create objects named as Circle and Rectangle of the above class and implement function overloading to calculate area of a rectangle and area of a circle based upon user input. [5][Polymorphism]

10. Write a C++ program to convert a primitive or built-in value such as int or float to a user defined type value such as class A or class X. [5][Type conversion]

11. Write a C++ program where you can only be able to create a single instance of a class. Upon trying to create a single instance of a class. Upon trying to create more than one number of instances will result in termination of program. [5][Classes and objects]

12. Create a class called Point with two integer attributes such as x and y to represent its x-coordinate and y-coordinate. Provide constructor to initialize the attributes. Provide another method named as move ( ) which will move the coordinates only in the direction of x-axis for 10

Page 12: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

units incrementing 1 unit at a time. Also display the new and old values of the coordinates. [5][Constructor]

13. With an appropriate example explain the role of virtual base class removing ambiguities n case of diamond inheritance which is a special case of multipath inheritance. [5][Inheritance]

14. Create an abstract class called Figure which contains a pure virtual function called fnd_area() and a protected attribute named as area. Create two derived classes from the above class named as Circle and Square having double type attribute named as radius and side respectively. Implemented dynamic polymorphism to find out area of a circle and a square and show result. [5][Inheritance]

15. Write a program to overload the pre-increment and post increment operators in a same program using non member operator functions. [5][Polymorphism]

16. Suppose there is a class X with a double type attribute. Write a C++ program to create three objects named as ob1, ob2, and ob3 of the above class and perform the following operation,

ob2=5.5+ob1; ob3=ob1+6.7; [5][Classes and objects]

17. Write a complete program to create a class named as Student with protected attributes such as id and marks. The attributes should be initialized only through constructors. The class contains a public method named as show( ) to display the initialized attributes. Provide a mechanism to create an array of student objects. The array should be given by the user at run time. [5][Constructors]

18. Provide at least one good reason to choose a const reference rather than a reference variable as parameter to a copy constructor. Explain with suitable example. [5][Constructor]

19. Write a program where you can achieve the following conversion within main( ) A ob1(10), ob2(101.5); int i=ob1; double d=ob2;

Where A is a user defined class. [5][Type conversion] 20. Write a complete program where you can restrict a user defined function to throw only int or

char type exception out of it. [5][Exception handling]

2009 1. Pure virtual functions force the programmer to redefine virtual functions inside derived

classes”. Comment on the statement. [5][polymorphism] 2. What is generic catch block? What are the restrictions while using a generic catch block?

Explain with an example. [5][Exception handling] 3. How do the properties of the following two derived classes differ? [3][Inheritance]

i. class X: private Y{//…..}; ii. class B: protected A{//….};

4. Describe various types of inheritance with suitable example. [5][Inheritance] 5. Define a class called Increment. The class contains one integer data member. Overload this

object of the class for both pre-increment and post-increment operator. [5][Polymorphism] 6. How to restore the access label of an inherited data member in derived class? Explain with the

help of C++ program? [5][Inheritance]

Page 13: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

7. Differentiate between private, public and protected data members of the class with example. [5][Classes and objects]

8. Differentiate between the following: [2.5*2] i. Operator overloading and Function overloading [Polymorphism]

ii. Virtual member function and Non-virtual member function [Polymorphism] 2008 1. When do we declare a member of a class as static? What is a class? How does it accomplish data

hiding concept? [5][Classes and objects] 2. Can class members/methods be private? Can they be public? Which ones are accessible to

methods outside the class in which they are implemented? [5][Classes and objects] 3. Given a two dimensional m×n double array A. Declare the variable A, and write the C++ code

required to allocate and deallocate the array (assume that m and n are declared and their values are known). [5][Classes and objects]

4. C++ allows for multiple inheritance which means that subclass can have more than one superclasses. In this context the mechanism of virtual inheritance becomes important. Give an example of multiple inheritance where virtual inheritance is essential. [4][Inheritance]

5. What is a class? How does it accomplish data hiding? Define a Student class with attributes and functions that you fell most essential. [5][Classes and objects]

6. What does polymorphism mean in C++? How polymorphism is achieved at computer time and runtime? Give an example of program that uses polymorphism. [5][Polymorphism]

7. Create a class FLOAT that contains one float data member. Overload all the four arithmetic operators so that they operate on the objects of FLOAT. [5][Polymorphism]

8. What are the differences between structures and classes in C++? [2.5][Classes and objects] 9. Explain the use of constructors in C++ program design. [2.5][Constructors] 10. What is the difference between public, private and protected members of a class? [2.5] [Classes

and objects]

Module-III

Short Question 2012

No Questions

2011 1. ____________ provide facilities for organizing the names in a program to avoid name classes.

[2][Namespace]

2010 1. What is the difference between template class and class template?[2][Template]

Page 14: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

2. What will be the output and why? [2][Template] #include<iostream.h> template<class T1>void f(T1 tt1) {

cout<<”tt1=”<<tt1<<endl; } template<class T2>void f(T2 tt2) {

cout<<”tt2=”<<tt2<<endl; } int main( ) {

f(1000); f(101.7); return 0;

}

2009 1. Explain the syntax and semantics of class template. [2][Template] 2. Distinguish between a namespace and a class. [2][Namespace] 3. What does RTTI mean? [2][Dynamic memory management]

2008

No Questions

Long Question 2012 1. Find errors if any, correct those, in the following code segment: [5][Template]

i. template<class T> T max(T,T) {….}; unsigned int m: int main( ) {

max(m,200); }

ii. template<class T> class temp {

T t; public: temp( ) {t=0;} void show( ) {cout<<;}

Page 15: Module wise format oops questions

OBJECT ORIENTED PROGRAMMING USING C++

}; void main( ) {

temp<void>x; x.show( );

} 2. Write a program in C++ to convert a given decimal number to binary using template function.

[5][Template] 3. Write short notes on: Object Oriented Design. [5][object oriented design]

2011 1. Can we use compiler generated default assignment operator in case our class is using dynamic

memory? Justify your answer. [3][Dynamic memory management] 2.

i. Differentiate between class template and template class with examples? [2.5][Template]

ii. Write short notes on STL [2.5][Standard template library] 3. Write a function template for finding the minimum value contained in an array. [5][Template] 4. Define a namespace named Constants that contains declaration of some constants. Write a

program that uses the constants defined in the namespace Constants. [4][Namespace]

2010

1. Write a complete program to declare and define a generic function that is capable of arranging any kind of elements in descending order. [5][Template]

2009

1. Write a template function alloc that takes two parameters; [5][Template] n: the size of the array to allocate, val: a value type T.

The alloc function should allocate an array of type T with n elements and set all elements in the array to value val, a pointer to array is returned.

2. Write a program in C++ to overload a function template? [4][Template]

2008

1. Write a template function alloc that takes two parameters; [5][Template] n: the size of the array to allocate, val: a value type T.

The alloc function should allocate an array of type T with n elements and set all elements in the array to value val, a pointer to array is returned.

2. What file is included to test if character read is alpha-numeric, uppercase? Write code to count how many lowercase letters and digits are in a given input? [5][File Handling]