Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Assignment: Arrays in C++

Assignment: Arrays in C++ | Basics of C++ - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which of the following is true about arrays in C++?
(a) Arrays can only store elements of the same data type.
(b) Arrays have a fixed size that cannot be changed.
(c) Arrays are always passed by reference to functions.
(d) Arrays can only be accessed using pointers.

Ans. (a)

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

Ans. 0

Q.3. Which operator is used to access individual elements of an array?
(a) . (dot) operator
(b) , (comma) operator
(c) * (asterisk) operator
(d) [] (subscript) operator

Ans. (d)

Q.4. What is the output of the following code snippet?
int numbers[] = {1, 2, 3, 4, 5};
cout << numbers[3];

(a) 1
(b) 2
(c) 3
(d) 4

Ans. (d)

Q.5. What is the correct way to pass an array to a function in C++?
(a) Pass the array by value.
(b) Pass the array by reference using the '&' operator.
(c) Pass the array by reference using the '*' operator.
(d) Pass the array by pointer.

Ans. (d)

High Order Thinking Questions (HOTS)

Q.1. Explain the concept of multidimensional arrays in C++ with an example.

Multidimensional arrays in C++ are arrays that have more than one dimension. They can be thought of as an array of arrays. For example:

int matrix[3][3] = {

    {1, 2, 3},

    {4, 5, 6},

    {7, 8, 9}

};

Q.2. Write a C++ program to find the sum of all elements in an array.

#include <iostream>

using namespace std;


int main() {

    int numbers[] = {1, 2, 3, 4, 5};

    int sum = 0;

    

    for (int i = 0; i < 5; i++) {

        sum += numbers[i];

    }

    

    cout << "Sum: " << sum << endl;

    return 0;

}

Q.3. How can you determine the length of an array in C++? Explain with an example.

The length of an array in C++ can be determined using the sizeof operator. For example:
int numbers[] = {1, 2, 3, 4, 5};

int length = sizeof(numbers) / sizeof(numbers[0]);

cout << "Length: " << length << endl;

Q.4. What are the advantages of using arrays in programming? Provide at least three reasons.

Advantages of using arrays in programming:

  • Arrays allow efficient storage and access of multiple elements of the same data type.
  • Arrays provide a convenient way to perform operations on a collection of data, such as searching, sorting, and filtering.
  • Arrays can be used to represent grids, matrices, and other multidimensional structures.

Q.5. Discuss the difference between an array and a vector in C++.

Difference between an array and a vector in C++:

  • Arrays have a fixed size determined at compile-time, whereas vectors can dynamically resize themselves at runtime.
  • Arrays cannot be easily resized or modified once declared, while vectors provide functions to add, remove, or modify elements.
  • Arrays have a fixed memory allocation, while vectors can automatically manage memory allocation and deallocation.

Fill in the Blanks

Fill in the blanks with the appropriate terms.

Q.1. The maximum number of elements that an array can hold is determined by its ________.

Ans. size

Q.2. The process of accessing each element in an array is called ________.

Ans. indexing

Q.3. The index of the last element in an array with 'n' elements is ________.

Ans. n - 1

Q.4. An array can be considered as a ________ of elements of the same data type.

Ans. collection

Q.5. The size of an array must be specified ________ it is declared.

Ans. when

True or False

Q.1. In C++, the size of an array must be known at compile-time.

Ans. True

Q.2. It is possible to change the size of an array after it has been declared.

Ans. False

Q.3. Arrays can store elements of different data types in C++.

Ans. False

Q.4. The index of the first element in an array is always 1.

Ans. False

Q.5. Arrays in C++ are always passed by value to functions.

Ans. False

Hands-On Questions

Q.1. Write a C++ program to find the largest element in an array.

#include <iostream>

using namespace std;


int main() {

    int numbers[] = {5, 2, 8, 1, 6};

    int largest = numbers[0];

    

    for (int i = 1; i < 5; i++) {

        if (numbers[i] > largest) {

            largest = numbers[i];

        }

    }

    

    cout << "Largest element: " << largest << endl;

    return 0;

}

Q.2. Write a C++ program to reverse the elements of an array.

#include <iostream>

using namespace std;


int main() {

    int numbers[] = {1, 2, 3, 4, 5};

    int length = sizeof(numbers) / sizeof(numbers[0]);

    

    for (int i = 0; i < length / 2; i++) {

        int temp = numbers[i];

        numbers[i] = numbers[length - i - 1];

        numbers[length - i - 1] = temp;

    }

    

    cout << "Reversed array: ";

    for (int i = 0; i < length; i++) {

        cout << numbers[i] << " ";

    }

    

    cout << endl;

    return 0;

}

Q.3. Write a C++ program to count the number of even and odd elements in an array.

#include <iostream>

using namespace std;


int main() {

    int numbers[] = {1, 2, 3, 4, 5};

    int evenCount = 0, oddCount = 0;

    int length = sizeof(numbers) / sizeof(numbers[0]);

    

    for (int i = 0; i < length; i++) {

        if (numbers[i] % 2 == 0) {

            evenCount++;

        } else {

            oddCount++;

        }

    }

    

    cout << "Even count: " << evenCount << endl;

    cout << "Odd count: " << oddCount << endl;

    return 0;

}

Q.4. Write a C++ program to merge two arrays into a single array.

#include <iostream>

using namespace std;


int main() {

    int array1[] = {1, 2, 3};

    int array2[] = {4, 5, 6};

    int length1 = sizeof(array1) / sizeof(array1[0]);

    int length2 = sizeof(array2) / sizeof(array2[0]);

    int mergedLength = length1 + length2;

    int mergedArray[mergedLength];

    

    for (int i = 0; i < length1; i++) {

        mergedArray[i] = array1[i];

    }

    

    for (int i = 0; i < length2; i++) {

        mergedArray[length1 + i] = array2[i];

    }

    

    cout << "Merged array: ";

    for (int i = 0; i < mergedLength; i++) {

        cout << mergedArray[i] << " ";

    }

    

    cout << endl;

    return 0;

}

Q.5. Write a C++ program to find the second smallest element in an array.

#include <iostream>

using namespace std;


int main() {

    int numbers[] = {5, 2, 8, 1, 6};

    int length = sizeof(numbers) / sizeof(numbers[0]);

    int smallest = numbers[0];

    int secondSmallest = numbers[0];

    

    for (int i = 1; i < length; i++) {

        if (numbers[i] < smallest) {

            secondSmallest = smallest;

            smallest = numbers[i];

        } else if (numbers[i] < secondSmallest && numbers[i] != smallest) {

            secondSmallest = numbers[i];

        }

    }

    

    cout << "Second smallest element: " << secondSmallest << endl;

    return 0;

}

The document Assignment: Arrays in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|44 docs|15 tests
70 videos|44 docs|15 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Free

,

MCQs

,

Semester Notes

,

Assignment: Arrays in C++ | Basics of C++ - Software Development

,

study material

,

Previous Year Questions with Solutions

,

pdf

,

Assignment: Arrays in C++ | Basics of C++ - Software Development

,

video lectures

,

shortcuts and tricks

,

Sample Paper

,

past year papers

,

Objective type Questions

,

ppt

,

practice quizzes

,

mock tests for examination

,

Viva Questions

,

Important questions

,

Exam

,

Extra Questions

,

Assignment: Arrays in C++ | Basics of C++ - Software Development

,

Summary

;