Search This Blog

Friday, July 9, 2010

Chapter-2 Control Instructions

CHAPTER- 2
CONTROL INSTRUCTIONS.
1.what would be the o/p of the following program
main()
{
int i=4;
switch(i)
{
default
printf(“\n a mouse”);
case 1:
printf(“\n a rabbit”);
break;
case 2:
printf(“\n a tiger’);
break;
case 3:
printf(“\n a lion”);
}
}


ans>a mouse
a rabbit


2. point out error in for loop if any-
main()
{
int i=0;
for( ; ; )
{
printf(“\n %d”,i++);
if(i>10)
break;
}
}

a.the condition in the for loop is must.
b.the two semicolons should be dropped
c.the for loop should replaced by a while loop
d.no error.

Ans:d

3. point error if any in the while loop
main()
{
int i=1;
while()
{
printf(“\n %d”, i++);
if(i>10)
break;
}
}

a.the condition in the while is must.
b.there should be at least one semicolon in the while()
c.the while loop must be replaced by a for loop.
d.no error.

Ans:a

4.point out error if any
main()
{
int x=1;
while(x<=5) { printf(“%d”,x); if(x>2)
goto here;
}
}
fun()
{
here:
printf(“\n Nilesh”);
}

ans:goto can not take control to different function.

5.point error if any
main()
{
x=4,y=2;
switch(x)
{
case 1:
printf(“\n To error is human”);
break;
case y:
printf(“\n don’t do it here’);
break;
}
}

ans:constant expression required in second case we cant use y.

6.point error if any
main()
{
int x=1;
switch(x)
{
case 1:
printf(“\n Hellow”);
break;
case 1*2+4:
printf(“\n the rock”);
break;
}
}

ans:no error constand expression like 1*2+4 are acceptable in cases of switch.


7.point out error if any
main()
{
int a=1;
switch(a)
{
{
printf(“\n Programmers don’t die. They just lost in the procressing”);
}

ans: no error but switch with no case is not required.

8.point out error if any.
Main()
{
int x=1;
switch(x)
{
printf(“Hellow’);
case 1:
printf(“\n Nilesh”);
break;
case 2;
printf(“\n Vivek’);
break;
}
}

ans:though there is no error ,the first printf statement can never be executed irrespective of the value of x . In other words all the statements in the switch have to belong to some case or other.

9.Rewrite the following set of statements using conditional operator.
Int a=1,b;
If(a>10)
B=20;

Ans: int a,b,dummy;
a>10?b=20:dummy=1;
note that the following would not work
a>10?b=20: ; ;

10.point out error if any.
Main()
{
int a=10,b;
a>=5?b=100:b=200;
printf(“%d”,b);
}

ans: lvalue required in function main().The second assignment should be written in the paranthesis as follows
a>=5?b=100b=200);


11. O/p?
main()
{
char str[]=”part-time musicians are semiconductors”;
int a=5;
printf(a>10?”%50s”:”%s”,str);
}

a. part-time musicians are semiconductors
b. part-time musicians are semiconductors
c. error
d. none of above

ans: a


12. What is more efficient a switch statement or an if-else chain?

Ans: There is hardly any difference in efficiency in both cases. But one should use switch where it can be because it is a cleaner way to program.


13.Can we us switch statement to switch between strings.

Ans:No. cases in switch must be either integer constants or constant expressions.


14. We want to test whether the value lies between 2 to 4 or 5 to 7. can we do this using switch.?

Ans:Yes. But the way is not practical if the ranges are bigger. It is as shown bellow:-
switch(a)
{
case 2:
case 3:
case 4:
/* some statements */
break;
case 5:
case 6:
case 7:
/* some statements */
break;
}


15. The way break is used to take control out of switch can continue be used to take the control to the beginning of the switch.

Ans:No. continue can work only with loops and not with switch.

No comments:

Post a Comment