Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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
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:
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:
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.
| Operation | Enqueue | Dequeue |
|---|---|---|
| Definition | Adding an item to the end (rear) of the queue | Removing an item from the front of the queue |
| Purpose | Increases the size of the queue | Decreases the size of the queue |
| Python Example | queue.append("Ahmed") | queue.pop(0) |
5. Name two basic operations performed on stack.
Ans:
6. What is the difference between enqueue() and dequeue()?
| Operation | Enqueue | Dequeue |
|---|---|---|
| Definition | Adds an item to the end (rear) of the queue | Removes an item from the front of the queue |
| Purpose | Increases the size of the queue | Decreases the size of the queue |
NOTE : IF have any Issue drop a Comment Below !..