Loops and Controls: 1. Consider The C Program Below
Loops and Controls: 1. Consider The C Program Below
#include <stdio.h>
{
case -1:
size = val;
break;
case 0:
if (stkTop < size ) A[stkTop++]=val;
break;
default:
if (stkTop) return A[--stkTop];
}
return -1;
}
int main()
{
int B[20];
A=B;
stkTop = -1;
stkFunc (-1, 10);
stkFunc (0, 5);
stkFunc (0, 10);
printf ("%d\n", stkFunc(1, 0)+ stkFunc(1, 0));
}
(a) 9 (b) 10
(c) 15 (d) 17
1
2. Which combination of the integer variables x, y and z makes the variable a get the value 4 in
the following expression?
a = ( x > y ) ? (( x > z ) ? x : z) : (( y > z ) ? y : z )
(a) x = 3, y = 4, z = 2 (b) x = 6, y = 5, z = 3
(c) x = 6, y = 3, z = 5 (d) x = 5, y = 4, z = 5
3.
#include<stdio.h>
int main()
{
int a = 5;
switch(a)
{
default:
a = 4;
case 6:
a--;
case 5:
a = a+1;
case 1:
a = a-1;
}
printf("%d \n", a);
return 0;
}
(a) 3 (b) 4
(c) 5 (d) None of these
4.
#include <stdio.h>
int main()
{
int x = 3;
if (x == 2); x = 0;
2
if (x == 3) x++;
else x += 2;
printf("x = %d", x);
return 0;
}
(a) x = 4 (b) x = 2
(c) Compiler Error (d) x = 0
5.
#include <stdio.h>
int main()
{
int i = 3;
while (i--)
{
int i = 100;
i--;
printf("%d ", i);
}
return 0;
}
#include<stdio.h>
int main()
{
int i = -5;
while (i <= 5)
{
if (i >= 0)
break;
else
3
{
i++;
continue;
}
printf("ravindra");
}
return 0;
}
#include <stdio.h>
int main()
{
int check = 20, arr[] = {10, 20, 30};
switch (check)
{
case arr[0]: printf("Gates ");
case arr[1]: printf("Quiz ");
case arr[2]: printf("GatesQuiz");
}
return 0;
}
#include <stdio.h>
int main()
{
char check = 'a';
switch (check)
{
case 'a' || 1: printf("Gates ");
case 'b' || 2: printf("Quiz ");
break;
4
default: printf("GatesQuiz");
}
return 0;
}
9. In the following program, X represents the Data Type of the variable check.
#include <stdio.h>
int main()
{
X check;
switch (check)
{
// Some case labels
}
return 0;
}
#include <stdio.h>
int main()
{
int i = 3;
switch(i)
{
printf("Outside ");
case 1: printf("Gates");
break;
case 2: printf("Quiz");
break;
5
defau1t: printf("GatesQuiz");
}
return 0;
}
11. What will be the output of the following C program segment? (GATE CS 2012)
switch (inchar)
{
case 'A' :
printf ("choice A \n") ;
case 'B' :
printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
printf ("No Choice") ;
}
(a) No choice
(b) Choice A
(c) Choice A
Choice B No choice
(d) Program gives no output as it is erroneous
12.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i != 10; i += 2)
6
printf(" GatesQuiz ");
return 0;
}
#include<stdio.h>
int main()
{
int i = 0;
for (printf("1st\n"); i < 2 && printf("2nd\n"); ++i && printf("3rd\n"))
{
printf("*\n");
}
return 0;
7
14.
# include <stdio.h>
int main()
{
int i = 0;
for (i=0; i<20; i++)
{
switch(i)
{
case 0:
i += 5;
case 1:
i += 2;
case 5:
i += 5;
default:
i += 4;
break;
}
printf("%d ", i);
}
return 0;
}
(a) 5 10 15 20 (b) 7 12 17 22
(c) 16 21 (d) Compiler Error
15. Output?
#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
printf ("%d\n", no);
return 0;
8
}
16.
#include<stdio.h>
int main()
{
int n;
for (n = 9; n!=0; n--)
printf("n = %d", n--);
return 0;
}
(a) 9 7 5 3 1 (b) 9 8 7 6 5 4 3 2 1
(c) Infinite Loop (d) 9 7 5 3
17.
#include <stdio.h>
int i;
int main()
{
if (i);
else
printf("Ëlse");
return 0;
}
9
18.
#include <stdio.h>
int main()
{
int i;
if (printf("0"))
i = 3;
else
i = 5;
printf("%d", i);
return 0;
}
(a) 3 (b) 5
(c) 03 (d) 05
#include <stdio.h>
#define EVEN 0
#define ODD 1
int main()
{
int i = 3;
switch (i & 1)
{
case EVEN: printf("Even");
break;
case ODD: printf("Odd");
break;
default: printf("Default");
}
return 0;
}
10
20.
#include <stdio.h>
int main()
{
int i = 3;
switch (i)
{
case 0+1: printf("Gates");
break;
case 1+2: printf("Quiz");
break;
default: printf("GatesQuiz");
}
return 0;
}
11