Sail E0 Webinar

MCQs

Total Questions : 15 | Page 2 of 2 pages
Question 11.


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;
}
  1.    5
  2.    4
  3.    6
  4.    7
 Discuss Question
Answer: Option B. -> 4

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("%dn", 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'.


Question 12.


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
  1.    1, 2, 3, 4, 5,
  2.    Garbage value, 1, 2, 3, 4,
  3.    0, 1, 2, 3, 4,
  4.    2, 3, 4, 5, 6,
 Discuss Question
Answer: Option B. -> Garbage value, 1, 2, 3, 4,

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 13.


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;
}
  1.    65486, 65488
  2.    65486, 65486
  3.    65486, 65490
  4.    65486, 65487
 Discuss Question
Answer: Option B. -> 65486, 65486

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`setminus`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 14.


What will be the output of the program ?


#include<stdio.h>
int main()
{
int arr[1]={10};
printf("%d\n", 0[arr]);
return 0;
}
  1.    1
  2.    10
  3.    0
  4.    6
 Discuss Question
Answer: Option B. -> 10

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`setminus`n", 0[arr]); It prints the first element value of the variable arr.

Hence the output of the program is 10.


Question 15.


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;
}
  1.    65474, 65476
  2.    65480, 65496
  3.    65480, 65488
  4.    65474, 65488
 Discuss Question
Answer: Option B. -> 65480, 65496

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`setminus`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



Latest Videos

Latest Test Papers