Consider the following pseudo-code. 𝐿1: 𝑑1 = βˆ’1 𝐿2: 𝑑2 = 0 𝐿3: 𝑑3 = 0…

2024

Consider the following pseudo-code.

Β  Β  Β  Β  𝐿1: 𝑑1 = βˆ’1

Β  Β  Β  Β  𝐿2: 𝑑2 = 0

Β  Β  Β  Β  𝐿3: 𝑑3 = 0

Β  Β  Β  Β  𝐿4: 𝑑4 = 4 βˆ— 𝑑3

Β  Β  Β  Β  𝐿5: 𝑑5 = 4 βˆ— 𝑑2

Β  Β  Β  Β  𝐿6: 𝑑6 = 𝑑5 βˆ— 𝑀

Β  Β  Β  Β  𝐿7: 𝑑7 = 𝑑4 + 𝑑6

Β  Β  Β  Β  𝐿8: 𝑑8 = π‘Ž[𝑑7]

Β  Β  Β  Β  𝐿9: if 𝑑8 <= π‘šπ‘Žπ‘₯ goto 𝐿11

Β  Β  Β  Β  𝐿10: 𝑑1 = 𝑑8

Β  Β  Β  Β  𝐿11: 𝑑3 = 𝑑3 + 1

Β  Β  Β  Β  𝐿12: if 𝑑3 < 𝑀 goto 𝐿4

Β  Β  Β  Β  𝐿13: 𝑑2 = 𝑑2 + 1

Β  Β  Β  Β  𝐿14: if 𝑑2 < 𝑁 goto 𝐿3

Β  Β  Β  Β  𝐿15: π‘šπ‘Žπ‘₯ = 𝑑1

Which one of the following options CORRECTLY specifies the number of basic

blocks and the number of instructions in the largest basic block, respectively ?

  1. A.

    6 and 6

  2. B.

    6 and 7

  3. C.

    7 and 7

  4. D.

    7 and 6

Attempted by 80 students.

Show answer & explanation

Correct answer: D

tep 1: Identify leaders

Rules for leaders (start of basic blocks):

  1. First instruction is a leader β†’ L1

  2. Targets of jumps are leaders β†’ L3, L4, L11, L15

  3. Instruction following a conditional or unconditional jump is a leader β†’ L10, L13

Leaders: L1, L3, L4, L10, L11, L13, L15

So, 7 basic blocks (one per leader).

Step 2: Form basic blocks

  1. BB1: L1, L2 β†’ straight-line until L3 (next leader)

  2. BB2: L3 β†’ only L3

  3. BB3: L4, L5, L6, L7, L8, L9 β†’ ends with conditional jump

  4. BB4: L10 β†’ single instruction after jump

  5. BB5: L11, L12 β†’ loop back to L4

  6. BB6: L13, L14 β†’ loop back to L3

  7. BB7: L15 β†’ final instruction

Step 3: Count instructions in each BB

BB

Instructions

BB1

2 (L1-L2)

BB2

1 (L3)

BB3

6 (L4-L9)

BB4

1 (L10)

BB5

2 (L11-L12)

BB6

2 (L13-L14)

BB7

1 (L15)

Largest BB = 7? Actually check: BB3 has 6 instructions (L4-L9), which is largest.

  • So largest BB = 6 instructions

  • Number of BB = 7

Step 4: Correct Answer

NumberΒ ofΒ BBΒ =Β 7,Β LargestΒ BBΒ =Β 6Β instructions\text{Number of BB = 7, Largest BB = 6 instructions}NumberΒ ofΒ BBΒ =Β 7,Β LargestΒ BBΒ =Β 6Β instructions

Answer: D β†’ 7 and 6

A video solution is available for this question β€” log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir