RejinpaulPlacement
Search

Arrays - C Programming Questions and Answers - RejinpaulPlacement

@ : Home > C Programming > Arrays


Find us on Facebook



Follow us on Google+


Google+

1.
In C, if you pass an array as an argument to a function, what actually gets passed?
Answer & Solution
Answer: (C) Base address of the array
Solution: The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.
close
2.
What does the following declaration mean?
int (*ptr)[10];
Answer & Solution
Answer: (B) ptr is a pointer to an array of 10 integers
Solution: No answer description available for this question.
close
3.
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
Answer & Solution
Answer: (C) The program may crash if some important data gets overwritten.
Solution: If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors.
close
4.
What will be the output of the program if the array begins 1200 in memory?


#include
int main()
{
int arr[]={2, 3, 4, 1, 6};
printf("%u, %u, %u\n", arr, &arr[0], &arr);
return 0;
}
Answer & Solution
Answer: (B) 1200, 1200, 1200
Solution: Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer array and initialized.
Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here,
The base address of the array is 1200.
=> arr, &arr is pointing to the base address of the array arr.
=> &arr[0] is pointing to the address of the first element array arr. (ie. base address)
Hence the output of the program is 1200, 1200, 1200
close
5.
What will be the output of the program ?


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

Answer & Solution
Answer: (B) 4
Solution: 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' .
close