• +91 9971497814
  • info@interviewmaterial.com

Chapter 2- Object Oriented Programming in C plus plus Interview Questions Answers

Question 1 :
Differentiate between data abstraction and data hiding.   

Answer 1 :

Data hiding can be defined as the mechanism of hiding the data of a class from the outside world. This is done to protect the data from any accidental or intentional access. Data hiding is achieved by making the members of the class private.
Data abstraction refers to, providing only essential information to’ the outside world and hiding their background details.
Members defined with a public label are accessible to all parts of the program. The data abstraction view of a type is defined by its public members.

Question 2 : How encapsulation and abstraction are implemented in C++ language ? Explain with an example.

Answer 2 :

Abstraction refers to the act of representing essential features without including the back-ground details or explanations.
Encapsulation is wrapping up of data and function together in a single unit.
We can say that encapsulation is way of achieving abstraction. Both these concepts are implemented in C++ in the form of a class. In a class data and function members are wrapped together. Only essential features are shown to the outside world in the form of member functions with public mode of accessibility. [1]
Example:     [1]

class Rectangle
 {
 int L, B;
 public: void Input() 
 {
      cin>>L>>B; 
 }
 void Output()
 {
 Cout<
 }
 };
In this example:
Data and member functions are together encapsulated
Only Input ( ) and Output ( ) functions are visible outside the class which can only can be accessed outside the class using objects of the same class – Abstraction

Question 3 : Explain data hiding with an example.

Answer 3 :

Data Hiding
Data Hiding is the mechanism where the details of the class are hidden from the user.
The user can perform only a restricted set of operations in the hidden member of the class For example,
In order to make the design and maintenance of a car reasonable the complexity of equipment is divided into modules with particular interfaces hiding design decisions.
General Form:

class classname
 {
 private:
datatype data;
    public:
       Member functions,-'
 };
 main()
 {
 classname objectnamel, objectname2
.........;
 {
 Example :
class Square
{
 private
 int Num;
 public:
 void Get() {
 cout<<"Enter Number:";
 cin>>Num;
 }
 void Display!) { cout<<"Square Is:"<
 }
 };
 void main()
 {
 Square Obj;
 Obj.Get();
 Obj.Display ();    [1]
 getch()
 }

Question 4 :
What do you understand by data encapsulation and Data hiding ?
Also, give a suitable C++ code to illustrate both. 

Answer 4 :

Data hiding is a property of OOP by which the crucial data of an object is made hidden from the rest of the program or outside the world. [1]
Encapsulation refers to the wrapping of data and associated functions together under a unit class. [1]

// Program for hiding & Encapsulation 
#include  
class Adder {    // Encapsulation
 public :
 Adder (int i = 0)
 { total = i;}
 void addNum (int Number)
 { total + = Number;
 }
 int getTotal ( )
 { return total;
 }
 private : int total ;
 //Data Hiding
};
int main ( )
{
Adder a;
a . addNum (10);
a . addNum (20);
a . addNum (30);
cout << "Total :<
) < < endl;
return 0 ;
}

Question 5 :
Write the output of the following C++ program code:   

class Eval
{
char Level; 
int Point;
Public :
Eval () {
Level = 'E' ; Point = 0;
}
void Sink (int L)
{
Level - = L;
}
void Float (int L)
{
Level + = L;
Point ++;
}
void Show ( )
{
cout<
}
};
void main ( )
{
Eval E;
E. Sink (3) ;
E. Show ( );
E. Float (7);
E. Show ( );
E. Sink (2) ;
E. Show ( );
}

Answer 5 :

B#0
I#1
G#1

Question 6 :
Observe the following program carefully and attempt the given questions :

#include
#include
#include
void main()
clrscr();
randomize();
char courses![ ] [10]={“M.Tech”.”MCA”, “MBA”,”B.Tech”};
int ch;
for(int i= l;i<=3;i++)
{
ch=random(i)+1;
cout<
}
getch () ;
}

(i) Out of all the four courses stored in the variable courses, which course will never be displayed in the output and which course will always be displayed at first in the output ?
(ii) Mention the minimum and the maximum value assigned to the variable ch ? 

Answer 6 :

(i) M. Tech will never be displayed in the output.
MCA will always be displayed at first in the output.
(ii) Minimum value of ch=l
Maximum value of ch=3

Question 7 :
Study the following program and select the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable VAL.
Note:
— Assume all required header files are already being included in the program.
— random(n) function generates an integer between 0 and n -1.   
void main ()
 {
 randomize ()    ;
 int VAL;
 VAL = random (3) + 2;
 chart GUESS [    ]    = "ABCDEFGHIJK"
 ;    for (int I = 1; I < = VAL;
 I ++)
 {
 for (int J = VAL; J < = 7; J ++)
 Cout<
 cout<
 }
 

Answer 7 :

(ii) and (iii)
Min Value of VAL = 2
Max Value of VAL. = 4

Question 8 :
Rewrite the following program after removing the syntax errors (if any). Under line each correction. 

#include
Class Item
{
long IId, Qty; 
public :
void Purchase {cin>>IId>>Qty;} 
void sale ()
{
cout<
<
cout<<"New :" <
}
};
void main ()
{
Item I;
Purchase ();
I. Sale () ;
I. Sale ()
}

Answer 8 :

#include
#include 
class Item 
{
long IId, Qty; 
public :
void purchase() 
{cin>>IId>>Qty;} 
void sale()
{
cout<
cout<< "New    Qty<
}
};
void main( )
{
Item I ;
I. purchase( ); // Object missing 
I. sale ( ) ;
I. sale( ); //; is missing
}

Question 9 :
Rewrite the following program after removing the syntax errors (if any). Underline each correction.  

# include  
class book {
long Bid, Qty; 
public :
void purchase () {cin>>BID>>Qty;} 
void sale ( )
{
cout<
dl;
cout<<"New; "<<—Qty<
}
};
void main ()
{
Book B;
B. Purchase(); 
sale ();
B. Sale ()
}

Answer 9 :

#include
#include 
class Book
 {
long Bid, Qty; 
public:
void purchase()(cin>>Bld>>Qty;) 
void sale( )
{
cout < < setw(5) < < Bld < < "0ld " < < Qty < < e n
dl;
cout<"New:"<<—Qty<
}
} ;
void main ()
{
Book B;
B. purchase ();
B. sale();
}

Question 10 :
Answer the questions (i) and (ii) after going through the following class: 

class Travel
{
int PlaceCode; char Place [20] ; 
float Charges; 
public :
Travel ( )    //Function 1
{
PlaceCode = 1; strcpy (Place,
"DELHI"); Charges =1000;
}
void TravelPlan (float C)
//Function 2
{
cout<

Selected

 

Chapter 2- Object Oriented Programming in C plus plus Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners