Question 57

Consider the following code segment in C++. How many times the for loop will be repeated?

Consider the following code segment in C++. How many times the for loop will be repeated? int main ( ) { int f = 1; for (; f ;) cout <<"f = " <<f++<<"\n"; return 0; }
  1. A10 times
  2. BNot even once
  3. CRepeated forever
  4. DOnly once
Show answerHide answer

Correct answer: (C) Repeated forever

Explanation

for(; f ;) has no update expression of its own, and f starts at 1 and is incremented each time by f++. f never becomes 0 in normal counting, so the condition stays true and the loop runs forever.

From DSSSB TGT Computer Science Practice Set 1

This is question 57 of DSSSB TGT Computer Science Practice Set 1. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.