# Data Structure Class 12 Important Questions

Contents

## Data Structure Class 12 Important Questions

### Q 1. What is a Data Structure ?

Ans. A data structure is a logical way of organizing data that makes them efficient to use. Different data structures are suited to different types of application, and some are highly specialized to specific tasks. For instance, stacks are best suited for reversal applications or recursive applications.

### Q 2. Compare a data type with a Data structure.

Ans. A Data type defines a set of values along with well-defined operations starting its input-output behavior e.g., you cannot put decimal point in an integer or two strings cannot be multiplied etc.

On the other hand, a Data structure is a physical implementation that clearly way of storing accessing, manipulating data structure. The data stored in a data structure has a specific data type e.g., in a stack, all insertions and deletions take place at one end only.

### Q 3. What are linear and nonlinear data Structures ?

Ans. Linear Data structure. A data structure is said to be linear if its elements form a sequenceor a linear list, e.g., Arrays or linear lists, stacks and Queues etc.
Non-Linear Data structue. A data structure is said to be non-linear if traversal of nodes is nonlinear in nature e.g., Graphs, Trees etc.

### Q 4. In general, what common operations are performed on different Data Structures ?

Ans. Some commonly performed operations data structures are :

(i) Insertion. To add a new data item in the given collection of data items.
(ii) Deletion. To delete an existing data item from the given collection of data items.
(iii) reversal. To access each data item exactly once so that it can be processed.
(iv) Searching. To find out the location of the data item if it exists in the given collection of data items.
(v) Sorting. To arrange the items in some order i.e., in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data.

### Q 5. What purpose Linear lists data structures are mostly used for ?

Ans. Linear lists data structures are used to store and process elements that are similar in type and are to be processed in the same way. For example, to maintain a shopping list, a linear list may be used where items to be shopped are inserted to it one by one and as soon as an item is shopped, it is removed from the list.

### Q 6. Consider the following similar codes (carefully go through these) and predict their outputs.

#### (i)

``````NList = [60, 32, 13, 'hello']
print(NList, NList[-2])
NList.append(15)
print(len(NList))
print(len(NList))
NList.pop(3)
NList.sort( )
NList.insert(2, [14, 15])
NList += NList
NList +=NList
print(NList)
NList.pop( )
NList.remove(14)
print(NList)
``````

#### (ii)

``````NList = [60, 32, 13, 'hello']
print(NList, NList[-2])
NList.append(15)
print(len(NList))
print(len(NList))
NList.pop(3)
NList.insert(2, [14, 15])
NList += NList
NList +=NList
print(NList)
NList.remove(14)
print(NList)
``````

Ans.
(i)

``````32  13
5
5
107
{13, 15, , 107]
``````

(ii)

``````32  13
5
5
43
[60, 32, , 43, 15]
``````

### Q 7. What will be the output produced by following code ?

``````text = ['h', 'e', 'l', 'l', 'o']
print(text)
vowels = "aeiou"
newText = [x.upper( ) for x in text if x not in vowels]
print(newText)
``````

Ans.

``````['h', 'e', 'l', 'l', 'o']
['H', 'L', 'L']
``````

### Q 8. What is a list comprehension ? How is it useful ?

Ans. A List Comprehension is a concise dscription of a list that shorthands the list creating for loop in the form of a single statement.
List comprehensions make the code compact and are faster to execute.

### Q 9. Predict the output.

(i)

``````LA = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
LB = [num/3 for num in LA if num % 3 == 0]
print(LB)
``````

(ii)

``````[x+y for x in 'ball' for y in 'boy']
``````

(iii)

``````li = [1, 2, 3, 4, 5, 6, 7, 8, 9]
k = [elem1*elem2 for elem1 in li if (elem1-4) > 1 for elem2 in li[:4] ]
print(k)
``````

Ans.

``````(i) [3.0, 12.0, 27.0]
(ii) ['bb', 'bo', 'by', 'ab', 'ao', 'ay', 'lb', 'lo', 'ly', 'lb', 'lo', 'ly']
(iii) [6, 12, 18, 24,7, 14, 21, 28, 8, 16, 24, 32, 9, 18, 27, 36]
``````

### Q 10. What is the difference between a regular 2D list and a ragged List ?

Ans. A regular two dimensional list is a list having lists as its elements and each elements -list has the same shape i.e., same number of elements(length).

On the other hand, a list that contains lists with different shapes as its elements is also a 2D list but it is an irregular 2D list, also known as a ragged list.

``````List1 = [[1, 2, 3], [7, 9, 8]]
List2 = [[4, 5, 6], [1, 7]]
``````

### Important Define

#### Data item :

Single unit of values of certain type.

#### Data structure :

Named group of data of some data types.

#### Linear data structures :

Single level data structure representing linear relationship among data.

A list of linked elements by means of pointers.

#### Queue :

A FIFO (First in First Out) list.

#### Simple data structures :

Data structures normally built from primitive data types.

#### Sorting :

Arranging elements of a data structure in some order (ascending or descending).

#### Stack :

A LIFO (Last in First Out) list.

#### Traversal :

Processing each and every element of a data structure.

#### Tree :

A multilevel data structure representing hierarchical relationship among its data.