C++ Imp. Question Unit 2
C++ Imp. Question Unit 2
2.1
1. What are the fundamental components of data structures and their basic
terminology?
Answer: Data structures provide efficient ways to organize and manage data in
computer programs. They facilitate operations like insertion, deletion, searching, and
sorting, which are essential for implementing algorithms efficiently. Without proper
data structures, managing data becomes inefficient, leading to slower execution
times and increased resource usage.
Answer:
• Efficient Data Organization: Data structures enable efficient organization
and management of data, leading to faster access and manipulation.
• Optimized Algorithms: Properly chosen data structures can significantly
improve algorithm efficiency, reducing time and space complexity.
• Resource Optimization: By optimizing memory usage and minimizing
overhead, data structures help in efficient resource utilization.
• Enhanced Code Readability and Maintainability: Well-defined data
structures lead to cleaner, more organized code, making it easier to
understand and maintain.
• Scalability: Data structures allow programs to handle large datasets and scale
effectively as requirements grow.
Answer: Data structures can be classified into various categories based on their
organization, including:
Answer:
• Bubble Sort: Iteratively compares adjacent elements and swaps them if they
are in the wrong order.
• Selection Sort: Repeatedly selects the minimum element from the unsorted
portion of the array and places it at the beginning.
• Insertion Sort: Builds the sorted array one element at a time by repeatedly
taking the next element and inserting it into its correct position.
• Merge Sort: Divides the array into smaller subarrays, sorts them recursively,
and then merges them back together.
• Quick Sort: Chooses a pivot element and partitions the array into two
subarrays such that elements less than the pivot are on its left and elements
greater than the pivot are on its right.
5. What are 2D arrays, and how are they different from 1D arrays?
Answer: 2D arrays, or two-dimensional arrays, are arrays of arrays. They store data in
rows and columns, forming a grid-like structure. Unlike 1D arrays, which store
elements sequentially, 2D arrays allow for the organization of data in a two-
dimensional space, suitable for representing matrices, tables, and grids.
Answer: Pointers in C++ are variables that store memory addresses as their values.
They allow direct manipulation of memory, enabling dynamic memory allocation and
efficient memory management. Pointers are essential for tasks like dynamic memory
allocation, passing parameters by reference, and implementing data structures like
linked lists and trees.
Answer: To change the value pointed by a pointer in C++, you can use the
dereference operator (*) to access the value at the memory address stored in the
pointer variable and then assign a new value to it. For example:
int num = 5;
int *ptr = # // ptr points to the memory
address of num
*ptr = 10; // Changing the value of num
through ptr