Chap 4 Solved Exercise

MCQs (Multiple Choice Questions) on Graphs and Trees.


What is a graph in computer science?
A) Vertices connected by edges ✅


In cities and roads analogy, what do vertices represent?
A) Roads
B) Cities ✅


How does a graph differ from a tree?
A) Graph has root, tree doesn’t
B) Trees have cycles, graphs don’t
C) Graphs aren’t hierarchical ✅
D) Trees don’t show relationships


Which is true about paths in a tree vs graph?
A) Trees have multiple paths
B) Graphs have one path only
C) Trees have one path, graphs can have many ✅
D) Both allow cycles


Real-world example of a graph?
A) Family tree
B) Organization chart
C) Social network ✅
D) Queue


Which is NOT true about graphs?
A) Represent networks
B) Have parent-child like trees ✅
C) Can have cycles
D) May not have root


Best data for trees?
A) Road networks
B) Web links
C) Hierarchical data ✅
D) Social networks


What does degree of a vertex mean?
A) Total vertices
B) Number of connected edges ✅
C) Edge weight
D) Edge direction


What does edge weight represent?
A) Number of vertices
B) Distance or cost ✅
C) Direction
D) Total edges


What describes a directed edge?
A) No direction
B) Two-way connection
C) One-way connection ✅
D) Always weighted


What type of edge represents a two-way connection between vertices?
A) Directed edge
B) Weighted edge
C) Undirected edge ✅
D) Cyclic edge


If a city has roads connecting it to three other cities, what is the degree of that city’s vertex?
A) 1
B) 2
C) 3 ✅
D) 4


Which graph has edges with direction?
A) Undirected
B) Weighted
C) Directed ✅
D) Cyclic


Can you travel from A to B without a directed edge from A to B?
A) Yes
B) No ✅
C) Only if weighted
D) Only if undirected


What is true about undirected graphs?
A) Edges have direction
B) Edges have no direction ✅
C) Edges have weights
D) One vertex only


Which example fits an undirected graph?
A) One-way streets
B) Mutual friendship ✅
C) Roads with distances
D) Family tree


What does a weighted graph have?
A) Edge weights ✅
B) One-way edges
C) No edges
D) Hierarchy


Why use weighted graphs in city maps?
A) Show one-way streets
B) Show distances/time ✅
C) Show friendships
D) Show hierarchy

Short Questions

1. Explain how the insert() function works in Python lists. Provide an example.
Ans:
The insert() function in Python is used to add an element to a list at a specific position. It takes two arguments:

  1. Index Position: The position at which the new element should be inserted.
  2. Element: The item you want to insert.

Syntax:

list_name.insert(index, element)

Example:

party_list = ["Buy drinks", "Buy decorations", "Buy snacks", "Buy cold drinks"]
party_list.insert(0, "Invite friends")  # Adds "Invite friends" at the start
print(party_list)

Output:
["Invite friends", "Buy drinks", "Buy decorations", "Buy snacks", "Buy cold drinks"]


2. Explain the potential issues with lists when two variables reference the same list in a program. Provide an example.
Ans:
When two variables reference the same list in a program, changes made through one variable will affect the original list. This can lead to unintended side effects. This happens because lists are mutable and variables can hold references to the actual list in memory, not separate copies.

Example:

original_list = [1, 2, 3]
reference_list = original_list

reference_list.append(4)
print("Original List:", original_list)
print("Reference List:", reference_list)

Output:

Original List: [1, 2, 3, 4]
Reference List: [1, 2, 3, 4]

3. Define a stack and explain the Last-In, First-Out (LIFO) principle.
Ans:
A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. This means that the last element added (or “pushed”) onto the stack is the first one to be removed (or “popped”) from it.

Key Characteristics of a Stack:

  • Push Operation: Adding an item to the top of the stack.
  • Pop Operation: Removing the item from the top of the stack.

Example:

# Create an empty stack
stack = []

# Push items onto the stack
stack.append("Book A")
stack.append("Book B")
stack.append("Book C")
print("Stack after pushing:", stack)

# Pop an item from the stack
top_item = stack.pop()
print("Popped item:", top_item)
print("Stack after popping:", stack)

Output:

Stack after pushing: ['Book A', 'Book B', 'Book C']
Popped item: Book C
Stack after popping: ['Book A', 'Book B']

4. Differentiate between the Enqueue and Dequeue operations of queue.

OperationEnqueueDequeue
DefinitionAdding an item to the end (rear) of the queueRemoving an item from the front of the queue
PurposeIncreases the size of the queueDecreases the size of the queue
Python Examplequeue.append("Ahmed")queue.pop(0)

5. Name two basic operations performed on stack.
Ans:

  1. Push Operation: Adding an item to the top of the stack.
  2. Pop Operation: Removing an item from the top of the stack.

6. What is the difference between enqueue() and dequeue()?

OperationEnqueueDequeue
DefinitionAdds an item to the end (rear) of the queueRemoves an item from the front of the queue
PurposeIncreases the size of the queueDecreases the size of the queue

NOTE : IF have any Issue drop a Comment Below !..

Leave a Reply

Your email address will not be published. Required fields are marked *