0% found this document useful (0 votes)
31 views3 pages

Some Questions of First 2013-14

The code fragment implements dynamic scoping using deep access. When fun3 executes, the runtime stack will contain activation records for fun3, fun2, and fun1, with each frame containing the parameters, local variables, return address, and pointers to the dynamic links. The output is 8 using dynamic scoping due to variable bindings at runtime. Using static scoping, the output is 5 since bindings are determined at compile time. Deep access is preferred for separate name spaces while shallow access is preferred for efficiency.

Uploaded by

INDRANEEL GHOSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Some Questions of First 2013-14

The code fragment implements dynamic scoping using deep access. When fun3 executes, the runtime stack will contain activation records for fun3, fun2, and fun1, with each frame containing the parameters, local variables, return address, and pointers to the dynamic links. The output is 8 using dynamic scoping due to variable bindings at runtime. Using static scoping, the output is 5 since bindings are determined at compile time. Deep access is preferred for separate name spaces while shallow access is preferred for efficiency.

Uploaded by

INDRANEEL GHOSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Part A

Q1. The code fragment given below follow all the features and syntax of C programming language but support dynamic
scoping. Using the code argue that the use of dynamic scoping need dynamic typing instead of static typing.
(Answer to the point. Unnecessary explanation will not fetch marks). 2M

char x ; void q(void) main()


void p(void) { {
{ if (x = = "a") char* x = "a"; q();
printf("No error\n"); p(); return 0;
else printf("oops error\n"); } }
}

Q2. Given the C-based program fragment, Classify the variables in the program according to their lifetime. 2M

static int x; Variable Category


int m; 1. x
int fun(double a)
2
{ m
static int z = 0; 3.
int y[100] ; a
int *ptr = new (int); 4. z
static char *p= {"B","I","T","S"};
... 5. y
} 6. ptr
7 the anonymous object
to which ptr points.
8 p
9 the string BITS which
p is pointing to.

Q3. (Programming Language evaluation criteria) 2M

a. In C global variables need not be declared in the beginning of the program i.e., global variables can be declared
external to any function and are visible to all functions which follow its declaration. What property of programming
languages it satisfies and what property of programming language it violate.

b. If True and False are of Boolean type, True < False is legal statement in C but illegal in Java. Make two wise
comments on implementation of Boolean type in C and Java w.r.t properties of programming language.

Q4. Consider the code fragment written in some program language where parameters are passed by reference. The
removeDuplicates( ) function is intended to release the memory allocated for the duplicate variables. Make two
wise negative comment on the program. 2M

void driver( ) removeDuplicates(reference char a, reference char b)


{ {
char *a ={ "abcdefgh"}; //pointer to character
int i,j; if(a= =b)
int n=strlen(a); // length of string a {
for(i=0, j=n-1;i<n;i++) delete(b); //release the memory allocated
{ }
removeDuplicates(a[i],a[j]); }
j--;
}
}
Part B

Q1. Consider the program fragment which support dynamic scoping. Assume that execution of the
program will always start from driver( ) function. (Expected time 15 min. 2+2+2+2+1+3=12M)

void fun3( ) void fun2( ) void fun1( ) Global declare int min=0,a=9,c=8;
{ { { driver( )
min=(min<c)?min:c; declare int b=15; declare int b; {
} declare int c=3; a=6; declare int a=15,b=13,c=22;
min= (a<b)?a :b; b=1; fun1( );
fun3( ); c=5; Write( min );
} fun2( ); }
}

a. Show the run time stack with all activation record instances when fun3 is executing and dynamic
scoping is implemented using i. Deep access ii. Shallow access.
(Content of ARI should be arranged as follows: Return address, Dynamic Link, Static Link,
Parameter list, Local variables, Return value).
b. What is the output when dynamic scoping is used ? Justify.
c. What is the output when static scoping is used ? Justify.
d. When is shallow access implementation of dynamic scoping is preferred over deep access
implementation ?
e. If the above functions are nested as given below. Give the reference pair (chain offset, local
offset) for each variables at Position 1, Position 2 and Position 3 in the functions. (assume depth
of driver function is 1)

Global declare int min=0,a=9,c=8;


driver( )
{
declare int a=15,b=13,c=22;
void fun1( )
{
declare int b;
a=6; b=1; c=5; -----------------------> Position 1
void fun2( )
{
declare int b=15,c=3;
min= (a<b)? a : b;----------------------> Position 2

void fun3( )
{
min=(min<c)? min : c;--------> Position 3
}//endfun3
fun3( );
}//end fun2
fun2( );
}//end fun1
fun1( );
Write( min );
}
Q2. Consider the program fragment of programming language which support static scoping.
Assume that execution of the program will always start from driver function.
(Expected time 10 min. 2+ 4+2= 8M )
Global declare int n =6;
driver( )
{
declare int a[n], i, j;
for( i=n-1; i>=0; i--)
a[i]=i+1;
fun1( a[n-3], a[n-4], a[n-5]);
i=n;
fun2(i, a[i-2], a[i-3], a[i-4]);
Write( i );
for( i=n; i>=0; i--)
Write( a[i] );
}
void fun1( int p, int q, int r) void fun2(int x, int p, int q, int r)
{ {
int min; while(x>4)
n=n+2; {
min= (p<q) ? p : q; x=x-1;
min= (min<r) ? min : r; p= q+r;
Write( min ); q= q++;
} r=r++;
}
}

i. What is the output if parameters to fun1 are passed by name? Justify.


ii. What is the output if parameters to fun2
• are passed by value result and address binding take place at the time of call?
• are passed by value result and address binding take place at the time of return?
iii. What is the output of fun1 and fun2 if parameters are passed by reference?

You might also like