Find Output Of Program

Question 6 :

What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?

#include < stdio.h >
int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}


A). 65474, 65476
B). 65480, 65496
C). 65480, 65488
D). 65474, 65488
Answer : Option B

Explanation :

Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.
Step 2: printf("%u, %u\n", a+1, &a+1);

The base address(also the address of the first element) of array is 65472.
For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480
Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496

Hence the output of the program is 65480, 65496


Question 7 :

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

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.


Question 8 :

What will be the output of the program ?

#include < stdio.h >
int main()
{
    int arr[1]={10};
    printf("%d\n", 0[arr]);
    return 0;
}


A). 1
B). 10
C). 0
D). 6
Answer : Option B

Explanation :

Step 1: int arr[1]={10}; The variable arr[1] is declared as an integer array with size '2' and it's first element is initialized to value '10'(means arr[0]=10)
Step 2: printf("%d\n", 0[arr]); It prints the first element value of the variable arr.

Hence the output of the program is 10.


Question 9 :

What will be the output of the program if the array begins at address 65486?

#include < stdio.h >
int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return 0;
}


A). 65486, 65488
B). 65486, 65486
C). 65486, 65490
D). 65486, 65487
Answer : Option B

Explanation :

Step 1: int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized.
Step 2: printf("%u, %u\n", arr, &arr); Here,
The base address of the array is 65486.
=> arr, &arr is pointing to the base address of the array arr.

Hence the output of the program is 65486, 65486


Question 10 :

What will be the output of the program ?

#include < stdio.h >
int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%d\n", sizeof(arr)/sizeof(arr[0]));
    return 0;
}


A). 5
B). 4
C). 6
D). 7
Answer : Option B

Explanation :

The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes

Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point array and it is initialized with the values.
Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0]));
The variable arr has 4 elements. The size of the float variable is 4 bytes.

Hence 4 elements x 4 bytes = 16 bytes
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is '4'.