Arrays
Question 1 |
Consider the following C program.
#includeint main () { int a [4] [5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}}; printf (“%d\n”, *(*(a+**a+2) +3)); return (0); }
The output of the program is _______.
19 |
#include
int main()
{
int a[4][5] = { {1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20}
};
printf("%d\n",a); //880 (consider base address = 880)
printf("%d\n",*a); //880
printf("%d\n",**a); //1
printf("%d\n",**a+2); //3
printf("%d\n",a+**a+2); //940
printf("%d\n",*(a+**a+2));//940
printf("%d\n",*(a+**a+2)+3);//952
printf("%d\n",*(*(a+**a+2)+3));//19
return 0;
}

Question 2 |
#include
{
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for( i = 0; i < 3; i++ ){
for(k = 0; k < 3; k++ )
printf("%d ", a[i][j][k]);
printf("\n");
}
return 0;
}
1 2 3 10 11 12 19 20 21 | |
1 4 7 10 13 16 19 22 25 | |
1 2 3 4 5 6 7 8 9 | |
1 2 3 13 14 15 25 26 27 |
Hence, 1, 2, 3 will be 1^st row
10 , 11, 12 will be 2^nd row
19, 20, 21 will be 3^rd row
Question 3 |
Let A be a two dimensional array declared as follows:
A: array [1 ... 10] [1 ... 15] of integer;
Assuming that each integer takes one memory location, the array is stored in row-major order and the first element of the array is stored at location 100, what is the address of the element a[i][j]?
15i + j + 84 | |
15j + i + 84 | |
10i + j + 89 | |
10j + i + 89 |
100 + 15 * (i-1) + (j-1)
= 100 + 15i - 15 + j - 1
= 15i + j + 84
Question 4 |
Suppose you are given an array s[1...n] and a procedure reverse (s,i,j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence do, where 1 ≤ k ≤ n:
reverse(s, 1, k) ; reverse(s, k + 1, n); reverse(s, l, n);
Rotates s left by k positions | |
Leaves s unchanged | |
Reverses all elements of s | |
None of the above |
Question 5 |
Consider the following declaration of a two dimensional array in C:
char a[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a [40][50] is:
4040 | |
4050 | |
5040 | |
5050 |
= 0 + [40 * 100 * 1] + [50 * 1]
= 4000 + 50
= 4050
Question 6 |
Assume the following C variable declaration
int *A [10], B[10][10];
Of the following expressions
I. A[2] II. A[2][3] III. B[1] IV. B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
I, II, and IV only | |
II, III, and IV only | |
II and IV only | |
IV only |
ii) A[2][3] This results an integer, no error will come.
iii) B[1] is a base address of an array. This will not be changed it will result a compile time error.
iv) B[2][3] This also results an integer. No error will come.
Question 7 |
A program P reads in 500 integers in the range [0, 100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies?
An array of 50 numbers | |
An array of 100 numbers | |
An array of 500 numbers | |
A dynamically allocated array of 550 numbers |
→ Then using array of 50 numbers is the best way to store the frequencies.
Question 8 |
A Young tableau is a 2D array of integers increasing from left to right and from top to bottom. Any unfilled entries are marked with ∞, and hence there cannot be any entry to the right of, or below a ∞. The following Young tableau consists of unique entries.
1 2 5 14 3 4 6 23 10 12 18 25 31 ∞ ∞ ∞
When an element is removed from a Young tableau, other elements should be moved into its place so that the resulting table is still a Young tableau (unfilled entries may be filled in with a ∞). The minimum number of entries (other than 1) to be shifted, to remove 1 from the given Young tableau is ____________.
4 | |
5 | |
6 | |
7 |

Question 9 |
Consider the following C program:
#include <stdio.h> int main () { int arr [] = {1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip = arr+4; printf ("%d\n", ip[1]); return 0; }
The number that will be displayed on execution of the program is _____.
5 | |
6 | |
7 | |
8 |

We know that arr is a pointer to arr[ ] & hence arr+4 is pointer to 4th index of array (starting from 0 to 4).
Now *ip is a pointer of int type pointing to memory location 108, which is part of arr.
Hence, when we will print ip[1] it will be equivalent to *(ip+1).
Address of ip will be incremented by 1 & value inside 110 will be printed.
Question 10 |
What is the output of the following Pascal program segment?
{c: array [1..10] of integer;
m, n, temp : integer;
procedure r (k, j : integer)
begin
k :=k+1; j :=j+2;
end r;
…
m :5; n := 3;
r(m, n)
temp;=m; m:=n; n:temp;
write m, n;
}
3, 5
| |
5, 3 | |
Either 3, 5 or 5, 3 | |
Unpredictable | |
Answer the above question with explanation |
Question 11 |
{ int a|5| = {2,3}; Printf (“/n%d%d%d”, a[2], a[3], a[4]) ; }
Garbage values | |
2 3 3
| |
3 2 2 | |
0 0 0 |
Question 12 |
&x[t-1] + size of (int) | |
x + size of (int)*t | |
x+t | |
x=5 |
Question 13 |
m | |
m2 | |
m(m+1) | |
m(m+2) |
Hence no. of values that can be held in array A is m*(m+2).
Question 14 |
An array of integers is declared in C language as
int pat [32] [10] ;
Which of the following array elements are in adjacent locations in memory?
pat [31] [6], pat [0] [7] | |
pat [28] [0], pat [28] [9] | |
pat [15] [0], pat [16] [0 | |
None of the Above. |
After pat[28][0] the next location will be pat[28][1].
After pat[15][0] the next location will be pat[15][1].
Question 15 |
Array is
A non linear data structure | |
A primitive data structure
| |
A linear data structure | |
None from (1), (2), and (3) |