C Programming Questions and Answers on For Loop for Freshers

1. The following code ‘for(;;)’ represents an infinite loop. It can be terminated by.

a) break
b) exit(0)
c) abort()
d) all of the mentioned
Answer: a
2. The correct syntax for running two variable for loop simultaneously is.

a) for (i = 0; i < n; i++)
     for (j = 0; j < n; j += 5)
b) for (i = 0, j = 0;i < n, j < n; i++, j += 5)
c) for (i = 0; i < n;i++){}
    for (j = 0; j < n;j += 5){}
d) None of the mentioned
Answer: b
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–)
Answer: d
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
Answer: d
5. What is the output of this C code?

  1.     #include 
  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
Answer: c
6. What is the output of this C code?

  1.     #include 
  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
Answer: a
7. What is the output of this C code?

  1.     #include 
  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
Answer: a
8. What is the output of this C code?

  1.     #include 
  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
Answer: b
9. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         double k = 0;
  5.         for (k = 0.0; k < 3.0; k++);
  6.             printf("%lf", k);
  7.     }
a) 2.000000
b) 4.000000
c) 3.000000
d) Run time error
Answer: c
10. What is the output of this C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int k;
  5.         for (k = -3; k < -5; k++)
  6.             printf("Hello");
  7.     }
a) Hello
b) Infinite hello
c) Run time error
d) Nothing
Answer: d
11. What is the output of this C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         for (; ; ;)
  6.             printf("In for loop\n");
  7.             printf("After loop\n");
  8.     }
a) Compile time error
b) Infinite loop
c) After loop
d) Undefined behaviour
Answer: a
12. What is the output of this C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         for (i++; i == 1; i = 2)
  6.             printf("In for loop ");
  7.             printf("After loop\n");
  8.     }
a) In for loop after loop
b) After loop
c) Compile time error
d) Undefined behaviour
Answer: a
13. What is the output of this C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         for (foo(); i == 1; i = 2)
  6.             printf("In for loop\n");
  7.             printf("After loop\n");
  8.     }
  9.     int foo()
  10.     {
  11.         return 1;
  12.     }
a) After loop
b) In for loop after loop
c) Compile time error
d) Infinite loop
Answer: a
14. What is the output of this C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int *p = NULL;
  5.         for (foo(); p; p = 0)
  6.             printf("In for loop\n");
  7.             printf("After loop\n");
  8.     }
a) In for loop after loop
b) Compile time error
c) Infinite loop
d) Depends on the value of NULL
Answer: b
15. What is the output of this C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         for (int i = 0;i < 1; i++)
  5.             printf("In for loop\n");
  6.     }
a) Compile time error
b) In for loop
c) Depends on the standard compiler implements
d) Depends on the compiler
Answer: c

Related

Multiple Choice Questions 7176473935236152027

Post a Comment

emo-but-icon

item