GATE / CS ARCHIVE

GATE CSE 1995 / QUESTION 1.4

A dangling else under a false outer test

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 1995 paper

GATE 1995, Q1.4 1 mark · MCQ
Report issue

Parsing & Syntax Analysis

Question 1.4, GATE 1995: Pascal executes X:=−10; Y:=20; if X>Y then if X<0 then X:=abs(X) else X:=2*X. What is X?
Tap to enlarge
Your answer
Answer & solution

Answer: C: −10 (no official key was published)

The outer condition X>Y evaluates as −10>20, which is false. Its entire then statement, including the nested if and its else, is skipped. Therefore neither abs(X) nor doubling executes, and X stays −10. Pascal associates the else with the nearest unmatched if, namely the inner X<0 test. Treating that else as belonging to the outer test would incorrectly execute the doubling branch and obtain −20.

Original paper, page 1