“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Given int n, i=10, j=20, x=3, y = 100; What is the value of n and y at the end of each of the following expressions? a) n = (i > j) && (x < ++y); b) n = (j - i) && (x < y++); c) n = (i < j)
According to MSDN:
When a postfix | prefix operator is applied to a function argument, the value of the argument is not guaranteed to be incremented or decremented before it is passed to the function.
The result also depends on the compiler that how it evaluates the expression.
Here is the solution by the g++ compiler
n = 0 , y = 100
n = 1 , y = 101
n = 1 , y = 101
Submitted By: Saroj Kanta Behera — October 27, 2006
how can we assing a value to “n” ? the expressions are contain logical operator. i think it not possible,
dependent upon the compliers the default values are assigned to “n”.
The answer for the above program is
n=0,y=100
n=1,y=101
n=1,y=101
because
-> For(a)the condition becomes false so ‘0′ will be stored in ‘n’ & y value willnot be changed.
-> For(b) the condition is becoming true so ‘1′ will be stored in ‘n’.y value will be incremented by ‘1′ i.e y becomes 101.
->For(c) the condition is true so ‘1′ will be stored in ‘n’ & y will remain same i.e y=101
1> n = 0, y = 100, second condition will not be evaluated.
2> n = 1, y = 101
3> n = 1, y = 100
According to MSDN:
When a postfix | prefix operator is applied to a function argument, the value of the argument is not guaranteed to be incremented or decremented before it is passed to the function.
The result also depends on the compiler that how it evaluates the expression.
Here is the solution by the g++ compiler
n = 0 , y = 100
n = 1 , y = 101
n = 1 , y = 101
Given int n, i=10, j=20, x=3, y = 100;
a) n = (i > j) && (x
I agree with Jas
The results are compiler specific
In addition
You do not state whether the statements should be considered as independent of the others
Regards: Pedandic Pat
Look at the question once again –
Given int n, i=10, j=20, x=3, y = 100;
What is the value of n and y at the end of each of the following expressions?
a) n = (i > j) && (x
a)n=(i>j) && (xj(i.e 10>20) is false(0)
So the compiler doesn’t evaluate the remaining expression.
Thus the result is
n=0 & y=100.
b) n=(j-i) && (x
n = 0 , y = 100
n = 1 , y = 101
n = 1 , y = 101
how can we assing a value to “n” ? the expressions are contain logical operator. i think it not possible,
dependent upon the compliers the default values are assigned to “n”.
n=0 y=100
n=1 y=101
n=1 y=101
The answer for the above program is
n=0,y=100
n=1,y=101
n=1,y=101
because
-> For(a)the condition becomes false so ‘0′ will be stored in ‘n’ & y value willnot be changed.
-> For(b) the condition is becoming true so ‘1′ will be stored in ‘n’.y value will be incremented by ‘1′ i.e y becomes 101.
->For(c) the condition is true so ‘1′ will be stored in ‘n’ & y will remain same i.e y=101
1. n=0, y=100 (n=0 because condition evaluated is false)
2. n=1,y=101(n=1 because condition evaluated is true and also increment in y)
3. n=1,y=101.(because condition evaluated is true but nothing change in n and y. both are having their previous values)
Leave an Answer/Comment