Do While Loop MCQ in C

The do-while loop in C is very closely related to the while loop. The do keyword is placed on a line of code at the top of the loop. A block of statements follows it with a test expression after the keyword while, at the bottom of the loop.

It is a post-test loop. The post-test loop executes at least, only then control expression is tested. If the expression is true, the loop repeats. When the expression is false then the loop terminates.

Q1) Choose the correct syntax of the do-while loop in C language?

a)

do
{
    // statement(s)
}while(condition)

b)

do while(condition)
{
    //statement(s)
}

c)

do
{
    //statement(s)
}while(condition);

d)

dowhile(condition)
{
    //statement(s)
}
View Answer Answer:- C)

do
{
//statement(s)
}while(condition);

Q2) Find the output of the given program?

#include<stdio.h>
int main()
{
  do
  {
    printf("Know Program");
  } while();
  return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- d) Compile-time error

We must use conditional statements with for loop. Here we will get a compile-time error: expected expression before ‘)’ token

Q3) Find the output of the given program?

#include<stdio.h>
int main()
{
  int a = 0;
  do
  {
    printf("Know Program");
  } while(a);
  
  return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- a) Know Program

The statements inside the do block will be executed. In “while” the condition is a=0 which represents false therefore condition becomes false and do-while execution is ended.

Q4) What is the output of the given below program?

#include<stdio.h>
int main()
{
  int a = 0;
  do
  {
    printf("Know Program");
  } while(++a);
  
  return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- b) Know Program is printed infinite times.

Before every condition checking a value is increment by 1. Hence a value will be always > 0 which means the condition is true therefore it becomes an infinite loop.

Q5) Find the output of the given program?

#include<stdio.h>
int main()
{
  int a = 1;
  do
  {
    printf("N");
    a++;
  } while(a <= 5);
  
  return 0;
}

a) N
b) NNNN
c) NNNNN
d) Compile-time error

View Answer Answer:- c) NNNNN

Here, the do-block will be executed until a <= 5. Inside do-block on each iteration, a value is incremented by 1. After 5 iterations the value of “a” becomes 6 and the condition becomes false.

Q6) What is the output of the given below program?

#include<stdio.h>
int main()
{
  int a = 1;
  do
  {
    printf("N");
    a++;
  } while(a = 5);
  
  return 0;
}

a) N
b) NNNNN
c) N is printed infinite times.
d) Compile-time error

View Answer Answer:- c) N is printed infinite times.

In each iteration “a” value is assigned to 5 through the while-condition therefore condition becomes true. It is also an example of an infinite do-while loop in C.

Q7) Find the output of the given below program?

#include<stdio.h>
int main()
{
  int a = 9;
  do
  {
    printf("%d ", a);
    a++;
  } while(a <= 5);
  
  return 0;
}

a) 9
b) 98765
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- a) 9

Initially, the value of a is 9. After executing do-block value of a becomes 10. In while-condition a <= 5, 10 <= 5 is false. Hence do-block will be executed only once.

Q8) Find the output of the given program?

#include<stdio.h>
int main()
{
  do
  {
    printf("Know Program");
  } while(true);
  
  return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- d) Compile-time error

Here, “true” is considered as an identifier. Since it is not declared before use therefore we get a compile-time error:- ‘true’ undeclared (first use in this function)

Q9) Find the output of the given program?

#include<stdio.h>
int main()
{
  int a = 1;
  do
  {
    printf("N");
    a++;
    if(a==3) break;
  } while(a);
  
  return 0;
}

a) N
b) NN
c) NNN
d) N is printed infinite times.
e) Compile-time error

View Answer Answer:- b) NN

When N becomes 3 then due to the “break” statement loop is terminated.

Q10) What is the output of the given below program?

#include<stdio.h>
int main()
{
  int a = 5;
  do
  {
    printf("Know Program");
    break;
  } while(a);
  
  return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- a) Know Program

In the first iteration itself, the “break” statement is executed and do-while loop execution is terminated.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *