CS FundamentalsVery hardProduct-company placement · Python

Counting each if, while and conditional expression as one decision (V(G) = decisions + 1), what is the cyclomatic complexity of g, and what do g(49) and g(1) return?

Counting each if, while and conditional expression as one decision (V(G) = decisions + 1), what is the cyclomatic complexity of g, and what do g(49) and g(1) return?
def g(n):
    i, found = 2, False
    while i * i <= n:
        if n % i == 0:
            found = True
            break
        i += 1
    else:
        return 'prime' if n > 1 else 'neither'
    return 'composite' if found else 'odd'
  1. A. 5; 'composite' and 'neither'
  2. B. 6; 'composite' and 'neither'
  3. C. 5; 'prime' and 'neither'
  4. D. 5; 'composite' and 'prime'

Correct answer: A. 5; 'composite' and 'neither'

Decisions: while, if and two conditional expressions, so V(G) = 5 (the loop's else is part of the while decision). g(49) breaks at i = 7, skipping the else, and returns 'composite'. For g(1) the loop never runs, so the else returns 'neither'.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic