AP Computer Science: Unit 4

¡Supera tus tareas y exámenes ahora con Quizwiz!

What will the call to method patternGrid(3,4,'#') print? public void patternGrid(int rows, int columns, char symbol) { for(int m = 0; m < rows; m++) { for(int n = 0; n < columns; n++) { System.out.print(symbol); } System.out.println(); } }

#### #### ####

int p = 5; while(p > 0) { for(int j = p; j < p*2; j++) { System.out.print(" * "); } System.out.println(); p--; }

* * * * * * * * * * * * * * *

int a = 1; String result = ""; while (a < 20) { result += a; a += 5; } System.out.println(result); What, if anything, is printed as a result of executing the code segment?

161116

What would the method call myMethod("Karel The Dog", 'e') output? public int myMethod(String x, char y) { int z = 1; for(int i = 0; i < x.length(); i++) { if(x.charAt(i) == y) { z++; } } return z; }

3

int x = 10; int y = 2; int count = 1; while(x > y) { x /= y; count++; } What will the value of count be after executing the code segment?

3

What will the call to the method funWithNumbers(314159) print to the screen? public void funWithNumbers(double myDouble) { int myInt = (int) myDouble; String myString = ""; while(myInt != 0) { myString = myInt % 10 + myString; myInt /= 10; } System.out.println(myString); }

314159

What would the call to method divideByTen(340) output? public int divideByTen(int num) { while(num / 10 >= 10) { num /= 10; } return num; }

34

int count = 0; int number = 20; while (number > 0) { number = number / 2; count++; } What will be the value of count after executing the code segment?

5

int a = 100; while (a > 1) { System.out.println("$"); a /= 2; } How many $'s are printed as a result of executing the code segment?

6

int k = 35; while (k >= 0) { System.out.println("X"); k -= 5; } How many times will the string "X" be printed as a result of executing the code segment?

8

public int countPs(String word) { int sum = 0; for(int i = 0; i < word.length(); i++) { word = word.toLowerCase(); if(word.substring(i,i+1).equals("p")) { sum++; } } return sum; } What would the value of sum be after the method call countPs("Peter Piper picked a pack of pickled peppers")?

9

public static void mix(String word, String letter) { int counter = 0; while(word.indexOf(letter) > 0) { if(word.substring(counter,counter + 1).equals(letter)) { word = word.substring(0, counter) + "0" + word.substring(counter +2, word.length()); } counter++; } System.out.println(word); } What value word would be printed as a result of the call mix("Hippopotamus", "p")?

Hi0o0tamus

String str = "AP-CSA"; for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 1).equals("A")) { System.out.print(i + " "); } } What is printed as a result of executing the code segment?

0 5

What does the call to the method someMethod(3,1) output? public int someMethod(int x, int y) { int sum = 0; while (x < 10) { sum += x % y; x++; y++; } return sum; }

10

int b = 10; String result = ""; while(b < 100) { result += b; b *= 2; } System.out.println(result); What, if anything, is printed as a result of executing this statement?

10204080

How many times will the word "Heyo!" be printed in the following code segment? int count = 3; while(count <= 7) { for(int i = 2; i < 5; i++) { System.out.println("Heyo!"); } count ++; }

15

int counter = 5; int sum = 0; while(counter < 10) { sum+= counter; counter+=2; } Which of the following for loops would return the same value if System.out.println(sum) were printed? I. int sum = 0; for(int i = 0; i < 3; i++) { sum += i; } II. int sum = 11; for(int i = 0; i < 5; i ++) { sum+=i; } III. int sum = 0; for(int i = 5; i < 10; i+=2) { sum +=i; } IV. int counter = 5; int sum = 0; for(int i = counter; i < 10; i+=2) { sum+= counter; }

II and III

public int digitSum(int num) { int total = 0; while (num > 0) { total += num % 10; num /= 10; } return total; } Which of the following code segments could replace the while loop in the method digitSum without changing the value returned by the method? I. for (int h = 0; h < num; h++) { total += num % 10; num /= 10; } II. for (int j = num; j > 0; j--) { total += j % 10; } III. for (int k = num; k > 0; k /= 10) { total += k % 10;

III only

for(int j = 0; j < 4; j++) //line 1 { for(int k = 0; k < j+1; k++) { System.out.print(k + ""); } } Which of the following best explains the result of changing j < 4 to j > 4 on line 1?

No output will be produced, as the boolean condition will never be met in the outer for loop.

for (int i = 0; i < 5; i++) // Line 1 { for (int j = 0; j < 5; j++) { int k = i + j; System.out.print(k + " "); } } Which of the following best describes the result of changing i < 5 to i > 5 in line 1?

Nothing will be printed because the body of the outer for loop will not execute at all.

What kind of error would the method call myMethod("Frog") cause? public void myMethod(String x) { for(int i = 0; i <= x.length(); i++) { System.out.println(x.substring(i, i+1)); } }

Runtime Error: String index out of range

Code Segment I for (int i = 0; i < 10; i++) { System.out.print( "*" ); } Code Segment II for (int i = 1; i <= 10; i++) { System.out.print( "*" ); } Which of the following best explains how the difference in the two loop headers affects the output?

The output of the code segments is the same because the loops in both code segments iterate 10 times.

for (int j = 0; j < 4; j++) { for (int k = 0; k < j; k++) { System.out.println("hello"); } } Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment?

The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

int num = 8; for(int i = num; i > 0; i -= 3) //Line 2 { System.out.print(" " + i + " "); } Which of the following best explains how changing i > 0 to i >= 0 will change the result?

There will be no change in the program because the for loop will iterate the same number of times.

int n = 6; for (int i = 1; i < n; i = i + 2) // Line 2 { System.out.print(i + " "); } Which of the following best explains how changing i < n to i <= n in line 2 will change the result?

There will be no change to the program output because the loop will iterate the same number of times.

int a = 1; while (a <= 2) { int c = 1; while (/* missing condition */) { System.out.print("*"); c++; } a++; } The code segment is intended to print "******". Which of the following can be used to replace /* missing condition */ so that the code segment works as intended?

c <= 3

public String mystery(String word, int num) { String result = ""; for (int k = num; k >= 0; k--) { result += word.substring(0, k); } return result; } What is returned as a result of the call mystery("computer", 3) ?

comcoc

Consider the following incomplete code segment, which is intended to increase the value of each digit in a String by one. For example, if num is 12345, then the resulting String would be 23456. String num = "12345"; int counter = 0; String result = ""; while(/* Missing Loop Header */) { int newNum = Integer.valueOf(num.substring(counter,counter+1)); result+= (newNum + 1); counter++; } System.out.println(result); Which of the following should replace /* Missing Loop Header */ so that the code segment works as intended?

counter < num.length()

for (int k = 0; k < 4; k++) { /* missing loop header */ { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0 11 222 3333 Which of the following can be used to replace /* missing loop header */ so that the code segment will work as intended?

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

int total = 0; for (int k = 0; k <= 100; k += 2) { total += k; } Which of the following for loops could be used to replace the for loop in the original code segment so that the original and the revised code segments store the same value in total?

for (int k = 1; k <= 101; k += 2) { total += k + 1; }

String word = "Cafeteria"; for(/* missing condition */) { System.out.print(word.substring(i+1,i+2) + " "); } The code segment is intended to print every other letter in the word, starting with index 0, to produce the result C f t r a. Which of the following can be used to replace /* missing condition */ so that the code segment works as intended?

int i = -1; i < word.length(); i+=2

Write a method that will ask for user input until user inputs the String "no". Allow the user to input any capitalization of the String "no" ("no", "No", "NO", "nO") Also, have the method return the number of loops. Assume that a Scanner object input has already been created.

public int loopTillNo() { int count = 0; String nextLine = ""; while(!nextLine.toLowerCase().equals("no")) { nextLine = input.nextLine(); count++; } return count; }

Write a method that loops until the user inputs the correct secret password or until the user fails to enter the correct password 10 times. The secret password the program should look for is the String "secret" Assume that a Scanner object called input has been correctly initialized.

public void secretPassword() { int count = 0; while(true) { if(count == 10) { System.out.println("You are locked out!"); return; } String readLine = input.nextLine(); if(readLine.equals("secret")) { System.out.println("Welcome!"); return; } count++; } }

Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /* missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum);

while (num > 0)

Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total. int x = 100; int total = 0; while( /* missing code */ ) { total = total + x; x = x - 10; } Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended?

x >= 10

int j = 1; while (j <= 5) { for (int k = 4; k > 1; k--) { System.out.println("ha"); // line 6 } j++; } How many times will the print statement on line 6 execute?

15

public static int mystery(String string1, String string2) { String temp = string1; int position = 0; int result = 0; while(temp.indexOf(string2) >= 0) { position = temp.indexOf(string2); result++; temp = temp.substring(position + 1); } return result; } The following code segment appears in another method in the same class. System.out.println(mystery("Mississippi", "si")); What, if anything, is printed as a result of executing the code segment?

2

{ String result = ""; for(int i = word.length()- 1; i > index; i--) { result += word.substring(i, word.length()); } return result; } What is returned as a result of the call mystery("Lunchbox", 4)?

xoxbox


Conjuntos de estudio relacionados

Evolve Maternity and Women's Health Nursing - Pregnancy, At Risk

View Set

PrepU Ch. 26: Acute Renal Failure and Chronic Kidney Disease

View Set

Chapter 12:Miscellaneous commercial policies

View Set

03) 1.3 Compare and Contrast RAM types and features

View Set

Network Security Essentials Exam

View Set