Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.


Please wait...

C++ Programming :: Objects and Classes

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

1. 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;
}

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


Tags:  Commvault

2. 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?

Answer: Option B

Explanation:

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

Workspace


Tags:  Wipro

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


Tags:  Wipro

4. Objects created using new operator are stored in __ memory.

Answer: Option B

Explanation:

"new" operator allocates memory dynamically in Heap

Workspace


Tags:  Wipro

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

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


Tags:  No Tags on this question yet!

6. Derivation of class from several base classes called_________inheritance.

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace


Tags:  No Tags on this question yet!

7. Which of the following class constructor will invoked first?

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


Tags:  No Tags on this question yet!

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

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


Tags:  No Tags on this question yet!

9. A program can directly access the_________members of a class.

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


Tags:  No Tags on this question yet!

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


Tags:  No Tags on this question yet!