Practice Top 30 C++ Programming Questions on Dynamic Memory Allocation
Q1. Write a program to dynamically allocate memory for an integer, assign a value, and display it.
Input: Value = 10
Output: Value: 10
Q2. Write a program to dynamically allocate memory for a float, assign a value, and display it.
Input: Value = 5.5
Output: Value: 5.5
Q3. Write a program to dynamically allocate memory for an array of integers and initialize it.
Input: Array Size = 3, Values = [1, 2, 3]
Output: Array: 1 2 3
Q4. Write a program to read and display elements of a dynamically allocated array.
Input: Size = 4, Values = [5, 6, 7, 8]
Output: Array: 5 6 7 8
Q5. Write a program to calculate the sum of elements in a dynamically allocated array.
Input: Size = 3, Values = [2, 4, 6]
Output: Sum: 12
Q6. Write a program to find the maximum value in a dynamically allocated array.
Input: Size = 5, Values = [3, 7, 1, 9, 5]
Output: Maximum: 9
Q7. Write a program to dynamically allocate memory for a string and display it.
Input: String = "Dynamic"
Output: String: Dynamic
Q8. Write a program to dynamically allocate and then deallocate memory for an integer.
Input: Value = 42
Output: Memory deallocated.
Q9. Write a program to dynamically resize an array using realloc (or equivalent in C++).
Input: Initial Size = 2, Final Size = 4, Initial Values = [1, 2], New Values = [3, 4]
Output: Resized Array: 1 2 3 4
Q10. Write a program to create and display a 2D dynamic array.
Input: 2x2 Array = [[1, 2], [3, 4]]
Output:
1 2
3 4
Q11. Write a program to read and display elements of a 2D dynamic array.
Input: 2x3 Array = [[1, 2, 3], [4, 5, 6]]
Output:
1 2 3
4 5 6
Q12. Write a program to calculate the transpose of a 2D dynamically allocated array.
Input: Array = [[1, 2], [3, 4]]
Output:
Transpose:
1 3
2 4
Q13. Write a program to create a dynamic array of strings and display them.
Input: Strings = ["Hello", "World"]
Output: Strings: Hello World
Q14. Write a program to pass a dynamically allocated array to a function to calculate the average.
Input: Array = [4, 8, 12]
Output: Average: 8
Q15. Write a program to allocate and then free memory for a dynamically allocated array.
Input: Size = 3, Values = [7, 14, 21]
Output: Memory deallocated.
Q16. Write a program to create a dynamic array and initialize all elements to zero.
Input: Size = 4
Output: Array: 0 0 0 0
Q17. Write a program to find the minimum value in a dynamically allocated array.
Input: Size = 4, Values = [9, 3, 5, 7]
Output: Minimum: 3
Q18. Write a program to delete a specific element from a dynamically allocated array.
Input: Array = [2, 4, 6, 8], Element to Delete = 6
Output: Array: 2 4 8
Q19. Write a program to dynamically allocate memory for an object of a custom class and display its data.
Input: Class: Student, Name = "Alice", Age = 20
Output: Student: Alice, Age: 20
Q20. Write a program to create a dynamic array of objects and display their data.
Input: Objects: [Student(Name: John, Age: 18), Student(Name: Jane, Age: 19)]
Output:
John, 18
Jane, 19
Q21. Write a program to create a dynamic array of pointers to integers.
Input: Values: [1, 2, 3]
Output: 1 2 3
Q22. Write a program to double the size of a dynamic array when it is full.
Input: Initial Size = 2, Final Values = [1, 2, 3, 4]
Output: Resized Array: 1 2 3 4
Q23. Write a program to reverse the elements of a dynamically allocated array.
Input: Array: [3, 6, 9]
Output: Reversed Array: [9, 6, 3]
Q24. Write a program to sort a dynamically allocated array in ascending order.
Input: Array: [5, 3, 8]
Output: Sorted Array: [3, 5, 8]
Q25. Write a program to merge two dynamically allocated arrays.
Input: Array1: [1, 3], Array2: [2, 4]
Output: Merged Array: [1, 2, 3, 4]
Q26. Write a program to create a dynamic array of integer pointers and initialize them.
Input: Values: [4, 8]
Output: Array: 4 8
Q27. Write a program to copy one dynamic array to another.
Input: Array: [6, 9, 12]
Output: Copied Array: [6, 9, 12]
Q28. Write a program to resize a 2D dynamically allocated array.
Input: Initial: [[1, 2]], Final: [[1, 2], [3, 4]]
Output:
1 2
3 4
Q29. Write a program to calculate the sum of diagonal elements of a 2D dynamic array.
Input: Matrix = [[1, 2], [3, 4]]
Output: Diagonal Sum: 5
Q30. Write a program to free memory allocated for a 2D array.
Input: Array: [[1, 2], [3, 4]]
Output: Memory deallocated.
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!