All Exams  >   Software Development  >   DSA in C++  >   All Questions

All questions of Dynamic Allocation for Software Development Exam

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;
}
  • a)
    5
  • b)
    0
  • c)
    Garbage value
  • d)
    Compilation error
Correct answer is option 'A'. Can you explain this answer?

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.

Which keyword is used to allocate dynamic memory in C++?
  • a)
    malloc
  • b)
    calloc
  • c)
    new
  • d)
    allocate
Correct answer is option 'C'. Can you explain this answer?

Deepak Shah answered
Dynamic Memory Allocation in C

Dynamic memory allocation is a feature in the C programming language that allows programmers to allocate memory dynamically at runtime. This is particularly useful when the amount of memory required is not known in advance or when memory needs to be allocated and deallocated as needed.

Keyword for Dynamic Memory Allocation

The keyword used to allocate dynamic memory in C is "malloc". Here's a detailed explanation of the keyword and its usage:

1. malloc()
The malloc() function in C is used to dynamically allocate a block of memory on the heap. It stands for "memory allocation". It takes the number of bytes as an argument and returns a pointer to the allocated memory. The general syntax for using malloc is:

```c
ptr = (cast-type*) malloc(size);
```

Where:
- `ptr` is the pointer to the allocated memory block.
- `cast-type` is the type of data to be allocated.
- `size` is the number of bytes to be allocated.

Example:
```c
int* ptr;
ptr = (int*) malloc(5 * sizeof(int));
```
In this example, malloc is used to allocate memory for an array of 5 integers. The sizeof(int) is used to determine the size of each integer element. The pointer `ptr` now points to the allocated memory.

2. calloc()
The calloc() function is another keyword used for dynamic memory allocation in C. It is similar to malloc(), but it initializes the allocated memory block to zero. The general syntax for using calloc is:

```c
ptr = (cast-type*) calloc(n, size);
```

Where:
- `ptr` is the pointer to the allocated memory block.
- `cast-type` is the type of data to be allocated.
- `n` is the number of elements to be allocated.
- `size` is the size of each element.

Example:
```c
int* ptr;
ptr = (int*) calloc(5, sizeof(int));
```
In this example, calloc is used to allocate memory for an array of 5 integers. The pointer `ptr` now points to the allocated memory, which is initialized to zero.

3. free()
After dynamically allocating memory using malloc() or calloc(), it is essential to deallocate the memory once it is no longer needed. This is done using the free() function. The general syntax for using free is:

```c
free(ptr);
```

Where `ptr` is the pointer to the memory block that needs to be deallocated.

Summary
In summary, the keyword used to allocate dynamic memory in C is malloc(). It is used in conjunction with the free() function to allocate and deallocate memory dynamically at runtime. The calloc() function can also be used for dynamic memory allocation, but it initializes the allocated memory to zero.

Which operator is used to deallocate memory allocated for a single object in C++?
  • a)
    delete
  • b)
    dealloc
  • c)
    free
  • d)
    remove
Correct answer is option 'A'. Can you explain this answer?

Nilotpal Jain answered
delete operator in C++
The delete operator in C++ is used to deallocate memory that was previously allocated for a single object using the new operator. It is important to free up memory that is no longer needed in order to prevent memory leaks and improve the efficiency of your program.

How to use the delete operator:
- To deallocate memory for a single object, you simply use the delete operator followed by the pointer to the object that was allocated using new.
- For example, if you allocated memory for an object of class MyClass using new, you can deallocate the memory using delete as follows: delete myClassObject;

Importance of using the delete operator:
- Failure to deallocate memory can lead to memory leaks, where memory is allocated but never released, causing your program to consume more and more memory over time.
- By using the delete operator properly, you can ensure that memory is released when it is no longer needed, improving the overall performance and stability of your program.
In conclusion, the delete operator is essential in C++ for deallocating memory allocated for a single object. It is important to use it correctly to prevent memory leaks and optimize the memory usage of your program.

Which operator is used to dynamically allocate memory in C++?
  • a)
    new
  • b)
    malloc
  • c)
    alloc
  • d)
    allocate
Correct answer is option 'A'. Can you explain this answer?

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

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;
}
```
  • a)
    10
  • b)
    5
  • c)
    8
  • d)
    Compilation error
Correct answer is option 'B'. Can you explain this answer?

Yogesh Dwivedi answered
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.

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;
}
  • a)
    2
  • b)
    3
  • c)
    5
  • d)
    8
Correct answer is option 'C'. Can you explain this answer?

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.

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;
}
  • a)
    0, 2
  • b)
    0, 1
  • c)
    1, 1
  • d)
    0, 0
Correct answer is option 'A'. Can you explain this answer?

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.

Which keyword is used to deallocate memory in C++?
  • a)
    free
  • b)
    deallocate
  • c)
    delete
  • d)
    remove
Correct answer is option 'C'. Can you explain this answer?

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

Which keyword is used to declare a constant variable in C++?
  • a)
    final
  • b)
    const
  • c)
    constant
  • d)
    var
Correct answer is option 'B'. Can you explain this answer?

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.

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;
}
  • a)
    10
  • b)
    30
  • c)
    36
  • d)
    Compilation error
Correct answer is option 'C'. Can you explain this answer?

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.

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;
}
  • a)
    5
  • b)
    10
  • c)
    Runtime error
  • d)
    Compilation error
Correct answer is option 'A'. Can you explain this answer?

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.

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;
}
```
  • a)
    11, 5
  • b)
    6, 5
  • c)
    11, 10
  • d)
    6, 10
Correct answer is option 'A'. Can you explain this answer?

Qudrat Chauhan answered
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.

Chapter doubts & questions for Dynamic Allocation - DSA in C++ 2026 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 2026 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Dynamic Allocation - DSA in 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.

DSA in C++

153 videos|115 docs|24 tests

Top Courses Software Development