C++ Programming :: Objects and Classes

Home > C++ Programming > Objects and Classes > General Questions

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

1 / 41

 What is the output of the C++ program?

#include
using namespace std;

class test {
float x, y;
public:
test(float a = 1.0, float b = 2.0)
{
x = a;
y = b;
}

test operator + (test & obj) {
return test(this->x + obj.x, y + obj.y);
}

operator float () {
return (x + y) ;
}
};

int main () {
test obj1(1.23, 4.56), obj2;
obj2 = obj1 + obj2;
cout << obj2;
return 0;
}

A8.79

B5.79

C3

DCompilation error: binary '<<': no operator found which takes a right-hand operand of type 'test'

Answer: Option A

Explanation:

Operator overloading can be defined as assigning new meaning to the overloaded operator. In our program, we overloaded '+' operator which gets called when two instances of type 'test' are added. 

operator float () is a conversion operator which converts type 'test' to float. 

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

2 / 41

 Consider the following program:

# include
class x {
public:
int a;
x();
};
x::x() { a=10; cout< class b:public x {
public:
b();
};
b::b() { a=20; cout< main ()
{
b temp;
}
what will be the output of this program?

A10

B20

C2010

D1020

Answer: Option B

Explanation:

can not execute or understand the above program. There are multiple issues with it.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

3 / 41

 What is the use of virtual base class in c++

AMultiple lines between derived classes.

BMultiple lines between base classes.

Cinheritance

DNone of these

Answer: Option A

Explanation:

The answer is Option C, inheritance.

Virtual keyword, when assigned to base class while deriving a new class helps to prevent having multiple instances of the base class. so that there will be no ambiguity.

Lets say class b and class c are derived classes of class a and class a has a member function void foo(). Now, if we derive class d from both class b and class c, then the statement d.foo() creates ambiguity between the versions of class b and class c. To avoid this behavior, we must use Virtual keyword before the base class ('a') while deriving the classes b and c.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

4 / 41

 Objects created using new operator are stored in __ memory.

ACache

BHeap

CStack

DNone of the above.

Answer: Option B

Explanation:

"new" operator allocates memory dynamically in Heap

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

5 / 41

 Derivation of a class from other derived called .. inheritance.

Amultiplath

Bhybrid

Cmultilevel

Dnone of these

Answer: Option A

Explanation:

The answer is Option C.

Deriving a class from another derived class is called 'Multilevel' inheritance.

Deriving a class from more than one base class at a time is called 'Multiple' inheritance.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

6 / 41

 Derivation of class from several base classes called_________inheritance.

Amultiple

Bmultilevel

Cmultipath

Dnone of these

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

7 / 41

 Which of the following class constructor will invoked first?

ABase class

BVirtual base class

CAbstract class

DDerived class

Answer: Option B

Explanation:

The order of constructor execution in c++ is depends upon the order of inheritance.

In general inheritance, the constructor of base class will be invoked first and then the constructor of derived class will be invoked.

In multiple inheritance, base classes constructors are executed in the order of the inheritance and then the derived class constructor is called.

In multilevel inheritance, the constructor of virtual base class will be executed first.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

8 / 41

 All the public members of a base class becomes protectedc member of the derived classThis happen in

AVirtual inheritance

Bprotected inheritance

Cprivate inheritance

Dpublic inheritance

Answer: Option B

Explanation:

There are 3 access specifiers in c++. Public, Private and Protected. 

Public members are accessible outside the class through an object of the class.

Protected members are also accessible outside the class but only from the class which derived it.All public members of a base class becomes protected members of the derived class.

Private members can not be accessed from outside the class.

In Private inheritance, all public and protected members of base class becomes private to the derived class and can be accessible.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

9 / 41

 A program can directly access the_________members of a class.

Ahidden

Bprivate

Cpublic

Dboth (b) and (c)

Answer: Option C

Explanation:

we can access all members of class directly from inside a class.

we can only access public members of a class by creating an object to that class from outside the class.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

10 / 41

 The program can access the private members of a class

Adirectly

Bonly through other private members of the class

Conly through other public members of the class

Dnone of these

Answer: Option C

Explanation:

we can only access public members of a class by creating an object to that class from outside the class.

The public members in turn can have access to the private members of the class.

So, The program can access the private members of a class only through other public members of the class.

Workspace