Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with those question with sample answer please!!. 1. Define parametri

ID: 3675718 • Letter: I

Question

I need help with those question with sample answer please!!.

1. Define parametric and subtype polymorphism. Explain the distinction between

explicit and implicit parametric polymorphism.Which is also known as

genericity?

2. What is an object closure? How is it related to a subroutine closure?

3. Explain the distinction between limited and unlimited extent of objects in a

local scope.

43. What are macros? What was the motivation for including them in C? What

Problems may they cause?

3.6 Consider the following pseudocode, assuming nested subroutines and static

scope.

procedure main

g : integer

procedure B(a : integer)

x : integer

procedure A(n : integer)

g := n

procedure R(m : integer)

write integer(x)

x /:= 2 – – integer division

if x > 1

R(m + 1)

else

A(m)

– – body of B

x := a × a

R(1)

– – body of main

B(3)

write integer(g)

(a) What does this program print?

(b) Show the frames on the stack when A has just been called. For each

frame, show the static and dynamic links.

(c) Explain how A finds g.

3.15 As noted in Section 3.6.3, C# has unusually sophisticated support for firstclass

subroutines. Among other things, it allows delegates to be instantiated

from anonymous nested methods, and gives local variables and parameters

unlimited extent when they may be needed by such a delegate. Consider the

implications of these features in the following C# program.

using System;

public delegate int UnaryOp(int n);

// type declaration: UnaryOp is a function from ints to ints

public class Foo {

static int a = 2;

static UnaryOp b(int c) {

int d = a + c;

Console.WriteLine(d);

return delegate(int n) { return c + n; };

}

public static void Main(string[] args) {

Console.WriteLine(b(3)(4));

}

}

What does this program print? Which of a, b, c, and d, if any, is likely to

be statically allocated?Which could be allocated on the stack?Which would

need to be allocated in the heap? Explain.

Explanation / Answer

1)
Parametric polymorphism:

Parametric Polymorphism is just another term for "Generics" in Java.
Parametric polymorphism is a programming language technique that enables the generic definition of functions and types, without a great deal of concern for type-based errors. It allows language to be more expressive while writing generic code that applies to various types of data. Functions written in context with parametric polymorphism work on various data types


Subtype Polymorphism:

>Subtype polymorphism is also called runtime polymorphism for a good reason. The resolution of polymorphic function calls happens at runtime through an indirection via the virtual table.
>Another way of explaining this is that compiler does not locate the address of the function to be called at compile-time, instead when the program is run, the function is called by dereferencing the right pointer in the virtual table
>Subtype polymorphism is exemplified by the fact that sort can take any sort of List an ArrayList, a LinkedList, etc

6)
a)
1. Variable g is declared as an integer
2. B(integer a = 3)
3. Variable x is declared as an integer
4. x : = a*a (x=9)
5. R(m = 1)

6. Print x = 9

7.x = 9/2 = 4
8. if (x = 4 > 1
9. R(m + 1 = 2)

10. Print x = 4

11. x = 4/2 = 2
12. if (x = 2 > 1)
13. R (m + 1 = 3)
14. Print x = 2
15. x = 2/2 = 1
16. if (x = 1 > 1)
17. else
18. A (m = 3)
19. g = 3

20.Print x = 3

Output
9 4 2 3
c)
It dereferences its static link to find the stack frame of B. Within this frame it finds B’s static link at a statically known offset. It dereferences that to find the stack frame of main, within which it finds g, again at a statically known offset