Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

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 function
that returns true if x is a prime number and false otherwise.
i := 0; j := 0; k := 0;
for (m := start; 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. What will be output of following 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
Ans: C

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

You might also like