You are on page 1of 7

DATA STRUCTURES: ARRAYS

(Solutions)

1. Let A be a two dimensional array declared as follows:

A: array [1 … 20] [1 … 10] of integers;

Assuming that each integer takes one memory location the array is stored in row-major order and
first element of the array is stored at location 200, what is the address of the element A [i] [j]?

(a) i + 10j + 189 (b) i + 10j – 184

(c) 10i – j + 184 (d) 189 + 10i + j

Solution: Option (d)

Explanation:

Given row major order, and array index is starting from 1.

Given 𝑅𝑀𝑂 = 𝐿0 + (𝑖 − 1)𝑟2 + (𝑗 − 1) [L0 : Starting Location]

= 200 + (𝑖 − 1)10 + (𝑗 − 1)

= 200 + 10𝑖 − 10 + 𝑗 − 1

= 189 + 10𝑖 + 𝑗

2. Study the following program written in a block structured language.

var p, q : integer;
begin
p:= p+q;
q:= p−q;
p:= p−q;
end;

(a) exchanges (p) and (q) (b) doubles (p) and stored in (q)

(c) doubles (q) and stores in (p) (d) leaves (p) and (q) unchanged

Solution: Option (a)

1
Explanation:

Above program is just an example of swapping.

3. What is the value of M printed by the following program?

Program COMPUTE (input, output);

var M: integer;
procedure FIND (M: real);
begin
M:=sqrt(M);
end;
begin
M:= 25;
FIND (M)
Writeln (M)
end.

(a) 5 (b) 25

(c) Runtime error (d) None of these

Solution: Option (b)

Explanation:

Because using ‘static swapping’ global variable value cannot be changed.

4. The correct matching for the following pairs is

A. Immediate addressing I. Pointers

B. Auto decrement addressing II. Constants

C. Indirect addressing III. Loops

(a) A – III, B – II, C – I (b) A – I, B – II, C – III

(c) A – III, B – I, C – II (d) A – II, B – III, C – I

Solution: Option (d)

2
Explanation:

*In Indirect addressing mode the instruction does not have the address of the data to be operated
on, but the instruction points where the address is stored.

*In Auto decrement addressing mode, before determining the effective address, the value in the
base register is decremented by the size of the data item which is to be accessed.

* In immediate addressing mode the data is to be used is immediately given in instruction itself,
so it deals with constant data.

5. Consider the following declaration of a two dimensional array in C. Assume RMO and index
starting from ‘0’.

char L[200] [200];

Assuming that the main memory is byte addressable and that the array is stored starting from
memory address 100, the address of L [50] [60] is

(a) 6100 (b) 10160

(c) 5050 (d) 6650

Solution: Option (b)

Explanation:

Address of L[50][60]

= Base address + 50 × 200 × element size + 60 × element size


= 100 + 50 × 200 × 1 + 60 × 1
= 100 + 10000 + 60
= 10160

6. Assume the following C variable declaration

int * M[10], N[10][10];

of the following expressions.

1. M [2][4]

2. N [2]

3
3. M [3]

4. N [2][4]

Which will not give compile time errors if used as left hand sides of assignment statements in a
C program?

(a) 2 and 4 only (b) 2, 3 and 4 only

(c) 4 only (d) 1, 3 and 4 only

Solution: Option (d)

Explanation:

Here int *M[10] is an array of 10 pointers which can be M[2][4] as its left hand side.

It can also be M[3] as its left hand side and also N[2][4] can obviously be used as left hand side
as it is given in declaration of N[10][10] only, but N[2] cannot be used because it is declared as
2-dimensional array.

7. Consider the C program shown below

#include <stdio.h>
#define print (a) printf (“%d”, a)

int a;
void A (int p)
{
p+ = a;
print (p);
}
void B(int *q)
{
int p = *q + 3;
A (a);
*q = a – 2;
print (a);
}

Main (void)
{
a = 6;

4
B(&a);
print(a);
}

The output of this program is

(a) 15, 7, 6 (b) 15, 7, 7

(c) 13, 7, 6 (d) 13, 7, 7

Solution: Option (a)

Explanation:

a = 6;

B(&a);

p = *q + 3 = 6 + 3 = 9;

A(a), a = 6;

p = p + a = 9 + 6 = 15 => print (p) = 15

*q = a – 2 = 9 – 2 = 7 => print (a) = 7

print (a) = 6

a 6

100

p=6+3=9

A(6)

P = 15

*q = 7

8.main ( )
{
int a = 2, b, c;
a* = b = c = 4;
a = b = c;
printf (“%d”, a);

5
a = = (b = c);
printf (“%d”, a);
}

What will be the output?

(a) 1, 4 (b) 4, 4

(c) 1, 1 (d) 8, 1

Solution: Option (c)

Explanation:

The statement (a* = b = c = 4;) assign the value of a with 8, but in the next statement a = b = c.

Here “ = = “ is for the test of quality operator. It will get priority over ‘=’, so equality operator
yield a result of TRUE (b = c) and integer 1 will be assigned to a.

*After a becomes 1 program control will goto a = = (b = c) statement. Here b = c = 4, but a = 1.

So equality operator yield FALSE and value of expression is ‘0’. But as equality operator does
not change the operand value, so value of a will be remain 1.

9. Let P be a square matrix of size m×m. Consider the following program.

What is the expected output?

A = 100
for i = 1 to m do
for j = 1 to m do
{
Temp = B [i] [j] + A
B [i] [j] = B [j] [i]
B [j] [i] = Temp – A
}
for i = 1 to m do
for j = 1 to m do
Output (B[i][j]);

(a) Transpose of matrix B

(b) The matrix B itself

6
(c) Adding 100 to the upper diagonal elements and subtracting 100 from diagonal elements of B

(d) None of the above

Solution: Option (b)

Explanation:

If we take a look at the inner statements of first loop, we can notice that the statements Swap
B[i][j] and B[j][i] for all i and j. Since the loop runs for all elements, every element B[l][m]
would be swapped twice, once for i = l and j = m and then for i = m and j = l,

Swapping twice means the matrix does not change.

10. A p×p array ‘M’ is defined as follows

M[a, b] = a – b for all a,b, 1 ≤ a, b ≤ p.

The sum of elements of the array M is

(a) p – 1 (b) p2 – 3p + 2

(c) 0 (d) p2(p+1)/ 2

Solution: Option (c)

Explanation:

In this case, the matrix would be

1−1 1−2 … 1−𝑝 1 1 … 1 1 2 … 𝑝


2−1 2−2 … 2−𝑝 2 2 … 2 1 2 … 𝑝
∑∑( ) = ∑∑(⋮ ⋮ ⋱ ⋮) − ∑∑(⋮ ⋮ ⋱ ⋮)
⋮ ⋮ ⋱ ⋮
𝑝−1 𝑝−2 … 𝑝−𝑝 𝑝 𝑝 … 𝑝 1 2 … 𝑝

1 1 … 1 1 2 … 𝑝
2 2 … 2 1 2 … 𝑝
Total sum = ∑𝑝𝑖=1 ∑𝑝𝑗=1 ( ⋮ ⋮ 𝑝 𝑝
⋱ ⋮ ) − ∑𝑖=1 ∑𝑗=1 ( ⋮ ⋮ ⋱ ⋮
)=0
𝑝 𝑝 … 𝑝 1 2 … 𝑝

You might also like