----------------------------------C Interview Question--------------------------------------
CONTROL
INSTRUCTIONS.
1.what would be the o/p of the following program
void 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”);
}
}
Output:-
a mouse
a rabbit
2. point out error in for loop if any-
void 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.
Output:d
3. point error if any in the while loop
void 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.
Output:a
4.point out error if any
void main()
{
int x=1;
while(x<=5)
{
printf(“%d”,x);
printf(“%d”,x);
if(x>2)
goto here;
}
}
fun()
{
here:
printf(“\n Mukesh”);
}
Output:-goto can not take control to different function.
5.point error if any
void main()
{
x=4,y=2;
switch(x)
{
case 1:
case 1:
printf(“\n To error is human”);
break;
case y:
printf(“\n don’t do it here’);
break;
}
}
Output:constant expression required in second case we cant
use y.
6.point error if any
void main()
{
int x=1;
switch(x)
{
case 1:
case 1:
printf(“\n Hellow”);
break;
case 1*2+4:
printf(“\n the rock”);
break;
}
}
Output:-no error constant expression like 1*2+4 are
acceptable in cases of switch.
7.point out error if any
void main()
{
int a=1;
switch(a)
{
{
printf(“\n Programmers don’t die. They just lost in the procressing”);
{
printf(“\n Programmers don’t die. They just lost in the procressing”);
}
Output:- no error but switch with no case is
not required.
8.point out error if any.
void main()
{
int x=1;
switch(x)
{
printf(“Hello’);
printf(“Hello’);
case 1:
printf(“\n Mukesh”);
break;
case 2;
printf(“\n Rakesh’);
break;
}
}
Output:-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;
Output:-
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.
void main()
{
int a=10,b;
a>=5?b=100:b=200;
printf(“%d”,b);
}
Output: lvalue required in function
main().The second assignment should be written in the parenthesis as follows
(a>=5?b=100:b=200);
11.
void 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
Output: a
13. What is more efficient a switch
statement or an if-else chain?
Output: 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.
14.Can we us switch statement to
switch between strings.
Output: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.?
Output: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.
Output:No. continue can work only with
loops and not with switch.
No comments:
Post a Comment