Principle of programming languages | Set 1

1

Following questions have been asked in GATE CS exam.

1.The most appropriate matching for the following pairs

X: Indirect addressing                  1: Loops
Y: Immediate addressing                 2: Pointers
Z: Auto decrement addressing            3. Constants

is (GATE CS 2000)
(a) X—3 Y—2 Z—1
(b) X—1 Y—3 Z—2
(c) X—2 Y—3 Z—1
(d) X—3 Y—1 Z—2

Answer (c)

Reference:
http://en.wikipedia.org/wiki/Addressing_mode
http://www.cs.nmsu.edu/~pfeiffer/classes/273/notes/immdirext.html

2. Aliasing in the context of programming languages refers to (GATE CS 2000)
(a) multiple variables having the same memory location
(b) multiple variables having the same value
(c) multiple variables having the same identifier
(d) multiple uses of the same variable

Answer (a)
Aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program.

Reference
http://en.wikipedia.org/wiki/Aliasing_%28computing%29


3. What is printed by the print statements in the program P1 assuming call by reference parameter passing? (GATE CS 2001)

Program Pl() 
{ 
  x=10; 
  y=3; 
  func1(y, x, x); 
  print x; 
  print y; 
}
func1 (x, y, z) 
{ 
   y = y + 4; 
   z = x + y + z; 
} 

a) 10, 3
b) 31, 3
c) 27, 7
d) None of the above

Answer (b)
Note the order in which parameters are passed. Inside func1(), x will actually refer to y of main(); and y and z will refer to x of main(). The Statement y = y + 4; will result in 14 and statement z = x + y + z will make z = 3 + 14 + 14 = 31 (because y and z point to same variable x of main). Since z refers to x of main(), main will print 31.


4. Consider the following program

Program P2 
    var n: int: 
     procedure W(var x: int) 
     begin 
         x=x+1; 
         print x;   
     end 

     procedure D 
     begin  
         var n: int; 
         n=3; 
         W(n);  
     end 
begin //beginP2 
  n=10; 
  D; 
end 

If the language has dynamic scoping and parameters are passed by reference, what will be printed by the program? (GATE CS 2001)
a) 10
b) 11
c) 3
d) None of the above

Answer(d)
Program will print 4.

5. The- results returned by functions under value-result and reference parameter passing conventions (GATE CS 2002)

a) Do not differ
b) Differ in the presence of loops
c) Differ in all cases
d) May differ in the presence of exceptions

Answer(d)
In call-by-reference evaluation, a function receives an implicit reference to the argument, rather than a copy of its value. This typically means that the function can modify the argument- something that will be seen by its caller. Note that C doesn’t support call by reference but call-by-reference can be implemented using pointers.

Call-by-value-result uses a combination of call-by-value and call-by-reference. Call-by-value-result works by creating local versions of the parameters passed in. However, the values in these local versions are copied back to the original arguments after the end of the procedure.

In case of exceptions, results may differ. Let us see below example in an arbitrary language

  int addTwo(a, b)
  {
     a = a + b;
     b = a + b;
     return b;
  }

If call-by-value-result is used then calling addTwo(x, x) will return 3x (see below for explanation).

     a = a + b; will result in a = x + x
     b = a + b; will result in  b = 2x + x 

If call-by-reference is used then addTwo(x, x) will return 4x (see below for explanation).

     a = a + b; will result in a = x + x
     b = a + b; will result in  b = 2x + 2x 

References:
http://c2.com/cgi/wiki?CallByValueResult
http://en.wikipedia.org/wiki/Evaluation_strategy



Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.

Please write comments if you find any of the above answers/explanations incorrect or you want to share more information about the topics discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.