Output of C++ Programs | Set 49
- What is the output of the following program?
#include <iostream>usingstd::cout;intmain(){inti = 0;cout << (i = 0 ? 1 : 2 ? 3 : 4);return0;}chevron_rightfilter_nonea. 1
b. 2
c. 3
d. 4
e. compile error
Answer : C
Explanation: Ternary operator is right to left associative. So the expression becomes (i = 0 ? 1 : (2 ? 3 : 4)) which evaluates as (i = 0 ? 1 : 3). Note the expression “i = 0” assigns 0 to i and returns 0. So 3 is returned and printed. - What is the output of the following program?
#include <iostream>intmain(){printf("%.3lf",sizeof"Geeks"/sizeof"forGeeks");return0;}chevron_rightfilter_nonea. 0.000
b. 1.000
c. 0.667
d. Compile error
Answer : a
Explanation: The operator sizeof returns an integer and not a double. In case of strings it returns the length of the string including the \0. The sizeof of “Geeks” is 6, and the sizeof the string “forGeeks” is 9. In terms of intgers, 6/9 is equal to 0. - What is the output of the following program?
#include <iostream>usingstd::cout;intmain(){cout << 5 ["GeeksforGeeks"];return0;}chevron_rightfilter_nonea. Geeks
b. f
c. forGeeks
d. compile errorAnswer : b. Explanation: In C/C++, the statement X[Y] is identical to *(X+Y). In this case the statement is equal to *(5+"GeeksforGeeks"). The + operator is commutative, so we can write the exprasion as *("GeeksforGeeks"+5). This statement means 5th charcter of the string from the beginning of the string. So finally we print f. - What is the output of the following program?
#include <iostream>usingstd::cout;intmain(){inti = 0, j = 0;++++j = ++++i + i++;cout << i;cout << j;return0;}chevron_rightfilter_nonea. 12
b. 34
c. 22
d. compile dependentAnswer : d Since ++ operator does not define a sequence point, applying it multiple times on same variable in an expression causes undefined behavior.
- What is the output of the following program?
#include <iostream>usingstd::cout;intmain(){inti = 0, j = 0, k;i++;j++ ++;k = (i++) + j;cout << i;cout << j;cout << k;return0;}chevron_rightfilter_nonea. 123
b. 124
c. 224
d. Compile ErrorAnswer is: d.
Explanation: Unlike ++++j, the statement j++ ++; is illegal.
Recommended Posts:
- Output of C++ programs | Set 36
- Output of C programs | Set 52
- Output of C++ programs | Set 45
- Output of C programs | Set 48
- Output of C programs | Set 34
- Output of C++ programs | Set 23
- Output of C++ programs | Set 24 (C++ vs C)
- Output of C programs | Set 65 (If-Else)
- Output of C programs | Set 51
- Output of C programs | Set 37
- Output of C programs | Set 32
- Output of C programs | Set 29
- Output of C++ programs | Set 21
- Output of C++ programs | Set 35
- Output of C programs | Set 54
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



