1. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A
{
static int a;
public:
void show()
{
a++;
cout<<"a: "<<a<<endl;
}
};
int A::a = 5;
int main(int argc, char const *argv[])
{
A a;
return 0;
}
A. Error as a private member a is referenced outside the class
B. Segmentation fault
C. No output
D. Program compiles successfully but gives run-time error
Answer: C
Explanation:
As every static member must be initialized and we have initialized variable ‘a’ so no run time error. Also as variable ‘a’ is a static member and is referenced using the class for initialization therefore no compiler error.
2. What happens when objects s1 and s2 are added?
string s1 = "Hello";
string s2 = "World";
string s3 = (s1+s2).substr(5);
A. Error because s1+s2 will result into string and no string has substr() function
B. Segmentation fault as two string cannot be added in C++
C. The statements runs perfectly
D. Run-time error
Answer: A
Explanation:
string is class in C++, therefore when we do (s1+s2) a temporary object is created which stores the result of s1+s2 and then that object calls the function substr() and as that is an object of string class hence substr is a callable function for that temporary string object.
3. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A
{
static int a;
public:
A()
{
cout<<"Object of A is created\n";
}
void show()
{
a++;
cout<<"a: "<<a<<endl;
}
};
class B
{
public:
};
int main(int argc, char const *argv[])
{
A a1, a2;
A a3 = a1 + a2;
return 0;
}
A. Runs perfectly
B. Run-time Error
C. Segmentation fault
D. Compile-time Error
Answer: D
Explanation:
As the programmer has not defined what action should be taken when two objects of class A are added, so the program doesn’t know and gives compile time error.
4. What is operator overloading in C++?
A. Overriding the operator meaning by the user defined meaning for user defined data type
B. Redefining the way operator works for user defined types
C. Ability to provide the operators with some special meaning for user defined data type
D. All of the mentioned
Answer: D
Explanation:
Operator overloading helps programmer to give his/her own meaning to an operator for user defined data types(eg, classes).
5. What is the syntax of overloading operator + for class A?
A. A operator+(argument_list){}
B. A operator[+](argument_list){}
C. int +(argument_list){}
D. int [+](argument_list){}
Answer: A
Explanation:
The general syntax for operator overloading is:
return_type operator_keywordOperator(argument_list){}
eg.
A opeartor+(argument_list){}
6. How many approaches are used for operator overloading?
A. 1
B. 2
C. 3
D. 4
Answer: C
Explanation:
There are 3 different approaches used for operator overloading:
i. Overloading unary operator.
ii. Overloading binary operator.
iii. Overloading binary operator using a friend function.
7. Which of the following operator cannot be overloaded?
A. +
B. ?:
C. –
D. %
Answer: B
Explanation:
?:, :: and . cannot be overloaded +, -, % can be overloaded.
8. Which of the following operator can be overloaded?
A. ?:
B. ::
C. .
D. ==
Answer: D
Explanation:
?:, :: and . cannot be overloaded whereas == can be overloaded.
9. Which of the following operator cannot be used to overload when that function is declared as friend function?
A. -=
B. ||
C. ==
D. []
Answer: D
Explanation:
When an operator overlaoded function is declared as friend function then [] cannot be overloaded.
10. Which of the following operator can be used to overload when that function is declared as friend function?
A. []
B. ()
C. ->
D. |=
Answer: D
Explanation:
When an operator overlaoded function is declared as friend function then [], () and -> cannot be overloaded.
11. In case of non-static member functions how many maximum object arguments a unary operator overloaded function can take?
A. 1
B. 2
C. 3
D. 0
Answer: D
Explanation:
In the case of non-static member functions unary operator overloaded function should not take any object argument.
12. In case of non-static member functions how many maximum object arguments a binary operator overloaded function can take?
A. 1
B. 2
C. 3
D. 0
Answer: A
Explanation:
In the case of non-static member functions binary operator overloaded function should take maximum one object argument only.
13. In the case of friend operator overloaded functions how many maximum object arguments a unary operator overloaded function can take?
A. 1
B. 2
C. 3
D. 0
Answer: A
Explanation:
In the case of friend operator overloaded functions unary operator overloaded function should take maximum one object argument only.
14. In the case of friend operator overloaded functions how many maximum object arguments a binary operator overloaded function can take?
A. 1
B. 2
C. 3
D. 0
Answer: B
Explanation:
In the case of friend operator overloaded functions binary operator overloaded function should take maximum two object argument.
15. What will be the output of the following C++ code?
#include
#include
using namespace std;
class A
{
static int a;
public:
void show()
{
a++;
cout<<"a: "<<a<<endl;
}
void operator.()
{
cout<<"Objects are added\n";
}
};
class B
{
public:
};
int main(int argc, char const *argv[])
{
A a1, a2;
return 0;
}
A. Run-time Error
B. Runs perfectly
C. Segmentation fault
D. Compile-time error
Answer: D
Explanation:
.(dot) operator cannot be overloaded therefore the program gives error.