100% found this document useful (1 vote)
126 views11 pages

Loops and Controls: 1. Consider The C Program Below

The document contains 20 multiple choice questions related to C programming concepts like loops, control flow, switch statements, break and continue keywords, logical and bitwise operators. It tests understanding of how programs with these constructs will execute and output.

Uploaded by

sanskriti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
126 views11 pages

Loops and Controls: 1. Consider The C Program Below

The document contains 20 multiple choice questions related to C programming concepts like loops, control flow, switch statements, break and continue keywords, logical and bitwise operators. It tests understanding of how programs with these constructs will execute and output.

Uploaded by

sanskriti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LOOPS AND CONTROLS

1. Consider the C program below.

#include <stdio.h>

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

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

(a) Infinite loop (b) 99 99 99


(c) 99 98 97 (d) 2 2 2

6. How many times ravindra is printed?

#include<stdio.h>

int main()
{
int i = -5;
while (i <= 5)
{
if (i >= 0)
break;
else

3
{
i++;
continue;
}
printf("ravindra");
}
return 0;
}

(a) 10 times (b) 5 times


(c) Infinite times (d) 0 times

7. Predict the output of the following program:

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

8. What is the output of the following program?

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

(a) Gates (b) Gates Quiz


(c) Gates Quiz GatesQuiz (d) Compile-time error

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

Which of the following cannot represent X?

(a) int (b) char


(c) enum (d) float

10. Predict the output of the below program:

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

(a) Outside GatesQuiz


(b) GatesQuiz
(c) Nothing gets printed

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

12.

#include <stdio.h>

int main()
{
int i;
for (i = 1; i != 10; i += 2)

6
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

13. Output of following C program?

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

(a) 1st (b) 1st


2nd 2nd
* *
3rd 3rd
2nd 2nd
* *
3rd

(c) 1st (d) 1st


2nd 2nd
3rd 3rd
* *
2nd 1st
3rd 2nd
3rd

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
}

(a) 1 (b) Runtime Error


(c) 0 (d) Compiler Error

16.

#include<stdio.h>

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

17.

#include <stdio.h>

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

9
18.

#include <stdio.h>

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

19. Predict the output of the below program:

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

(a) Even (b) Odd


(c) Default (d) Compile-time error

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

What is the output of the above program?

(a) Gates (b) Quiz


(c) GatesQuiz (d) Compile-time error

11

You might also like