Prerequisite : for loop
Q.1 What is the output of this program?
Options
a) compilation error
b) Hello
c) infinite loop
d) none of the above
ans: c
Explanation : Putting a non zero value in condition part makes it infinite.
Q.2 What is the output of this program?
Options
Options:
a) 2 4 6
b) compilation error
c) garbage value
d) no output
ans : a
Explanation : After first iteration, program looks like (0, 2, 2). It breaks when i = 6.
Q.3 What is the output of this program?
Options
a) compilation error
b) can't be predicted
c) 8 5 2
d) none of the above
ans: c
Explanation :
At first iteration:
for(10; 9; fun()) //condition true
printf("%d", 8) //8 prints
At second iteration:
for(10; fun(); 7)
for(7; 6 ;fun()) //condition true
printf("%d", 5) //5 prints
At third iteration:
for(7; fun(); 4)
for(4; 3; fun()) //condition true
printf("%d", 2) //2 prints
At fourth iteration:
for(4; fun(); 1)
for(1; 0; fun()) //condition false
Program terminates
Q.4 What is the output of this program? Options
a) compilation error
b) run time error
c) 10
d) Infinite loop
ans : d
Explanation : Since no condition is provided so loop runs infinitely.
Q.5 What is the output of this program?
Options
a) 0
b) 1
c) Infinite loop
d) compilation error
ans: b
Explanation : The following condition fails for first time so loop terminates and value of i is incremented to 1.
for(; 0; printf("%d", i))
Q.6 What is the output of this program? Optionsa) error
b) 1, 3
c) program never ends
d) none of these
ans: c
Explanation :-
Considers two conditions:
(a)i<0 fails for first iteration
(b)5 in condition part makes it infinite loop as it never becomes 0.