What is the value of y in the following code?x=7;y=0;if(x=6)y=7;else y=1;a.7b.0c.1d.6

C
This question is related to TCS Interview

• Apr 20th, 2005

Answer for this is : a
that is y value is 7. Here assignment statement get executed in if condition which evaluates to true, so 7 will be assigned to y.

a tirupathi rao

• May 7th, 2005

if(x=6) meens in this x will be assigned to 6
and the condition is non zero value.
so if part will excute and y will be assigned to 7. thus the y value is 7.

rohit

• Jul 21st, 2005

"value of y =7"

Ragu

• Sep 2nd, 2005

hello how r u saying y=7, if condition itself false than it will come to else statement only.
Ans:C

Timothy

• Nov 17th, 2005

A

Now this will assign 6 to x.

If we print the value of x, It will display 6.

if the compailer saw if(x=6), it will simply assign 6 to x,

but If we mistakenly give if(6=x) , it will display an error

So It's better to use if(6 ==x)

• Nov 20th, 2005

x=7 and y=0

now if(x=6)

//assigns 6 to x and what is >0 in if is true so  condition is  true

so y=7

Rahul Gupta

• Jul 1st, 2006

ans  (a)7

• Jul 2nd, 2008

correct ans is A: y=7.....bcoz condition is if(x=7) (assignment)and not (x==7) (comparison)

• Jun 21st, 2009

Here the statement if(x=6) is true because this is not a conditional operator it assigns 6 to x so it is true, the if condition is true
then y=7;

vishwanathpillay Profile Answers by vishwanathpillay Questions by vishwanathpillay

• Dec 8th, 2009

Here the line:

LINE 1:      if(x=6)
LINE 2:      y=7;
LINE 3:      else
LINE 4:      y=1;

In the line 1 it is not assigning any values, its just a conditional operator which checks the value of x.

If line 1 is true then value can be 7, BUT

In this case the value will be 1, bcoz its is just checking the condition

• Dec 8th, 2009

Ans: y=1; c is right
because x=0 then if condition false and control goes to else part then value becomes y=1;

• Feb 11th, 2010

The value of y is 7. The condition in if loop if(x=6) is true since the variable x is assigned the value 6 as an assignment operator(=) is used, not a comparison operator(==). so the value of y is 7.

• Sep 1st, 2011

Because if(x=6) it means we are assigning value 6 in x. So condition becomes if(6) and if statement is always executed when there is a positive value inside the braces. So Y=7 will be executed. Now if part is true then no need to run else part. So final value of Y remains 7.

abc

• Sep 4th, 2011

ans: y=7
since in if clause '=' is used and not'==' , it's a successful assignment operation.
it returns y=7 after comin across a true value and hence y is 7

VANDANA TYAGI

• Sep 11th, 2011

(c) 1