GATE / CS ARCHIVE

GATE CSE 2015 SET 1 / QUESTION 38

GATE CSE 2015 Set 1 Q38

Engineering Mathematics & Other Topics · Software Engineering · Full GATE CSE 2015 Set 1 paper

GATE 2015 Set 1, Q38 2 marks · NAT
Not in GATE 2027 syllabusReport issue

Software Engineering

Question 38, GATE 2015: Find cyclomatic complexity of: while(first<=last){if(array[middle]<search) first=middle+1; else if(array[middle]==search) found=TRUE; else last=middle-1; middle=(first+last)/2;} if(first>last) notPresent=TRUE;.
Tap to enlarge
Answer & solution

Official answer: 5

For a single connected control-flow graph with binary conditions, McCabe complexity is one plus the number of decisions. Count while, if(<), else-if(==), and the final if(>): four decisions, hence five. An unconditional else is not an additional test; assignments are not branches. The code can fail to terminate after finding the value, but that semantic defect does not remove a syntactic decision from its control-flow graph.

Solution: The control-flow graph has four binary decisions: the while condition, the first if, the else-if, and the final if. For one connected entry/exit graph, cyclomatic complexity is decisions+1=5. This graph count does not require every syntactic path to be feasible or terminating.

Original paper, page 14 · Answer key