Objects(C++ Programming ) Questions and Answers

Question 1.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
class number
4.
{
5.
int i;
6.
public:
7.
int geti();
8.
void puti(int j);
9.
};
10.
int number::geti()
11.
{
12.
return i;
13.
}
14.
void number::puti(int j)
15.
{
16.
i = j;
17.
}
18.
int main()
19.
{
20.
number s;
21.
s.puti(10);
22.
cout
  1.    10
  2.    11
  3.    20
  4.    22
Explanation:-
Answer: Option A. -> 10

We are getting the number and copying it to j and printing it.
Output:
$ g++ obj2.cpp
$ a.out
10



Question 2.

Which special character is used to mark the end of class?


  1.    ;
  2.    :
  3.    #
  4.    $
Explanation:-
Answer: Option A. -> ;

None.



Question 3.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
class sample
4.
{
5.
private:
6.
int var;
7.
public:
8.
void input()
9.
{
10.
cout
  1.    Enter an integer 5 Variable entered is 5
  2.    runtime error
  3.    error
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> error

While using private member, you cant access it variable.



Question 4.
Pick out the other definition of objects.
  1.    member of the class
  2.    associate of the class
  3.    attribute of the class
  4.    instance of the class
Explanation:-
Answer: Option D. -> instance of the class

None.



Question 5.

How many objects can present in a single class?


  1.    1
  2.    2
  3.    3
  4.    as many as possible
Explanation:-
Answer: Option D. -> as many as possible

Because a class may contain any number of objects according to it’s compliance.



Question 6.


What is the output of the program?


1.
#include
2.
using namespace std;
3.
class Rect
4.
{
5.
int x, y;
6.
public:
7.
void set_values (int,int);
8.
int area ()
9.
{
10.
return (x * y);
11.
}
12.
};
13.
void Rect::set_values (int a, int b) {
14.
x = a;
15.
y = b;
16.
}
17.
int main ()
18.
{
19.
Rect recta, rectb;
20.
recta.set_values (5, 6);
21.
rectb.set_values (7, 6);
22.
cout << "recta area: " << recta.area();
23.
cout << "rectb area: " << rectb.area();
24.
return 0;
25.
}
  1.    recta area: 30 rectb area: 42
  2.    recta area: 20 rectb area: 34
  3.    recta area: 30 rectb area: 21
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> recta area: 30 rectb area: 42

We are calculating the area of rectangle by two objects.



Question 7.


What is the output of the following program?


1.
#include
2.
using namespace std;
3.
class Box
4.
{
5.
public :
6.
double length;
7.
double breadth;
8.
double height;
9.
};
10.
int main( )
11.
{
12.
Box Box1;
13.
double volume;
14.
Box1.height = 5;
15.
Box1.length = 6;
16.
Box1.breadth = 7.1;
17.
volume = Box1.height * Box1.length * Box1.breadth;
18.
cout
  1.    210
  2.    213
  3.    215
  4.    217
Explanation:-
Answer: Option B. -> 213

In the above program, we are calculating the area of the cube by using the cube formula
Output:
$ g++ obj1.cpp
$ a.out
213



Question 8.

Which of these following members are not accessed by using direct member access operator?


  1.    public
  2.    private
  3.    protected
  4.    Both b & c
Explanation:-
Answer: Option D. -> Both b & c

Because of the access given to the private and protected, We can’t access them by using 

direct member access operator.



Question 9.

How to access the object in the class?


  1.    scope resolution operator
  2.    ternary operator
  3.    direct member access operator
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> direct member access operator

Objects in the method can be accessed using direct member access operator which is (.).



Question 10.

Where does the object is created?


  1.    class
  2.    constructor
  3.    destructor
  4.    attributes
Explanation:-
Answer: Option A. -> class

In class only all the listed items except class will be declared.