All Exams  >   EmSAT Achieve  >   C++ for EmSAT Achieve  >   All Questions

All questions of Arrays for EmSAT Achieve Exam

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 output of the following code snippet?
#include <iostream>
using namespace std;
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
}
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    printArray(arr, 5);
    return 0;
}
  • a)
    1 2 3 4 5
  • b)
    1 2 3 4
  • c)
    0 0 0 0 0
  • d)
    Compiler error
Correct answer is option 'A'. Can you explain this answer?

Explanation:

printArray Function:
- The printArray function takes an integer array arr and its size as arguments.
- It iterates through the array using a for loop and prints each element followed by a space.

Main Function:
- In the main function, an integer array arr is initialized with values {1, 2, 3, 4, 5}.
- The printArray function is called with the array arr and size 5 as arguments.
- The printArray function then prints the elements of the array arr which are 1, 2, 3, 4, 5.
Therefore, the output of the code snippet is:
1 2 3 4 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 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.

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;
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.

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 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.

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 << 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.

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 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.

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 << 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.

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

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

C++ for EmSAT Achieve

71 videos|45 docs|15 tests

Top Courses EmSAT Achieve