148k views
2 votes
What are the values of a[k] and a[k+1] after code corresponding to the following pseudocode runs?

a[k] = 10;
a[k+1] = 20;
temp = a[k];
a[k] = a[k+1] ;
a[k+1] = temp ;

Group of answer choices

A. a[k] = 20, a[k + 1] = 20

B. a[k] = 20, a[k + 1] = 10

C. a[k] = 10, a[k + 1] = 10

D. a[k] = 10, a[k + 1] = 20

1 Answer

4 votes

Answer:

Option B: a[k] = 20, a[k + 1] = 10 is the correct answer.

Step-by-step explanation:

We will dry run the pseudocode line by line and monitor the values stored in elements to get the final answer.

We have three elements to be used in the code.

a[k] , a[k+1] and temp

a[k] = 10;

This line assigns value 10 to a[k]

a[k+1] = 20;

This line assigns value 20 to a[k+1]

temp = a[k];

This line will assign the value in a[k] to temp i.e. we have following values currently stored at the three locations a[k] = 10, a[k+1] = 20 and temp = 10

a[k] = a[k+1] ;

This line of code assigns the value of a[k+1] to a[k] which means now we have a[k] = 20 , a[k+1] = 20 and temp =10

a[k+1] = temp ;

This line will assign the value in temp to a[k+1] which means now we have

a[k] = 20 , a[k+1] = 10 and temp = 10

Hence, looking at the final values in a[k] and a[k+1] it can be concluded that

Option B: a[k] = 20, a[k + 1] = 10 is the correct answer

User Daniel Hedberg
by
7.1k points