Questions on Dynamic Array

Jan 11 • Notes • 3009 Views • 7 Comments on Questions on Dynamic Array

 

1) Which of the following expression is true?
a. 3 < 3
b. 3 < 3.0
c. string(“ABLE”) == string(“able”)
d. string(“aBlE”) < string(“AbLe”)
e. string(“able”) != string(“ABLE”)

2) An array declared int A[100] is to be passed as a parameter to a function. Which of the following may not be used to declare the function’s formal parameter?
a. int *P
b. int P[] c. int P[50] d. int P[100] e. int P[int]

3) If a list contains an array declared with int A[100]. What will be the value of Size when the list is full?
a. -1
b. 0
c. 99
d. 100
e. 101

4) Consider an unordered list of integers which includes an array and a Size value. The Size value equals 100. What is the minimum number of assignment operations needed in order to delete (or remove) the data stored in location 1of the list’s array? In other word, when the work is completed, Size will be 99 and the contents of the array will be adjusted appropriately.
a. 1
b. 2
c. 99
d. 100
e. 101

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

a. The element will be set to 0.
b.The compiler would report an error.
c.The program may crash if some important data gets overwritten.
d.The array size would appropriately grow.

6. What does the following declaration mean?
int (*ptr)[10];

a. ptr is array of pointers to 10 integers
b. ptr is a pointer to an array of 10 integers
c. ptr is an array of 10 integers
d.ptr is an pointer to array

7. In C, if you pass an array as an argument to a function, what actually gets passed?
a. Value of elements in array
b.First element of the array
c. Base address of the array
d. Address of the last element of array

8. A pointer to a block of memory is effectively same as an array
a. True
b. False

9. Does this mentioning array name gives the base address in all the contexts?
a. True
b. False

10. Is there any difference int the following declarations?
int fun(int arr[]);
int fun(int arr[2]);
a. True
b. False

11.Can the sizeof operator be used to tell the size of an array passed to a function?

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

 

The second method is to have some convention about when the array ends. For example, a C “string” is just a pointer to the first character; the string is terminated by an ASCII NUL (”) character. This is also commonly done when you have an array of pointers; the last is the null pointer. Consider the following function, which takes an array of char*s. The last char* in the array is NULL; that’s how the function knows when to stop.

 

void printMany( char *strings[] )
{
        int     i;
        i = 0;
        while ( strings[ i ] != NULL )
        {
             puts( strings[ i ] );
             ++i;
        }
}

 

Most C programmers would write this code a little more cryptically:

 

void  printMany( char *strings[] )
{
        while ( *strings )
        {
                puts( *strings++ );
        }
}

 

C programmers often use pointers rather than indices. You can’t change the value of an array tag, but because strings is an array parameter, it’s really the same as a pointer. That’s why you can increment strings. Also,

while ( *strings )

means the same thing as

while ( *strings != NULL )

and the increment can be moved up into the call to puts().

If you document a function (if you write comments at the beginning, or if you write a “manual page” or a design document), it’s important to describe how the function “knows” the size of the arrays passed to it. This description can be something simple, such as “null terminated,” or “elephants has numElephants elements.” (Or “arr should have 13 elements,” if your code is written that way. Using hard coded numbers such as 13 or 64 or 1024 is not a great way to write C code, though.)

 

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

7 Responses to Questions on Dynamic Array

  1. Ritika says:

    Dynamic Array finds a lot of application in practical world!

  2. sakshi chaudhary says:

    A good collection of questions and answers on DYNAMIC ARRAY. It will be very helpful for the candidates who are looking to get information about the dynamic array…!!!As it prove very useful to boost the INTERVIEW PREPARATION…

  3. Ankita Prajapati says:

    Necessary information regarding Dynamic Array….

  4. saurabh singh says:

    This article is beneficial for those who want to know which type of questions can be asked from”DYNAMIC ARRAY”.which will help canditates to learn” necessary” and “selective’ and save their time also.

  5. Siddhant Tripathi says:

    FAQs and related queries on Dynamic Arrays in computer language.

  6. madhusudan samantray says:

    pointers are the boon for c. array can otherwisely known as static pointers .

  7. Pallavi sinha says:

    The dynamic array has done same things like array, addition of new operations to add and remove elements from the end. It gives benefit in many cases like good locality of reference and data cache utilization, compactness and random access. Those who awn to elaborate their regarding this can go thru it.

« »