Codehs AP Computer Science A (Nitro) Iteration quiz

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

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

ANSWER: A #### #### ####

QUESTION 4: 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); }

ANSWER: A 314159

QUESTION 14: Consider the following method: 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")?

ANSWER: B 9

QUESTION 17: Consider the following method: 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")?

ANSWER: B Hi0o0tamus

QUESTION 3: 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)); } }

ANSWER: B Runtime Error: String index out of range

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

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

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

ANSWER: C 10204080

QUESTION 2: 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; }

ANSWER: C 3

QUESTION 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; }

ANSWER: D 10

QUESTION 11: Consider the following code segment: int p = 5; while(p > 0) { for(int j = p; j < p*2; j++) { System.out.print(" * "); } System.out.println(); p--; } What will the final result be when run in the console?

ANSWER: A For some reason the answer won't show It's: 5 stars 4 stars 3 stars 2 stars 1 star

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

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

QUESTION 1: 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.

ANSWER: C 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++; } }

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

ANSWER: E 3

QUESTION 18: 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?

ANSWER: C counter < num.length()

QUESTION 9: Consider the following method: public static String mystery(String word, int index) { 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)?

ANSWER: C xoxbox

QUESTION 15: 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 ++; }

ANSWER: D 15

QUESTION 13: Consider the following code segment: 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; }

ANSWER: D II and III

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

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

QUESTION 6: 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.

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

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

ANSWER: E 34


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

Week 11- Eukaryotic Gene Expression

View Set

Campaign 1: Unit 1 - Echo (English to Arabic)

View Set

HHSC 105 Chapter 10 Muscle Tissue and Organization: What did you learn and Content Review.

View Set

Science 7 - Comets, Asteroids, and Meteors

View Set

Multiplying and Dividing Integers

View Set