0% found this document useful (0 votes)
8 views19 pages

Dynamic Scoping and Recursive Functions

Uploaded by

oomgupta99
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)
8 views19 pages

Dynamic Scoping and Recursive Functions

Uploaded by

oomgupta99
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

Day-2

1. For the program given below what will be printed by the write statement marked (1) and (2) in the program if the
variable are dynamically scoped?

Varx,y :integer;

procedure P(n:integer);
begin
x:=(n+2)/(n-3)
end;

procedure Q
Varx,y :integer;
begin
x:=3;
y:=4;
P(y);
write (x)--------(1)
end;
begin
x:=7;
y:=8;
Q:
Write (x);------------(2)
end.

A) 3, 6
B) 6, 7
C) 3, 7
D) None of the above

Answer B

2. Consider the following recursive definition of fib:

fib (n) := if n=0 then 1


else if n=1 then 1
else fib(n-1)+fib(n-2)

The number of times fib is called (including the first call) for an evaluation of fib(7) is

A) 40
B) 41
C) 42
D) 39

Answer B

3. What will be the output of the following procedure if n=3

procedure count(Integer n) {

static d=1;

print(n);
print(d);
d++;
if(n>1) count(n-1);
print(d);
}

A) 3 1 2 2 1 3 4 4 4
B) 3 1 2 1 1 1 2 2 2
C) 3 1 2 2 1 3 4
D) 3 1 2 1 1 1 2

Answer A

4. What does the following function compute in terms of n and d, for integer values of n and d, d > 1? Note that a//b
denotes the quotient
(integer part) of a ÷ b, for integers a and b. For instance 7//3 is 2.

function foo(n,d){
x := 0;
while (n >= 1) {
x := x+1;
n := n//d;
}
return(x);
}

(a) The number of ways of choosing d elements from a set of size n.


(b) The number of ways of rearranging d elements from a set of size n.
(c) The number of digits in the base d representation of n.
(d) The number of ways of partitioning n elements into groups of size d.

Answer: (c)

5. What is the output of the following program?

intfuncf (int x);


intfuncg (int y);

main()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count)
{
y += funcf(x) + funcg(x);
printf ("%d ", y);
}
}

funcf(int x)
{
int y;
y = funcg(x);
return (y);
}

funcg(int x)
{
staticint y = 10;
y += 1;
return (y+x);
}

A) 43 80
B) 42 74
C) 33 37
D) 32 32

Answer A

6. Let x be an integer which can take a value of 0 or 1. The statement if(x = =0) x = 1; else x = 0; is equivalent to which one of
the following?

A) x = 1 + x;
B) x = 1 - x;
C) x = x - 1;
D) x = 1 % x;

Answer B

7. Let a and b be two sorted arrays containing n integers each, in non-decreasing order. Let c be a sorted array containing
2n integers obtained by merging the two arrays a and b. Assuming the arrays are indexed starting from 0, consider the
following four statements

I a[i] ≥ b [i] =>c[2i] ≥ a [i]


II a[i] ≥ b [i] =>c[2i] ≥ b [i]
III a[i] ≥ b [i] => c[2i] ≤ a [i]
IV a[i] ≥ b [i] =>c[2i] ≤ b [i]

Which of the following is TRUE?

A) only I and II
B) only I and IV
C) only II and III
D) only III and IV

Answer C

8. The following function computes the value of mCn correctly for all legal values m and n (m≥1,n≥0 and m>n)

intfunc(int m, int n)
{
if (E) return 1;
else return(func(m -1, n) + func(m - 1, n - 1));
}

In the above function, which of the following is the correct expression for E?

A) (n == 0) || (m == 1)
B) (n == 0) && (m == 1)
C) (n == 0) || (m == n)
D) (n == 0) && (m == n)
Answer C

9. Consider the program below in a hypothetical programming language which allows global variables and a choice of static
or dynamic scoping.

int i ;
program main ()
{
i = 10;
call f();
}

procedure f()
{
int i = 20;
call g ();
}
procedure g ()
{
print i;
}

Let x be the value printed under static scoping and y be the value printed under dynamic scoping. Then, x and y are

A) x = 10, y = 10
B) x = 20, y = 10
C) x = 10, y = 20
D) x = 20, y = 20

Answer C

10. Consider the following functions f() and g()


f(){ g(){
w = 5; z = w+1;
w = 2*z + 2; z = 3*z - w;
} print(z);
}

We start with w and z set to 0 and execute f() and g() in parallel—that is, at each step we either execute one statement
from f() or one statement from g(). Which of the following is not a possible value printed by g()?

A) -2
B) -1
C) 2
D) 4

Answer: C

11. What does the following function compute in terms of n and d, for integer values of d?
Note that the operation / denotes floating point division, even if the arguments are both integers.

function foo(n,d){
if (d == 0){
return 1;
}else{
if (d < 0){
return foo(n,d+1)/n;
}else{
return n*foo(n,d-1);
}}}

A) logd n if d < 0, nd if d > 0.


B) nd for all values of d.
C) n × d if d > 0, n ÷ d if d < 0.
D) n × d for all values of d.

Answer: B

12. In the code fragment to the right, start


and end are integer values and prime(x) is a i := 0; j := 0; k := 0;
function that returns true if x is a prime for (m := start;
number and false otherwise. m <= end;
m := m+1){
if (prime(m)){
i := i + m;
k := k - m;
}else{
j := j - m;
k := k + m;
}
At the end of the loop: }
A) k == i-j.
B) k == j-i.
C) k == -j-i.
D) Depends on start and end.

Answer C

13. How many times is the comparison i ≥ n performed in the following program?

int i=85, n=5;


main() {
while (i >= n) {
i=i-1;
n=n+1;
}
}

A) 40
B) 41
C) 42
D) 43

Answer: C

14. Consider the code below, defining the function mystery:

mystery(a,b){
if (a < 0 or b < 0) return 0;
else if (a == 0) return b+1;
else if (b == 0) return mystery(a-1,1);
else return mystery(a-1, mystery(a,b-1));
}
Express mystery(1, n) as a function of n

A) n+2
B) n+1
C) n
D) n+3

Answer A

15. Consider the code below:

procedure mystery (A : array [1..100] of int)


inti,j,position,tmp;
begin
for j := 1 to 100 do
position := j;
for i := j to 100 do
if (A[i] > A[position]) then
position := i;
endfor
tmp := A[j];
A[j] := A[position];
A[position] := tmp;
endfor
end
When the procedure terminates, the array A has been:

A) Reversed
B) Left unaltered
C) Sorted in descending order
D) Sorted in ascending order

Answer C

16. Consider the code below:

procedure mystery (A : array [1..100] of int)


inti,j,position,tmp;
begin
for j := 1 to 100 do
position := j;
for i := j to 100 do
if (A[i] > A[position]) then
position := i;
endfor
tmp := A[j];
A[j] := A[position];
A[position] := tmp;
endfor
end

The number of times the test A[i] > A[position] is executed is:
A) 100
B) 5050
C) 10000
D) Depends on contents of A
Answer: B

17. In the code fragment on i := 0; j := 0; k := 0;


the right, start and end for(m :=start;m<= end;m := m+1){
are integer values and k := k + m;
prime(x) is a function if (prime(m)){
that returns true if x is i := i + m;
a prime number and false }else{
otherwise. j := j + m;
}
At the end of the loop: }

A) k<i+j
B) k = i+j
C) k>i+j
D) Depends on start and end

Answer: B

18. Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or
call by value methods of parameter passing.

int i ;

program main ()
{
int j = 60;
i = 50;
call f (i, j);
print i, j;
}

procedure f (x, y)
{
i = 100;
x = 10;
y=y+i;
}

Which one of the following options represents the correct output of the program for the two parameter passing
mechanisms?

A) Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70


B) Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70
C) Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60
D) Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70

Answer D

19. Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression?
a = (x > y)? ((x > z)? x : z): ((y > z)? y : z)

A) x = 3, y = 4, z = 2
B) x = 6, y = 5, z = 3
C) x = 6, y = 3, z = 5
D) x = 5, y = 4, z = 5
Answer A

20. What is the value printed by the following program

int f(int *a, int n)


{
if(n <= 0) return 0;
else if(*a % 2 == 0) return *a + f(a+1, n-1);
else return *a - f(a+1, n-1);
}

int main()
{
int a[] = {12, 7, 13, 4, 11, 6};
printf("%d", f(a, 6));
getchar();
return 0;
}

A) -9
B) 5
C) 15
D) 19

Answer C

21. Consider the following C program:

#include <stdio.h>

int counter = 0;

intcalc (int a, int b) {


int c;

counter++;
if (b==3) return (a*a*a) ;
else {
c = calc (a, b/3) ;
return (c*c*c) ;
}
}

int main () {
calc (4, 81);
printf ("%d", counter) ;
}

The output of this program is ______.

A) 4
B) 5
C) 6
D) 7
Answer A

22. Consider the following C program.

#include <stdio.h>
structOutnode {
char x, y, z ;
};

int main () {
structOurnode p = {'1', '0', 'a'+2} ;
structOurnode *q = &p ;
printf ("%c, %c", *((char*)q+1), *((char*)q+2)) ;
return 0 ;
}
The output of this program is

A) 0, c
B) 0, a+2
C) '0', 'a+2'
D) '0', 'c'

Answer A

23. Consider the following C code. Assume that unsigned long int type length is 64 bits.

unsigned long int fun(unsigned long int n) {


unsigned long int i, j, j=0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return sum;
}
The value returned when we call fun with the input 2^40 is
A) 4
B) 5
C) 6
D) 40

Answer B

24. Consider the following program written in pseudo-code. Assume that x and y are integers.

Count (x, y) {
if (y != 1) {
if (x != 1) {
print("*") ;
Count (x/2, y) ;
}
else {
y=y-1;
Count (1024, y) ;
}
}
}
The number of times that the print statement is executed by the call Count(1024, 1024) is ______.

A) 10230
B) 10231
C) 10232
D) 10233

Answer A

25. Consider the following two functions.

void fun1(int n) { void fun2(int n) {


if(n == 0) return; if(n == 0) return;
printf("%d", n); printf("%d", n);
fun2(n - 2); fun1(++n);
printf("%d", n); printf("%d", n);
} }
The output printed when fun1(5) is called is

A) 53423122233445
B) 53423120112233
C) 53423122132435
D) 53423120213243

Answer A

26. Consider the following function implemented in C:

voidprintxy (int x, int y) {


int *ptr;
x = 0;
ptr = &x;
y = *ptr;
*ptr = 1;
printf("%d,%d",x,y);
}
The output of invoking printxy(1, 1) is

A) 0, 0
B) 0, 1
C) 1, 0
D) 1, 1

Answer C

27. Consider the C program fragment below which is meant to divide x and y using repeated subtractions.

The variables x, y, q and r are all unsigned int.


while (r >= y) {
r = r - y;
q = q + 1;
}

Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure
that the loop terminates in a state satisfying the condition x == (y*q + r)?
A) (q == r) && (r == 0)
B) (x> 0) && (r == x) && (y > 0)
C) (q == 0) && (r == x) && (y > 0)
D) (q == 0) && (y > 0)

Answer C

28. Consider the following C program.


#include
int main () {
int m=10;
int n, n1;
n=++m;
n1=m++;
n--;
--n1;
n-=n1;
printf(“%d”, n);
return 0;
}
The output of the program is ______
A) 0
B) 1
C) 2
D) 3

Answer A

29. Consider the following C program.

#include<stdio.h>
#include<string.h>
int main () {
char* c = "GATECSIT2020";
char* p = c;
printf("%d", (int) strlen (c + 2[p] - 6[p] - 1));
return 0;
}
The output of the program is __________.

A) 1
B) 2
C) 4
D) 6

Answer B

30. What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires
four bytes of memory.
#include <stdio.h>
int main()
{
unsignedint x[4][3] = {{1, 2, 3}, {4, 5, 6},
{7, 8, 9}, {10, 11, 12}};
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}

A) 2036, 2036, 2036


B) 2012, 4, 2204
C) 2036, 10, 10
D) 2012, 4, 6

Answer A
31. What will be the output of the following pseudocode?
Integer i
Set i = 3
do
Print i + 3
i=i-1
while(i not equals 0)
end while

A) 6 6 6
B) 6 5 6
C) 5 5 5
D) 6 5 4 *

32. What is the output of code given below?


Integer i
Set i=1
while(++i<=5)
Print(i++)
End Loop
A-135
B-24 *
C-246
D-2

33. What is the output?


void show();
int main()
{
show();
printf("ARGENTINA ");
return 0;
}

void show()
{
printf("AFRICA ");
}
A) ARGENTINA AFRICA
B) AFRICA ARGENTINA *
C) ARGENTINA
D) Compiler error

34. What is the output of C Program.?

int myshow(int);
void main()
{
myshow(5);
myshow(10);
}

int myshow(int b)
{
printf("Received %d, ", b);
}
A) Received 5, Received 10, *
B) Received 10, Received 5,
C) Received 0, Received 0,
D) Error

35. What would be the output of the following pseudocode?


Integer i, j, k
Set k = 8
for(each i from 1 to 1)
for(each j from the value of i to 1)
print k+1
end for
end for

A. 2
B. 9 *
C. 7
D. 8

[Link] will be the output of the following pseudocode if we call fun(2) ?

1. int fun (int n)


2. if (n EQUALS 4)
3. return n
4. else
5. return 2 * fun (n + 1)

a) 2
b) 4
c) 8
d) 16 *

37. What is the output?

int show();
main()
{
int a;
printf("PISTA COUNT=");
a=show();
printf("%d", a);
}

int show()
{
return 10;
}
A) PISTA COUNT=
B) PISTA COUNT=0
C) PISTA COUNT=10 *
D) Compiler error

38. Consider an array of float. Calculate the difference between the address of the 1st and 4th element, assuming float
occupies 4 bytes of memory.

A. 16
B. 4
C. 12 *
D. 8

39. What will be the output of the following pseudocode?


Integer arr[]={10, 20, 30, 40, 5}
Integer a, s
Set s = 0
Set a = arr[1] + arr[2]
Print a

A) 25
B) 5
C) 50 *
D) 40

40. What will be the output of the following pseudocode ?

Integer x, y , z, a
Set x = 2, y = 1, z = 5
z = (x AND y) OR (z + 1)
Print z

a) 2
b) 5
c) 1 *
d) 3

41 What will be the output of the following pseudocode for fun(99)?

Integer fun(Integr n)
if (n > 100)
return n - 10
return fun(fun(n+11))
end function fun()

a) 91 *
b) 100
c) 110
d) 121

42 What will be the output of the following pseudocode?

Integer a, b, c
Set b = 2, c = 4
for (each a from 1 to 4)
b = b >> 1
c = c << b
end for
Print b+c

a) 2
b) 4
c) 1
d) 8*

43 Find the output of the following pseudo-code:


Integer c, d
Set c = 15, d = 12
d=c–1
Print c //line
c = d + (c – 2)
if(c < 40)
Goto line
end if

A) 14 26 38
B) 27 39
C) 15 27 39 *
D) None of the above

44 Find the output of the following pseudo-code if x= 4 and y=5:

Integer fun(int x, int y)


if(x > 1)
fun(x – 2, y + 2)
end if
print y
End function fun()

A) 456
B) 765
C) 975 *
D) None of the above

45 How many times will the print statement be executed:

Integer a, b, c
Set a = 8, b = 10, c = 6
If(a > c AND (b + c) > a)
Print a
end if
if(c > b OR (a + c) > b)
Print b
end if
if((b+c) MOD a EQUALS 0)
Print c
end if

A) 2
B) 3 *
C) 1
D) 0

46 What will be the output of the following pseudocode?


Integer a=5, b=4, c=3
a=b+c
c=a–b
c=c+a
c=b+c
b=b+c
Print a, b, c

A) 7 14 7
B) 7 14 10
C) 7 8 14
D) 7 18 14 *

47What will be the output if limit = 6 ?


Read limit
n1 = 0, n2= 1, n3=1, count = 1;
while count <= limit
count=count+1
print n3
n3 = n1 + n2
n1 = n2
n2 = n3
End While

a) 1235813 *
b) 12358
c) 123581321
d) 12358132

48 What will be the output of the following pseudocode?

Integer a = 0 , b = 1 , c = 2
b=( ( a+1==1) ? a&b : c&a) ? b : c;
Print a , b, c

a) 0 1 2
b) 0 2 0
c) 0 2 2 *
d) 0 0 2

49 Select the most appropriate output for the following code:

Integer r = -5, s = 1
s = r &= s && 100
Print(r,s)

a. -100 1
b. 1 0
c. 1 1 *
d. Error

50 Select the most appropriate output for the following code:

Integer a1,b1
Set a1=51
Set b1 = 0;
Integer c2 = a1 || --b1
Integer d3 = a1-- && --b1
Print(a1, b1, c2, d3)

a. 50 -1 1 1 *
b. 50 -2 1 1
c. 51 -1 1 1
d. None

51What is the output of this code?

Integer x
Set x=10
If (x = 0)
Print("Its zero")
Else
Print("Its not zero")

A. Its not zero *


B. Its zero
C. Run time error
D. None

52 Select the output:


If(!Print(""))
Print("Okkk")
Else
Print("Hiii")

A. Hiii
B. Okkk *
C. Error
D. None

53 What will be the output of following program ?


Integer val
Set val=1
If(val--==0)
Print("TRUE")
Else
Print("FALSE")

A. TRUE
B. Error
C. FALSE *
D. None

54 What will bw output of folowing code?


Integer loop
Set loop=10
while(Print("Hello ") && loop--)

A)Hello
B)Hello Hello Hello Hello ... (Infinite times)
C)Hello Hello Hello Hello ...(10 times)
D)Hello Hello Hello Hello ... (11 times) *

Q25) What will bw output of folowing code?


Integer i
Set i=6
while(i>0)
Print(i--)
i-=2
End Loop

A)6 4 3 2
B)6 3 *
C)6 5 4 3 2 1
D)compilation error

56 How many times Hi is printed on console?


Integer a
Set a = 0
while(a++)
Print("Hi")
End Loop

(A) Nothing is printed on screen *


(B) Error
(C) 1 time
(D) Infinite times (Untill Stack overflow)

57 What is the output of C Program.?


Integer a
Set a=5
while(a=123)
Print("HELLO")
Jump out of loop
End Loop
Print("HI")

A) HI
B) HELLO *
HI
C) HELLO is printed unlimited number of times.
D) Error

58 What will bw output of folowing code?


Integer i
Set i=1
While (i!=10)
Print(" Hello ")
i+=2
End Loop

(A) Hello Hello Hello Hello Hello


(B) Hello Hello Hello …. infinite times *
(C) Hello Hello Hello Hello
(D) Hello Hello Hello Hello Hello Hello

59 What will bw output of folowing code?


Integer i,j
Set i = 0
Set j = 0
while (i < 5, j < 10)
i+=1
j+=1
End Loop
Print(i, j)
A) 5, 5
B) 5, 10
C) 10,10 *
D) Error

60 What will be output of following code?


Integer n
Set n=9
While(n!=0)
Print(n--)
n-=1
End Loop
(A) 9 7 5 3 1
(B) 9 8 7 6 5 4 3 2 1
(C) Infinite Loop *
(D) 9 7 5 3

You might also like