Consider the following pseudo-code: If (A > B) and (C > D) then A = A + 1 B =…

2012

Consider the following pseudo-code:

If (A > B) and (C > D)
  then A = A + 1

B = B + 1

If the cyclomatic complexity of the pseudo-code is

  1. A.

    2

  2. B.

    3

  3. C.

    4

  4. D.

    5

Attempted by 59 students.

Show answer & explanation

Correct answer: B

The cyclomatic complexity of a program can be calculated using the formula:

V(G) = E − N + 2P

Where:

V(G) is the cyclomatic complexity.
E is the number of edges in the control flow graph.
N is the number of nodes in the control flow graph.
P is the number of connected components (usually 1).
In your pseudo code, there are two conditional statements (A > B and C > D), and an “ENDIF” which represents the end of the conditional block. This forms a basic control flow structure.

Let’s break it down into a control flow graph:

Start (Node 1)
Condition A > B (Node 2)
Condition C > D (Node 3)
A = A + 1 (Node 4)
B = B + 1 (Node 5)
EndIF (Node 6)

Now, count the number of edges (E) and nodes (N):

E = 7 (7 transitions between nodes)
N = 6 (6 nodes)

Since there is only one connected component (P = 1), we have:

V(G) = E − N + 2P = 7 − 6 + 2 * 1 = 3

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

Explore the full course: Mppsc Assistant Professor