| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
Share
About Exam
medium Solved by 1 students
Runtime Comparison of Loop Structures
< Prev
Next >

Calculate statement execution counts and informal run-time comparison of iterative statements.

Consider these two code segments that accomplish the same task:

// Segment A
int sum = 0;
for (int i = 0; i < n; i++) {
    sum += arr[i];
}

// Segment B  
int sum = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1; j++) {
        sum += arr[i];
    }
}

Which statement best compares the run-time efficiency of these segments?

A

Segment B is less efficient because it has quadratic time complexity

B

Segment A is more efficient because it has fewer loop headers to execute

C

Segment B is more efficient because nested loops are always faster

D

Both segments have the same run-time efficiency since they perform the same number of operations

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1hitrishabhatia11 0m 00s 100
#2suhanakochhar00602 0m 00s -20
#3ravi.palepu01 0m 58s -68
#4y.seong202701 1m 10s -80
Items per page:
10
1 – 4 of 4

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy