ISRO Computer Science (CSE) 2013 - Question 7

All ISRO Computer Science (CSE) papers | ISRO Computer Science (CSE) 2013

Consider the following psuedocode: x : integer := 1; y : integer := 2; procedure add x := x + y; procedure second (P : procedure) x : integer := 2; P(); procedure first y : integer := 3; second(add); first(); write_integer(x). What does it print if the language uses dynamic scoping with deep binding?

  • A. 2
  • B. 3
  • C. 4
  • D. 5

Answer: A. 2

Explanation: Deep binding binds the environment at the time the procedure is passed as an argument. 'add' is passed in 'first'. In 'first', y=3. Global x=1. When 'second' calls P (which is add), it uses the environment from 'first' (where y=3) and the global x (because x is not redefined in first, but it is in second? Dynamic scoping uses the most recent x). With deep binding, the environment is frozen when 'add' is passed. Passed in 'first', so y=3. Where is x? 'add' modifies x. In dynamic scoping, x refers to caller's x? No, deep binding is usually associated with static scoping concepts or specific Lisp variants. However, standard interpretation: P is bound to 'add' inside 'first'. Environment has y=3. Global x=1. 'second' defines local x=2. P() is called. 'add' does x = x+y. x is 1 (global, as 'add' is defined globally). y is 3 (from 'first'). x becomes 4. write_integer(x) prints global x? No, it prints local x of main/global? If write_integer(x) is in main. Result depends heavily on specific definitions. ISRO key says 2.

| | All questions of ISRO Computer Science (CSE) 2013