like

Tuesday, December 20, 2011

Control Funtion in C

1] What would be the output of the following program?

main()
{
int i = 4;
switch(i)
{
default:
printf("\n a mouse is an elephant built by the japanese.");

case 1:
printf("\n Breeding rabbits is a hare raising experience.");

case 2:
printf("\n Friction is drag.");

case3:
printf("\n If practice makes perfect, then nobody's perfect");
}
}


2] Point out the error, if any, in the for loop:

main()
{
int i = 1;
for( ; ; )
{
printf("%d",i++);
if (i > 10)
break;
}
}

OPTIONS:
(a) The condition in the for loop is a must.
(b) The two semicolons should be dropped.
(c) The for loop should be replaced by a while loop.
(d) No error.


3] Point out to the error, if any, in the while loop:

main()
{
int i = 1;
while()
{
printf("%d",i++);
if (i > 10)
break;
}
}

OPTIONS:
(a) The condition in the while loop is a must.
(b) Thereshould be a semicolons in the while loop.
(c) The while loop should be replaced by a for loop.
(d) No error.


4] Point out the error, if any, in the while loop:

main()
{
int i = 1;
while(i <= 5) { printf("%d",i); if( i > 2)
goto here;
}
}
fun()
{
here:
printf("\n If it works, Don't fix it.");
}


5] Point out the error, if any, in the following program:

main()
{
int i = 4, j = 2;
switch(i)
{
case 1:
printf("\n To err is human, to forgive is against company policy.");

case j:
printf("\n If you have nothing to do, don't do it here.");
break;
}
}


6] Point out the error, if any, in the following program:

main()
{
int i = 1;
switch(i)
{
case 1:
printf("\n Radioactive cats have 18 half-lives.");
break;

case 1*2+4:
printf("\n Bottle for rent-inquire within.");
break;
}
}


7] Point out the error, if any, in the following program:

main()
{
int i = 10;
switch(a)
{
}
printf("Programmers never die. They just get lost in the processing.");
}


8] Point out the error,if any, in the following program:

main()
{
int i = 1;
switch(i)
{
printf("Hello");

case 1:
printf("\n Individualists unite.");
break;

case 2:
printf("\n Money is the root of all wealth.");
break;
}
}


9] Rewrite the following set of statements using conditional operators:

int a = 1,b;
if(a > 10)
b = 20;


10] Point out the error, if any, in the following program:

main()
{
int a = 10, b;
a >= 5? b = 100: b = 200;
printf("\n %d",b);
}


11] What would be the output of the following program?

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

OPTIONS:
(a) Part-time musicians are semiconductors
(b) Part-time musicians are semiconductors
(c) Error
(d) None of the above


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


13] Can we use a 'switch' statement to switch on strings?


14] We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a 'switch'?


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








************************ ANSWERS ***********************


1] A mouse is an elephant built by the Japanese
Breeding rabbits is a hare raising experience


2] D


3] A


4] 'goto' cannot take control to a different function.


5] Constant expression required in the second case, we cannot use j.


6] No error. Constant exprerssions like 1*2+4 are acceptable in cases of a 'switch'.


7] Though never required, there can exist a 'switch which has no cases.


8] Though there is no error, irrespective of the value of i the first 'printf()' can never get executed. In other words, all statements in a switch have to beling to some case or the other.


9] int a = 1,b,dummy;
a > 10 ? b = 20: dummy = 1;

Note that the following would not have worked:
a > 10 ? b = 20: ; ;


10] 1value required in function 'amin()'. The second assignment should be written in parentheses as follows:

a >= 5 ? b = 100: (b = 200);


11] A


12] As far as eficiency is concerned there would hardly be any difference if at all. If the cases in a 'switch' are sparsely distributed the compiler may internally use the equivalent of an 'if-else' chain instead of a compact jump table. However, one should use 'switch' where one can. It is definitely a cleaner way to program and certainly is not any less efficient than the 'if-else' chain.


13] No. The cases in a 'switch' must either have integer constants or constant experssion.


14] Yes, though in a way which would not be very pratical if the ranges are bigger. The vay is ahown below:

switch(a)
{
case 2:
case 3:
case 4:
/* statements */
case 5:
case 6:
case 7:
/* some other statements */
break;
}


15] No. 'continue' can work only with loops and not with 'switch'.

No comments:

Post a Comment