Arrays

Question 1

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 _____.

A
5
B
6
C
7
D
8
Question 1 Explanation: 

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 2

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 ____________.

A
4
B
5
C
6
D
7
Question 2 Explanation: 
Question 3
Consider the following two C code segments. Y and X are one and two dimensional arrays of size n and n × n respectively, where 2 ≤ n ≤ 10. Assume that in both code segments, elements of Y are initialized to 0 and each element X[i][j] of array X is initialized to i + j. Further assume that when stored in main memory all elements of X are in same main memory page frame.
Code segment 1:
// initialize elements of Y to 0
// initialize elements X[i][j] of X to i+j
for (i = 0; i < n; i++)
y[i] + = X[0][i];

Code segment 2:
// initialize elements of Y to 0
// initialize elements X[i][j] of X to i+j
for (i = 0; i < n; i++)
y[i] + = X[i][0];
Run on IDE
Which of the following statements is/are correct?
S1: Final contents of array Y will be same in both code segments.
S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory.
S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory.
A
Only S2 is correct
B
Only S3 is correct
C
Only S1 and S2 are correct
D
Only S1 and S3 are correct
Question 4

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?

A
An array of 50 numbers
B
An array of 100 numbers
C
An array of 500 numbers
D
A dynamically allocated array of 550 numbers
Question 4 Explanation: 
→ Here we are storing values above 50 and we are ignoring the scores which is less than 50.
→ Then using array of 50 numbers is the best way to store the frequencies.
Question 5

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?

A
I, II, and IV only
B
II, III, and IV only
C
II and IV only
D
IV only
Question 5 Explanation: 
i) A[2] can be consider as a pointer and this will not give any compile-time error.
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 6

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:

A
4040
B
4050
C
5040
D
5050
Question 6 Explanation: 
Address for a[40][50] = BaseAddress + [40 * 100 * element size] + [50 * element size]
= 0 + [40 * 100 * 1] + [50 * 1]
= 4000 + 50
= 4050
Question 7

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);  
A
Rotates s left by k positions
B
Leaves s unchanged
C
Reverses all elements of s
D
None of the above
Question 7 Explanation: 
If we perform the three given open operations it will result left rotation by K positions. If we perform n time it will result the initial array.
Question 8

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]?

A
15i + j + 84
B
15j + i + 84
C
10i + j + 89
D
10j + i + 89
Question 8 Explanation: 
The address of element A[i][j] will be,
100 + 15 * (i-1) + (j-1)
= 100 + 15i - 15 + j - 1
= 15i + j + 84
Question 9

Consider the following C program.

#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+**a+2) +3));
     return (0);
} 

The output of the program is _______.

A
19
Question 9 Explanation: 
Check out the step by step program and its output in the comment:
#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 10
What is printed by the following ANSI C program?
#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;
}
A
1 2 3
10 11 12
19 20 21
B
1 4 7
10 13 16
19 22 25
C
1 2 3
4 5 6
7 8 9
D
1 2 3
13 14 15
25 26 27
Question 10 Explanation: 
Array dimension sizes are 3,3,3 for all 3 dimensions hence, the value assigned to a[3] [3] [3] will be assigned from only first 3 indices i.e. 0^th, 1^st, & 2^nd and 3 dimensions shown by { }
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 11
An array A consists of n integers in locations A[0], A[1] ….A[n-1]. It is required to shift the elements of the array cyclically to the left by k places, where 1 <= k <= (n-1). An incomplete algorithm for doing this in linear time, without using another array is given below. Complete the algorithm by filling in the blanks. Assume alt the variables are suitably declared.
A
i > min; j!= (n+i)mod n; A[j + k]; temp; i + 1 ;
B
i < min; j!= (n+i)mod n; A[j + k]; temp; i + 1;
C
i > min; j!= (n+i+k)mod n; A[(j + k)]; temp; i + 1;
D
i < min; j!= (n+i-k)mod n; A[(j + k)mod n]; temp; i + 1;
Question 11 Explanation: 
This question need presence of mind.
Step-1: Observe all options, 4 options they are given 4th and 5th blank is temp and i+1.
So, they given clue that, 4th and 5th options must be temp and i+1.
Step-2: Based on the 4th and 5th options, we can guess that 1st blank is i → Suppose if we are considering i>min then the control goes out of the while loop in the initial case when i=0 and min=n. So, 1st blank should be i Step-3: According to 1st blank, we can eliminate A and C options.
Step-4: Assume 2nd blank is correct, we are considering j!= (n+i) mod n this condition in while loop.
→ The meaning of the condition is “j becomes equal to (n+i)mod n then control goes out of the while loop.”
Step-5: The condition (n+i)mod n=i and j is always equal to i because we are assigning the value of i to j in the code segment line 3.
Step-6: based on these constraint we can say that 4th option is correct.
Reason: The control never enters the 2nd while loop. Sometimes it will enter the 2nd while loop, when we shift the numbers. It means total K places left.
Question 12
Which of the following is an illegal array definition?
A
Type COLOGNE : (LIME, PINE, MUSK, MENTHOL); var a : array [COLOGNE] of REAL;
B
var a : array [REAL] of REAL;
C
var a : array [‘A’…’Z’] of REAL;
D
var a : array [BOOLEAN] of REAL;
Question 12 Explanation: 
Array index should be integer not real numbers.
Expect the option B, All remaining indexes are not real numbers.
Option A , takes enum value as index which is integer number.
Option C, takes character which is having equivalent decimal value.
Option D, has boolean value as index whose value may be 0 or 1
Question 13
A one dimensional array A has indices 1….75. Each element is a string and takes up three memory words. The array is stored at location 1120 decimal. The starting address of A[49] is
A
1267
B
1164
C
1264
D
1169
Question 13 Explanation: 
Given data is each string takes up three memory words which is nothing but size is 3.
Base or starting address of the array is 1120.
The address of the 49th element = base address of array + number of elements before current element * size of element
= 1120 + 48 * 3 = 1264
Question 14
In an array of 2N elements that is both 2-ordered and 3-ordered, what is the maximum number of positions that an element can be from its position if the array were 1-ordered?
A
1
B
2
C
N/2
D
2N-1
Question 14 Explanation: 
Given data,
Array=2N elements
Elements are used 2-ordered and 3-ordered
Step-1: If array used 2-ordered, if it contains an element which is at most two positions away from its original position in a sorted array.
Sep-2: Maximum number of positions that an element can be from its position if the array were
1-ordered = 1
Question 15
Let A(1:8, -5:5, -10:5) be a three dimensional array. How many elements are there in the array A?
A
1200
B
1408
C
33
D
1050
Question 15 Explanation: 
Array declaration in C language is int A[10] which means there are 10 elements in the array. Similarly int A[10][10] means array consists of the 100 elements.
In the given question, Array is represented with lower bound and upper bound.
The following ARRAY statement defines an array containing a total of five elements, a lower bound of 72, and an upper bound of 76. It represents the calendar years 1972 through 1976: array years{72:76} first second third fourth fifth.
The following ARRAY statement arranges the variables in an array by decades. The rows range from 6 through 9, and the columns range from 0 through 9.
array X{6:9,0:9} X60-X99.
Question 16
Let A be a square matrix of size nxn. consider the following program. What is the expected output?
C=100
for i=1 to n do
for j=1 to n do
{
 Temp=A[i][j]+C
 A[i][j]=A[j][i]
 A[j][i]=Temp-C
}
for i=1 to n do
for j=1 to n do
 output(A[i][j]);
A
The matrix A itself
B
Transpose of matrix A
C
Adding 100 to the upper diagonal elements and subtracting 100 from diagonal elements of A
D
None of the option
Question 16 Explanation: 
Question 17
A program P reads in 1000 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?
A
An array of 50 numbers
B
An array of 100 numbers
C
An array of 500 numbers
D
A dynamically allocated array of 550 numbers
Question 17 Explanation: 
→ Here we are storing values above 50 and we are ignoring the scores which is less than 50.
→ Then using array of 50 numbers is the best way to store the frequencies.
There are 17 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access