AP Computer Science Unit 4 Test Project Stem

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

int a = 20;

while (/ missing condition /)

{

System.out.println("a = " + a);

a = a + 3;

}

For which of the following replacements for / missing condition / will the segment result in an infinite loop (the code will continue running and never stop)?

I.

a <= 20

II.

a >= 20

III.

a != 50

2 only

2
New cards

Which of the following code segments will print all the multiples of 5 between 1 and 50 inclusive? The numbers may be printed in ascending or descending order.

I.

for (int k = 50; k > 0; k--)

{

if ((k % 5) == 0)

{

System.out.println(k);

}

}

II.

for (int k = 5; k <= 50; k += 5)

{

System.out.println(k);

}

III.

for (int k = 50; k >= 0; k -= 5)

{

System.out.println(k);

}

1 and 2 only

3
New cards

Consider the following code segment?

String str = "result";

String result = "";

for (int i = 0; i < str.length(); i++)

{

int index = (i + 3) % str.length();

result = str.substring(index, index + 1) + result;

}

What is stored by the string result after this code segment has been executed?

sertlu

4
New cards

What is output to the screen by the following code?

int f = 0;

while (f < 7)

{

System.out.print(f % 3 + " ");

f++;

}

0 1 2 0 1 2 0

Correct. The loop iterates 7 times, so there should only be 7 values. The postfix operator inside the print statement is valid syntax, and the increment will be applied after the value of f % 3 is printed. So if f = 0, the print statement will print 0 % 3, then increment f to 1.

5
New cards

Assuming that str is a correctly initialized String, which of the following best describes what the algorithm below does?

int i = str.indexOf("a");

while (i != -1)

{

System.out.print(str.substring(0,i));

str = str.substring(i + 1);

i = str.indexOf("a");

}

System.out.print(str);

Prints each character in the string, str, except for the lowercase a's

Correct. On each iteration of the loop, the code will check if there is a lowercase a in the string. If so, the loop will print all the characters up to the index of the a. Then check if there are any a's in the remaining characters, and repeat the process.

6
New cards

What is output by the following code?

String str1 = "abcd";

String str2 = "efghi";

int i = 0;

int j = str2.length() - 1;

while (i < str1.length())

{

System.out.print(str2.substring(j, j + 1));

System.out.print(str1.substring(i, i + 1));

i++;

j--;

}

iahbgcfd

Correct. This loop will interleave the characters of str1 and str2, starting with the end of str2 and start of str1. The loop will only interleave 4 sets of characters, because str1.length() == 4. This prevents the e in str2 from being printed.

7
New cards

Which of the following statements will return a random even number between 6 and 14 inclusive?

6 + 2 (int) (5 Math.random())

8
New cards

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 1 3 5 7 9.

Note that the choices will fill in the three blanks in the order which they appear.

for (int i = ___; i ____; i ___ ) { System.out.print(i + " ");}

1, < 10, += 2

Correct. We want to print all the positive integers from 1 to 9, inclusive. So we start at i=1. To stop at i=10, we can set our condition to be i<10 or i<=9. To print every other number, our increment should be i+=2.

9
New cards

What does short circuit evaluation mean in the following code?

if (a < b && c != d){System.out.print("True");}

if c != d is false, then a < b is not evaluated

10
New cards

Assume that x and y are boolean variables and have been properly initialized.

(x && y) || !(x || !y)

Which of the following best describes the result of evaluating the expression above?

true only when y is true

Correct. Using De Morgan's Law, we can reduce this expression to the following: (x && y) || (!x && y). From here we can simplify the expression even further to look like this: y && (x || !x). This can be even further simplified to just y.

11
New cards

Consider the following output.

4 3 2 1

3 2 1

2 1

1

Which of the following code segments will produce this output?

for (int j = 4; j > 0; j--)

{

for (int k = j; k > 0; k--)

{

System.out.print(k + " ");

}

System.out.println();

}

12
New cards

Which of the following code segments prints the number of times the code substring "ab" appears in the String str?

int count = 0;

for (int i = 2; i <= str.length(); i++)

{

if (str.substring(i - 2, i).equals("ab"))

{

count += 1;

}

}

System.out.println(count);

Correct. The loop starts at i = 2 and increments i by one for each iteration of the loop. It will check each pair of adjacent characters to check if they are equal to the string "ab".

13
New cards

Which of the following would print the numbers:

94 73 52 31

I.

for (int t = 94; t > 30; t -= 21)

{

System.out.print(t + " ");

}

II.

int t = 115;

while (t > 31)

{

t -= 21;

System.out.print(t + " ");

}

III.

int t = 94;

while (t > 31)

{

System.out.print(t + " ");

t -= 21;

}

I and II only

Correct. Choice 1 uses a for-loop to print 4 values, starting with 94, decreasing each value by 21 while t >= 31. Choice 2 uses a while loop that decrements the initial value of t before printing the value of t. So each print will be t - 21.

14
New cards

Consider the following code segment.

for (int n = 1; n < 20; n += 7)

{

for (int k = n; k < n + 5; k++)

{

if (k % 2 == 0)

{

System.out.print(k + " ");

}

}

System.out.println();

}

What will be printed as a result of executing the code segment?

2 4

8 10 12

16 18

Correct. First things first, the outer loop will iterate 3 times, with n = 1, 8, and 15. The inner loop will print all the even numbers (divisible by 2) from k = n to k + 5, excluding k + 5. For n = 1, k = 1 and k + 5 = 6, the inner loop prints 2 and 4. For n = 8, k = 8 and k + 5 = 13, the inner loop prints 8, 10, and 12. For n = 15, k = 15 and k + 5 = 20, the inner loop prints 16 and 18.

15
New cards

Consider the following code:

String w = "Hello World";

for (int i = 0; i < w.length(); i++)

{

if (i % 5 != 2)

{

System.out.print(w.substring(i, i + 1) + " ");

}

}

What is output?

H e l o W r l d

Correct. The loop will print each character in the string w such that i % 5 != 2. This loop will iterate 11 times, once for each character in the string w. Out of those 11 times i % 5 == 2 when i = 2, and when i = 7. The characters at index 2 and 7 in the string w are l and o. These two characters are the only two that will not be printed out.

16
New cards

What is output to the screen by the following code?

int c = 1;

while (c < 5)

{

System.out.print((int)Math.pow(-1, c) + " ");

c++;

}

-1 1 -1 1

Correct. To answer this question, you don't really need to know much about what Math.pow is doing. The loop iterates 4 times, so there should be 4 outputs.

17
New cards

Consider the following code segment.

int x = 1;

while (x < 10 || (x % 4) != 0)

{

x += 3;

}

System.out.println(x);

What is printed as a result of executing the code segment?

16

Correct. This loop will iterate 5 times, and won't stop until x is greater than or equal to 10 and x is divisible by 4. The values of x are as follows: 1 4 7 10 13 16. 16 is both greater than 10 and divisible by 4, so the loop will terminate when x = 16.

18
New cards

Consider the following code segment

for (int n = 1; n < 4; n++)

{

int k;

for (k = n / 2; k < n; k++)

{

System.out.print("b");

}

for (k = n / 2; k > 0; k--)

{

System.out.print("a");

}

System.out.print(" ");

}

What is printed as a result of executing the code segment?

b ba bba

Correct. The outer for-loop iterates 3 times, with n = 1, 2, 3. When n = 1, n / 2 = 0, so the first inner for-loop will iterate 1 time printing 1 b, and the second inner for-loop will iterate 0 times. When n = 2, n / 2 = 1, so the first inner for-loop will iterate 1 time printing 1 b, and the second inner for-loop will iterate 1 time printing 1 a. When n = 3, n / 2 = 1, so the first inner for-loop will iterate 2 times, printing 2 b's and the second inner for-loop will iterate 1 time, printing 1 a.

19
New cards

Which of the following describes a valid way to compare the efficiency of two equivalent algorithms written in Java?

Write lines of code which calculate statement execution counts and compare these values for a range of inputs.

Correct. The efficiency of an algorithm can be measured by the number of computations, or executions performed by that algorithm on a range of inputs.

20
New cards

Consider the following code segment.

int sum = 1;

for (int a = 1; a < 4; a++)

{

for (int b = a; b < 4; b++)

{

sum *= a;

}

}

System.out.println(sum);

What is printed as a result of executing the code segment?

12

Correct. The outer for-loop iterates 3 times with a = 3, 2, 1. For a = 3, b = 3, and the inner loop will iterate 1 time with sum = 3 1. For a = 2, b = 2, and the inner loop will iterate 2 times, leaving sum = 2 2 3 1. For a = 1, b = 1 and the inner loop will iterate 3 times, sum = 1 1 1 2 2 3 1. This leaves sum = 12.