Interviewer And Interviewee Guide

Role-specific Data Structure Arrays Interview Questions & Answers:

1. Can you please explain the difference between string and an array?

An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length.
There are two kinds of programming languages: those in which a string is just an array of characters, and those in which it's a special type. In C, a string is just an array of characters (type char), with one wrinkle: a C string always ends with a NUL character. The "value" of an array is the same as the address of (or a pointer to) the first element; so, frequently, a C string and a pointer to char are used to mean the same thing.
An array can be any length. If it's passed to a function, there's no way the function can tell how long the array is supposed to be, unless some convention is used. The convention for strings is NUL termination; the last character is an ASCII NUL ('') character.

String literals are arrays of characters (type char), not arrays of constant characters (type const char). The ANSI C committee could have redefined them to be arrays of const char, but millions of lines of code would have screamed in terror and suddenly not compiled. The compiler won't stop you from trying to modify the contents of a string literal. You shouldn't do it, though. A compiler can choose to put string literals in some part of memory that can't be modified-in ROM, or somewhere the memory mapping registers will forbid writes. Even if string literals are someplace where they could be modified, the compiler can make them shared.

2. Tell me why can't constant values be used to define an array's initial size

There are times when constant values can be used and there are times when they can't. A C program can use what C considers to be constant expressions, but not everything C++ would accept.
When defining the size of an array, you need to use a constant expression. A constant expression will always have the same value, no matter what happens at runtime, and it's easy for the compiler to figure out what that value is. It might be a simple numeric literal:
char a[ 512 ];
Or it might be a "manifest constant" defined by the preprocessor:
#define MAX 512
/* ... */
char a[ MAX ];
Or it might be a sizeof:
char a[ sizeof( struct cacheObject ) ];
Or it might be an expression built up of constant expressions:
char buf[ sizeof( struct cacheObject ) * MAX ];
Enumerations are allowed too.
An initialized const int variable is not a constant expression in C:
int max = 512; /* not a constant expression in C */
char buffer[ max ]; /* not valid C */
Using const ints as array sizes is perfectly legal in C++; it's even recommended. That puts a burden on C++ compilers (to keep track of the values of const int variables) that C compilers don't need to worry about. On the other hand, it frees C++ programs from using the C preprocessor quite so much.

3. 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."

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

5. 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 */
}

6. 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 );

7. 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.

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

Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.