Arrays

Question 1

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 1 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 2

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 2 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 3

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 3 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 4

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 4 Explanation: 
Address for a[40][50] = BaseAddress + [40 * 100 * element size] + [50 * element size]
= 0 + [40 * 100 * 1] + [50 * 1]
= 4000 + 50
= 4050
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
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 6 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 7

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 7 Explanation: 
Question 8

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 8 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 9

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 9 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 10
The number of elements in a one-dimensional array with lowest and highest index values as -1024 and 1024 is:
A
2048
B
0
C
1024
D
2049
Question 10 Explanation: 
→The number of elements in [-n:n] is n+n+1=2n+1
→Example:[-2:2] consists of -2,-1,0,1,2 indexes.
Question 11
Total number of spanning tree of a complete graph of 4 vertices k4 is:
A
15
B
16
C
3
D
17
Question 11 Explanation: 
The number of spanning trees of Kn is nn-2= n4-2=n2=42=16
Question 12

Which of the following statements is INCORRECT with respect to pointers declared in the following ‘C’ language code?

Void main()

Int a[10], *p, *q;

p=&a[5];

q=&a[7];

}
A
Q-p
B
P+1
C
Q-3
D
P+q
Question 12 Explanation: 
→Pointers arithmetic operation is not possible. →Here p and q are two pointers , which consists of two addresses , adding addresses is invalid.
Question 13
If p is a two-dimensional array having 10 and 20 columns, then which of the following cannot be used to access the element in row 2 and column 5?
A
P[2][5]
B
*(*(P+2)+5)
C
*(P[2]+5)
D
*(P+2+5)
Question 13 Explanation: 
Option (A):
P[2][5] represents the element present in column five of row two. So it can be used to access the required given element in question.

Option (B):
*(*(P+2)+5)
*(P+2) → will lead to enter row two.
(*(P+2)+5) will give address of element present at fifth column of row 2.
→ *(*(P+2)+5) will return element present at fifth column of row 2.
Hence option (B) is Correct.

Option (C): *(P[2]+5)
P[2] → will give starting address of row 2
P[2]+5 → will give address of element present at 5th column of row 2.
*(P[2]+5) → will return the element present at 5th column of row 2.
Hence it is correct option.

Option (D): *(P+2+7)
= (P+7)
*(P+7) will return starting address of 7th row.
→ Hence it is not the correct answer.
Question 14

Consider the list of numbers 1, 2, 3, ..., 1000 is stored in a[0..999]. What will be the total number of comparisons to search x = 501 using the following binary_search() function?

int binary_search(int a[ ], int n, int x)

{

int low=0, high=n-1;

while(low <= high)

{

int m = (low + high) /2:

if(x > a[m])

low = m+1;

else if(x < a[m])

high= m-1:

else

return m;

}

return -1;

}
A
2
B
15
C
17
D
1
Question 15
What is the output of the following C program?
	{ int a|5| = {2,3};
	  Printf (“/n%d%d%d”, a[2], a[3], a[4]) ;
	} 
A
Garbage values
B
2 3 3
C
3 2 2
D
0 0 0
Question 15 Explanation: 
First two elements of array will get the value 2 and 3 and the remaining 3 elements of the array will get the value 0 by default.
Question 16
If x is an array of integer, then the value of &x [t] is same as
A
&x[t-1] + size of (int)
B
x + size of (int)*t
C
x+t
D
x=5
Question 17
How many values can be held by an array A (-1 … m, 1, … m)?
A
m
B
m2
C
m(m+1)
D
m(m+2)
Question 17 Explanation: 
Index of rows is from -1 to m, so no. of rows is m+2. And index of column is from 1 to m,so no. of rows is m.
Hence no. of values that can be held in array A is m*(m+2).
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