AP Computer Science A Project Stem Chp. 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

What is x equal to after the following code is executed? int x = -7; x++;

-6

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

What is output by the following code? for (int i = 0; i < 4; i++){ for (int j = i; j < 5; j++){ System.out.print(j + " ");} System.out.println();}

0 1 2 3 4 1 2 3 4 2 3 4 3 4

What is output? int a = 0; int b = 3; while(a < 4 && b < 4){ System.out.println(a + " " + b); a++; b--;}

0 3 1 2 2 1 3 0

What is the output? int n = 70; while(n > 35){ System.out.print(n % 10 + " "); n -=5;}

0 5 0 5 0 5 0

What is output by the following code? for (int i = 1; i < 4; i++){ for (int j = 0; j < i; j++){ System.out.print(i);} System.out.println();}

1 22 333

What is output? for (int i = 1; i <= 6; i++){ System.out.print(i + " ");}

1 2 3 4 5 6

int x = 1; while (x < 9){ System.out.print(x + " "); x += 3;} What is the output?

1 4 7

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

What is x equal to after the following code is executed? int x = 11; x--;

10

What is output by the following code? for (int i = 5; i >= 2; i--){ for (int j = 2; j <= 4; j++){ System.out.print(i * j + " ");} System.out.println();}

10 15 20 8 12 16 6 9 12 4 6 8

for (int i = 10; i > 2; i -= 2){ System.out.print(i + " ");} What is the output?

10 8 6 4

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 10 9 8 7 6 5 4 3 2 1. 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 + " ");}

10, >= 1, --

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

What is output by the following code? int a = 0; for (int i = 1; i < 5; i++){ for (int j = 1; j < 4; j++){ a++;}} System.out.println(a);

12

for (int i = 12; i >= 3; i -= 3){ System.out.print(i + " ");} What is output by the code segment?

12 9 6 3

int a = 4; int s = 0; while (a % 7 != 0){ s += a; a++;} System.out.println(s); What is the output?

15

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

What is the last number printed by this code? int n = 14; while(!(n % 2 == 0 && n % 3 == 0)){ System.out.println(n); n += 5;}

19

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

What is output by the following code? int x = 0; while (x < 5){ int y = 5; while (x < y){ y--; x++;} System.out.print(y + " ");}

2 4 4

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 2 4 6 8. 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 + " ");}

2, <= 8, += 2

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 3 6 9 12 15. 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 + " ");}

3, <= 15, += 3

Consider the following code segment. for (int k = 30; k > 0; k = k - 3){ if (k % 5 == 0) { System.out.print(k + " ");} } What is printed as a result of executing the code segment?

30 15

What is printed by the following code? String str = "an anaconda or an ant"; int count = 0; for (int i = 0; i < str.length() - 1; i++){ if (str.substring(i, i + 2).equals("an")){ count++;} } System.out.println(count);

4

int count = 4; while (count <= 7) { count++; System.out.print(count + " ");} What are the first and last numbers output?

5 8

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

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

What is the output? int n = 5; while(n <= 18){ n += 2; System.out.print(n + " ");}

7 9 11 13 15 17 19

What is output to the screen by the following code? int num = 1987; while (num > 0){ num = num / 10; System.out.print(num % 10 + " ");}

8 9 1 0

When is the increment statement (shown in bold below) run in a for loop? for(int i = 0; i < 10; i++)

At the end of every run of the loop, before the boolean expression is checked for the next iteration of the loop.

The following code segment displays the number of times the character 'b' appears in the String str. int count = 0; for (int i = 0; i < str.length(); i++){ String letter = str.substring(i, i + 1); if (letter.equals("b")){ count++;} } System.out.println(count); Which of the following modifications to the code will ensure the number it displays includes times the capital 'B' appears?

Change the if statement condition to letter.equals("b") || letter.equals("B")

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

How many times will the following loop repeat? int num = 49; while (num > 0){ if (num % 2 == 0){ num++;} else{num--;}}

Infinitely

What is output by the following code? String str = "RAM"; for (int i = str.length(); i > 0; i--){ for (int j = 1; j <= 3; j++){ System.out.print(str.substring(i - 1, i));}}

MMMAAARRR

When is the boolean statement (shown in bold below) run in a for loop? for(int i = 0; i < 10; i++)

On every iteration of the loop, including the first and last iteration

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

Consider the following code segment in which str is a String with length greater than 1. for (int i = 0; i < str.length() - 1; i++){ if (str.substring(i, i + 2).equals("hi")){ System.out.println("Founded!");} } Which of the following best describes what this code segment does?

Prints out "Founded!" for every substring which is equal to "hi".

What is one potential problem with the following loop? int n = 7; while(n != 6){ System.out.println(n);}

Since n does not change the loop will not stop

​​Which of the following best describes what the following algorithm does? You may assume the variable s is initialized as a non-empty String. boolean r = true; for (int i = 0; i < s.length() - 1; i++){ if (!s.substring(i, i + 1).equals(s.substring(i + 1, i + 2))){ r = false;}} if (r){ System.out.println("pass");}

The algorithm prints "pass" if all the letters in the string are the same

The following loop is intended to print the even numbers from 20 to 26 inclusive: int x = 20; while (x < 26){ System.out.print(x); x++;} What would you need to change in order for the code to work correctly?

The x++ needs to be x += 2 and the x < 26 needs to be <=

What is one potential problem with the following loop? Scanner scan = new Scanner(System.in); System.out.print("Enter integers. Enter -1 to exit."); System.out.println("Count of integers entered will be returned."); int n = 0; int c = 0; while(n != -1){ n = scan.nextInt(); c++;} System.out.println(c);

There are no problems with the loop

If A is false, B is true and C is false, is the following compound statement true or false? (B || A) || (A && C)

True

When might it be more helpful to use a while loop instead of a for loop?

When you do not know how many times a loop will repeat.

When would it be more beneficial to use a while loop instead of a for loop?

When you need your loop to be controlled by user input.

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.

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.

a & b

Consider the following code segment: 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)?

a >= 20

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

What is output by the following code? String str = "compiler"; System.out.print(str.substring(0, 3)); System.out.print(str.substring(1, str.length() - 1));

comompile

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();}

String w = "hellothere"; for (int i = 0; i < w.length(); i++){ System.out.print(w.substring(i, i + 1) + " "); if (i % 4 == 3){ System.out.println();} } What is output?

h e l l o t h e r e

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

What does short circuit evaluation mean in the following code? if (a < b && c != d) {System.out.print("True");}

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

Which of the following code segments will produce the same output as the segment below? for (int i = 2; i <= 17; i += 3){ System.out.print(i);}

int i = 2; while (i <= 17){ System.out.print(i); i += 3;}

String str = "pokemon"; for (int i = 1; i < str.length(); i += 2){ System.out.print(str.substring(i, i + 1));} What is the output?

oeo

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

Suppose the String variable str is initialized with a value of "python". Which of the following is equal to the string "tho"?

str.substring(2, 5)

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


संबंधित स्टडी सेट्स

ABEKA WORLD HISTORY AND CULTURES APPENDIX QUIZ M

View Set

med-surg. Pharm. Chapter 91: Miscellaneous Antibacterial Drugs: Fluoroquinolones, Metronidazole, Daptomycin, Rifampin, Rifaximin, Bacitracin, and Polymyxins

View Set

Chapter 18 9th grade Geography Test

View Set

H&C Ch. 4: Health Education and Health Promotion

View Set