Explanation:
The correct answer is option "a". An uninitialized element in a 2D array of integers in C has an undefined default value. C does not initialize variables or elements of arrays automatically. Thus, uninitialized elements include "garbage" values, which are the residual information that was previously stored in that memory location. Results from accessing these elements are not always predictable. Programmers have to explicitly initialize array elements to prevent this.
- How can a 2D array copied to another array?
- dest = src;
- for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) desti = srci;
- memcpy(dest, src, sizeof(src));
- None of the above
Explanation:
The correct answer is option "b". In C++, each element of the source array (src) must be iterated through and assigned to the matching element in the destination array (dest) in order to properly duplicate a 2D array. To ensure that every value from "src" gets transferred to dest, it accesses each element in both dimensions of the array using nested loops.
- Which of the following method deallocates a dynamically created 2D array correctly?
- for (int i = 0; i < cols; i++) free(arr[i]); free(arr);
- free(arr);
- free(arr); free(arr[0]);
- for (int i = 0; i < rows; i++) free(arr[i]); free(arr);
Explanation:
The correct answer is option "d". The most effective way to work with a dynamically produced 2D array in C is to use the for (int i = 0; i < rows; i++) free(arr[i]); free(arr); function. Iterating through each row first, this strategy frees up memory for each sub-array. After the release of each sub-array, it releases the memory allotted to the array of pointers itself. Memory leaks are avoided and the correct deallocation of all dynamically allocated memory is ensured by doing this.
- How can a 2D array arr be initialized so that all of its elements are set to zero?
- int arr3 = {0, 0, 0, 0, 0, 0, 0, 0, 0};
- int arr3 = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
- int arr3 = {0};
- All of the above
Explanation:
The correct answer is option "b". Using int arr3 = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; is an effective way to initialize a 2D array in C with all elements set to zero. This approach explicitly sets each element to zero through a nested brace-enclosed list, ensuring clear and comprehensive initialization. Each sub-array {0, 0, 0} represents a row, guaranteeing that all 3 rows and 3 columns are zeroed out. This method is both simple and reliable, ensuring consistency across different compilers and environments.
- When allocating memory for a 2D array in C, which of the following statements is correct?
- int *arr = malloc(rows sizeof(int )); for (int i = 0; i < rows; i++) arr[i] = malloc(cols sizeof(int));
- int arr = malloc(rows cols * sizeof(int));
- int arr = malloc(rows sizeof(int)); for (int i = 0; i < rows; i++) arr[i] = malloc(cols * sizeof(int));
- int *arr = malloc(rows cols * sizeof(int));
Explanation:
The correct answer is option "a".
Memory Allocation for Row Pointers: When utilizing the malloc(rows sizeof(int )) method, memory is assigned for an array consisting of pointers to integers (int *). Each of these pointers is responsible for pointing to an individual row within the 2D array.
Memory Allocation for Each Row: A loop (for (int i = 0; i < rows; i++)) is used to allocate memory for each row individually once memory for the array of pointers has been allocated. The arr[i] = malloc(cols * sizeof(int)); method allocates memory for the 2D array's columns, cols, which are integers, and assigns arr[i] the address.
- Considering 2D arrays in C, which of the following statement is true?
- 2D arrays are always passed to functions by reference.
- The number of columns must be specified when declaring a 2D array.
- Memory for a 2D array is allocated in a non-contiguous manner.
- 2D arrays in C are implemented as arrays of pointers to arrays.
Explanation:
The accurate choice is alternative "d". A 2D array in C is commonly structured as a contiguous memory block, where each subsequent row directly follows the previous one. Memory location for array indexing (arri) is determined by pointer arithmetic performed by the compiler. Conversely, an array of pointers (int **arr) offers increased versatility in memory administration as each pointer directs to a distinct memory block.