1.What will the following program print?

var x,y:integer;

procedure p(x:integer; var y:integer);

begin

x:=10;

y:=20;

end;

begin

x:=1;

y:=2;

p(x,y);

writeln(x,' ',y);

end.

#include<iostream.h>

int x,y;

void p(int x, int& y)

{

x=10;y=20;

}

void main()

{

x=1;y=2;

p(x,y);

cout<<x<<" "<<y;

}

 

 



2.Given the declarations below choose the number and type of the elements of a:

type vector=array[`..30] of real;

var a:array[0..9] of vector;

typedef float vector[30];

vector a[10];

 

9; vector

10; vector

270; real




3.The following function returns the result o if:

type vector=array[1..10] of real;

var x:vector;

function f(n:integer):integer;

var i:byte;

begin

f:=1;

for i:=2 to n do

if x[i]=x[i-1] then f:=0;

end;

 

typedef float vector[10];

vector x;

float f(int n)

{

int i;

for (i=2;i<=n;i++)

if (x[i]==x[i-1]

return 0;

return 1;

}

 

any 2 elements of x are different

any 2 elements of x are equal

the first 2 elements of x are not equal

 




4.The Fibonacci que is a que of numbers 0 1 1 2 3 5 8 13 21...., the n-th element of the que being defined as:

f(n)=

-0 if n=1

-1 if n=2

-f(n-2)+f(n-1) if n>2

In the following program the function f computes the n-th Fibonacci element.

var n:integer;

function f(n:integer):integer;

begin

if n=1 then f:=0

else if n=1 then f:=1

else if n=2 then f:=f(n-2)+f(n-1);

end;

begin

readln(n);

writeln(f(n));

end;

#include <iostream.h>

int f(int n)

{

if (n==1) return 0;

else if (n==2) return 1;

else return(f(n-2)+f(n-1));

}

void main()

{int n;

cin>>n;

cout<<f(n);}

 

How many recursive calls are being made for n=5?

 

8

25

9




5. A student finds the following problem: N integer numbers are being read, n<=100 and then another integer d. Is the result of multiplying the numbers divisible by d?

The student thinks of making a simple program first. He wants to keep the result of multiplying the numbers in the integer p and check if p is divisible by d.

The second time he thinks he should check if any of the read numbers is divisible by d. If so, the result of the multiplication is divisible by d, else it is not.

Which of the 2 versions solves the problem correctly for any test cases?

the first version

the second version

none of the versions




You are given the following program written in pseudocode and in which n is an integer:

procedure p(n,x)

if n=0 then write(x)

else call p(n-1,n*x)

endif

endp

program

read(n)

call p(n,1)

endprogram

6. Which of the following values will be written if n=4?




7.Which of the following values is the total number of calls of the procedure which are being made for a certain read value n?




8. 3 students come up with algorithms of checking if a number is prime for an integer number n, n>1 read. Which of the following algorithms is correct?

read(n)

prim=1

for (i=1,n-1) do

if n%i=0 then

begin

prim=0

i=n-1

end

endfor

if prim=1 then write("the number is prime")

else write("the number is not prime")

read(n)

prim=1

for (i=2,[sqrt(n)]) do

if n%i=0 then prim=0

endfor

if prim=1 then write("the number is prime")

else write("the number is not prime")

read(n)

i=2

prim=1

repeat

if n%i=0 then prim=0

i=i-1

until (i>[sqrt(n)]) or (prim=0)

if prim=1 then write("the number is prime")

else write("the number is not prime")




9. You are given the following algorithm in which n is an integer number, n>1.

read(n)

for (i=1,n) do

read(a[i])

endfor

k=1;

for (i=1,k) do

for (j=i+1,n) do

if a[i]<a[j] then

begin

x=a[i]

a[i]=a[j]

a[j]=x

end

endfor

endfor

for (i=1,k) do

if i<>k then write(a[i]," ") else write(a[i])

endfor

Which are the values printed after executing the program knowing that, for n=6, a's elements are: 12 4 8 7 1 9?




10. We are given the following algorithm, where a and b are integer numbers:

read(a,b)

x=0

z=1

repeat

if a%10>b%10 then c=a%10 else c=b%10

x=x+c*z

z=z*10

a=[a/10]

b=[b/10]

until a=0 and b=0

write (x)

Write the value printed for a=5612 and b=782