1 Crore+ students have signed up on EduRev. Have you? |
The operator which is having the highest precedence is postfix and lowest is equality.
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a;
a = 5 + 3 * 5;
cout << a;
return 0;
}
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
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 6, c, d;
c = a, b;
d = (a, b);
cout << c << ' ' << d;
return 0;
}
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
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 5;
y = ++x * ++x;
cout << x << y;
x = 5;
y = x++ * ++x;
cout << x << y;
return 0;
}
Because of the precedence the pre-increment and post increment operator, we got the output as 749736.
Output:
$ g++ op.cpp
$ a.out
749735
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
main()
{
double a = 21.09399;
float b = 10.20;
int c ,d;
c = (int) a;
d = (int) b;
cout << c <<' '<< d;
return 0;
}
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
Which operator is having the right to left associativity in the following?
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
In this operator, if the condition is true means, it will return the first operator, otherwise second operator.
Because the dynamic_cast operator is used to convert from base class to derived class.
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int i, j;
j = 10;
i = (j++, j + 100, 999 + j);
cout << i;
return 0;
}
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
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 6, c;
c = (a > b) ? a : b;
cout << c;
return 0;
}
Here the condition is false on conditional operator, so the b value is assigned to c.
Output:
$ g++ op1.cpp
$ a.out
6
15 videos|20 docs|13 tests
|
Use Code STAYHOME200 and get INR 200 additional OFF
|
Use Coupon Code |
15 videos|20 docs|13 tests
|
|
|
|
|
|
|
|
|
|