1.
If class A is friend of class B and if class B is friend of class C, which of the following is true?
Correct Answer
D. None of the above
Explanation
If class A is a friend of class B and class B is a friend of class C, it does not necessarily mean that class C is a friend of class A. The friendship relationship is not transitive in this case. Therefore, the statement "Class C is a friend of class A" is not true. Additionally, there is no information given about whether class B can be a friend of any other class, so the statement "Class B cannot be a friend of any other class" cannot be determined. Therefore, the correct answer is "None of the above".
2.
Where does keyword ‘friend’ should be placed?
Correct Answer
C. Function declaration
Explanation
The keyword 'friend' should be placed in the function declaration. This is because the 'friend' keyword is used in C++ to grant access to private and protected members of a class to other classes or functions. By placing the 'friend' keyword in the function declaration, we are specifying that the function is a friend of the class and can access its private and protected members.
3.
How does C++ compiler differs between overloaded postfix and prefix operators?
Correct Answer
B. A postfix ++ has a dummy parameter
Explanation
The correct answer is that a postfix ++ operator in C++ has a dummy parameter. This means that when overloading the postfix ++ operator, a dummy parameter is added to the function signature. This is done to differentiate between the prefix and postfix ++ operators, as they have different behaviors and return values. The dummy parameter is not used in the function implementation, but it serves as a way for the compiler to distinguish between the two overloaded operators.
4.
Assume a class C with obj1, obj2, and obj3. For the statement obj3 = 1 - obj2 to work correctly, the overloaded operator must:
Correct Answer
D. Both (a) and (c)
Explanation
The correct answer is both (a) and (c). In order for the statement obj3 = 1 - obj2 to work correctly, the overloaded operator must return a value and use the object of which it is a member as an operand. Returning a value ensures that the result of the operation can be assigned to obj3, and using the object as an operand allows the operator to access the necessary data and perform the desired calculation.
5.
Which of the following is true statement?
Correct Answer
D. Like class member, it can access the class members directly.
Explanation
When a function is declared as a friend within a class, it is granted special access privileges. This means that it can access both public and private members of the class directly, just like any other member function of the class. This feature allows the friend function to manipulate class data effectively, enhancing the flexibility and power of the class design.
6.
Which of the following is used to define a member function outside of the class in C++?
Correct Answer
C. Scope Resolution Operator (::)
Explanation
In C++, the scope resolution operator (::) is used to define a member function outside of its class definition. When a function is declared inside a class but defined separately, the :: operator is used to specify that the function belongs to that class. For example, ClassName::FunctionName links the function definition to the class. This allows for better code organization and separation of class declarations and definitions.
7.
Predict the output:#include<iostream.h> int i; class A{public: ~A() { i=10; }}; int foo(){ i=3; A ob; return i;} int main(){ cout << foo() << endl; return 0;}
Correct Answer
B. 3
Explanation
The output of the code will be 3. The function `foo()` initializes the global variable `i` to 3 and creates an object `ob` of class A. The destructor of class A is called when `foo()` ends, and it sets the value of `i` to 10. However, since `i` is a global variable, its value is not affected by the destructor and remains 3. Finally, the value of `i` is printed in the `main()` function, resulting in the output of 3.
8.
Assume that an integer takes 4 bytes and there is no alignment in following classes, predict the output.#include<iostream> class base { int arr[10];}; class b1: public base { }; class b2: public base { }; class derived: public b1, public b2 {}; int main(void){ cout << sizeof(derived); return 0;}
Correct Answer
B. 80
Explanation
The output of the program is 80. This is because the derived class inherits from both b1 and b2, which in turn inherit from the base class. Each instance of the base class takes up 40 bytes (10 integers * 4 bytes per integer), and since there is no alignment, both b1 and b2 also take up 40 bytes each. Therefore, when the derived class is created, it will have the combined size of all its base classes, which is 80 bytes.
9.
Predict the output -
#include <iostream>
class Test {
public:
int x;
};
int main() {
Test t;
t.x = 42; // Initializing the member variable x
std::cout << t.x;
return 0;
}
Correct Answer
C. 42
Explanation
An object of the Test class is created with the variable name t.
The member variable x of the t object is set to the value 42 (t.x = 42;).
The value of t.x (which is now 42) is then printed to the console using std::cout.
The program returns 0, indicating successful execution.
10.
Are these statements "Objects of a class do not share non-static members. Every Object has its own copy."
Correct Answer
A. True
Explanation
In object-oriented programming, each instance of a class (object) typically has its own set of non-static member variables. These member variables represent the object's state, and each object maintains its own copy of these variables. Therefore, changes made to one object's member variables do not affect the member variables of other objects of the same class.
However, it's important to note that static members are shared among all instances of a class. So, if a member variable is declared as static, it will be shared among all objects of that class.