CS FundamentalsVery hardTCS NQT · Infosys · product-company placement
Counting each if, loop header, and/or operator, conditional expression and except clause as one decision (V(G) = decisions + 1), what is the cyclomatic complexity of f, and what does f([3, -1, 4], 5) return?
Counting each if, loop header, and/or operator, conditional expression and except clause as one decision (V(G) = decisions + 1), what is the cyclomatic complexity of f, and what does f([3, -1, 4], 5) return?
def f(items, limit):
total = 0
for x in items:
if x < 0 or x > limit:
continue
total += x if x % 2 == 0 else 2 * x
else:
total += 1
try:
r = 100 // total
except ZeroDivisionError:
r = -1
return r- A. 7 and 9
- B. 6 and 10
- C. 6 and 9
- D. 7 and 10
Correct answer: C. 6 and 9
Decisions: for, if, or, the conditional expression and except = 5, so V(G) = 6; the loop's else adds nothing because there is no break, so it always runs. total = 6 (3 doubled) + 4 = 10, then the else makes it 11 and 100 // 11 = 9. Forgetting the else gives 10.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- A function has three if-else statements in sequence, followed by a loop that runs either 0 or 1 time (decided by the input) and whose body…
- For a function of two independent bounded inputs, match each boundary-value technique with the number of test cases it needs. P. Normal…
- A decision table has three yes/no conditions and four rules: R1: C1 = N (C2, C3 don't care) → A; R2: C1 = Y, C2 = N (C3 don't care) → B…
- A state machine has transitions a: S1→S2, b: S2→S3, c: S3→S1 and d: S2→S1. How many test items does 0-switch (every transition) coverage…
- How many of these statements about data-flow coverage criteria are correct? 1. All-uses subsumes all-defs (when every definition reaches…
- The condition x > 5 and x >= 0 (x an integer) is mutated four ways. Which mutant is equivalent, so that no test can kill it?