Bird
Raised Fist0
Compiler Designknowledge~20 mins

Basic blocks and flow graphs in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Basic Blocks and Flow Graphs
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Basic Blocks

Which of the following best describes a basic block in compiler design?

AA group of functions called together in a program
BA set of instructions that can be executed in parallel
CA block of code that contains only conditional branches
DA sequence of instructions with no branches except at the entry and exit points
Attempts:
2 left
💡 Hint

Think about a block where control flow enters at the beginning and leaves at the end without interruption.

📋 Factual
intermediate
2:00remaining
Flow Graph Components

In a flow graph representing a program's control flow, what do the nodes and edges represent?

ANodes represent variables; edges represent data dependencies
BNodes represent basic blocks; edges represent possible control flow between blocks
CNodes represent instructions; edges represent memory access
DNodes represent functions; edges represent function calls
Attempts:
2 left
💡 Hint

Consider what a flow graph models in terms of program execution paths.

🔍 Analysis
advanced
2:00remaining
Identifying Leaders in Basic Blocks

Given the following sequence of instructions, which instructions are leaders that start a new basic block?

1: a = 5
2: if a > 0 goto 5
3: b = a + 1
4: goto 6
5: b = a - 1
6: print(b)
AInstructions 1, 3, 5, and 6
BInstructions 1, 3, and 5
CInstructions 1, 2, and 6
DInstructions 1, 2, and 5
Attempts:
2 left
💡 Hint

Leaders include the first instruction, targets of jumps, and instructions following jumps.

❓ Comparison
advanced
2:00remaining
Difference Between Basic Blocks and Flow Graphs

Which statement correctly compares basic blocks and flow graphs?

ABasic blocks represent function calls; flow graphs represent variable dependencies
BFlow graphs are sequences of instructions; basic blocks show possible paths between instructions
CBasic blocks are nodes in a flow graph; flow graphs show control flow between these blocks
DFlow graphs are used only for data flow analysis; basic blocks are used only for syntax checking
Attempts:
2 left
💡 Hint

Think about how basic blocks and flow graphs relate to each other structurally.

❓ Reasoning
expert
2:00remaining
Effect of Removing an Edge in a Flow Graph

In a flow graph of a program, what is the most likely effect of removing an edge that represents a conditional jump?

AIt may cause some basic blocks to become unreachable, changing the program's behavior
BIt will have no effect since edges do not affect execution
CIt will convert the flow graph into a tree structure
DIt will merge two basic blocks into one larger block
Attempts:
2 left
💡 Hint

Consider what happens if a path of execution is removed from the control flow.

Practice

(1/5)
1. What is a basic block in compiler design?
easy
A. A single instruction that causes a jump
B. A sequence of instructions with no jumps or jump targets inside
C. A graph showing all possible paths in a program
D. A data structure used to store variables

Solution

  1. Step 1: Understand the definition of a basic block

    A basic block is a group of instructions executed sequentially without any jumps or jump targets inside it.
  2. Step 2: Compare options with the definition

    A sequence of instructions with no jumps or jump targets inside matches this definition exactly, while others describe different concepts.
  3. Final Answer:

    A sequence of instructions with no jumps or jump targets inside -> Option B
  4. Quick Check:

    Basic block = sequence without jumps [OK]
Hint: Basic block = straight-line code with no jumps inside [OK]
Common Mistakes:
  • Confusing basic block with flow graph
  • Thinking a basic block is a single instruction
  • Mixing up jumps inside and at block boundaries
2. Which of the following correctly represents a flow graph in compiler design?
easy
A. A list of variables used in a program
B. A table of function names and their parameters
C. A set of instructions executed sequentially
D. A directed graph where nodes are basic blocks and edges show possible control flow

Solution

  1. Step 1: Define flow graph

    A flow graph is a directed graph where each node is a basic block and edges represent possible paths the program can take.
  2. Step 2: Match options to definition

    A directed graph where nodes are basic blocks and edges show possible control flow describes this exactly. Other options describe unrelated concepts.
  3. Final Answer:

    A directed graph where nodes are basic blocks and edges show possible control flow -> Option D
  4. Quick Check:

    Flow graph = graph of basic blocks and edges [OK]
Hint: Flow graph = nodes as blocks, edges as jumps [OK]
Common Mistakes:
  • Confusing flow graph with a list or table
  • Thinking flow graph is just a sequence of instructions
  • Ignoring direction of edges in the graph
3. Consider a program with three basic blocks: B1, B2, and B3. B1 jumps to B2 and B3; B2 jumps to B3; B3 has no jumps. Which edges appear in the flow graph?
medium
A. Edges: B1->B2, B1->B3, B2->B3
B. Edges: B1->B2, B2->B3 only
C. Edges: B1->B3, B3->B2
D. Edges: B2->B1, B3->B1

Solution

  1. Step 1: Identify jumps from each block

    B1 jumps to B2 and B3, so edges B1->B2 and B1->B3 exist. B2 jumps to B3, so edge B2->B3 exists. B3 has no jumps, so no outgoing edges.
  2. Step 2: List all edges in the flow graph

    Edges are B1->B2, B1->B3, and B2->B3, matching Edges: B1->B2, B1->B3, B2->B3.
  3. Final Answer:

    Edges: B1->B2, B1->B3, B2->B3 -> Option A
  4. Quick Check:

    Edges match jumps from blocks [OK]
Hint: Edges = jumps from each block [OK]
Common Mistakes:
  • Missing one of the edges from B1
  • Adding edges that don't exist
  • Confusing direction of edges
4. A flow graph is constructed but one edge is missing between two basic blocks that should be connected. What is the most likely cause?
medium
A. The basic blocks were not identified correctly, missing a jump instruction
B. The program has no jumps at all
C. The flow graph always excludes edges between blocks
D. Basic blocks are not connected by edges in flow graphs

Solution

  1. Step 1: Understand flow graph construction

    Edges in a flow graph represent jumps between basic blocks. Missing an edge usually means a jump was missed when identifying blocks.
  2. Step 2: Analyze options

    The basic blocks were not identified correctly, missing a jump instruction correctly identifies the cause as incorrect block identification. Other options are false statements about flow graphs.
  3. Final Answer:

    The basic blocks were not identified correctly, missing a jump instruction -> Option A
  4. Quick Check:

    Missing edge = missed jump in blocks [OK]
Hint: Missing edge means missed jump in block detection [OK]
Common Mistakes:
  • Assuming flow graphs exclude edges by design
  • Thinking no jumps means missing edges
  • Confusing basic blocks with edges
5. Given a program with nested if-else statements, how can flow graphs help optimize the program?
hard
A. By converting the program into machine code directly
B. By listing all variables used in the program
C. By showing all possible paths, they help identify unreachable code and optimize execution paths
D. By removing all loops automatically

Solution

  1. Step 1: Understand the role of flow graphs in optimization

    Flow graphs display all possible execution paths, making it easier to spot unreachable code and redundant branches.
  2. Step 2: Match options to optimization benefits

    By showing all possible paths, they help identify unreachable code and optimize execution paths correctly states that flow graphs help identify unreachable code and optimize paths. Other options describe unrelated or incorrect uses.
  3. Final Answer:

    By showing all possible paths, they help identify unreachable code and optimize execution paths -> Option C
  4. Quick Check:

    Flow graphs reveal paths for optimization [OK]
Hint: Flow graphs reveal unreachable code and optimize paths [OK]
Common Mistakes:
  • Thinking flow graphs list variables
  • Confusing flow graphs with code generation
  • Assuming flow graphs remove loops automatically
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard