Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Assignment: Dynamic Allocation

Assignment: Dynamic Allocation | DSA in C++ - Software Development PDF Download

Instructions

  • Read each question carefully and choose the correct option or provide the appropriate answer.
  • Attempt all the questions.
  • The answers to the questions are provided at the end of the worksheet.

Multiple Choice Questions (MCQs)


Q.1. Which keyword is used to allocate memory dynamically in C++?
(a) new
(b) malloc
(c) allocate
(d) create
Ans. (a)

Q.2. What is the return type of the 'new' operator in C++?
(a) int
(b) void*
(c) char*
(d) double
Ans. (b)

Q.3. Which operator is used to deallocate memory allocated dynamically in C++?
(a) delete
(b) free
(c) destroy
(d) remove
Ans. (a)

Q.4. Which function is used to allocate memory for a two-dimensional array dynamically?

(a) new
(b) malloc
(c) allocate
(d) create
Ans. (a)

Q.5. In C++, which keyword is used to allocate memory for objects of a class?

(a) new
(b) malloc
(c) allocate
(d) create
Ans. (a)

HOTS (Higher Order Thinking Questions)


Q.1. Explain the difference between static allocation and dynamic allocation of memory.

Static allocation of memory is done at compile-time, while dynamic allocation is done at runtime. Static allocation assigns memory to variables or data structures before the program runs, while dynamic allocation assigns memory during program execution.

Q.2. Write a C++ code snippet to dynamically allocate an integer array of size 'n' using the 'new' operator.

int* arr = new int[n];

Q.3. What is the difference between 'new' and 'malloc' in C++?

'new' is an operator in C++ that is used to dynamically allocate memory for objects. It is type-safe and returns a pointer to the allocated memory. 'malloc' is a function from the C library that can be used to allocate memory, but it is not type-safe and returns a 'void*' pointer, which needs to be explicitly casted.

Q.4. Explain how memory is deallocated using the 'delete' operator in C++.

Memory allocated using 'new' can be deallocated using the 'delete' operator. The 'delete' operator releases the memory and calls the destructor of the object if it is a dynamically allocated object.

Q.5. Discuss the advantages and disadvantages of dynamic memory allocation.

Advantages of dynamic memory allocation:

  • Flexibility in memory usage as memory can be allocated and deallocated as needed.
  • Efficient utilization of memory, especially for data structures with varying sizes.
  • Disadvantages of dynamic memory allocation:
  • Overhead of memory management operations.
  • Possibility of memory leaks or invalid memory accesses if not managed properly.

Fill in the blanks


1. Dynamic memory allocation is performed using the _____ keyword in C++.

Dynamic memory allocation is performed using the new keyword in C++.

2. The 'delete' operator is used to deallocate memory allocated dynamically using the _____ keyword.

The 'delete' operator is used to deallocate memory allocated dynamically using the delete keyword.

3. The size of dynamically allocated memory can be determined at _____.

The size of dynamically allocated memory can be determined at runtime.

4. In C++, dynamic allocation of memory can be performed for _____.

In C++, dynamic allocation of memory can be performed for objects.

5. 'new' operator returns the address of the _____ allocated.

'new' operator returns the address of the memory allocated.

True or False


1. Dynamic memory allocation is more efficient than static memory allocation.

False

2. The 'delete' operator can deallocate memory for both single and multiple objects.

True

3. Memory allocated using 'new' should always be deallocated using 'delete'.

True

4. Dynamic memory allocation can help in reducing memory wastage.

True

5.Dynamic memory allocation is useful when the size of the data structure is known at compile time.

False

Hands-On Questions


Q.1. Write a C++ function called 'squareArray' that takes an integer array and its size as parameters. The function should dynamically allocate memory for a new array of the same size and square each element of the original array in the new array. The function should return the pointer to the new array.

int* squareArray(int arr[], int size) {

    int* newArr = new int[size];

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

        newArr[i] = arr[i] * arr[i];

    }

    return newArr;

}

Q.2. Write a C++ code snippet to dynamically allocate a two-dimensional integer array of size 'm'x'n' using the 'new' operator. Prompt the user to enter the values for each element of the array.

int** create2DArray(int m, int n) {

    int** arr = new int*[m];

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

        arr[i] = new int[n];

        for (int j = 0; j < n; j++) {

            cout << "Enter value for element (" << i << "," << j << "): ";

            cin >> arr[i][j];

        }

    }

    return arr;

}

Q.3. Implement a C++ class called 'Rectangle' with private member variables 'length' and 'width'. Write member functions to dynamically allocate memory for a rectangle object using the 'new' operator, initialize its length and width, calculate its area, and deallocate memory using the 'delete' operator.

class Rectangle {

private:

    int length;

    int width;

public:

    Rectangle(int len, int wid) {

        length = len;

        width = wid;

    }

    int calculateArea() {

        return length * width;

    }

};

Rectangle* createRectangle(int len, int wid) {

    Rectangle* rect = new Rectangle(len, wid);

    return rect;

}

void destroyRectangle(Rectangle* rect) {

    delete rect;

}

Q.4. Write a C++ program to create a dynamic array of characters using the 'new' operator. Prompt the user to enter a string and store each character in the dynamic array. Print the string in reverse order.

#include <iostream>

#include <cstring>

using namespace std;

int main() {

    char* dynamicArray = new char[100];

    cout << "Enter a string: ";

    cin.getline(dynamicArray, 100);

    int length = strlen(dynamicArray);

    cout << "Reverse string: ";

    for (int i = length - 1; i >= 0; i--) {

        cout << dynamicArray[i];

    }

    cout << endl;

    delete[] dynamicArray;

    return 0;

}

Q.5. Write a C++ function called 'findMaximum' that takes a dynamically allocated integer array and its size as parameters. The function should find and return the maximum element in the array.

#include <iostream>

int findMaximum(int* array, int size) {

    int maximum = array[0];  // Initialize maximum with the first element

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

        if (array[i] > maximum) {

            maximum = array[i];  // Update maximum if a larger element is found

        }

    }

    return maximum;

}

int main() {

    int size;

    std::cout << "Enter the size of the array: ";

    std::cin >> size;

    int* array = new int[size];  // Dynamically allocate the integer array

    std::cout << "Enter the elements of the array: ";

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

        std::cin >> array[i];

    }

    int maximum = findMaximum(array, size);  // Call the function

    std::cout << "The maximum element is: " << maximum << std::endl;

    delete[] array;  // Deallocate the memory

    return 0;

}

The document Assignment: Dynamic Allocation | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

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
Related Searches

Assignment: Dynamic Allocation | DSA in C++ - Software Development

,

Sample Paper

,

pdf

,

Previous Year Questions with Solutions

,

Viva Questions

,

ppt

,

Free

,

Objective type Questions

,

past year papers

,

Exam

,

shortcuts and tricks

,

Semester Notes

,

Assignment: Dynamic Allocation | DSA in C++ - Software Development

,

Assignment: Dynamic Allocation | DSA in C++ - Software Development

,

Summary

,

study material

,

MCQs

,

video lectures

,

Extra Questions

,

Important questions

,

mock tests for examination

,

practice quizzes

;