Arrays And Strings(C Program ) Questions and Answers

Question 1. What is right way to Initialize array?
  1.    int num[6] = { 2, 4, 12, 5, 45, 5 };
  2.    int n{} = { 2, 4, 12, 5, 45, 5 };
  3.    int n{6} = { 2, 4, 12 };
  4.    int n(6) = { 2, 4, 12, 5, 45, 5 };
Explanation:-
Answer: Option A. -> int num[6] = { 2, 4, 12, 5, 45, 5 };
option (B), (C) and (D) are incorrect because array declaration syntax is wrong. Only square brackets([]) must be used for declaring an array.

Question 2. What will be the output of the program ?
#include
void 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);
}
  1.    3, 2, 15
  2.    2, 3, 20
  3.    2, 1, 15
  4.    1, 2, 5
Explanation:-
Answer: Option A. -> 3, 2, 15
>> 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.
>> int i, j, m; The variable i, j, m are declared as an integer type.
>> i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
>> j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
>> m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
>> 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 3. Let x be an array. Which of the following operations are illegal?
I.   ++x
II. x+1
III. x++
IV. x*2
  1.    I and II
  2.    I, II and III
  3.    II and III
  4.    I, III and IV
  5.    III and IV
Explanation:-
Answer: Option D. -> I, III and IV
int x[10]; * x will store the base address of array. *
Statement I, III and IV is invalid.
Statement I and III : ++x and x++ are throwing en error while compile (lvalue required as increment operand )
Since, x is storing in the address of the array which is static value which cannot be change by the operand.
Statement IV : x*2 is also throw an error while compile (invalid operands to binary * (have 'int *' and 'int') )
Statement II : x+1 is throw a warning: assignment makes integer from pointer without a cast [enabled by default]

Question 4. What will be the output of following program code?
#include
int main(void)
{
char p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf + 1)[5];
printf("%d", p);
return 0;
}
  1.    5
  2.    6
  3.    9
  4.    Error
  5.    None of the above
Explanation:-
Answer: Option C. -> 9
x[i] is equivalent to *(x + i),
so (buf + 1)[5] is *(buf + 1 + 5), i.e. buf[6].

Question 5. An array elements are always stored in ________ memory locations.
  1.    Sequential
  2.    Random
  3.    Sequential and Random
  4.    None of the above
Explanation:-
Answer: Option A. -> Sequential

Question 6. What will be the output of the following code?
void main()
{
int a[10];
printf("%d %d", a[-1], a[12]);
}
  1.    0 0
  2.    Garbage value 0
  3.    0 Garbage Value
  4.    Garbage vlaue Garbage Value
  5.    Code will not compile
Explanation:-
Answer: Option D. -> Garbage vlaue Garbage Value
In c compiler does not check array with its bounds, value at the computed location is displayed.

Question 7. Array passed as an argument to a function is interpreted as
  1.    Address of the array.
  2.    Values of the first elements of the array.
  3.    Address of the first element of the array.
  4.    Number of element of the array.
Explanation:-
Answer: Option C. -> Address of the first element of the array.

Question 8. What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include
void main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u", a+1, &a+1);
}
  1.    65474, 65488
  2.    65480, 65488
  3.    65480, 65496
  4.    65474, 65476
  5.    None of these
Explanation:-
Answer: Option C. -> 65480, 65496
>> 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.
>> 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

Question 9. What does the following declaration mean?
int (*ptr)[10];
  1.    ptr is array of pointers to 10 integers
  2.    ptr is a pointer to an array of 10 integers
  3.    ptr is an array of 10 integers
  4.    ptr is an pointer to array
Explanation:-
Answer: Option B. -> ptr is a pointer to an array of 10 integers

Question 10. What will be the output of the program ?
#include
int main()
{
int arr[1] = {10};
printf("%d", 0[arr]);
return 0;
}
  1.    1
  2.    0
  3.    10
  4.    6
  5.    None of these
Explanation:-
Answer: Option C. -> 10
>> int arr[1]={10}; The variable arr[1] is declared as an integer array with size '2' i.e. arr[0] and arr[1] and it's first element is initialized to value '10'(means arr[0]=10) and arr[1] = garbage value or zero
>> printf("%d", 0[arr]); It prints the first element value of the variable arr.
Hence the output of the program is 10.