1. int i=1;
2. while (i <= n) {
3. for (int j=1; j<10; j++)
4. sum++;
5. i++;
6. }
7. for( int j = 1; j <= n; j++ )
8. for( int k = 1; k <= n; k=k*2 )
9. sum++;

1. How many times is statement 4 executed?
A. O(n)
B. O(n2)
C. O(log n)
D. O(n log n)
E. none of the above

Here I chose A

2. How many times is statement 9 executed?
A. O(n)
B. O(n2)
C. O(log n)
D. O(n log n)
E. none of the above

Because of line 8 (k=k*2) I chose C

3. What is the running time of the entire code fragment?
A. O(n)
B. O(n2)
C. O(log n)
D. O(n log n)

Since O(n)+O(logn)=O(n) so I chose A

What do u think?

sergent commented: Do your OWN homework! -1

Recommended Answers

All 4 Replies

I can only help you with number 4.

4. How to do your homework?
A. By yourself
B. By yourself
C. Both A and B
D. Both A, B, C, and D

The correct answer is D

I just want to know if I am on the right track
It is an exercise to a past exam paper. for revision

Sorry, I did not see you already solved it. I am not sure about any of these because these is not C++ but computer science.
Next time post this to Software Engineering --> computer science

For 1, you choose A, which is correct.

For 2, your almost there but its not correct, because there are 2 nested loop you have to take into account both of them.

For 3, redo 2 then see what you get. But your almost there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.