Machine-translated by Google. Questions are authored in English.
Top

Arrays Questions and Answers

21.

Consider the statement

int val[2] [4] = { 1, 2, 3, 4, 5, 6, 7, 8} ;

4 will be the value of

Answer: A

Correct answer: val[0 ][ 3]

A brace-list without inner braces fills a two-dimensional array row by row. With 4 columns, the first row takes 1, 2, 3, 4 and the second 5, 6, 7, 8. The value 4 is therefore the fourth element of row 0, that is val[0][3]. val[0][4] is out of bounds, and val[1][1] is 6.

Please log in or register to report this question.

22.

Let A[1...n] be an array of n distinct numbers. If \(i < j\) and \(A[i] > A[j]\), then the pair (i, j) is called an inversion of A. What is the expected number of inversions in any permutation on n elements ?

Answer: B

Correct answer: n(n-1)/4

There are \(\binom{n}{2} = \dfrac{n(n-1)}{2}\) pairs \((i, j)\) with \(i < j\). In a permutation chosen uniformly at random, each such pair is inverted with probability \(\tfrac{1}{2}\), since the two orders are equally likely. By linearity of expectation:

\[ E[\text{inversions}] = \frac{n(n-1)}{2} \times \frac{1}{2} = \frac{n(n-1)}{4} \]

The maximum, \(n(n-1)/2\), occurs only for the fully reversed permutation.

Please log in or register to report this question.

23.

What is the output of the following piece of code?

public class array
{
 public static void main(String args[])
 {
  int []arr = {1,2,3,4,5};
  System.out.println(arr[2]);
  System.out.println(arr[4]);
 }
}

 

Answer: A

Correct answer: 3 and 5

Array indices start at 0, so in {1,2,3,4,5} the element at index 2 is the third value and the one at index 4 is the fifth:

index01234
value12345

So the two println calls print 3 and 5.

Please log in or register to report this question.

24.

Which of the following are applications of a multidimensional array?

Answer: D

Correct answer: All the above

A two-dimensional array is the natural representation of a matrix, so matrix multiplication uses one directly. Graph algorithms such as minimum spanning tree and connectivity between nodes commonly store the graph as an adjacency matrix, which is again a two-dimensional array. All three qualify.

Please log in or register to report this question.

25.

Consider an array consisting of -ve and +ve numbers. What would be the worst case time complexity of an algorithm to segregate the numbers having same sign altogether i.e all +ve on one side and then all -ve on the other ?

Answer: A

Correct answer: O(N)

Segregating the numbers needs only a single partition pass: keep one index scanning forward and one backward, and swap whenever a positive on the left meets a negative on the right. Each element is examined once, so the worst case is \(O(N)\). Sorting would also separate them, but at \(O(N \log N)\) it does more work than the question requires.

Please log in or register to report this question.

Answer: B

Correct answer: In the first statement 6 specifies a array size, whereas in the second statement it specifies a particular element of array.

In a declaration, int num[6];, the 6 is the size - how many elements to reserve. In an expression, num[6] = 21;, the 6 is a subscript naming one element. The same notation means different things in the two positions, and here the second use is in fact out of bounds: valid subscripts run 0 to 5.

Please log in or register to report this question.

27.

The parameter passing mechanism for an array is

Answer: C

Correct answer: call by reference

C passes everything by value, but what is passed for an array is its base address, so the function works on the caller’s own storage and any change it makes is visible afterwards. That effect is what “call by reference” describes, and it is why a function can fill an array supplied by its caller.

Please log in or register to report this question.

28.

What is the maximun number of dimensions an array in C may have?

Answer: D

The maximum size of an array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes. The actual limit may be less, depending on operating system implementation details.

Please log in or register to report this question.

29.

Predict the output of below code: 

#include <stdio.h>

int main()
{
    int arr[1]={10};
    printf("%d\n", 0[arr]);
    return 0;
}

 

Answer: B

Correct answer: Output : 10

a[i] is defined as *(a + i), and addition commutes, so i[a] means the same thing. 0[arr] is therefore arr[0], and the program prints 10.

Please log in or register to report this question.

30.

How many elements does the array below have?

int a[20]

 

Answer: C

Correct answer: 20

The number in the declarator is the count of elements, so int a[20] has 20 of them, with subscripts 0 to 19. Do not confuse this with the number of bytes, which would be \(20 \times \text{sizeof(int)}\).

Please log in or register to report this question.

Loading…
Tags: Arrays Questions and Answers || Arrays MCQ Questions and Answers || Arrays GK Questions and Answers || Arrays GK MCQ Questions || Arrays Multiple Choice Questions and Answers || Arrays GK || GK on Arrays || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming