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

All questions of Dynamic Allocation for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

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

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

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.

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.

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?
```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++ 2024 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 2024 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

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev