C Programming Questions and Answers on While Loops for Freshers

https://www.computersprofessor.com/2017/11/c-programming-questions-and-answers-on_29.html
1. What is the output of this C code?
#include
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
b) After loop
c) Compile time error
d) Infinite loop
Answer: c
2. What is the output of this C code?
#include
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
b) In while loop
after loop
c) After loop
d) Infinite loop
Answer: b
3. What is the output of this C code?
#include
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
In while loop
In while loop
b) In while loop
In while loop
c) Depends on the compiler
d) Compile time error
Answer: a
4. How many times i value is checked in the below code?
#include
int main()
{
int i = 0;
do {
i++;
printf("in while loop\n");
} while (i < 3);
}
a) 2
b) 3
c) 4
d) 1
b) 3
c) 4
d) 1
Answer: b
5. How many times i value is checked in the below code?
#include
int main()
{
int i = 0;
while (i < 3)
i++;
printf("In while loop\n");
}
a) 2
b) 3
c) 4
d) 1
b) 3
c) 4
d) 1
Answer: c
6. What is the output of this C code?
#include
void main()
{
int i = 2;
do
{
printf("Hi");
} while (i < 2)
}
a) Compile time error
b) Hi Hi
c) Hi
d) Varies
b) Hi Hi
c) Hi
d) Varies
Answer: a
7. What is the output of this C code?
#include
void main()
{
int i = 0;
while (++i)
{
printf("H");
}
}
a) H
b) H is printed infinite times
c) Compile time error
d) Varies
b) H is printed infinite times
c) Compile time error
d) Varies
Answer: b
8. What is the output of this C code?
#include
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
b) H is printed infinite times
c) Hello
d) Run time error
Answer: c
9. What is the output of this C code?
#include
void main()
{
char *str = "";
do
{
printf("hello");
} while (str);
}
a) Nothing
b) Run time error
c) Varies
d) Hello is printed infinite times
b) Run time error
c) Varies
d) Hello is printed infinite times
Answer: d
10. What is the output of this C code?
#include
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
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
11. Example of iteration in C.
a) for
b) while
c) do-while
d) all of the mentioned
Answer: d
12. Number of times while loop condition is tested is, i is initialized to 0 in both case.
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
b) n, n+1
c) n+1, n
d) n+1, n+1
Answer: d
13. What is the output of this C code?
#include
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
b) True (1 time) False
c) False
d) Compiler dependent
Answer: c
14. What is the output of this C code?
#include
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
b) 5, 10
c) 10, 10
d) Syntax error
Answer: c
15. 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