AP Computer Science MC Question

<p>How would you guys approach this question? I'm at dead end. </p>

<p>int k= a random number such that 1<=k<=n
for(int p=2; p<=k; p++)
for(int r=1; r<k; r++)
System.out.println("Hello");</p>

<p>What is the maximum number of times Hello will be printed out?
A. 0
B. 1
C. 2
D. n-1
E. n-2</p>

<p>What is the minimum?
A. 2
B. n-1
C. n-2
D. (n-1)^2
e. n^2</p>

<p>make up random numbers for the variable</p>

<p>Is that an entirely accurate method of going about these types of problems? I always get paranoid I’m choosing the “wrong” test values that are giving me incorrect answers!</p>

<p>Did you switch up the multiple choice choices by accident? For maximum I would have thought it would be (n-1)^2 and minimum I would have thought it was 0?</p>

<p>Well minimum is easy. Let’s say k is 1 and then neither loop runs and the answer is 0. You got the answers mixed up.</p>

<p>Assuming you did – minimum is the easiest because it might not even enter the for loops and it would be 0. Then for maximum I plugged in 5 as K and then it would go through the outer loop 4 times and each time it goes through the outer loop it would go through 4 times. So 4*4 = 16 and thats (5-1)^2</p>

<p>You have k-1 iterations of k-1 Hellos
The maximum is (n-1)(n-1) = n^2-1 while the minimum is 0 when k=1 since (1-1)(1-1)=0
The first for loop is the exact same as the second for loop.</p>

<p>Yeah just make up test values to plug in. Sounds like a cheap tactic for SATs and AP Calculus but simply put programmers do this to test their programs. There are no hard and fast rules for these types of problems, just practice.</p>

<p>AHHH I did! So sorry for the confusion.</p>

<p>int k= a random number such that 1<=k<=n
for(int p=2; p<=k; p++)
for(int r=1; r<k; r++)
System.out.println(“Hello”);</p>

<p>What is the minimum number of times Hello will be printed out?
A. 0
B. 1
C. 2
D. n-1
E. n-2</p>

<p>What is the maximum?
A. 2
B. n-1
C. n-2
D. (n-1)^2
e. n^2</p>

<p>But thank you so much everyone! This makes a lot more sense now.</p>