Question 14

Consider the code segment written below in C++ : if (count < 10)//if#1

Consider the code segment written below in C++ : if (count < 10)//if#1 if ((count%4) ==2)//if#2 cout<<" condition;white/n"; else//(Indentation is wrong) cout<<" condition is:tan/n"; There are 2 if statements and one else. To which if, the else statement belong?
  1. AIt belongs to if # 1
  2. BIt belongs to if # 2
  3. CIt belongs to both the if statements.
  4. DIt is independent.
Show answerHide answer

Correct answer: (B) It belongs to if # 2

Explanation

In C++ an else pairs with the nearest preceding unmatched if (the dangling-else rule), whatever the indentation. So the else belongs to if#2.

From DSSSB TGT Computer Science Practice Set 3

This is question 14 of DSSSB TGT Computer Science Practice Set 3. 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.