Here is a listing of C language Objective Questions on “For Loops” along with answers, explanations and/or solutions:
1. The C code ‘for(;;)’ represents an infinite loop. It can be terminated by ___________
a) break
b) exit(0)
c) abort()
d) terminate
Answer: a
Clarification: None.
2. What will be the correct syntax for running two variable for loop simultaneously?
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
Clarification: None.
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
Clarification: 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
Answer: d
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
short i;
-
for (i = 1; i >= 0; i++)
-
printf("%dn", i);
-
-
}
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
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int k = 0;
-
for (k)
-
printf("Hello");
-
}
a) Compile time error
b) hello
c) Nothing
d) Varies
Answer: a
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int k = 0;
-
for (k < 3; k++)
-
printf("Hello");
-
}
a) Compile time error
b) Hello is printed thrice
c) Nothing
d) Varies
Answer: a
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
double k = 0;
-
for (k = 0.0; k < 3.0; k++)
-
printf("Hello");
-
}
a) Run time error
b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely
Answer: b
Clarification: None.