Week 6 Assignment Solution
1. What does the expression sizeof(arr) / sizeof(arr[0]) evaluate to, where arr is an integer array of 100
elements?
a) 100
b) 400
c) 25
d) 4
Answer: a) 100
Explanation: The size of operator gives the total size in bytes of the array. For an integer array arr[100],
assuming an integer is 4 bytes, sizeof(arr) equals 400 bytes. sizeof(arr[0]) equals 4 bytes (size of one
integer). Thus, 400 / 4 evaluates to 100, giving the number of elements in the array.
2. Considering a two-dimensional array declared as int arr[3][4];, what is the correct way to access the
second element of the third row?
a) arr[3][2]
b) arr[2][1]
c) arr[2][3]
d) arr[1][2]
Answer: b) arr[2][1]
Explanation: In C, array indexing starts from 0. So, the third row is indexed as 2 (arr[2]) and the second
element of this row is indexed as 1 (arr[2][1]).
3. How does a C program react when an attempt is made to access an array element using a floating-point
index, such as arr[1.5]?
a) The element corresponding to the floor of the index is accessed.
b) The element corresponding to the ceiling of the index is accessed.
c) A syntax error is reported by the compiler.
d) The program executes successfully without errors.
Answer: c) A syntax error is reported by the compiler.
Explanation: C does not allow non-integer index values for arrays. Using a floating-point number as an
index will result in a syntax error during compilation.
4. In a C program, what is the outcome of declaring an array with a negative size, such as int arr[-10];?
a) The compiler will treat it as a positive size.
b) The array will be initialized with zero elements.
c) The compiler will generate an error.
d) A memory allocation error will occur at runtime.
Answer: c) The compiler will generate an error.
Explanation: Declaring an array with a negative size is illegal in C, and it will cause a compile-time error.
The size of an array must be a positive integer.
5. An integer array of size 15 is declared in a C program. The memory location of the first byte of the
array is 2000. What will be the location of the 13th element of the array?
Week 6 Assignment Solution
a) 2013
b) 2024
c) 2026
d) 2030
Explanation: (b) Integer takes two bytes of memory. As the memory assignment to the elements is
consecutive and the index starts from 0, the 13th element will be located at 2000+(12×2) =2024.
6. The elements of an array are stored in contiguous memory due to
a) This way, the computer can keep track of only the address of the first element, and the
addresses of other elements can be calculated.
b) The architecture of the computer does not allow arrays to store other than serially
c) Both
d) None
Explanation: (a) This way, the computer can keep track of only the address of the first element, and the
addresses of other elements can be calculated easily.
7. What is the effect of executing the statement int arr[5] = {1, 2}; in C?
a) The first two elements are set to 1 and 2, rest are uninitialized.
b) The first two elements are set to 1 and 2, rest are set to 0.
c) All elements are set to 1 and 2 alternatively.
d) The compiler generates an error.
Answer: B) The first two elements are set to 1 and 2; the rest are set to 0.
Explanation: In C, when an array is partially initialized, the uninitialized elements are automatically
initialized to 0.
8. How can you pass an entire array to a function in C?
a) By specifying the size of the array in the function parameter.
b) By passing the address of the first element of the array.
c) By declaring the function parameter as an array size.
d) By passing the array name only.
Answer: D) By passing the array name only.
Explanation: In C, an array can be passed to a function by simply passing the array name. This passes a
pointer to the first element of the array, effectively passing the entire array by reference.
9. Find the output of the following C program
#include<stdio.h>
int main()
{
int a;
int arr[5] = {1, 2, 3, 4, 5};
arr[1] = ++arr[1];
a = arr[1]++;
arr[1] = arr[a++];
printf("%d,%d", a, arr[1]);
return 0;
}
a) 5,4
Week 6 Assignment Solution
b) 5,5
c) 4,4
d) 3,4
Answer: (c)
Explanation: The statement arr[1] = ++arr[1]; increments arr[1] (which is 2) before assignment.
So, arr[1] becomes 3. Then, a = arr[1]++; assigns arr[1] (which is now 3) to a and then
increments arr[1] to 4. So, a is 3. arr[1] = arr[a++]; assigns the value at arr[a] (which is
arr[3], value 4) to arr[1], and then a is incremented to 4. Finally, the program prints a and arr[1],
which are 4 and 4, respectively.
10. What will be the output?
#include <stdio.h>
int main()
{
int arr[]={1,2,3,4,5,6};
int i,j,k;
j=++arr[2];
k=arr[1]++;
i=arr[j++];
printf("i=%d, j=%d, k=%d", i,j,k);
return 0;
}
a) i=5, j=5, k=2
b) i=6, j=5, k=3
c) i=6, j=4, k=2
d) i=5, j=4, k=2
Explanation: (a) k=arr[1]++ due to post increment operation, assignment is done first. so it actually becomes
k=arr[1]=2. j=++arr[2]=++3=4. i=arr[j++]=arr[4++]=arr[4]=5 (as its post increment hence assignment is
done first). Due to post increment in i=arr[j++], value of j is also incremented and finally becomes 5. So,
finally i=5, j=5, k=2.