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

Showing Answers 1 - 15 of 15 Answers

Sarada

  • 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
 

answer is :a 
"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

  Was this answer useful?  Yes

Timothy

  • Nov 17th, 2005
 

A

answer is y = 7

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)

Rohan

  • 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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

visitnitya

  • 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;
so answer is C

  Was this answer useful?  Yes

kamaljeet16

  • 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.

  Was this answer useful?  Yes

Correct Answer is Y=7
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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

VANDANA TYAGI

  • Sep 11th, 2011
 

(c) 1

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions