Here is a listing of C++ language interview questions on “Operators” along with answers, explanations and/or solutions:
1. Which operator is having the right to left associativity in the following?
a) Array subscripting
b) Function call
c) Addition and subtraction
d) Type cast
Answer: d
Clarification: There are many rights to left associativity operators in C++, which means they are evaluation is done from right to left. Type Cast is one of them. Here is a link of the associativity of operators: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/cpp-built-in-operators-precedence-and-associativity.md
2. Which operator is having the highest precedence?
a) postfix
b) unary
c) shift
d) equality
Answer: a
Clarification: The operator which is having the highest precedence is postfix and lowest is equality.
3. What is this operator called ?:?
a) conditional
b) relational
c) casting operator
d) unrelational
Answer: a
Clarification: In this operator, if the condition is true means, it will return the first operator, otherwise second operator.
4. What will be the output of the following C++ code?
-
#include -
using namespace std;
-
int main()
-
{ -
int a;
-
a = 5 + 3 * 5;
-
cout << a;
-
return 0;
-
}
a) 35
b) 20
c) 25
d) 30
Answer: b
Clarification: Because the * operator is having highest precedence, So it is executed first and then the + operator will be executed.
Output:
5. What is the use of dynamic_cast operator?
a) it converts virtual base class to derived class
b) it converts the virtual base object to derived objects
c) it will convert the operator based on precedence
d) it converts the virtual base object to derived class
Answer: a
Clarification: Because the dynamic_cast operator is used to convert from base class to derived class.
6. What will be the output of the following C++ code?
-
#include -
using namespace std;
-
int main()
-
{ -
int a = 5, b = 6, c, d;
-
c = a, b;
-
d = (a, b);
-
cout << c << ' ' << d;
-
return 0;
-
}
a) 5 6
b) 6 5
c) 6 7
d) 6 8
Answer: a
Clarification: It is a separator here. In C, the value a is stored in c and in d the value b is stored in d because of the bracket.
Output:
$ g++ op3.cpp $ a.out 5 6
