KEMBAR78
C Loops | PDF | Control Flow | Computer Programming
0% found this document useful (0 votes)
22 views16 pages

C Loops

Uploaded by

dhakshanms06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

C Loops

Uploaded by

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

1. What is the output of the following code?

#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
if (i % 2)
continue;
printf("%d ", i++);
}
return 0;
}
Options:
A) 0 2 4
B) 0 1 2 3 4
C) Infinite loop
D) No output

2. What will be printed?


#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++) {
switch (i) {
case 0: printf("A ");
case 1: printf("B "); break;
default: printf("C ");
}
}
return 0;
}
Options:
A) A B C
B) A B B C
C) A B C C
D) A B B C
3. What is the output?
#include <stdio.h>
int main() {
int x = 1;
do {
x += x;
} while (x++ < 10);
printf("%d", x);
return 0;
}
Options:
A) 8
B) 10
C) 11
D) 16

4. What does this code print?


#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i = 3)
printf("%d ", i);
}
return 0;
}
Options:
A) 0 1 2 3 4
B) 3
C) 3 3 3 ...
D) No output

5. What is the output?


#include <stdio.h>
int main() {
int i = 5;
while (i--) {
if (i == 2)
goto skip;
printf("%d ", i);
skip: ;
}
return 0;
}
Options:
A) 4 3 1 0
B) 4 3 2 1 0
C) 4 3 1 0
D) 5 4 3 1 0

6. What will be printed?


#include <stdio.h>
int main() {
for (int i = 0; ; ) {
if (i++ == 3) break;
printf("%d ", i);
}
return 0;
}
Options:
A) 0 1 2
B) 1 2 3
C) 1 2 3
D) Infinite loop

7. What is the output?


#include <stdio.h>
int main() {
int a = 1, b = 0;
if (a || (b = 2))
printf("%d ", b);
if (a && (b = 3))
printf("%d ", b);
return 0;
}
Options:
A) 0 3
B) 0 2
C) 0 3
D) 1 3

8. What does this code print?


#include <stdio.h>
int main() {
int i = 10;
do {
printf("%d ", i--);
} while (i > 5 ? 1 : 0);
return 0;
}
Options:
A) 10 9 8 7 6 5
B) 10 9 8 7 6
C) 10 9 8 7 6 5 4
D) Infinite loop

9. What is the output?


#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (i == j) break;
printf("%d%d ", i, j);
}
}
return 0;
}
Options:
A) 00 01 10 11 20 21
B) 01 10 20 21
C) 01 10 20
D) 00 01 10 20 21

10. What does this code print?


#include <stdio.h>
int main() {
int x = 0;
for (; x < 5; x++) {
if (x % 2 ? ++x : x++)
printf("%d ", x);
}
return 0;
}
Options:
A) 1 3 5
B) 1 3
C) 0 2 4
D) 2 4

11. What is the output?


#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
if (i++ == 3)
continue;
printf("%d ", i);
}
return 0;
}
Options:
A) 1 2 4 5 6
B) 2 3 4 5 6
C) 2 3 5 6
D) 1 2 3 4 5

12. What will be printed?


#include <stdio.h>
int main() {
for (int i = 0; i < 4; ++i) {
if (i & 1)
printf("%d ", i);
else
i += 2;
}
return 0;
}
Options:
A) 1 3
B) 1
C) 0 2
D) No output

13. What is the output?


#include <stdio.h>
int main() {
int x = 0;
do {
x = x++;
} while (x++ < 2);
printf("%d", x);
return 0;
}
Options:
A) 2
B) 3
C) 4
D) Infinite loop

14. What does this code print?


#include <stdio.h>
int main() {
int i = 5;
for (; i; i -= 2) {
if (i-- == 3)
break;
printf("%d ", i);
}
return 0;
}
Options:
A) 4 2
B) 4
C) 5 3
D) 5 3 1

15. What is the output?


#include <stdio.h>
int main() {
int a = 1, b = 0;
switch (a && b) {
case 0: printf("A ");
case 1: printf("B "); break;
default: printf("C ");
}
return 0;
}
Options:
A) A
B) B
C) A B
D) C

16. What will be printed?


#include <stdio.h>
int main() {
int i = 0;
while (i < 3) {
if (i++ == 1)
printf("%d ", i);
else
i++;
}
printf("%d", i);
return 0;
}
Options:
A) 1 3
B) 2 4
C) 2 4
D) 1 4

17. What is the output?


#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 2)
i += 2;
printf("%d ", i);
}
return 0;
}
Options:
A) 0 1 2 3 4
B) 0 1 4
C) 0 1 3 4
D) 0 1 2 4

18. What does this code print?


#include <stdio.h>
int main() {
int x = 1;
do {
switch (x) {
case 1: x++; continue;
case 2: x += 3; break;
default: x += 2;
}
printf("%d ", x);
} while (x < 10);
return 0;
}
Options:
A) 5 8 11
B) 5 8
C) 4 7 10
D) 4 7

19. What is the output?


#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (i == 1 && j == 1)
goto exit;
printf("%d%d ", i, j);
}
}
exit: printf("END");
return 0;
}
Options:
A) 00 01 10 11 20 21 END
B) 00 01 10 END
C) 00 01 10 END
D) 00 01 10 20 21 END

20. What does this code print?


#include <stdio.h>
int main() {
int i = 0;
while (1) {
if (i++ == 2)
break;
else if (i % 2)
continue;
printf("%d ", i);
}
return 0;
}
Options:
A) 1 2
B) 2
C) 2
D) No output

21. What is the output?


#include <stdio.h>
int main() {
int i = 0;
for (; i < 5; i++) {
if (i == 2)
i = i++ + 1;
printf("%d ", i);
}
return 0;
}
Options:
A) 0 1 3 4 5
B) 0 1 2 3 4
C) 0 1 4 5
D) 0 1 3 4

22. What will be printed?


#include <stdio.h>
int main() {
int x = 0;
while (x < 5) {
if (x++ < 3)
continue;
printf("%d ", x);
}
return 0;
}
Options:
A) 3 4 5
B) 4 5
C) 1 2 3 4 5
D) 3 4

23. What is the output?


#include <stdio.h>
int main() {
int i = 0;
do {
if (i == 1)
break;
else if (i == 0)
i += 2;
printf("%d ", i);
} while (i++ < 3);
return 0;
}
Options:
A) 2
B) 2 3
C) 0 2 3
D) 0 1 2

24. What does this code print?


#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (i == j)
continue;
printf("%d%d ", i, j);
}
}
return 0;
}
Options:
A) 00 01 10 11 20 21
B) 01 10 20 21
C) 01 10 20 21
D) 00 01 10 20 21

25. What is the output?


#include <stdio.h>
int main() {
int x = 5;
while (x--) {
if (x == 2)
goto skip;
printf("%d ", x);
skip: ;
}
return 0;
}
Options:
A) 4 3 1 0
B) 4 3 2 1 0
C) 4 3 1 0
D) 5 4 3 1 0

26. What will be printed?


#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
if (i % 2 == 0)
i += 2;
else
i++;
printf("%d ", i);
}
return 0;
}
Options:
A) 2 5
B) 2 5
C) 2 3 5
D) 2 4 6

27. What is the output?


#include <stdio.h>
int main() {
int a = 0, b = 0;
if (a = b + 1)
printf("A ");
else if (b = a + 1)
printf("B ");
else
printf("C ");
return 0;
}
Options:
A) A
B) B
C) C
D) A B

28. What does this code print?


#include <stdio.h>
int main() {
int i = 0;
for (; i < 5; i = i + 2) {
if (i % 2 == 0)
i--;
printf("%d ", i);
}
return 0;
}
Options:
A) -1 1 3
B) -1 1 3
C) 0 2 4
D) Infinite loop

29. What is the output?


#include <stdio.h>
int main() {
int x = 0;
do {
switch (x) {
case 0: x++; break;
case 1: x += 2; break;
default: x += 3;
}
printf("%d ", x);
} while (x < 5);
return 0;
}
Options:
A) 1 3 6
B) 1 3 6
C) 1 3 5
D) 0 1 3 6
Explanation:
o x=0 → case 0: x=1 → prints 1.
o x=1 → case 1: x=3 → prints 3.
o x=3 → default: x=6 → prints 6.
o Loop ends (x < 5 fails).

30. What does this code print?


#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (i == 1 && j == 1)
break;
printf("%d%d ", i, j);
}
if (i == 1)
continue;
printf("X ");
}
return 0;
}
Options:
A) 00 01 X 10 11 X 20 21 X
B) 00 01 X 10 20 21 X
C) 00 01 X 10 20 X
D) 00 01 10 20 21

You might also like