Arrays

  Home  Data Structure  Arrays


“Arrays job interview questions and answers guide. The one who provides the best Arrays answers with a perfect presentation is the one who wins the interview race. Learn Data Structure Arrays and get preparation for the job of Arrays”



31 Arrays Questions And Answers

5⟩ Explain Array of pointers?

An array of pointers is an array consisting of pointers. Here, each pointer points to a row of the matrix or an element. E.g char *array [] = {“a”, “b”}. This is an array of pointers to to characters.

 130 views

6⟩ What is two-dimensional array?

An array with two dimensions is called as a two-dimensional array. It is also called as a matrix. In C, a two dimensional array is initialized as int arr[nb_of_rows] [nb_of_columns]. Hence, two dimensional arrays can be considered as a grid. An element in a two dimensional can be accessed by mentioning its row and column. If the array has 20 integer values, it will occupy 80 bytes in memory (assuming 4 bytes for an integer). All of the bytes are in consecutive memory locations, the first row occupying the first 20 bytes, the second the next 20, and so on.

 121 views

7⟩ What is Array?

An array is a series of elements. These elements are of the same type. Each element can be individually accessed using an index. For e.g an array of integers. Array elements are stored one after another (contiguous) in the memory. An array can have more than one dimension. First element in an array starts with 0.

 130 views

8⟩ Tell me is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?

It's easier for a C compiler to generate good code for pointers than for subscripts.

Say that you have this:

/* X is some type */

X a[ MAX ]; /* array */

X *p; /* pointer */

X x; /* element */

int i; /* index */

Here's one way to loop through all elements:

/* version (a) */

for ( i = 0; i < MAX; ++i )

{

x = a[ i ];

/* do something with x */

}

On the other hand, you could write the loop this way:

/* version (b) */

for ( p = a; p < & a[ MAX ]; ++p )

{

x = *p;

/* do something with x */

}

 147 views

9⟩ Tell me can the size of operator be used to tell the size of an array passed to a function?

No. There's no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element. This is a Good Thing. It means that passing pointers and arrays to C functions is very efficient.

It also means that the programmer must use some mechanism to tell how big such an array is. There are two common ways to do that. The first method is to pass a count along with the array. This is what memcpy() does, for example:

char source[ MAX ], dest[ MAX ];

/* ... */

memcpy( dest, source, MAX );

 143 views

11⟩ Explain can you assign a different address to an array tag?

No, although in one common special case, it looks as if you can. An array tag is not something you can put on the left side of an assignment operator. (It's not an "lvalue," let alone a "modifiable lvalue.") An array is an object; the array tag is a pointer to the first element in that object.

For an external or static array, the array tag is a constant value known at link time. You can no more change the value of such an array tag than you can change the value of 7.

Assigning to an array tag would be missing the point. An array tag is not a pointer. A pointer says, "Here's one element; there might be others before or after it." An array tag says, "Here's the first element of an array; there's nothing before it, and you should use an index to find anything after it." If you want a pointer, use a pointer.

 140 views

13⟩ Tell me do array subscripts always start with zero?

Yes. If you have an array a[MAX] (in which MAX is some value known at compile time), the first element is a[0], and the last element is a[MAX-1]. This arrangement is different from what you would find in some other languages. In some languages, such as some versions of BASIC, the elements would be a[1] through a[MAX], and in other languages, such as Pascal, you can have it either way.

 148 views

17⟩ Explain is it valid to address one element beyond the end of an array?

It's valid to address it, but not to see what's there. (The really short answer is, "Yes, so don't worry about it.") With most compilers, if you say

int i, a[MAX], j;

then either i or j is at the part of memory just after the last element of the array. The way to see whether i or j follows the array is to compare their addresses with that of the element following the array. The way to say this in C is that either

& i == & a[ MAX ]

is true or

& a[ MAX ] == & j

is true. This isn't guaranteed; it's just the way it usually works.

 158 views

19⟩ Can you please explain the difference between array_name and &array_name?

One is a pointer to the first element in the array; the other is a pointer to the array as a whole.

An array is a type. It has a base type (what it's an array of ), a size (unless it's an "incomplete" array), and a value (the value of the whole array). You can get a pointer to this value:

char a[ MAX ]; /* array of MAX characters */

char *p; /* pointer to one character */

/* pa is declared below */

pa = & a;

p = a; /* = & a[ 0 ] */

After running that code fragment, you might find that p and pa would be printed as the same value; they both point to the same address. They point to different types of MAX characters.

The wrong answer is

char *( ap[ MAX ] );

which is the same as this:

char *ap[ MAX ];

This code reads, "ap is an array of MAX pointers to characters."

 120 views