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

Dynamic Allocation - 2 - Software Development DSA in C++ Free MCQ Test


MCQ Practice Test & Solutions: Test: Dynamic Allocation - 2 (15 Questions)

You can prepare effectively for Software Development DSA in C++ with this dedicated MCQ Practice Test (available with solutions) on the important topic of "Test: Dynamic Allocation - 2". These 15 questions have been designed by the experts with the latest curriculum of Software Development 2026, to help you master the concept.

Test Highlights:

  • - Format: Multiple Choice Questions (MCQ)
  • - Duration: 30 minutes
  • - Number of Questions: 15

Sign up on EduRev for free to attempt this test and track your preparation progress.

Test: Dynamic Allocation - 2 - Question 1

Which operator is used to dynamically allocate memory in C++?

Detailed Solution: Question 1

The 'new' operator is used for dynamic memory allocation in C++. It returns the address of the memory block allocated.

Test: Dynamic Allocation - 2 - Question 2

Which keyword is used to deallocate memory in C++?

Detailed Solution: Question 2

The 'delete' keyword is used to deallocate dynamically allocated memory in C++. It frees the memory block and makes it available for future allocations.

Test: Dynamic Allocation - 2 - Question 3

What is the purpose of using macros in C++?

Detailed Solution: Question 3

Macros in C++ are used to define constants and perform text substitution during compilation.

Test: Dynamic Allocation - 2 - Question 4

What does the keyword "inline" indicate in a function declaration?

Detailed Solution: Question 4

The 'inline' keyword in a function declaration suggests the compiler to replace the function call with its body at the call site, reducing function call overhead.

Test: Dynamic Allocation - 2 - Question 5

Which keyword is used to declare a constant variable in C++?

Detailed Solution: Question 5

The 'const' keyword is used to declare a constant variable in C++. The value of a constant variable cannot be modified once it is assigned.

Test: Dynamic Allocation - 2 - Question 6

What is the output of the following code?
#include <iostream>
using namespace std;

int main() {
    int* ptr = new int(5);
    cout << *ptr << endl;
    delete ptr;
    return 0;
}

Detailed Solution: Question 6

The code dynamically allocates an integer with the value 5 using 'new'. The value is then printed using the dereferencing operator '*'. The allocated memory is deallocated using 'delete' before the program exits.

Test: Dynamic Allocation - 2 - Question 7

What is the output of the following code?
#include <iostream>
using namespace std;

#define SQUARE(x) x*x

int main() {
    int num = 5;
    cout << SQUARE(num + 1) << endl;
    return 0;
}

Detailed Solution: Question 7

The code defines a macro SQUARE that squares its argument. In the main function, the expression SQUARE(num + 1) is expanded as (num + 1 * num + 1) due to the absence of parentheses in the macro definition. This leads to the expression being evaluated as (5 + 1 * 5 + 1) = 11. The output is 11 * 11 = 121, which is printed.

Test: Dynamic Allocation - 2 - Question 8

What is the output of the following code?
#include <iostream>
using namespace std;

int sum(int x = 2, int y = 3);

int main() {
    cout << sum(5) << endl;
    return 0;
}

int sum(int x, int y) {
    return x + y;
}

Detailed Solution: Question 8

The 'sum' function is declared with default parameters. In the main function, only the first argument is provided, and the default value for the second argument is used. Thus, the function call sum(5) is equivalent to sum(5, 3), resulting in 5 + 3 = 8. The output is 8.

Test: Dynamic Allocation - 2 - Question 9

What is the output of the following code?
#include <iostream>
using namespace std;

static int count = 0;

int main() {
    cout << count++ << endl;
    count++;
    cout << count << endl;
    return 0;
}

Detailed Solution: Question 9

The 'count' variable is declared as static, which means it retains its value between function calls. In the main function, count is printed with the post-increment operator, resulting in 0 being printed. Then, count is incremented twice, resulting in its value being 2 when printed again.

Test: Dynamic Allocation - 2 - Question 10

What is the output of the following code?
#include <iostream>
using namespace std;

int main() {
    const int x = 5;
    int* ptr = (int*)&x;
    *ptr = 10;
    cout << x << endl;
    return 0;
}

Detailed Solution: Question 10

The 'const' keyword is used to declare a constant variable 'x'. However, the code tries to modify the value of 'x' by using a pointer and assigning a new value through it. Modifying a constant variable results in undefined behavior, but in this case, the value of 'x' remains unchanged as it is optimized by the compiler.

Test: Dynamic Allocation - 2 - Question 11

What is the output of the following code?
```cpp
#include <iostream>
using namespace std;

inline int multiply(int a, int b) {
    return a * b;
}

int main() {
    int result = multiply(2, 3) + multiply(4, 5);
    cout << result << endl;
    return 0;
}
```

Detailed Solution: Question 11

The 'inline' keyword suggests the compiler to replace the function call to 'multiply' with its body. Thus, the expression becomes 2 * 3 + 4 * 5, which simplifies to 6 + 20 = 26.

Test: Dynamic Allocation - 2 - Question 12

What is the output of the following code?
```cpp
#include <iostream>
using namespace std;

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
    int num1 = 10, num2 = 5, num3 = 8;
    int maxNum = MAX(num1++, num2++);
    cout << maxNum << endl;
    return 0;
}
```

Detailed Solution: Question 12

The macro MAX is defined to return the maximum value between two arguments.
In the main function, num1++ and num2++ are passed as arguments.
Since the macro directly substitutes the arguments, the expressions become num1++ > num2++ ? num1++ : num2++, resulting in 10 > 5 ? 10++ : 5++, which evaluates to 5.

Test: Dynamic Allocation - 2 - Question 13

What is the output of the following code?
```cpp
#include <iostream>
using namespace std;

const int num = 5;

void increment() {
    const int num = 10;
    cout << ++num << endl;
}

int main() {
    increment();
    cout << num << endl;
    return 0;
}
```

Detailed Solution: Question 13

The function increment() has a local constant variable 'num' with a value of 10. When incremented (++num), its value becomes 11, which is printed. In the main function, the global constant variable 'num' with a value of 5 is printed. Constants with the same name in different scopes are treated as separate entities.

Test: Dynamic Allocation - 2 - Question 14

What is the output of the following code?
```cpp
#include <iostream>
using namespace std;

int getValue() {
    static int num = 0;
    num++;
    return num;
}

int main() {
    cout << getValue() << endl;
    cout << getValue() << endl;
    cout << getValue() << endl;
    return 0;
}
```

Detailed Solution: Question 14

The function 'getValue' has a static local variable 'num' that is initialized to 0. Each time the function is called, 'num' is incremented by 1 and returned. In the main function, 'getValue' is called three times, resulting in the sequence 1, 2, 3 being printed.

Test: Dynamic Allocation - 2 - Question 15

What is the output of the following code?
```cpp
#include <iostream>
using namespace std;

int main() {
    const int arr[] = {1, 2, 3};
    int* ptr = const_cast<int*>(arr);
    *ptr = 10;
    cout << arr[0] << endl;
    return 0;
}
```

Detailed Solution: Question 15

The code tries to modify a constant array 'arr' by using const_cast to remove the const qualifier. Modifying a constant object results in undefined behavior, which in this case leads to a runtime error.

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