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

Arrays Questions and Answers

51.

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.

52.

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.

54.

A three dimensional array in ‘C’ is declared as int A[x][y][z]. Consider that array elements are stored in row major order and indexing begins from 0. Here, the address of an item at the location A[p][q][r] can be computed as follows

Answer: B

Correct answer: &A[0][0][0] + w(y * z * p + z*q + r)

In row-major order the last index varies fastest. Moving one step in \(p\) skips a whole \(y \times z\) plane, one step in \(q\) skips a row of \(z\) elements, and one step in \(r\) skips a single element:

\[ \text{addr} = \&A[0][0][0] + w\,(y \cdot z \cdot p + z \cdot q + r) \]

where \(w\) is the size of one element. The dimension \(x\) never appears, which rules out two of the options immediately - the number of planes does not affect where any one of them starts.

Please log in or register to report this question.

55.

C does no automatic array bound checking. This is best described as 

Answer: D

Correct answer: C's shortcoming

C performs no bounds check, so an out-of-range subscript silently reads or writes memory that belongs to something else. It is generally counted a shortcoming: it is the root of buffer overflows and of a great many hard-to-find bugs. The design was deliberate - omitting the check keeps array access as fast as raw address arithmetic - but the cost in safety is the reason later languages put the check back.

Please log in or register to report this question.

56.

Are the expressions arr and &arr same for an array of 10 integers?

Answer: B

Correct answer: No

They hold the same address but are not the same expression: arr has type int * while &arr has type int (*)[10]. The difference appears in arithmetic - arr+1 advances by one integer, &arr+1 by the whole array of ten.

Please log in or register to report this question.

57.

What is the output of C program with arrays?

int main() 
{ 
int a[3] = {20,30,40}; 
int b[3]; 
b=a; 
printf("%d", b[0]); 
}

 

Answer: A

Correct answer: Compiler Error

An array is not assignable. b = a; fails because b is not a modifiable lvalue - the name denotes fixed storage, not a pointer variable that could be repointed. Copying an array means copying its elements, with a loop or with memcpy(b, a, sizeof(a)).

Please log in or register to report this question.

Answer: B

Correct answer: ptr is a pointer to an array of 10 integers

Read the declaration from the identifier outwards, respecting the parentheses. (*ptr) says ptr is a pointer; [10] then says to an array of 10; int gives the element type. Without the parentheses, int *ptr[10] would bind [10] first and mean an array of 10 pointers - the two are quite different, and ptr + 1 advances by 40 bytes in the first case and by one pointer in the second.

Please log in or register to report this question.

59.

In general, the index of the first element in an array is __________

Answer: D

Correct answer: 0

Array indexing begins at 0 in C, C++, Java, Python and most other languages. The reason is that the index is an offset from the base address: the address of element \(i\) is \(\text{base} + i \times \text{size}\), so the first element sits at an offset of zero.

Please log in or register to report this question.

60.

What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?

Answer: C

Correct answer: The program may crash if some important data gets overwritten.

C does no bounds checking. Writing past the end of an array simply stores to whatever address the index computes to, so the effect depends on what happens to be there: often nothing visible, sometimes a corrupted variable, sometimes a crash when a return address or a pointer is overwritten. The compiler cannot generally report it, the element is not zeroed, and the array certainly does not grow.

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