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

Arrays Questions and Answers

41.

The following program

main( )
{
static int a[ ] = { 7, 8, 9 } ;
printf( "%d", 2[ a ] + a[ 2 ] ) ;
}

 

Answer: D

a[2] will be converted to *(a + 2).
*(a + 2) can as well be written as *(2 + a ) .
 *(2 + a ) is nothing but 2 [a] . So. a [2] is essentially same as 2 [a] which is same as * ( 2 + a ). So. it prints 9 + 9 = 18. Some of the modem compilers don't accept 2[a].

Please log in or register to report this question.

Answer: A

Correct answer: Base address is the address of 0th index element.

The base address of an array is simply where its first element sits - the address of the element at index 0. Every subscript is computed from it: a[i] lives at \(\text{base} + i \times \text{sizeof(element)}\), which is why index 0 costs no offset at all.

Please log in or register to report this question.

Answer: D

Correct answer: Easier to store elements of same data type

The advantage of an array is exactly that it stores many values of one type under one name, reached by index, which makes iterating and computing over them straightforward. The other options are false: an array holds one type, its elements can certainly be sorted, and its first index is 0.

Please log in or register to report this question.

Answer: C

Correct answer: int arr[3] = {1,2,3};

C uses square brackets for the dimension and braces for the initialiser list: int arr[3] = {1,2,3};. Parentheses in either position are a syntax error - arr(3) would be read as a function call, and (1,2,3) as a comma expression, not a list of initialisers.

Please log in or register to report this question.

45.

Consider the declaration

char street[10] = "abcdefghi";

Choose the correct remark(s)

Answer: D

Correct answer: None of the above

All three remarks are false.

  • &street and street hold the same address - the start of the array - and differ only in type.
  • &street is perfectly meaningful: it has type char (*)[10].
  • Because of that type, &street + 1 advances 10 bytes while street + 1 advances 1, so the two differ.

Please log in or register to report this question.

Answer: C

Correct answer: Both A and B

A is true: an element of an array may itself be an array, which is exactly what a two-dimensional array is. B is true in the same sense - C has only one-dimensional arrays, and int a[3][4] is an array of 3 objects each of type int[4]. The remaining option is false: array elements always occupy contiguous memory, which is what makes index arithmetic possible.

Please log in or register to report this question.

Answer: C

Correct answer: int arr[] = new int[3];

In Java an array object is created with new followed by the type and the length in square brackets: int arr[] = new int[3];. Round brackets after a type mean a constructor call, and int is a primitive with no constructor. int arr[]; alone only declares the reference - no array exists until new runs.

Please log in or register to report this question.

48.

O(N)(linear time) is better than O(1) constant time.

Answer: B

Correct answer: False

\(O(1)\) means the work does not grow with the input at all, while \(O(N)\) grows in proportion to it. For any large enough \(N\) the constant-time operation wins, so the statement is false. This is why a hash lookup is preferred to a linear scan.

Please log in or register to report this question.

49.

When does the ArrayIndexOutOfBoundsException occur?

Answer: B

Correct answer: Run-time

The exception occurs at run time. The compiler generally cannot know what an index will evaluate to - it may come from a variable, from input, or from a computation - so the check is made by the JVM as each access executes, and the failure surfaces only when that line runs.

Please log in or register to report this question.

Answer: D

Correct answer: All of above

All three will do. A two-dimensional array of characters gives every name the same fixed width. A one-dimensional array of strings is the same structure described differently. An array of pointers to char lets each name occupy only as much space as it needs, and makes sorting cheap, since only pointers are swapped.

Please log in or register to report this question.

Loading…

Other Subjects

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