Texas Instruments Interview Question






Comment hidden because of low score. Click to expand.
1
of 1 vote

Output will be 5 ..

as 'default' case is there on the top

hence it will execute the statement
i+=5
and i-=4;

after that it will get a break
and finally comes out of the block ..

I think basically interviewer was testing you C..

switch case is defined as

switch( i )
case a:
///code
break;

...

default
//code
break;
}

- Aditya September 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will execute whole case 3 as break is not written in default:

- Anonymous September 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Aditya's explanation isn't very accurate but he/she did get some points right.

Here's the breakdown.

i = 4
then it goes into the switch case.
since there isn't a defined case for the input it goes into the default case.
it runs the default case and because there wasn't a 'break;' defined, it continued right through case 3.
Which is why we end up adding 5 to i, making i = 9.
Skipping the 'if ( i == 8 )' statement.
then subtracting 4 from i, making i = 5.
then 'break' out of the switch case.
then onto 'printf()' which displayed 'i = 5'

- Anonymous September 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We know switch case is base on continues flow without break statement. Here first control will come to default, where there is nothing to execute and no break as well, so it will come to case 3: where it will add 5 with original i value which is 9 now and there is condition if i==8, which is not true, so control will come to i-=4 and i value becomes 5, as there is one break statement after that line, control will come out of the scope and print the i value, which is 5... :)

- Tiku September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 4;
switch (i)
{
default:
;
case 3:
i += 5;
if ( i == 8)
{
i++;
if (i == 9) break;
i *= 2;
}
i -= 4;
break;
case 8:
i += 5;
break;
}
printf("i = %d\n", i);

- Anonymous August 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 4;
switch (i)
{
default:
;
case 3:
i += 5;
if ( i == 8)
{
i++;
if (i == 9) break;
i *= 2;
}
i -= 4;
break;
case 8:
i += 5;
break;
}
printf("i = %d\n", i);

- Anonymous August 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1753

- Anonymous August 06, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'+9548

- Anonymous August 06, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 4;
i++;
switch(i)
{
default: ;
case 5:
i += 2;
if (i==7) i -=4;
i *=2;
case 7:
i +=5;
if(i == 8)
{
i--;
if (i == 9) break;
i *=3;
}
i -=9;
break;
case 4 : i +=7; break;
}
printf("i = %d\n",i);

- Anonymous March 12, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 4;
i++;
switch(i)
{
default: ;
case 5:
i += 2;
if (i==7) i -=4;
i *=2;
case 7:
i +=5;
if(i == 8)
{
i--;
if (i == 9) break;
i *=3;
}
i -=9;
break;
case 4 : i +=7; break;
}
printf("i = %d\n",i);

- innocent March 12, 2023 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More