Consider the following program segmentint n, sum=1;switch(n){case 2:sum=sum+2;case 3:sum*=2;break;default: sum=0;}If n=2, what is the value of sumA. 0B. 6C. 3D. None of these

This question is related to TCS Interview

Showing Answers 1 - 28 of 28 Answers

Guest

  • Jan 3rd, 2006
 

c is the correct answer

  Was this answer useful?  Yes

Rohan

  • Feb 23rd, 2006
 

Ans is ( B )

  sum=6 reason is ,

   case 2 :  sum=sum+2 //sum=1+2 sum=3 and no break so

   case 3 : sum*=2 //sum=3*2 sum=6

   break;

sum=6 is the answer

  

venkatakrishnan

  • Jun 8th, 2006
 

the answer is 0 not either 3 or 6

  Was this answer useful?  Yes

SreeAravind

  • Oct 17th, 2006
 

Hi Some of them wrote wrong answers

its answer is  B:6

Because there is no break statement in case 2 so it further exec case 3 also...

  Was this answer useful?  Yes

answer is b.

first, case 2 is matched and sum is added with 2. now sum=3

then case 2 has no break so it performs case 3 also.. so now sum=3*2=6

  Was this answer useful?  Yes

anita

  • Aug 11th, 2011
 

answer is 6
because no break statement after case 2
therefore
sum=1+2=3
sum=3*2=6

  Was this answer useful?  Yes

SURLA GOVINDU

  • Sep 14th, 2011
 

Ans is 6 because there is no break statement in case2

  Was this answer useful?  Yes

Pabitra Kumar Tarei

  • Sep 25th, 2011
 

C=3

  Was this answer useful?  Yes

haritha

  • Sep 25th, 2011
 

6.
because..there is no break statement after first case...

  Was this answer useful?  Yes

n=2 to switch moves control to case 2:

where value of sum will become 2+1=3

as there is no break so all statement below it will be executed
so now case 3: will be executed n sum will hold value 3*2=6

now break is used so control will move out of loop i.e. switch case

  Was this answer useful?  Yes

belinda

  • Oct 6th, 2011
 

c answer is 3

  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