In the following Pascal program segment, what is the value of X after the execution of the program segment?
X := -10; Y := 20; If X > Y then if X < 0 then X := abs(X) else X := 2*X;
View worked solution
Answer: C
Let's trace the execution of the given Pascal program segment step-by-step:
- Initialization:
- Conditional Evaluation:
The statement is:
If X > Y then if X < 0 then X := abs(X) else X := 2*X;We first evaluate the outer condition:
X > Y.Substituting the values:
- Execution Flow:
Since the outer
ifcondition isFalse, the control bypasses the entire nested statement (regardless of how theelseis paired due to the dangling-else rule).Consequently, no assignment to takes place.
Thus, the value of remains unchanged at its initial value, which is .
Therefore, the correct option is C.
Free practice · Your answers on this page are not saved.
