C++ Operators | Set 1
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
Explanation: 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.
2. What are the essential operators in c++?
a) +
b) |
c) <=
d) All of the mentioned
Answer: D
Explanation: Essential operators in c++ are +, |, <=.
3. In which direction does the assignment operation will take place?
a) left to right
b) right to left
c) top to bottom
d) bottom to top
Answer: B
Explanation: In assignment operation, the flow of execution will be from right to left only.
4. Pick out the compound assignment statement.
a) a = a – 5
b) a = a / b
c) a -= 5
d) a = a + 5
Answer: C
Explanation: When we want to modify the value of a variable by performing an operation on the value currently stored, We will use compound assignment statement. In this option, a -=5 is equal to a = a-5.
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
cout << "a: " << a;
cout << "\nb: " << b;
return 0;
}
a) a:4 b:7
b) a:10 b:4
c) a:4 b:10
d) a:4 b:6
Answer: A
Explanation: In this program, we are reassigning the values of a and b because of this we got the output as a:4 b:7
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int a, b, c;
a = 2;
b = 7;
c = (a > b) ? a : b;
cout << "c: " << c;
return 0;
}
a) 2
b) 7
c) 9
d) 14
Answer: B
Explanation: We are using the ternary operator to evaluate this expression. It will return first option, if first condition is true otherwise it will return second
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int a = 0;
int b = 10;
a = 2;
b = 7;
if (a && b) {
cout << "true: " << endl;
}
else
{
cout << "false: " << endl;
}
return 0;
}
a) true
b) false
c) error
d) 10
Answer: B
Explanation: && is called as Logical AND operator, if there is no zero in the operand means, it will be true otherwise false.
8. What is the associativity of add(+); ?
a) right to left
b) left to right
c) right to left & left to right
d) top to bottom
Answer: B
Explanation: left to right is the associativity of add(+);.
9. What is the name of | operator?
a) sizeof
b) or
c) and
d) modulus
Answer: B
Explanation: | operator is used to find the ‘or’ of given values.
10. Which operator is having the highest precedence in c++?
a) array subscript
b) Scope resolution operator
c) static_cast
d) dynamic_cast
Answer: B
Explanation: Scope resolution operator is having the highest precedence in c++.