C Programming Questions and Answers – For Loops – 1

«
»

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

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

advertisement

1. The C code ‘for(;;)’ represents an infinite loop. It can be terminated by ___________
a) break
b) exit(0)
c) abort()
d) terminate
View Answer

Answer: a
Explanation: None.

2. What will be the correct syntax for running two variable for loop simultaneously?
a)

advertisement
advertisement
   for (i = 0; i < n; i++)
   for (j = 0; j < n; j += 5)

b)

advertisement
for (i = 0, j = 0; i < n, j < n; i++, j += 5)

c)

advertisement
   for (i = 0; i < n;i++){}
   for (j = 0; j < n;j += 5){}

d) none of the mentioned
View Answer

Answer: b
Explanation: None.
advertisement

3. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–)
b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–)
View Answer

Answer: d
Explanation: None.

4. Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3)?
a) variable
b) function
c) typedef
d) macros
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.         short i;
  5.         for (i = 1; i >= 0; i++)
  6.             printf("%d\n", i);
  7.  
  8.     }

a) The control won’t fall into the for loop
b) Numbers will be displayed until the signed limit of short and throw a runtime error
c) Numbers will be displayed until the signed limit of short and program will successfully terminate
d) This program will get into an infinite loop and keep printing numbers with no errors
View Answer

Answer: c
Explanation: None.

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int k = 0;
  5.         for (k)
  6.             printf("Hello");
  7.     }

a) Compile time error
b) hello
c) Nothing
d) Varies
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int k = 0;
  5.         for (k < 3; k++)
  6.         printf("Hello");
  7.     }

a) Compile time error
b) Hello is printed thrice
c) Nothing
d) Varies
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         double k = 0;
  5.         for (k = 0.0; k < 3.0; k++)
  6.             printf("Hello");
  7.     }

a) Run time error
b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely
View Answer

Answer: b
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.