Operators(C++ Programming ) Questions and Answers
Question 1.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
main()
4.
{
5.
double a = 21.09399;
6.
float b = 10.20;
7.
int c ,d;
8.
c = (int) a;
9.
d = (int) b;
10.
cout
- 20 10
- 10 21
- 21 10
- none of the mentioned
Explanation:-
Answer: Option C. -> 21 10In this program, we are casting the operator to integer, So it is printing as 21 and 10
Output:
$ g++ op5.cpp
$ a.out
21 10
Question 2.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a = 5, b = 6, c;
6.
c = (a > b) ? a : b;
7.
cout
- 6
- 5
- 4
- 7
Explanation:-
Answer: Option A. -> 6Here the condition is false on conditional operator, so the b value is assigned to c.
Output:
$ g++ op1.cpp
$ a.out
6
Question 3.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main ()
4.
{
5.
int x, y;
6.
x = 5;
7.
y = ++x * ++x;
8.
cout
- 749736
- 736749
- 367497
- none of the mentioned
Explanation:-
Answer: Option A. -> 749736Because of the precedence the pre-increment and post increment operator, we got the output
as 749736.
Output:
$ g++ op.cpp
$ a.out
749736
Question 4.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int i, j;
6.
j = 10;
7.
i = (j++, j + 100, 999 + j);
8.
cout
- 1000
- 11
- 1010
- 1001
Explanation:-
Answer: Option C. -> 1010:j starts with the value 10. j is then incremented to 11. Next, j is added to 100. Finally, j
(still containing 11) is added to 999 which yields the result 1010.
Output:
$ g++ op2.cpp
$ a.out
1010
Question 5.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a = 5, b = 6, c, d;
6.
c = a, b;
7.
d = (a, b);
8.
cout
- 5 6
- 6 5
- 6 7
- none of the mentioned
Explanation:-
Answer: Option A. -> 5 6It is a separtor 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
Explanation:-
Answer: Option A. -> it converts virtual base class to derived class Because the dynamic_cast operator is used to convert from base class to derived class.
Question 7.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a;
6.
a = 5 + 3 * 5;
7.
cout
- 35
- 20
- 25
- 30
Explanation:-
Answer: Option B. -> 20Because the * operator is having highest precedence, So it is executed first and then the + operator
will be executed.
Output:
$ g++ op1.cpp
$ a.out
20
Explanation:-
Answer: Option D. -> Type castNone
Explanation:-
Answer: Option A. -> conditionalIn this operator, if the condition is true means, it will return the first operator, otherwise second operator.
Explanation:-
Answer: Option A. -> postfixThe operator which is having highest precedence is postfix and lowest is equality.