Tuesday 24 December 2013

Programming Org. Language


Programming Org. Language
1. Consider the following program written in C syntax:
void fun (int first, int second) {
first += second;
second += first;
}
void main () {
int list[2] = {6, 7};
fun (list[0], List [1]);
}
For each of following parameter-passing methods, what are the values of the list array after execution?
a. Passed by value
b. Passed by reference
c. Passed by value-result
2. Consider the following program written in C syntax:
#include <iostream>
typedef float(*pFooType)(float, float);
float A(float num1, float num2) {return num1 + num2;}
float B(float num1, float num2) {return num1 – num2;}
float C(float num1, float num2) {return num1 * num2;}
float D(float num1, float num2) {return num1 / num2;}
float Calc(float num1, float num2, float (*pCalcMethod)(float,
float))
{
return pCalcMethod(num1, num2);
}
int main()
{
pFooType pFoo[] = ;
float result;
for(int i = 0; i < 4; ++i)
{
result = Calc(10, 2, pFoo[i]);
std::cout << result << ‘‘, ‘‘;
}
return 0;
}
Fill out the blank in order to generate the output — 8, 12, 5, 20,
3. Show the stack with all activation record instances, including static and dynamic chains, when execution reaches position 1 in the following skeletal program. Assume Bigsub is at level 1.
procedure Bigsub is
MySum : Float;
procedure A is
X : Integer;
procedure B(Temp : Float) is
Y, Z : Float;
begin — of B
. . .
C(Z);
. . .
end; — of B
begin — of A
. . .
B(X);
. . .
end; — of A
procedure C(Plum1 : Float) is
begin — of C
. . . position 1
end; — of C
L : Float;
begin — of Bigsub
. . .
A;
. . .
end; — of Bigsub
Note that, B is nested in A
4. The following producer-consumer programs written in C.
Void producer(void){
int item;
while (TRUE) {
produce item(&item);
produce_item(&item);
if (count == N) sleep();
enter_item(item);
count = count + 1;
if (count == 1)
wakeup(consumer);
}
}
Suppose that the programs share items and work concurrently. This can result in both processes (producer and consumer) going to sleep. Explain what it is.
5. Consider the following program written in a pseudo-code.
int x = 10;
void a(){
x = x + x;
}
void b(){
c(a);
}
void c(d){
int x = 20;
d();
}
b();
print(x);
a) Suppose this program uses static scoping. What is the output of the program? Explain why?
b) Suppose this program uses dynamic scoping with deep binding. What is the output of the program? Explain why?
c) Suppose this program uses dynamic scoping with shallow binding. What is the output of the program? Explain why?
6. To reclaim garbage for heap management, there are two approaches: reference counters and mark-sweep. The mark-sweep garbage collection occurs more frequently than reference counters before memory is exhausted, resulting in more effective garbage collection. Explain the side-effect of mark-sweep
Note that, the question is the side-effect but not disadvantage.
7. The following program is written on the form of a selection guarded command
if i = 1 -> sum := sum + i
[ ] i > j -> sum := sum + j
[ ] j > i -> sum := sum + j
fi
print sum
a) Suppose the initial values: sum=0, i=1 and j=2. What is the output of this program?
b) Explain why

CLICK HERE FOR MORE ON THIS TOPIC

No comments:

Post a Comment