Two Dimensional Array In C Mcq Exercise 1

  • A single column of elements
  • A single row of elements
  • None of the above

Explanation:

The right choice is alternative "a". In the C programming language, a two-dimensional array can be depicted as a grid with both rows and columns, or as a matrix. Two separate indices are employed, one for rows and one for columns, to access any specific element within this matrix.

Using the declaration int arr3;, a two-dimensional array with three rows and four columns has been displayed.

  1. What is the declaration syntax for a two-dimensional array in C?
  • int arr[10, 10];
  • int arr[10];
  • int arr10;
  • int arr10[10];

Explanation:

The correct answer is option "c". It is correct to declare a two-dimensional array in C using arr10. The defined array may store a total of 100 integer elements (10 * 10) because it consists of 10 rows and 10 columns.

  1. In a 2D array arr3, how do you access the element in the first row and second column?
  • int arr2 = {1, 2, 3, 4};
  • int arr2 = [1, 2, 3, 4];
  • int arr2 = {{1, 2}, {3, 4}};
  • int arr2 = {{1, 2, 3, 4}};

Explanation:

The correct answer is option "c". With the values provided, a 2x2 array is appropriately initialized. The entire array is enclosed by the outer braces {}, and each row is enclosed by the inner braces {{1, 2}, {3, 4}}.

  1. In a 2D array arr3, how do you access the element in the first row and second column?
  • arr0
  • arr0
  • arr2
  • arr1

Explanation:

The correct answer is option "b". In a 2D array, the index of the first row is 0. In a 2D array, the index is 1 for the second column. Therefore, the indices 0 for the row and 1 for the column are used to access the elements in the first row and second column. This method is used to accesses the elements in the first row (0) and second column (1), which is the correct way to access the desired element.

  1. Which of the following approaches for passing a 2D array to a function is correct?
  • void function(int array3);
  • void function(int *array);
  • void function(int array);
  • void function(int array );

Explanation:

The accurate choice is option "d". The correct method for defining a function is as illustrated. In the scenario of a two-dimensional array with a set number of columns and an unspecified number of rows, the value assigned here is 3. The function can effectively retrieve elements within each row as it has knowledge of the column count.

  1. What is the expected result of the provided code snippet?
  2. Example
    
    int arr[2][2] = {{1, 2}, {3, 4}}; 
    printf("%d", arr[1][0]);
    

Explanation:

The right choice is alternative "b". Accessing the element positioned in the first column (0) and the second row (1) is achievable by using arr1. Considering the initialization, the value stored at arr1 is 3. Consequently, the program will display the value 3.

  1. What is the maximum number of elements that can be stored in an int arr4?

Explanation:

Multiplying the total number of rows by the total number of columns provides the overall count of elements within the array. In this instance, there are 4 rows and 5 columns.

Now, elements total = 4 * 5 = 20. The int arr4 can store 20 elements. Therefore, 20 is the correct answer.

  1. What is the correct syntax in C to allocate a 2D array dynamically?
  • int *array = malloc(4 sizeof(int)); for(int i = 0; i < 4; i++) array[i] = malloc(5 sizeof(int));
  • int *array = malloc(4 5 * sizeof(int));
  • int array = malloc(4 5 sizeof(int));
  • int array = malloc(4 sizeof(int));

Explanation:

Memory allocation for a two-dimensional array in C can be dynamically achieved through a two-step process. Initially, memory is allocated for an array of pointers where each pointer corresponds to a row. Subsequently, memory is assigned for each individual row. This approach allows for separate access to each row, effectively transforming the array into a genuine two-dimensional structure.

Input Required

This code uses input(). Please provide values below: