0% found this document useful (0 votes)
22 views17 pages

Day 4-Looping Statements (Question+Answer)

The document contains a series of multiple-choice questions and answers related to loops in C programming, including for loops, while loops, and do-while loops. It covers topics such as syntax, output of code snippets, and the behavior of control flow statements like break and continue. Each question is followed by the correct answer, providing insights into common pitfalls and correct usage in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views17 pages

Day 4-Looping Statements (Question+Answer)

The document contains a series of multiple-choice questions and answers related to loops in C programming, including for loops, while loops, and do-while loops. It covers topics such as syntax, output of code snippets, and the behavior of control flow statements like break and continue. Each question is followed by the correct answer, providing insights into common pitfalls and correct usage in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

FOR LOOP, WHILE LOOP, DO-WHILE LOOP, BREAK, CONTINUE

1. The C code ‘for(;;)’ represents an infinite loop. It can be terminated by ___________

a) break

b) exit(0)

c) abort()

d) terminate

Answer: a

2. What will be the correct syntax for running two variables for loop simultaneously?

a)

for (i = 0; i < n; i++)

for (j = 0; j < n; j += 5)

b)

for (i = 0, j = 0; i < n, j < n; i++, j += 5)

c)

for (i = 0; i < n;i++){}

for (j = 0; j < n;j += 5){}

d) none of the mentioned

Answer: b

3. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?

a) for (i = n; i>0; i–)

b) for (i = n; i >= 0; i–)

c) for (i = n-1; i>0; i–)

d) for (i = n-1; i>-1; i–)


Answer: d

4. What will be the output of the following C code?

#include <stdio.h>

int main()

short i;

for (i = 1; i >= 0; i++)

printf("%d\n", i);

a) The control won’t fall into the for loop

b) Numbers will be displayed until the signed limit of short and throw a runtime error

c) Numbers will be displayed until the signed limit of short and program will successfully
terminate

d) This program will get into an infinite loop and keep printing numbers with no errors

Answer: c

5. What will be the output of the following C code?

#include <stdio.h>

void main()

int k = 0;

for (k)

printf("Hello");

a) Compile time error


b) hello

c) Nothing

d) Varies

Answer: a

6. What will be the output of the following C code?

#include <stdio.h>

void main()

int k = 0;

for (k < 3; k++)

printf("Hello");

a) Compile time error

b) Hello is printed thrice

c) Nothing

d) Varies

Answer: a

7. What will be the output of the following C code?

#include <stdio.h>

void main()

double k = 0;

for (k = 0.0; k < 3.0; k++)

printf("Hello");
}

a) Run time error

b) Hello is printed thrice

c) Hello is printed twice

d) Hello is printed infinitely

Answer: b

8. What will be the output of the following C code?

#include <stdio.h>

void main()

double k = 0;

for (k = 0.0; k < 3.0; k++);

printf("%lf", k);

a) 2.000000

b) 4.000000

c) 3.000000

d) Run time error

Answer: c

9. What will be the output of the following C code?

#include <stdio.h>

int main()

int i = 0;
for (; ; ;)

printf("In for loop\n");

printf("After loop\n");

a) Compile time error

b) Infinite loop

c) After loop

d) Undefined behaviour

Answer: a

10. What will be the output of the following C code?

#include <stdio.h>

int main()

int i = 0;

for (i++; i == 1; i = 2)

printf("In for loop ");

printf("After loop\n");

a) In for loop after loop

b) After loop

c) Compile time error

d) Undefined behaviour

Answer: a

11. What will be the output of the following C code?


#include <stdio.h>

int main()

while ()

printf("In while loop ");

printf("After loop\n");

a) In while loop after loop

b) After loop

c) Compile time error

d) Infinite loop

Answer: c

12. What will be the output of the following C code?

#include <stdio.h>

int main()

do

printf("In while loop ");

while (0);

printf("After loop\n");

a) In while loop

b)

In while loop
after loop

c) After loop

d) Infinite loop

Answer: b

13. What will be the output of the following C code?

#include <stdio.h>

int main()

int i = 0;

do {

i++;

printf("In while loop\n");

} while (i < 3);

a)

In while loop

In while loop

In while loop

b)

In while loop

In while loop

c) Depends on the compiler


d) Compile time error

Answer: a

14. How many times i value is checked in the following C code?

#include <stdio.h>

int main()

int i = 0;

while (i < 3)

i++;

printf("In while loop\n");

a) 2

b) 3

c) 4

d) 1

Answer: c

15. What will be the output of the following C code?

#include <stdio.h>

void main()

int i = 2;

do

printf("Hi");
} while (i < 2)

a) Compile time error

b) Hi Hi

c) Hi

d) Varies

Answer: a

16. What will be the output of the following C code?

#include <stdio.h>

void main()

int i = 0;

while (++i)

printf("H");

a) H

b) H is printed infinite times

c) Compile time error

d) Varies

Answer: b

17. What will be the output of the following C code?

#include <stdio.h>
void main()

int i = 0;

do

printf("Hello");

} while (i != 0);

a) Nothing

b) H is printed infinite times

c) Hello

d) Run time error

Answer: c

18. What will be the output of the following C code?

#include <stdio.h>

void main()

int i = 0;

while (i < 10)

i++;

printf("hi\n");

while (i < 8)

{
i++;

printf("hello\n");

a) Hi is printed 8 times, hello 7 times and then hi 2 times

b) Hi is printed 10 times, hello 7 times

c) Hi is printed once, hello 7 times

d) Hi is printed once, hello 7 times and then hi 2 times

Answer: d

19. What is an example of iteration in C?

a) for

b) while

c) do-while

d) all of the mentioned

Answer: d

20. How many times while loop condition is tested in the following C code snippets, if i is
initialized to 0 in both the cases?

while (i < n)

i++;

————-

do

i++;

while (i <= n);

a) n, n
b) n, n+1

c) n+1, n

d) n+1, n+1

Answer: d

21. What will be the output of the following C code?

#include <stdio.h>

int main()

int i = 0;

while (i = 0)

printf("True\n");

printf("False\n");

a) True (infinite time)

b) True (1 time) False

c) False

d) Compiler dependent

Answer: c

22. What will be the output of the following C code?

#include <stdio.h>

int main()

int i = 0, j = 0;

while (i < 5, j < 10)


{

i++;

j++;

printf("%d, %d\n", i, j);

a) 5, 5

b) 5, 10

c) 10, 10

d) Syntax error

Answer: c

23. Which loop is most suitable to first perform the operation and then test the condition?

a) for loop

b) while loop

c) do-while loop

d) none of the mentioned

Answer: c

24. What will be the output of the following C code?

#include <stdio.h>

int main()

int a = 0, i = 0, b;

for (i = 0;i < 5; i++)

{
a++;

continue;

a) 2

b) 3

c) 4

d) 5

Answer: d

25. What will be the output of the following C code?

#include <stdio.h>

int main()

int a = 0, i = 0, b;

for (i = 0;i < 5; i++)

a++;

if (i == 3)

break;

a) 1

b) 2

c) 3
d) 4

Answer: d

26. The keyword ‘break’ cannot be simply used within _________

a) do-while

b) if-else

c) for

d) while

Answer: b

27. Which keyword is used to come out of a loop only for that iteration?

a) break

b) continue

c) return

d) none of the mentioned

Answer: b

28. What will be the output of the following C code?

#include <stdio.h>

void main()

int i = 0, j = 0;

for (i = 0;i < 5; i++)

for (j = 0;j < 4; j++)

if (i > 1)
break;

printf("Hi \n");

a) Hi is printed 5 times

b) Hi is printed 9 times

c) Hi is printed 7 times

d) Hi is printed 4 times

Answer: a

29. What will be the output of the following C code?

#include <stdio.h>

void main()

int i = 0;

int j = 0;

for (i = 0;i < 5; i++)

for (j = 0;j < 4; j++)

if (i > 1)

continue;

printf("Hi \n");

}
}

a) Hi is printed 9 times

b) Hi is printed 8 times

c) Hi is printed 7 times

d) Hi is printed 6 times

Answer: b

30. What will be the output of the following C code?

#include <stdio.h>

void main()

int i = 0;

for (i = 0;i < 5; i++)

if (i < 4)

printf("Hello");

break;

a) Hello is printed 5 times

b) Hello is printed 4 times

c) Hello

d) Hello is printed 3 times

Answer: c

You might also like