➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
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) NNNNSince 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 OutputThe 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 errorThe 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 errorHere, “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 ProgramThe 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 ProgramIn 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 ProgramThe 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) 56789Q10) 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) 5Q11) 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 OutputSince 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!
Question #10 is the same as Question #9 if I’m not mistaken.
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.