Friday, August 8, 2014

C Programming - Arrays

1. 
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.
2. 
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
3. 
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

4. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}
A.2, 1, 15B.1, 2, 5
C.3, 2, 15D.2, 3, 20
5. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i), 
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}
A.1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
B.1, 2, 1, 2
2, 3, 2, 3
3, 4, 3, 4
4, 2, 4, 2
C.1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3
D.1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3

No comments: