C  Programming Practice Question for GATE Exam




 Loops and Cotrols



1. Consider the C program below.

#include
int *A, stkTop;
int stkFunc (int opcode, int val)
{
static int size=0, stkTop=0;
switch (opcode)
{
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));
}
The value printed by the above program is ___________
(a) 9 (b) 10
(c) 15 (d) 17

Solution: Option (c)

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

Solution: Option (a)


3.

#include
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

Solution: Option (c)

4.

#include
int main()
{
int x = 3;
if (x == 2); x = 0;
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

Solution: Option (b)

5.

#include
int main()
{
int i = 3;
while (i--)
{
int i = 100;
i--;
printf("%d ", i);
}
4
return 0;
}
(a) Infinite loop (b) 99 99 99
(c) 99 98 97 (d) 2 2 2

Solution: Option (b)


6. How many times ravindra is printed?

#include
int main()
{
int i = -5;
while (i <= 5)
{
if (i >= 0)
break;
else
{
i++;
continue;
}
printf("ravindra");
}
return 0;
}
(a) 10 times (b) 5 times
(c) Infinite times (d) 0 times

Solution: Option (d)



7. Predict the output of the following program:

#include
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;
}

Solution: Option (d)



8. What is the output of the following program?

#include
int main()
{
char check = 'a';
switch (check)
{
case 'a' || 1: printf("Gates ");
case 'b' || 2: printf("Quiz ");
break;
default: printf("GatesQuiz");
}
return 0;
}
(a) Gates (b) Gates Quiz
(c) Gates Quiz GatesQuiz (d) Compile-time error

Solution: Option (d)

9. In the following program, X represents the Data Type of the variable check.

#include
int main()
{
X check;
switch (check)
{
// Some case labels
}
return 0;
}
Which of the following cannot represent X?
(a) int (b) char
(c) enum (d) float

Solution: Option (d)


10. Predict the output of the below program:

#include
int main()
{
int i = 3;
switch(i)
{
printf("Outside ");
7
case 1: printf("Gates");
break;
case 2: printf("Quiz");
break;
defau1t: printf("GatesQuiz");
}
return 0;
}
(a) Outside GatesQuiz
(b) GatesQuiz
(c) Nothing gets printed

Solution: Option (c)


11. What will be the output of the following C program segment? (GATE CS 2012)

char inchar = 'A';
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

Solution: Option (c)



12.

#include
int main()
{
int i;
for (i = 1; i != 10; i += 2)
printf(" GatesQuiz ");
return 0;
}
(a) GatesQuiz GatesQuiz GatesQuiz GatesQuiz GatesQuiz
(b) GatesQuiz GatesQuiz GatesQuiz …. infinite times
(c) GatesQuiz GatesQuiz GatesQuiz GatesQuiz
(d) GatesQuiz GatesQuiz GatesQuiz GatesQuiz GatesQuiz GatesQuiz

Solution: Option (b)



c programming practice questions for gate
                           


13. Output of following C program?
#include
int main()
{

int i = 0;
for (printf("1st\n"); i < 2 && printf("2nd\n"); ++i && printf("3rd\n"))
{
printf("*\n");
}
return 0;
}
(a) 1st (b) 1st
2nd 2nd
* *
3rd 3rd
2nd 2nd
* *
3rd
(c) 1st (d) 1st
2nd 2nd
3rd 3rd
* *
2nd 1st
3rd 2nd
3rd

Solution: Option (b)

14.
# include
int main()
10
{
int i = 0;
for (i=0; i<20 i="" p="">{
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

Solution: Option (c)


15. Output?


<20 i="" p="">#include
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
printf ("%d\n", no);
return 0;
}
(a) 1 (b) Runtime Error
(c) 0 (d) Compiler Error


<20 i="" p="">Solution: Option (b)


16.

#include
int main()
{
int n;
for (n = 9; n!=0; n--)
printf("n = %d", n--);
return 0;
}

What is the output?
(a) 9 7 5 3 1 (b) 9 8 7 6 5 4 3 2 1
(c) Infinite Loop (d) 9 7 5 3


<20 i="" p="">Solution: Option (c)

17.
#include
int i;
int main()
{
if (i);
else
printf("Ëlse");
return 0;
}
What is correct about the above program?
(a) if block is executed.
(b) else block is executed.
(c) It is unpredictable as i is not initialized.
(d) Error: misplaced else

Solution: Option (b)

18.

#include
int main()
{
int i;
if (printf("0"))
i = 3;
else
i = 5;
printf("%d", i);
return 0;
}
Predict the output of above program?
(a) 3 (b) 5
(c) 03 (d) 05

Solution: Option (c)


19. Predict the output of the below program:
#include
#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;
}
(a) Even (b) Odd
(c) Default (d) Compile-time error

Solution: Option (b)


20.

#include
int main()
{
int i = 3;
switch (i)
{
case 0+1: printf("Gates");
break;
case 1+2: printf("Quiz");
break;
default: printf("GatesQuiz");
}
return 0;
}
What is the output of the above program?
(a) Gates (b) Quiz
(c) GatesQuiz (d) Compile-time error

Solution: Option (b)

<20 i="" p="">
<20 i="" p="">