All Exams  >   Software Development  >   Basics of C++  >   All Questions

All questions of Arrays for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    cout << arr << endl;
    return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    Memory address of the first element in the array
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code declares an integer array 'arr' with 5 elements and prints the value of 'arr'. In C++, when an array name is used in an expression, it decays into a pointer to its first element. Therefore, 'cout << arr << endl;' prints the memory address of the first element in the array.

What is the index of the first element in an array?
  • a)
    0
  • b)
    1
  • c)
    -1
  • d)
    It depends on the size of the array
Correct answer is option 'A'. Can you explain this answer?

The index of the first element in an array is 0.

An array is a data structure that stores a collection of elements of the same type in contiguous memory locations. Each element in an array is accessed using its index, which represents its position within the array.

Understanding array indexing:
In most programming languages, array indexing starts from 0. This means that the first element in an array is accessed using an index of 0, the second element with an index of 1, and so on. The index value represents the offset from the beginning of the array.

Example:
Consider an array of integers named "numbers" with the following elements:
numbers = [5, 10, 15, 20]

To access the first element (5) in the array, you would use the index 0:
numbers[0] => 5

To access the second element (10), you would use the index 1:
numbers[1] => 10

Reasoning behind the index starting from 0:
The choice of starting array indexing from 0 is based on a few factors:

1. Simplicity: Starting indexing from 0 simplifies the computation of the memory address of each element in the array. It allows for a more straightforward and efficient implementation of array operations.

2. Consistency: By starting indexing from 0, the index directly corresponds to the offset from the beginning of the array. This consistency makes it easier to reason about array operations and avoids confusion.

3. Mathematical convenience: Starting indexing from 0 aligns with the mathematical concept of zero-based counting, which is widely used in computer science and mathematics.

4. Compatibility: Many programming languages, including C, C++, Java, and Python, follow the convention of starting array indexing from 0. This consistency across different languages allows for easier code portability and reduces confusion when working with arrays in different contexts.

Therefore, the correct answer to the given question is option A, which states that the index of the first element in an array is 0.

What is the maximum number of elements an array can hold in C++?
  • a)
    100
  • b)
    1000
  • c)
    65535
  • d)
    It depends on the implementation and system limitations
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The maximum number of elements an array can hold in C++ depends on various factors such as the implementation, system limitations, and available memory.

How can you access the nth element in an array?
  • a)
    array[n]
  • b)
    array(n)
  • c)
    array.n
  • d)
    array(n-1)
Correct answer is option 'D'. Can you explain this answer?

Accessing the nth element in an array

In order to access the nth element in an array, you need to understand how arrays are indexed and the concept of zero-based indexing.

Array Indexing
Arrays are data structures that hold a collection of elements of the same type. Each element in the array is assigned an index number, which is used to access or retrieve the value stored in that particular element.

Zero-Based Indexing
In most programming languages, including C, C++, Java, and Python, array indexing starts from 0. This means that the first element in the array has an index of 0, the second element has an index of 1, and so on.

Accessing the nth Element
To access the nth element in an array, you need to subtract 1 from the desired index value. This is because arrays are zero-indexed, so the element at index n is actually the (n-1)th element.

For example, let's say you have an array called "myArray" with the following elements: [10, 20, 30, 40, 50].

To access the first element (index 0), you would use: myArray[0]
To access the second element (index 1), you would use: myArray[1]
To access the third element (index 2), you would use: myArray[2]
To access the fourth element (index 3), you would use: myArray[3]
To access the fifth element (index 4), you would use: myArray[4]

So, to access the nth element, you would use: myArray[n-1]. This formula allows you to retrieve the value stored at the desired index in the array.

Summary
To access the nth element in an array, you need to use the formula myArray[n-1]. This formula takes into account the zero-based indexing used in most programming languages. By subtracting 1 from the desired index value, you can retrieve the value stored at that particular index in the array.

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int binarySearch(int arr[], int left, int right, int key) {
    if (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == key)
            return mid;
        if (arr[mid] > key)
            return binarySearch(arr, left, mid - 1, key);
        return binarySearch(arr, mid + 1, right, key);
    }
    return -1;
}
int main() {
    int arr[] = {2, 4, 6, 8, 10};
    int key = 8;
    int result = binarySearch(arr, 0, 4, key);
    cout << result;
    return 0;
}
  • a)
    -1
  • b)
    0
  • c)
    1
  • d)
    3
Correct answer is option 'C'. Can you explain this answer?

Explanation:

Binary Search Algorithm:
- Binary search is a divide and conquer algorithm that efficiently searches for a target value within a sorted array.
- It compares the target value to the middle element of the array and eliminates half of the remaining elements each time.

Code Explanation:
- The given code defines a binarySearch function that searches for a key in a sorted array recursively.
- In the main function, an array {2, 4, 6, 8, 10} is defined, and the key to be searched is 8.
- The binarySearch function is called with the array, left index 0, right index 4, and the key 8.

Output:
- The binarySearch function will return the index of the key if found, otherwise -1.
- In this case, the key 8 is found at index 3 in the array {2, 4, 6, 8, 10}.
- Therefore, the output of the code will be 3.
Therefore, the correct output of the code snippet is option 'C' (1).

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    cout << arr[10] << endl;
    return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    Garbage value
Correct answer is option 'D'. Can you explain this answer?

Fahad Al Mahdi answered
Explanation:

Array Index Out of Bounds:
- In the given code snippet, the array `arr` is declared with 5 elements (indexes 0 to 4).
- However, when trying to access `arr[10]`, the code is trying to access an element outside the bounds of the array.
- This results in accessing memory that is not allocated to the array, leading to undefined behavior.

Garbage Value:
- When accessing an element outside the bounds of an array, the value retrieved is considered a garbage value.
- Garbage values are unpredictable and can vary depending on the compiler, environment, and memory location.

Output:
- The output of the code snippet will be a garbage value since `arr[10]` is accessing memory beyond the allocated array size.
Therefore, the correct answer is option 'D) Garbage value'.

What is the output of the following code snippet?
#include <iostream>
using namespace std;
void modifyArray(int arr[]) {
    arr[0] = 100;
}
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    modifyArray(arr);
    cout << arr[0];
    return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    100
Correct answer is option 'D'. Can you explain this answer?

Explanation:

1. The Code Snippet:
- The code snippet defines a function modifyArray that takes an integer array as a parameter and changes the value of the first element in the array to 100.
- In the main function, an integer array arr is initialized with values {1, 2, 3, 4, 5}.
- The modifyArray function is called with the array arr as an argument.
- Finally, the value of the first element of the array arr is printed.

2. Passing Array to Function:
- In C++, arrays are passed to functions by reference, which means any changes made to the array elements inside the function will reflect in the original array.
- When the modifyArray function is called with the array arr, the first element of the array is modified to 100 inside the function.

3. Output:
- After calling the modifyArray function, the value of the first element of the array arr is changed to 100.
- The code snippet then prints the first element of the array arr, which is now 100.
- Therefore, the output of the code snippet is 100.
Therefore, the correct answer is option 'D) 100'.

Which sorting algorithm is known for its simplicity and suitability for small input sizes?
  • a)
    Bubble Sort
  • b)
    Selection Sort
  • c)
    Insertion Sort
  • d)
    Merge Sort
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
Insertion Sort is known for its simplicity and suitability for small input sizes. It works by gradually building a sorted portion of the array and inserting elements in the correct order.

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    cout << arr << endl;
    return 0;
}
  • a)
    0
  • b)
    1
  • c)
    2
  • d)
    Garbage value
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code declares an integer array 'arr' with 5 elements and initializes the first three elements with values 1, 2, and 3. The remaining two elements are not explicitly initialized and will contain garbage values. The statement 'cout << arr[4] << endl;' prints the value at index 4, which may be a garbage value.

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
    int arr[5];
    cout << arr[0] << endl;
    return 0;
}
  • a)
    0
  • b)
    1
  • c)
    -1
  • d)
    Garbage value
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code declares an integer array 'arr' with 5 elements but does not initialize them. In C++, uninitialized elements in an array are set to 0 by default. The statement 'cout << arr[0] << endl;' prints the value at index 0, which is 0.

Which sorting algorithm can be implemented using a recursive approach?
  • a)
    Bubble Sort
  • b)
    Selection Sort
  • c)
    Insertion Sort
  • d)
    Merge Sort
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
Merge Sort can be implemented using a recursive approach. It divides the array into two halves, recursively sorts them, and then merges the sorted halves.

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    cout << sizeof(arr) / sizeof(arr[0]) << endl;
    return 0;
}
  • a)
    1
  • b)
    5
  • c)
    20
  • d)
    24
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The code declares an integer array 'arr' with 5 elements. The 'sizeof(arr)' returns the size of the entire array in bytes, and 'sizeof(arr[0])' returns the size of each element in bytes. Therefore, 'cout << sizeof(arr) / sizeof(arr[0]) << endl;' prints the number of elements in the array, which is 5.

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    cout << arr[3] << endl;
    return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code declares an integer array 'arr' with 5 elements and initializes them with values 1, 2, 3, 4, and 5. The statement 'cout << arr[3] << endl;' prints the value at index 3, which is 4.

What is the time complexity of searching an element in an array using binary search (iterative method)?
  • a)
    O(log N)
  • b)
    O(N)
  • c)
    O(N^2)
  • d)
    O(1)
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Binary search has a time complexity of O(log N) when applied to a sorted array. It repeatedly divides the search space in half, reducing the search range by half with each comparison.

Which sorting algorithm has the worst-case time complexity of O(n^2)?
  • a)
    Bubble Sort
  • b)
    Selection Sort
  • c)
    Insertion Sort
  • d)
    Merge Sort
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Bubble Sort has a worst-case time complexity of O(n^2) when the array is reverse sorted. It involves repeatedly swapping adjacent elements if they are in the wrong order.

Which sorting algorithm has the best average-case time complexity?
  • a)
    Bubble Sort
  • b)
    Selection Sort
  • c)
    Insertion Sort
  • d)
    Merge Sort
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
Merge Sort has an average-case time complexity of O(n log n), which is the best among the given sorting algorithms. It recursively divides the array into halves and merges them in a sorted manner.

What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    cout << sizeof(arr) << endl;
    return 0;
}
  • a)
    1
  • b)
    5
  • c)
    20
  • d)
    24
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code declares an integer array 'arr' with 5 elements. The 'sizeof(arr)' returns the size of the entire array in bytes, which is equal to the number of elements (5) multiplied by the size of each element (4 bytes for an integer). Therefore, 'cout << sizeof(arr) << endl;' prints 20.

Chapter doubts & questions for Arrays - Basics of C++ 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Arrays - Basics of C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of C++

70 videos|45 docs|15 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev