Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the following program fragment

if(a > b)
if(b > c)
s1;
else s2;

s2 will be executed if

  • A.a <= b
  • B.b > c
  • C.b >= c and a <= b
  • D.a > b and b <= c

Answer: D

Correct answer: a > b and b <= c

An else attaches to the nearest unmatched if, whatever the indentation suggests - the dangling-else rule. So the else here belongs to if (b > c), not to the outer one. Reaching it therefore needs the outer condition to be true, \(a > b\), and the inner one false, \(b \le c\). If \(a \le b\) nothing at all is executed.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.