Software Development Exam  >  Software Development Tests  >  DSA in C++  >  Test: Dynamic Allocation - 1 - Software Development MCQ

Test: Dynamic Allocation - 1 - Software Development MCQ


Test Description

15 Questions MCQ Test DSA in C++ - Test: Dynamic Allocation - 1

Test: Dynamic Allocation - 1 for Software Development 2024 is part of DSA in C++ preparation. The Test: Dynamic Allocation - 1 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Dynamic Allocation - 1 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Dynamic Allocation - 1 below.
Solutions of Test: Dynamic Allocation - 1 questions in English are available as part of our DSA in C++ for Software Development & Test: Dynamic Allocation - 1 solutions in Hindi for DSA in C++ course. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free. Attempt Test: Dynamic Allocation - 1 | 15 questions in 30 minutes | Mock test for Software Development preparation | Free important questions MCQ to study DSA in C++ for Software Development Exam | Download free PDF with solutions
Test: Dynamic Allocation - 1 - Question 1

Which keyword is used to allocate dynamic memory in C++?

Detailed Solution for Test: Dynamic Allocation - 1 - Question 1

The new keyword is used to dynamically allocate memory in C++.

Test: Dynamic Allocation - 1 - Question 2

What is the purpose of the delete keyword in C++?

Detailed Solution for Test: Dynamic Allocation - 1 - Question 2

The delete keyword is used to deallocate memory that was allocated using new.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Dynamic Allocation - 1 - Question 3

Which operator is used to dynamically allocate memory for a single object in C++?

Detailed Solution for Test: Dynamic Allocation - 1 - Question 3

The new operator is used to dynamically allocate memory for a single object in C++.

Test: Dynamic Allocation - 1 - Question 4

What happens if we forget to deallocate the memory allocated using new?

Detailed Solution for Test: Dynamic Allocation - 1 - Question 4

If we forget to deallocate the memory allocated using new, a memory leak

Test: Dynamic Allocation - 1 - Question 5

Which operator is used to deallocate memory allocated for a single object in C++?

Detailed Solution for Test: Dynamic Allocation - 1 - Question 5

The delete operator is used to deallocate memory that was allocated for a single object using new.

Test: Dynamic Allocation - 1 - Question 6

What will be the output of the following code snippet?
int* ptr = new int(5);
cout << *ptr << endl;
delete ptr;
cout << *ptr << endl;

Detailed Solution for Test: Dynamic Allocation - 1 - Question 6

The first cout statement will print 5, but after deleting the memory using delete, accessing the memory using the pointer will result in a runtime error.

Test: Dynamic Allocation - 1 - Question 7

What will be the output of the following code snippet?
int* ptr = new int[3]{1, 2, 3};
cout << *ptr << endl;
delete[] ptr;
cout << *ptr << endl;

Detailed Solution for Test: Dynamic Allocation - 1 - Question 7

The first cout statement will print 1, but after deleting the memory using delete[], accessing the memory using the pointer will result in a runtime error.

Test: Dynamic Allocation - 1 - Question 8

What will be the output of the following code snippet?
int a = 10;
int& ref = a;
int* ptr = &ref;
cout << *ptr << endl;

Detailed Solution for Test: Dynamic Allocation - 1 - Question 8

The pointer 'ptr' is pointing to the address of 'a', so dereferencing the pointer will give the value 10.

Test: Dynamic Allocation - 1 - Question 9

What will be the output of the following code snippet?
void changeValue(int* ptr) {
    *ptr = 20;
}

int main() {
    int value = 10;
    changeValue(&value);
    cout << value << endl;
    return 0;
}

Detailed Solution for Test: Dynamic Allocation - 1 - Question 9

The function 'changeValue' accepts a pointer and modifies the value at the memory address pointed to by the pointer. So, the value of 'value' changes to 20.

Test: Dynamic Allocation - 1 - Question 10

What will be the output of the following code snippet?
void changeArray(int arr[]) {
    arr[0] = 5;
}

int main() {
    int arr[3] = {1, 2, 3};
    changeArray(arr);
    cout << arr[0] << endl;
    return 0;
}

Detailed Solution for Test: Dynamic Allocation - 1 - Question 10

When the array 'arr' is passed to the function 'changeArray', it is passed as a pointer. Thus, modifying the array inside the function will also affect the original array.

Test: Dynamic Allocation - 1 - Question 11

What will be the output of the following code snippet?
```cpp
int** matrix = new int*[2];
for (int i = 0; i < 2; i++) {
    matrix[i] = new int[3];
    for (int j = 0; j < 3; j++) {
        matrix[i][j] = i + j;
    }
}
cout << matrix[1][2] << endl;
```

Detailed Solution for Test: Dynamic Allocation - 1 - Question 11

The matrix is a dynamic 2D array with 2 rows and 3 columns. The value at matrix[1][2] is 2 (1 + 2).

Test: Dynamic Allocation - 1 - Question 12

What will be the output of the following code snippet?
```cpp
int** matrix = new int*[3];
for (int i = 0; i < 3; i++) {
    matrix[i] = new int[2];
    for (int j = 0; j < 2; j++) {
        matrix[i][j] = i * j;
    }
}
cout << matrix[2][1] << endl;
```

Detailed Solution for Test: Dynamic Allocation - 1 - Question 12

The matrix is a dynamic 2D array with 3 rows and 2 columns. The value at matrix[2][1] is 2 (2 * 1).

Test: Dynamic Allocation - 1 - Question 13

What will be the output of the following code snippet?

```cpp
void changeValue(int** matrix) {
    matrix[0][1] = 5;
}

int main() {
    int** matrix = new int*[2];
    for (int i = 0; i < 2; i++) {
        matrix[i] = new int[2];
        for (int j = 0; j < 2; j++) {
            matrix[i][j] = i + j;
        }
    }
    changeValue(matrix);
    cout << matrix[0][1] << endl;
    return 0;
}
```

Detailed Solution for Test: Dynamic Allocation - 1 - Question 13

The function 'changeValue' receives a pointer to a 2D array. Modifying the value at matrix[0][1] inside the function changes the original array as well.

Test: Dynamic Allocation - 1 - Question 14

What will be the output of the following code snippet?
 

```cpp
void changeValue(int** matrix) {
    matrix[0] = new int[2]{5, 5};
}

int main() {
    int** matrix = new int*[2];
    for (int i = 0; i < 2; i++) {
        matrix[i] = new int[2]{1, 1};
    }
    changeValue(matrix);
    cout << matrix[0][1] << endl;
    return 0;
}
```

Detailed Solution for Test: Dynamic Allocation - 1 - Question 14

The function 'changeValue' receives a pointer to a 2D array. It dynamically allocates a new row and assigns the values 5 and 5. Thus, matrix[0][1] becomes 5.

Test: Dynamic Allocation - 1 - Question 15

What will be the output of the following code snippet?

```cpp
void changeValue(int*** matrix) {
    matrix[1][0][0] = 10;
}

int main() {
    int*** matrix = new int**[2];
    for (int i = 0; i < 2; i++) {
        matrix[i] = new int*[2];
        for (int j = 0; j < 2; j++) {
            matrix[i][j] = new int[2]{i, j};
        }
    }
    changeValue(matrix);
    cout << matrix[1][0][0] << endl;
    return 0;
}
```

Detailed Solution for Test: Dynamic Allocation - 1 - Question 15

The function 'changeValue' receives a pointer to a 3D array. Modifying the value at matrix[1][0][0] inside the function changes the original array as well.

153 videos|115 docs|24 tests
Information about Test: Dynamic Allocation - 1 Page
In this test you can find the Exam questions for Test: Dynamic Allocation - 1 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Dynamic Allocation - 1, EduRev gives you an ample number of Online tests for practice

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF

Top Courses for Software Development