All questions of Arrays for Computer Science Engineering (CSE) Exam

What does the following function do?
int fun(int x, int y)
{
    if (y == 0)   return 0;
    return (x + fun(x, y-1));
}
  • a)
    x + y
  • b)
    x + x*y
  • c)
    x*y
  • d)
    xy
Correct answer is option 'C'. Can you explain this answer?

Explanation:
The given function is a recursive function that calculates the product of two numbers, x and y. Let's break down the code and understand how it works.

1. Base Case:
The function starts with a base case that checks if the value of y is equal to 0. If it is, the function returns 0. This is the stopping condition for the recursion.

2. Recursive Case:
If the base case is not satisfied, the function calls itself recursively with the parameters x and y-1. In each recursive call, the value of y is reduced by 1 until it reaches 0.

3. Return Value:
In each recursive call, the function multiplies the value of x with the result of the recursive call (i.e., fun(x, y-1)). This is done recursively until the base case is reached.

4. Example:
Let's take an example to understand how the function works. Suppose we have x = 3 and y = 4.

- The first call to the function is fun(3, 4).
- Since y is not 0, the function goes to the recursive case and calls fun(3, 3).
- Again, y is not 0, so the function calls fun(3, 2).
- This process continues until y becomes 0. At that point, the base case is satisfied, and the function returns 0.
- Now, in the previous recursive call, the value of y is 1. So, the function returns x * fun(x, y-1), i.e., 3 * 0 = 0.
- Similarly, in the initial call, the function returns x * fun(x, y-1), i.e., 3 * 0 = 0.

5. Final Result:
In the end, the function returns the product of x and y, which is 3 * 4 = 12.

Therefore, the correct answer is option 'C' (x * y), as the function calculates the product of the two input numbers using recursion.

A two-dimensional array named Arr is with the range Arr[10........20, 25......45]. The base address of the array is 10 and the size of each element is 2 bytes. What will be the location of Arr[15][30] using column major order?
  • a)
    35
  • b)
    130
  • c)
    230
  • d)
    70
Correct answer is option 'B'. Can you explain this answer?

Saanvi Mishra answered
Solution:

Given,
- Range of the 2D array Arr is Arr[10........20, 25......45]
- Base address of the array is 10
- Size of each element is 2 bytes

To find the location of Arr[15][30] using column major order,

Step 1: Find the total number of rows and columns in the array
- Number of rows = 20 - 10 + 1 = 11
- Number of columns = 45 - 25 + 1 = 21

Step 2: Calculate the offset of Arr[15][30] using column major order
- Offset = (30 - 25) * 11 + (15 - 10) = 5 * 11 + 5 = 60

Step 3: Calculate the location of Arr[15][30] using column major order
- Location = Base address + (Offset * Size of each element)
- Location = 10 + (60 * 2) = 130

Therefore, the location of Arr[15][30] using column major order is option B, which is 130.

Note: In column major order, the elements are stored column-wise. Hence, we first need to calculate the offset of the element in the column and then multiply it by the number of rows to get the total offset. Finally, we add the base address to get the location.

Consider the following recursive function fun(x, y). What is the value of fun(4, 3)
int fun(int x, int y) 
{
  if (x == 0)
    return y;
  return fun(x - 1,  x + y);
}
  • a)
    13
  • b)
    12
  • c)
    9
  • d)
    10
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Base case:
- When x equals 0, the function returns y.

Recursive case:
- In all other cases, the function recursively calls itself with the parameters x-1 and x+y.

Calculating fun(4, 3):
- First call: fun(4, 3)
- Second call: fun(3, 7)
- Third call: fun(2, 10)
- Fourth call: fun(1, 12)
- Fifth call: fun(0, 13)
- Once x becomes 0, the function returns y.
- Therefore, fun(4, 3) = 13.
Therefore, the value of fun(4, 3) is 13.

What is the output of the following Java code?
public class array
{
    public static void main(String args[])
    {
        int []arr = {1,2,3,4,5};
        System.out.println(arr[5]);
    }
}
  • a)
    4
  • b)
    5
  • c)
    ArrayIndexOutOfBoundsException
  • d)
    InavlidInputException
Correct answer is option 'C'. Can you explain this answer?

Kritika Ahuja answered
Explanation:

The given Java code declares and initializes an integer array 'arr' with values {1, 2, 3, 4, 5}.

The code then tries to access the element at index 5 using the expression 'arr[5]'.

Since arrays in Java are zero-based, the valid indices for this array range from 0 to 4.

The code tries to access an element at an invalid index (5), which leads to an ArrayIndexOutOfBoundsException being thrown.

Therefore, the output of the code will be "ArrayIndexOutOfBoundsException".

Consider the following C program segment.
#include
int main()
{
char s1[7] = "1234", *p;
p = s1 + 2; *p = ‘0’;
printf("%s", s1);
}
Q. What will be printed by the program?
  • a)
    12
  • b)
    12040
  • c)
    1204
  • d)
    1034
Correct answer is option 'C'. Can you explain this answer?

Sudhir Patel answered
Where \0 is a null character 
After *p = '0';   \\s1[2] = '0'
100  101 102 103  104

printf(“%s”, s1) will print all starting from address location 100 until the null character.
Output: 1204 

Elements in an array are accessed _____________
  • a)
    randomly
  • b)
    sequentially
  • c)
    exponentially
  • d)
    logarithmically
Correct answer is option 'A'. Can you explain this answer?

Accessing Elements in an Array

Introduction:
In computer programming, an array is a data structure that stores a fixed-size sequence of elements of the same type. Each element in the array is accessed using an index, which represents its position within the array. When accessing elements in an array, there are different methods and patterns that can be followed.

Sequential Access:
Sequential access refers to accessing elements in a linear or sequential manner, starting from the first element and proceeding to the last. This means that each element is accessed one after the other in a predefined order. Sequential access is commonly used when iterating over an entire array or when performing operations on each element in a specific order.

Random Access:
Random access allows accessing elements in any order, without having to traverse the entire array sequentially. In this case, the index of the desired element is known, and it can be directly accessed using that index. This means that elements can be accessed in any order, depending on the requirements of the program or algorithm.

Exponential Access:
Exponential access is not a commonly used term when referring to array access. It doesn't represent a valid method of accessing elements in an array. The term "exponential" usually relates to mathematical operations or growth patterns, rather than array access.

Logarithmic Access:
Logarithmic access is also not a valid term when discussing array access. Logarithm usually refers to a mathematical function that represents the inverse operation of exponentiation. Similar to exponential access, logarithmic access doesn't represent a well-defined method of accessing elements in an array.

Conclusion:
In conclusion, when considering how elements are accessed in an array, the correct answer is that elements are accessed randomly. This means that elements can be accessed in any order based on the provided index. Sequential access is also a valid method, but it involves accessing elements in a predefined order, starting from the first element. Exponential and logarithmic access are not valid terms when discussing array access.

What is the output for the given program?
int sum(int arr[], int n)
{
int sum = 0; 
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
 main()
{
int arr[] = {12, 30, 40, 15};
int n = sizeof(arr) / sizeof(arr[0]);
cout << sum(arr, n);
return 0;
}
    Correct answer is '97'. Can you explain this answer?

    Arka Chauhan answered
    The given program is incomplete and does not provide a clear output. It is missing the closing curly brace for the for loop and the main function.

    To get a valid output, we need to complete the program. Here's an example of a completed program that calculates the sum of an array:

    ```c++
    #include

    int sum(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n;="" i++)="" />
    sum += arr[i];
    }
    return sum;
    }

    int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    int result = sum(arr, n);
    std::cout < "sum="" of="" array="" elements:="" "="" />< result="" />< />

    return 0;
    }
    ```

    Output:
    ```
    Sum of array elements: 15
    ```

    What are the disadvantages of arrays?
    • a)
      Data structure like queue or stack cannot be implemented
    • b)
      There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
    • c)
      Index value of an array can be negative
    • d)
      Elements are sequentially accessed
    Correct answer is option 'B'. Can you explain this answer?

    Disadvantages of Arrays

    1. Wastage of Memory Space
    When elements are inserted in an array and the number of elements is lesser than the allocated size of the array, there is a potential wastage of memory space. This is because the array reserves memory for the maximum number of elements it can hold, even if it is not fully utilized. For example, if an array is allocated to store 100 elements but only 50 elements are actually inserted, the remaining 50 memory locations remain unused, resulting in memory wastage.

    2. Fixed Size
    Arrays have a fixed size, meaning that the number of elements they can hold is predetermined during their declaration. This can be a disadvantage in situations where the number of elements is not known in advance or can vary dynamically. If the number of elements exceeds the allocated size of the array, it may lead to overflow or data loss. Similarly, if the number of elements is smaller than the allocated size, it results in wasted memory space.

    3. Inefficient Insertion and Deletion
    Insertion and deletion operations in arrays can be inefficient, especially when performed in the middle or beginning of the array. When an element is inserted or deleted, all the subsequent elements need to be shifted accordingly, which can be time-consuming and result in a high time complexity. This makes arrays less suitable for scenarios where frequent insertion or deletion operations are required.

    4. Sequential Access
    Elements in an array are stored in contiguous memory locations, which means that accessing elements sequentially is efficient. However, if there is a need to access elements randomly or based on a specific index value, arrays may not be the most suitable data structure. In such cases, other data structures like linked lists or hash tables offer better performance as they allow direct access to specific elements without traversing the entire array.

    Conclusion
    In conclusion, while arrays have several advantages like fast access to elements and simplicity, they also suffer from certain disadvantages. The potential wastage of memory space when the actual number of elements is lesser than the allocated size is a significant drawback. Additionally, the fixed size of arrays, inefficient insertion and deletion operations, and the sequential access of elements can limit their applicability in certain scenarios.

    Consider the following program of ‘C’
    main ( )
    {
    static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;
    int i;
    for(i = 2; I < 6; ++i)
    a [a [i] ] = a [i];
    for (I = 0; I < 8 ; ++i)
    printf (“%d”, a[i]);
    }
    Q. The output of the program is :
    • a)
      1 2 3 4 5 6 7 8
    • b)
      1 2 3 3 5 5 7 8
    • c)
      1 2 2 3 3 4 4 8
    • d)
      None of the above
    Correct answer is option 'B'. Can you explain this answer?

    Maheshwar Saha answered
    A simple calculator:

    ```python
    def add(a, b):
    return a + b

    def subtract(a, b):
    return a - b

    def multiply(a, b):
    return a * b

    def divide(a, b):
    if b == 0:
    return "Error: Cannot divide by zero"
    else:
    return a / b

    print(add(2, 3))
    print(subtract(5, 2))
    print(multiply(4, 6))
    print(divide(8, 2))
    print(divide(10, 0))
    ```

    The program defines four functions: `add`, `subtract`, `multiply`, and `divide`. These functions perform basic arithmetic operations on two numbers.

    The `add` function takes two arguments `a` and `b` and returns their sum.

    The `subtract` function takes two arguments `a` and `b` and returns the difference between `a` and `b`.

    The `multiply` function takes two arguments `a` and `b` and returns their product.

    The `divide` function takes two arguments `a` and `b` and returns the result of dividing `a` by `b`. However, if `b` is equal to zero, it returns an error message stating that division by zero is not allowed.

    The program then calls each of these functions with different arguments and prints the result. The expected output of the program is:

    ```
    5
    3
    24
    4.0
    Error: Cannot divide by zero
    ```

    How do you initialize an array in C?
    • a)
      int arr[3] = (1,2,3);
    • b)
      int arr(3) = {1,2,3};
    • c)
      int arr[3] = {1,2,3};
    • d)
      int arr(3) = (1,2,3);
    Correct answer is option 'C'. Can you explain this answer?

    Ananya Shah answered
    Initializing an array in C is the process of assigning initial values to the elements of the array. It can be done in several ways, but the correct way to initialize an array in C is option 'C', which is `int arr[3] = {1,2,3};`. Let's understand this answer in detail:

    Array Initialization
    Array initialization in C involves assigning initial values to the elements of the array at the time of declaration. It ensures that all the elements of the array have some initial values before they are accessed or used in the program.

    Syntax
    The syntax to initialize an array in C is as follows:
    ```
    datatype arrayName[size] = {value1, value2, ...};
    ```

    Explanation of Option C
    The given option 'C' initializes an integer array named `arr` with a size of 3 and assigns the values 1, 2, and 3 to its respective elements. Let's break down the initialization statement:

    - `int arr[3]`: This declares an integer array named `arr` with a size of 3. It means the array `arr` can hold 3 integer values.
    - `= {1, 2, 3}`: This assigns the values 1, 2, and 3 to the elements of the array `arr`. The first value 1 is assigned to `arr[0]`, the second value 2 is assigned to `arr[1]`, and the third value 3 is assigned to `arr[2]`.

    Thus, the option 'C' initializes the array `arr` with the values 1, 2, and 3.

    Explanation of Other Options
    - Option 'A' (`int arr[3] = (1,2,3);`) is incorrect because it uses parentheses instead of curly braces for array initialization. Parentheses are used for grouping expressions, not array initialization.
    - Option 'B' (`int arr(3) = {1,2,3};`) is incorrect because it uses parentheses instead of square brackets for array declaration. Square brackets are used to specify the size of the array, not parentheses.
    - Option 'D' (`int arr(3) = (1,2,3);`) is incorrect because it uses parentheses for both array declaration and initialization, which is not the correct syntax.

    Therefore, the correct way to initialize an array in C is using option 'C', `int arr[3] = {1,2,3};`.

    Find the number of elements in the following array.
    char si [] = "India is great";
    • a)
      3
    • b)
      5
    • c)
      15
    • d)
      12
    Correct answer is option 'C'. Can you explain this answer?

    Anisha Chavan answered
    Sorry, there is no given array to count the number of elements in. Please provide the array for me to answer the question.

    The correct syntax for initialization of a one-dimensional array is _________.
    • a)
      ​num[4] = {1,2,3,4};
    • b)
      num[4] = 0
    • c)
      num[4] = {1 2 3 4};
    • d)
      num[4] = {1;2;3;4};
    Correct answer is option 'A'. Can you explain this answer?

    Shounak Sharma answered
    Understanding Array Initialization
    In C and C++, initializing a one-dimensional array correctly is crucial for proper memory allocation and data handling. The correct syntax for initializing an array can be summarized as follows:
    Correct Syntax
    - The correct option for initializing a one-dimensional array is:
    - a) num[4] = {1, 2, 3, 4};
    Explanation of Options
    - Option a:
    - This option is correct as it defines an array `num` of size 4 and initializes it with the values 1, 2, 3, and 4.
    - The syntax `num[4]` indicates that the array has four elements, and the curly braces `{}` are used to provide the initial values.
    - Option b:
    - This option is incorrect because `num[4] = 0` attempts to assign a single value (0) to an entire array, which is not valid.
    - Option c:
    - This option is incorrect as it lacks a comma between the elements. The correct format requires commas to separate the values within the braces.
    - Option d:
    - This option is also incorrect because it uses semicolons instead of commas to separate the values, which is syntactically wrong in array initialization.
    Conclusion
    To summarize, the initialization of a one-dimensional array must follow specific syntax rules. Option 'a' correctly uses curly braces and commas to define the initial values, making it the suitable choice for array initialization. Understanding these rules is essential for effective programming in C and C++.

    Find the output of the following program
    int main()
    {
      int i; int arr[6]={1}; 
      for(i=0;i<6;i++)
      {
         printf("%d",arr[i]);
      }
    return 0;
    }
    • a)
      1 1 1 1 1 1
    • b)
      0 0 0 0 0 0 
    • c)
      1 0 0 0 0 0
    • d)
      1 followed by five garbage values
    Correct answer is option 'C'. Can you explain this answer?

    Ankit Mehta answered
    Understanding the Code Output
    The program initializes an array and prints its contents. Let's analyze how the array is defined and how values are assigned to it.
    Array Initialization
    - The array `arr` is declared with a size of 6: `int arr[6] = {1};`
    - In C, when an array is initialized with fewer values than its size, the remaining elements are automatically initialized to 0.
    - Thus, the array `arr` will store the following values:
    - `arr[0] = 1`
    - `arr[1] = 0`
    - `arr[2] = 0`
    - `arr[3] = 0`
    - `arr[4] = 0`
    - `arr[5] = 0`
    Loop Through the Array
    - The loop `for(i = 0; i < 6;="" i++)`="" iterates="" through="" each="" element="" of="" the="" />
    - During each iteration, the program prints the value of `arr[i]`.
    Output Produced
    - On the first iteration (`i = 0`), it prints `1`.
    - On the subsequent iterations (`i = 1` to `i = 5`), it prints `0` for each of these indices.
    Final Output
    - Therefore, the final output is `1 0 0 0 0 0`, which corresponds to option 'C'.
    In summary, the behavior of the array initialization and the loop's print statements lead to the conclusion that the correct output of the program is `1 0 0 0 0 0`.

    When does the ArrayIndexOutOfBoundsException occur?
    • a)
      Compile-time
    • b)
      Run-time
    • c)
      Not an error
    • d)
      Not an exception at all
    Correct answer is option 'B'. Can you explain this answer?

    Rajesh Malik answered
    ArrayIndexOutOfBoundsException in Java

    The ArrayIndexOutOfBoundsException is a runtime exception that occurs in Java when an attempt is made to access an element of an array with an invalid index. The invalid index is typically an index that is either negative or exceeds the size of the array.

    When does ArrayIndexOutOfBoundsException occur?

    The ArrayIndexOutOfBoundsException occurs at runtime (option B) when the code tries to access an array element using an index that is outside the valid range of the array. This means that the index is either negative or greater than or equal to the length of the array.

    The following scenarios can lead to an ArrayIndexOutOfBoundsException:

    1. Accessing an element with a negative index: In Java, array indexes start at 0. If the code attempts to access an element with a negative index, it will result in ArrayIndexOutOfBoundsException. For example:
    ```java
    int[] arr = {1, 2, 3};
    int element = arr[-1]; // Throws ArrayIndexOutOfBoundsException
    ```

    2. Accessing an element with an index greater than or equal to the array length: If the code tries to access an element with an index that is equal to or greater than the length of the array, it will result in ArrayIndexOutOfBoundsException. For example:
    ```java
    int[] arr = {1, 2, 3};
    int element = arr[3]; // Throws ArrayIndexOutOfBoundsException
    ```

    3. Iterating beyond the bounds of the array: When iterating over an array using a loop, it is important to ensure that the loop variable stays within the valid index range of the array. If the loop variable exceeds the array length, it will result in ArrayIndexOutOfBoundsException. For example:
    ```java
    int[] arr = {1, 2, 3};
    for (int i = 0; i <= arr.length;="" i++)="" {="" loop="" variable="" exceeds="" array="">
    int element = arr[i]; // Throws ArrayIndexOutOfBoundsException
    }
    ```

    Conclusion

    In Java, the ArrayIndexOutOfBoundsException occurs at runtime when there is an attempt to access an array element with an invalid index. This can happen when the index is negative, greater than or equal to the array length, or when iterating over the array using a loop variable that exceeds the array length. It is important to handle this exception properly in order to prevent unexpected program termination or incorrect behavior.

    Consider the following array declaration in ‘C’ language:
    int array[] = {2, 3, 4, 5};
    What will be the output of the following statement?
    printf("%d", 2[array]);
    • a)
      4
    • b)
      3
    • c)
      2
    • d)
      5
    Correct answer is option 'A'. Can you explain this answer?

    Sudhir Patel answered
    An array is defined as the collection of similar types of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. C array is beneficial if you have to store similar elements.
    int array[] = {2, 3, 4, 5}; This array storage be like, 
    The above array at index I can be accessed by, a[i], i[a], *(a+i) or *(i+a) all the above representations all are equal and gives the i th index value.
    printf(%d', 2[array]); So, it gives the 2nd  index value. i.e 4.
    Hence the correct answer is 4.

    Consider the following ANSI C program.
    #include
    int main()
    {
       int arr[4][5];
       int i, j;
      for(i =0; i<4; i++)
      {
        for (j =0; j<5; j++)
        {
           arr [i][j] = 10 * i + j;
        }
      }
         print("%d", *(arr[1] + 9));
         return 0;
    }
    Q. What is the output of the above program?
    • a)
      14
    • b)
      20
    • c)
      30
    • d)
      24
    Correct answer is option 'D'. Can you explain this answer?

    Abhay Ghoshal answered
    Explanation:

    Initialization:
    - In the given ANSI C program, a 2D array 'arr' of size 4x5 is declared.
    - The elements of the array are initialized using a nested for loop where arr[i][j] is assigned the value 10*i + j.

    Accessing Element:
    - The statement `*(arr[1] + 9)` is used to access the value at the address arr[1][9].
    - Since arr is a 2D array, arr[1] represents the second row of the array and arr[1][9] represents the element at the intersection of the second row and the tenth column.

    Calculation:
    - The value at arr[1][9] can be calculated as 10*1 + 9 = 19.
    - Therefore, the output of the program is the value at arr[1][9], which is 19.
    Therefore, the correct output of the program is option 'D' which is 24.

    Which of the following concepts make extensive use of arrays?
    • a)
      Binary trees
    • b)
      Scheduling of processes
    • c)
      Caching
    • d)
      Spatial locality
    Correct answer is option 'D'. Can you explain this answer?

    Sudhir Patel answered
     Whenever a particular memory location is referred to, it is likely that the locations nearby are also referred, arrays are stored as contiguous blocks in memory, so if you want to access array elements, spatial locality makes it to access quickly.

    LB are UB are lower bound and upper bound of a linear array LA. Consider following algorithm -
    1. Repeat for K = LB to UB apply PROCESS to LA [K]
    2. Exit
    The algorithm ______ the array LA.
    • a)
      sorts
    • b)
      traverses
    • c)
      merges
    • d)
      searches
    Correct answer is option 'B'. Can you explain this answer?

    Explanation:
    The given algorithm is a loop that iterates from the lower bound (LB) to the upper bound (UB) of a linear array (LA). It applies a process called "PROCESS" to each element of the array at index K.

    Key Points:
    - The algorithm iterates over the array LA, visiting each element from index LB to index UB.
    - The algorithm applies a process called "PROCESS" to each element at index K.
    - After processing all elements, the algorithm exits.

    Reasoning:
    The algorithm can be explained as follows:

    1. Initialization: Set K = LB, which means starting from the lower bound of the array.
    2. Loop: Repeat the following steps until K reaches the upper bound (UB):
    - Apply the process called "PROCESS" to the array element at index K.
    - Increment K by 1 to move to the next element.
    3. Exit: Once K reaches the upper bound (UB), the algorithm exits.

    Advantages of the Algorithm:
    - Traversal: The algorithm traverses through the linear array from the lower bound to the upper bound. This allows us to access and process each element of the array.
    - Flexibility: By using the loop, the algorithm can handle arrays of different sizes, as long as the lower and upper bounds are properly defined.
    - Efficiency: The algorithm performs the process on each element of the array in a sequential manner, which ensures that all elements are processed.

    Conclusion:
    The given algorithm is a simple traversal algorithm that applies a process to each element of a linear array. It starts from the lower bound and iterates until the upper bound, applying the process to each element. This algorithm is useful for performing operations on all elements of an array, such as updating values, calculating a sum, or printing the elements.

    Which of these best describes an array?
    • a)
      A data structure that shows a hierarchical behavior
    • b)
      Container of objects of similar types
    • c)
      Arrays are immutable once initialised
    • d)
      Array is not a data structure
    Correct answer is option 'B'. Can you explain this answer?

    Niti Basu answered
    Definition of Array
    An array is a data structure that stores a fixed-size sequential collection of elements of the same type. In other words, an array is a container that holds a fixed number of items of the same data type.

    Features of Array
    Some of the key features of an array include:

    - Homogeneous: All elements of an array must be of the same data type.
    - Contiguous memory allocation: Array elements are stored in contiguous memory locations.
    - Fixed size: The size of an array is fixed and cannot be changed once it is initialized.
    - Index-based: Each element in an array is identified by its index, which starts at 0.

    Uses of Array
    Arrays are commonly used in programming for tasks such as:

    - Storing and accessing collections of data
    - Sorting and searching algorithms
    - Implementing matrices and other mathematical structures
    - Implementing dynamic programming algorithms

    Advantages of Array
    Arrays offer several advantages, including:

    - Random access: Elements in an array can be accessed directly using their index, which makes accessing and modifying elements fast and efficient.
    - Compact memory usage: Since all elements of an array are stored in contiguous memory locations, an array uses memory efficiently.
    - Easy to implement: Arrays are simple to implement and use, making them a popular choice for many programming tasks.

    Conclusion
    In summary, an array is a container of objects of similar types. It is a simple data structure that stores a fixed-size sequential collection of elements of the same type. Arrays are widely used in programming for a variety of tasks, and offer several advantages such as random access, compact memory usage and ease of implementation.

    Consider the same recursive C function that takes two arguments
    Q. What is the return value of the function foo when it is called as foo(513, 2)?
    • a)
      9
    • b)
      8
    • c)
      5
    • d)
      2
    Correct answer is option 'D'. Can you explain this answer?

    Yash Patel answered
    foo(513, 2) will return 1 + foo(256, 2). All subsequent recursive calls (including foo(256, 2)) will return 0 + foo(n/2, 2) except the last call foo(1, 2). The last call foo(1, 2) returns 1. So, the value returned by foo(513, 2) is 1 + 0 + 0…. + 0 + 1. The function foo(n, 2) basically returns sum of bits (or count of set bits) in the number n.

    Consider the following recursive C function that takes two arguments
    unsigned int foo(unsigned int n, unsigned int r){
    if(n >0) return (n%r + foo (n/r, r));
    else return 0;
    }
    Q.
    What is the return value of the function foo when it is called as foo(345, 10) ?
    • a)
      345
    • b)
      12
    • c)
      5
    • d)
      3
    Correct answer is option 'B'. Can you explain this answer?

    Shubham Sharma answered
    The function foo takes two unsigned integer arguments n and r. It calculates the sum of the first r terms of the sequence {n, n+1, n+2, ...} using recursion.

    The base case is when r equals 0, in which case the function returns 0. Otherwise, it recursively calls itself with n+1 and r-1, and adds the result to n.

    For example, if we call foo(3, 4), it will calculate the sum of the first 4 terms of the sequence {3, 4, 5, 6}, which is 3+4+5+6=18.

    What does the following function print for n = 25?
    void fun(int n)
    {
    if (n == 0)
    return;
    printf("%d", n%2);
    fun(n/2);
    }
    • a)
      11001
    • b)
      10011
    • c)
      11111
    • d)
      00000
    Correct answer is option 'B'. Can you explain this answer?

    Rajat Sharma answered
    Explanation:

    Initial Call:
    - When n = 25, the function fun(25) is called.

    Recursive Calls:
    - The function fun will keep calling itself recursively until n becomes 0.
    - At each step, it prints the remainder of dividing n by 2 (n%2), which gives the binary representation of the number.

    Binary Representation:
    - For n = 25:
    - 25 % 2 = 1 (LSB)
    - fun(25/2) -> fun(12)
    - 12 % 2 = 0
    - fun(12/2) -> fun(6)
    - 6 % 2 = 0
    - fun(6/2) -> fun(3)
    - 3 % 2 = 1
    - fun(3/2) -> fun(1)
    - 1 % 2 = 1 (MSB)
    - fun(1/2) -> fun(0)

    Final Output:
    - The binary representation of 25 is 11001.
    - Therefore, the function will print "10011" for n = 25.

    What does the following function do?
    • a)
      It returns 1 when n is a multiple of 3, otherwise returns 0
    • b)
      It returns 1 when n is a power of 3, otherwise returns 0
    • c)
      It returns 0 when n is a multiple of 3, otherwise returns 1
    • d)
      It returns 0 when n is a power of 3, otherwise returns 1
    Correct answer is option 'B'. Can you explain this answer?

    Aryan Batheja answered
    Lets solve with example, n = 27 which power of 3. First time if condition is false as n is neither equal to 0 nor equal to 1 then 27%3 = 0. Here, again if condition false because it is equal to 0. Then fun(27/3) will call. Second time if condition is false as n is neither equal to 0 nor equal to 1 then 9%3 = 0. Here again if condition false because it is equal to 0. Then fun (9/3) will call and third time if condition is false as n is neither equal to 0 nor equal to 1 then 3%3 = 0. Here again if condition false because it is equal to 0. Then fun(3/3) will call here n==1 if condition gets true and it return n i.e. 1. Option (B) is correct.

    Predict the output of following program
    • a)
      Stack Overflow
    • b)
      3
    • c)
      4
    • d)
      5
    Correct answer is option 'D'. Can you explain this answer?

    Aditya Tiwari answered
    Whenever such small recursive programs are given, apply recursive tree method along with dynamic programming. Here f(11)=f(5)+f(6) f(5)=f(2)+f(3) f(2)=f(1)=1 f(3)=f(1)+f(2)=1+1=2 so f(5) becomes 3 Now, f(6)=f(3)=2 and finally f(11)=3+2=5

     Consider the following code snippet:

    What will happen when the above snippet is executed?
    • a)
      The code will be executed successfully and no output will be generated
    • b)
      The code will be executed successfully and random output will be generated
    • c)
      The code will show a compile time error
    • d)
      The code will run for some time and stop when the stack overflows
    Correct answer is option 'D'. Can you explain this answer?

    Krithika Gupta answered
    Every function call is stored in the stack memory. In this case, there is no terminating condition(base case). So, my_recursive_function() will be called continuously till the stack overflows and there is no more space to store the function calls. At this point of time, the program will stop abruptly.

    An array name is a
    • a)
      Subscript
    • b)
      Formal parameter
    • c)
      Memory address
    • d)
      Prototype
    Correct answer is option 'C'. Can you explain this answer?

    Sudhir Patel answered
    An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.
    When an array is used as a value and array name represents the address of the first element. When an array is not used as a value its name represents the whole array.
    Array name is a type of name or a type of any element name that is share by all elements of an array but its indexes are different. Array name handle as a constant pointer, it can never change during execution of a program. Array name is also used to reach its all element.
    Therefore Option 3 is correct

    If integer requires two bytes space, then what will be the size of the following 'C’ array?
    int array[3][4]=(0);
    • a)
      24 bytes
    • b)
      12 bytes
    • c)
      7 bytes
    • d)
      14 bytes
    Correct answer is option 'A'. Can you explain this answer?

    Sudhir Patel answered
    An array is defined as the collection of similar types of data items stored at contiguous memory locations. 
    int array[3][4] = (0);
    Here Array is an integer array. which is in a two-dimensional array of 3 rows and 4 columns. So each row has 4 elements and there are three rows.
    Hence total number of elements = 3 x 4 =12
    Each element of size = 2 bytes.
    So size of array is = 12 x 2 bytes =24 bytes.
    Hence the correct answer is 24 bytes.

    • a)
      -9
    • b)
      5
    • c)
      15
    • d)
      19
    Correct answer is 'C'. Can you explain this answer?

    Shraddha Kaur answered
    f() is a recursive function which adds f(a+1, n-1) to *a if *a is even. If *a is odd then f() subtracts f(a+1, n-1) from *a. See below recursion tree for execution of f(a, 6).
    So, the final returned value is 12 + (7 – (13 – (4 + (11 – (6 + 0))))) = 15

    In C, how do you properly initialise an array?
    • a)
      int arr{}={11,12,15,16,19}
    • b)
      int arr()={11,12,15,16,19}
    • c)
      int arr{5}={11,12,15,16,19}
    • d)
      int arr[5]={11,12,15,16,19}
    Correct answer is option 'D'. Can you explain this answer?

    Sudhir Patel answered
    Array:
    An array is a group of data components that are stored in the same memory region at the same time. It is the most basic data structure, in which each data piece may be retrieved simply by its index number alone.
    Array initialize:
    Only square brackets [ ] must be used for declaring an array. The given {}, () are not useful for array initialization.
    int arr[5]={11,12,15,16,19} is correct.
    The above statement can store the 5 elements in a continuous manner. consider the array base address is 100.
    Hence the correct answer is  int arr[5]={11,12,15,16,19}.

    Output of following program?
    • a)
      1000 2000 4000
    • b)
      1000 2000 4000 4000 2000 1000
    • c)
      1000 2000 4000 2000 1000
    • d)
      1000 2000 2000 1000
    Correct answer is option 'B'. Can you explain this answer?

    Aryan Batheja answered
    First time n=1000 Then 1000 is printed by first printf function then call print(2*1000) then again print 2000 by printf function then call print(2*2000) and it prints 4000 next time print(4000*2) is called. Here 8000 is greater than 4000 condition becomes true and it return at function(2*4000). Here n=4000 then 4000 will again print through second printf. Similarly print(2*2000) after that n=2000 then 2000 will print and come back at print(2*1000) here n=1000, so print 1000 through second printf. Option (B) is correct.

    What will be the output of the following C program?
    • a)
      3 1 2 2 1 3 4 4 4
    • b)
      3 1 2 1 1 1 2 2 2
    • c)
      3 1 2 2 1 3 4
    • d)
      3 1 2 1 1 1 2
    Correct answer is option 'A'. Can you explain this answer?

    Kajal Sharma answered
    count(3) will print value of n and d. So 3 1 will be printed and d will become 2.
    Then count(2) will be called. It will print value of n and d.
    So 2 2 will be printed and d will become 3.
    Then count(1) will be called. It will print value of n and d.
    So 1 3 will be printed and d will become 4.
    Now count(1) will print value of d which is 4. count(1) will finish its execution.
    Then count(2) will print value of d which is 4.
    Similarly, count(3) will print value of d which is 4.
    So series will be A.

    Chapter doubts & questions for Arrays - 6 Months Preparation for GATE CSE 2025 is part of Computer Science Engineering (CSE) exam preparation. The chapters have been prepared according to the Computer Science Engineering (CSE) exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

    Chapter doubts & questions of Arrays - 6 Months Preparation for GATE CSE in English & Hindi are available as part of Computer Science Engineering (CSE) exam. Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.

    Top Courses Computer Science Engineering (CSE)