ARRAYS

1. 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, 15

B. 1, 2, 5

C. 3, 2, 15

D. 2, 3, 20

Explanation:

Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to

a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .

Step 2: int i, j, m; The variable i,j,m are declared as an integer type.

Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2

Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)

Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m

Hence the output of the program is 3, 2, 15        

2. What will be the output of the program ?

#include<stdio.h>

int main()

{

    void fun(int, int[]);

    int arr[] = {1, 2, 3, 4};

    int i;

    fun(4, arr);

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

        printf("%d,", arr[i]);

    return 0;

}

void fun(int n, int arr[])

{

    int *p=0;

    int i=0;

    while(i++ < n)

        p = &arr[i];

    *p=0;

}

A. 2, 3, 4, 5

B. 1, 2, 3, 4

C. 0, 1, 2, 3

D. 3, 2, 1 0

Explanation:

Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.

Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to

a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4

Step 3: int i; The variable i is declared as an integer type.

Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.

Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.

Hence the output of the program is 1,2,3,4

        

3. What will be the output of the program in Turb C (under DOS)?

#include<stdio.h>

int main()

{

    int arr[5], i=0;

    while(i<5)

        arr[i]=++i;

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

        printf("%d, ", arr[i]);

    return 0;

}

A. 1, 2, 3, 4, 5,

B. Garbage value, 1, 2, 3, 4,

C. 0, 1, 2, 3, 4,

D. 2, 3, 4, 5, 6

Explanation:

Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the TurboC Compiler (Windows) output.

Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.

        

4. Which of the following is correct way to define the function fun() in the below program?

#include<stdio.h>

int main()

{

    int a[3][4];

    fun(a);

    return 0;

}

A. void fun(int p[][4]){  }

B. void fun(int *p[4]){   }

C. void fun(int *p[][4]){  }

D. void fun(int *p[3][4]){  }

        

5. Which of the following statements are correct about the program below?

#include<stdio.h>

int main()

{

    int size, i;

    scanf("%d", &size);

    int arr[size];

    for(i=1; i<=size; i++)

    {

        scanf("%d", arr[i]);

        printf("%d", arr[i]);

    }

    return 0;

}

A. The code is erroneous since the subscript for array used in for loop is in the range 1 to size.

B. The code is erroneous since the values of array are getting scanned through the loop.

C. The code is erroneous since the statement declaring array is invalid.

D. The code is correct and runs successfully.

Explanation:

The statement int arr[size]; produces an error, because we cannot initialize the size of array dynamically. Constant expression is required here.

Example: int arr[10];

One more point is there, that is, usually declaration is not allowed after calling any function in a current block of code. In the given program the declaration int arr[10]; is placed after a function call scanf().

POINTERS

1. What will be the output of the program ?

#include<stdio.h>

int main()

{

    static char *s[] = {"black", "white", "pink", "violet"};

    char **ptr[] = {s+3, s+2, s+1, s}, ***p;

    p = ptr;

    ++p;

    printf("%s", **p+1);

    return 0;

}

A. ink

B. ack

C. ite

D. let

2. What will be the output of the program ?

#include<stdio.h>

int main()

{

    int x=30, *y, *z;

    y=&x; /* Assume address of x is 500 and integer is 4 byte size */

    z=y;

    *y++=*z++;

    x++;

    printf("x=%d, y=%d, z=%d\n", x, y, z);

    return 0;

}

A. x=31, y=502, z=502

B. x=31, y=500, z=500

C. x=31, y=498, z=498

D. x=31, y=504, z=504

3. What will be the output of the program ?

#include<stdio.h>

int main()

{

    char str[20] = "Hello";

    char *const p=str;

    *p='M';

    printf("%s\n", str);

    return 0;

}

A. Mello

B. Hello

C. HMello

D. MHell

4. What will be the output of the program If the integer is 4bytes long?

#include<stdio.h>

int main()

{

    int ***r, **q, *p, i=8;

    p = &i;

    q = &p;

    r = &q;

    printf("%d, %d, %d\n", *p, **q, ***r);

    return 0;

}

A. 8, 8, 8

B. 4000, 4002, 4004

C. 4000, 4004, 4008

D. 4000, 4008, 4016

5. What will be the output of the program?

#include<stdio.h>

int main(){

    int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};

    int *p, *q;

    p = &arr[1][1][1];

    q = (int*) arr;

    printf("%d, %d\n", *p, *q);

    return 0; }

A. 8, 10

B. 10, 2

C. 8, 1

D. Garbage values