Find Output Of Program

Question 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
Answer : Option C

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


Question 2 :

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
Answer : Option C

Question 3 :

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
Answer : Option B

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


Question 4 :

What will be the output of the program ?

#include < stdio.h >
void fun(int **p);

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%d\n", **p);
}


A). 1
B). 2
C). 3
D). 4
Answer : Option A

Explanation :

Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; The variable a is declared as an multidimensional integer array with size of 3 rows 4 columns.
Step 2: int *ptr; The *ptr is a integer pointer variable.
Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer variable *ptr.
Step 4: fun(&ptr); Now, the &ptr contains the base address of array a.
Step 5: Inside the function fun(&ptr); The printf("%d\n", **p); prints the value '1'.

because the *p contains the base address or the first element memory address of the array a (ie. a[0])
**p contains the value of *p memory location (ie. a[0]=1).

Hence the output of the program is '1'


Question 5 :

What will be the output of the program ?

#include < stdio.h >
int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}


A). 0, 0, 0
1, 1, 1
2, 2, 2
3, 3, 3
B). 1, 1, 2
2, 2, 3
3, 3, 4
4, 4, 1
C). 1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
D). 0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
Answer : Option C