While Loop MCQ in C

While loop in C is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

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

a)

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

b)

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

c)

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

d)

{
    //statements
}while(condition)
View Answer Answer:- c)

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

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

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

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

View Answer Answer:- b) NNNN

Since the condition contains “1” it means the condition will always be true. But inside the while loop, if statements check the value of “i”. When the value of “i” becomes 5, the “break” statement loop will be terminated.

Q3) Find the output of the given program?

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

a) NNNN
b) NNNNN
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- c) Compiled Successfully, No Output

The condition of the while loop is “0” which means false. Therefore statements inside while loop never got a chance to execute.

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

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

a) N
b) NN
c) NNN
d) Compiled Successfully, No Output
e) Compile-time error

View Answer Answer:- e) Compile-time error

The variable “i” is not declared before it’s used.

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

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

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

View Answer Answer:- d) Compile-time error

Here, “true” is an identifier, and it is not declared before it’s used.

Q6) Find the output of the given program?

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

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

View Answer Answer:- a) Know Program

The value of “a” is 3 therefore the condition becomes true. On the first iteration, the “break” statement was executed which terminated the loop.

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

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

a) Hello
b) Know Program
c) Hello Know Program
d) Compile-time error

View Answer Answer:- c) Hello Know Program

In the while condition, the value of a is assigned to 5 therefore condition will be always true. But in while block, “break” statement is called therefore on first iteration itself loop execution is completed. The remaining lines of code after the while loop will be executed.

Q8) Find the output of the given program?

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

a) Hello
b) Know Program
c) Hello Know Program
d) Compile-time error

View Answer Answer:- b) Know Program

The while loop condition a >= 5, 3 >= 5 becomes false. Therefore while loop never got a chance to execute. But remaining lines of code after the while loop will be executed.

Q9) Find the output of the given program?

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

a) 5
b) 5678
c) 56789
d) Compile-time error

View Answer Answer:- c) 56789

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

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

a) 5
b) 5678
c) 56789
d) Compile-time error

View Answer Answer:- a) 5

Q11) Find the output of the given program?

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

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

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

Due to condition “++a”, the value of a will be always incremented by 1 and it will be always > 0. Hence it becomes an infinite loop.

Q12) Find the output of the given program?

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

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

View Answer Answer:- b) Compiled Successfully, No Output

Since the condition of “a” is 0 which represents false. Hence while loop will never get a chance to execute.

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!

2 thoughts on “While Loop MCQ in C”

    1. No. In Question #10 there is a break statement inside the while loop but Question #9 doesn’t contain the break statement which makes difference in the output.

Leave a Comment

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