Answer: (b) append()
Exercise 4 Solutions · Unit 4: Computational Structures
5 reading sections · 15 MCQs · 6 short · 5 long questions
Exercise 4 — Exercise 4: MCQs, short and long questions
Answer: (c) Checks if an item exists in the list
Answer: (b) Pop
Answer: (c) Enqueue
Answer: (a) Number of edges from the root to the deepest node
Answer: (c) Representing connections in a social network
Answer: list.insert(index, item) puts the item at the given index and shifts the existing items from that position one place to the right.
Answer: Assigning a list to another variable (b = a) does not copy it – both names refer to the same list, so a change made through one variable also appears in the other, which can cause unexpected bugs. Use a.copy() or list(a) to make a separate copy.
Answer: A stack is a data structure where items are added and removed only at one end, the top. LIFO means the item added last is the first one removed, like a pile of books.
Answer: Enqueue adds an item at the back (rear) of the queue; dequeue removes and returns the item at the front. Together they make the queue FIFO.
Answer: Push (add an item to the top) and Pop (remove the item from the top).
Answer: enqueue() inserts an element at the rear of the queue; dequeue() deletes and returns the element at the front.
Answer: A Python list has no fixed size: items can be added (append, insert, +) or removed (remove, pop) at any time and the list grows or shrinks automatically. So we need not know the number of items in advance, memory is managed automatically and the same list can handle changing data.
Answer: A stack works LIFO with operations push (add to top), pop (remove from top), peek (see top) and isEmpty. Real-life example: a pile of plates/books or the browser back button. In Python a list is used: append() = push, pop() = pop, s[-1] = peek, len(s) == 0 = empty.
Answer: Use a list (or queue.Queue): enqueue with append() at the rear, dequeue with pop(0) from the front, checking for an empty queue first.
Answer: A tree is a hierarchical (non-linear) data structure of nodes joined by edges, starting from one root node, where each node may have child nodes. Properties: root node, nodes and edges, leaf nodes, height (longest root-to-leaf path), balanced trees; exactly one path between any two nodes and no cycles.
Answer: A graph is a data structure of vertices (nodes) connected by edges, used to model networks such as cities and roads. In a directed graph each edge has a direction (one-way, A → B); in an undirected graph edges are two-way (A — B means both A to B and B to A).
Exercises in Unit 4
- Exercise 4 solutions – Exercise 4: MCQs, short and long questions(17)