Professional Documents
Culture Documents
Accenture Psedocode -7
Accenture Psedocode -7
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
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)
A) 43 80
B) 42 74
C) 33 37
D) 32 32
Answer A
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
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
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
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