C Programming Questions and Answers – While Loops – 2

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “While Loops – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

advertisement

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *str = "";
  5.         do
  6.         {
  7.             printf("hello");
  8.         } while (str);
  9.     }

a) Nothing
b) Run time error
c) Varies
d) Hello is printed infinite times
View Answer

Answer: d
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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

Check this: C Books | BCA MCQs
advertisement
  1. #include <stdio.h>
  2. void main()
  3. {
  4.     int i = 0;
  5.     while (i < 10)
  6.     {
  7.         i++;
  8.         printf("hi\n");
  9.         while (i < 8) 
  10.         {
  11.             i++;
  12.             printf("hello\n");
  13.         }
  14.     }
  15. }

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
View Answer

Answer: d
Explanation: None.
advertisement

3. What is an example of iteration in C?
a) for
b) while
c) do-while
d) all of the mentioned
View Answer

Answer: d
Explanation: None.

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

advertisement
  1. while (i < n)
  2.          i++;
  3.     ————-
  4.     do
  5.          i++;
  6.     while (i <= n);

a) n, n
b) n, n+1
c) n+1, n
d) n+1, n+1
View Answer

Answer: d
Explanation: None.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         while (i = 0)
  6.             printf("True\n");
  7.         printf("False\n");
  8.     }

a) True (infinite time)
b) True (1 time) False
c) False
d) Compiler dependent
View Answer

Answer: c
Explanation: None.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0, j = 0;
  5.         while (i < 5, j < 10)
  6.         {
  7.             i++;
  8.             j++;
  9.         }
  10.         printf("%d, %d\n", i, j);
  11.     }

a) 5, 5
b) 5, 10
c) 10, 10
d) Syntax error
View Answer

Answer: c
Explanation: None.

7. 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
View Answer

Answer: c
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement

Leave a Comment

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.