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
  1.    20 10
  2.    10 21
  3.    21 10
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> 21 10

In 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
  1.    6
  2.    5
  3.    4
  4.    7
Explanation:-
Answer: Option A. -> 6

Here 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
  1.    749736
  2.    736749
  3.    367497
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> 749736

Because 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
  1.    1000
  2.    11
  3.    1010
  4.    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
  1.    5 6
  2.    6 5
  3.    6 7
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> 5 6

It 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



Question 6.

What is the use of dynamic_cast operator?


  1.    it converts virtual base class to derived class
  2.    it converts virtual base object to derived objeccts
  3.    it will convert the operator based on precedence
  4.    None of the mentioned
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
  1.    35
  2.    20
  3.    25
  4.    30
Explanation:-
Answer: Option B. -> 20

Because 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



Question 8.

Which operator is having right to left associativity in the following?


  1.    Array subscripting
  2.    Function call
  3.    Addition and subtraction
  4.    Type cast
Explanation:-
Answer: Option D. -> Type cast

None



Question 9.

What is this operator called ?: ?


  1.    conditional
  2.    relational
  3.    casting operator
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> conditional

In this operator, if the condition is true means, it will return the first operator, otherwise second operator.



Question 10.

Which operator is having the highest precedence?


  1.    postfix
  2.    unary
  3.    shift
  4.    equality
Explanation:-
Answer: Option A. -> postfix

The operator which is having highest precedence is postfix and lowest is equality.