AP Java Unit 3 Quiz

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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. Use readLine to get the user input. A: public int loopTillNo() { int count = 0; while(!readLine("Again?").equals("no")) { count++; } return count; } B: public int loopTillNo() { int count = 0; while(readLine("Again?").toLowerCase().equals("no")) { count++; } return count; } C: public int loopTillNo() { int count = 0; while(readLine("Again?").equals("no")) { count++; } return count; } D: public int loopTillNo() { int count = 0; while(!readLine("Again?").toLowerCase().equals("no")) { count++; } return count; }

D

Why do we use methods in Java? A: To make code easier to understand B: To avoid repeated code C: To simplify code D: All of the above

D

What could the method signature for a Karel command look like? A: public void move() { //some code } B: public String move() { //some code } C: public int move() { //some code } D: public boolean move() { //some code }

A

What is a method? A: A procedure that is defined by the user. B: A keyword used to loop for a fixed amount of time. C: A place where we can store data. D: The type that is given to a variable.

A

What will this method call print to the screen? numberMadness(12345) EXAMPLE: public void numberMadness(double myDouble) { int myInt = (int) myDouble; String myString = ""; while(myInt != 0) { myString = myInt % 10 + myString; myInt /= 10; } System.out.println(myString); } A: 12345 B: 54321 C: 123 D: 543

A

What will this method call print? patternGrid(3,4,'#'); EXAMPLE: 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(); } } A: #### #### #### B: ### ### ### C: #### #### #### #### D: This code will error.

A

What is returned by this method call: translator("pokemon")? EXAMPLE: public String translator(String word) { return word.substring(1) + word.charAt(0) + "ay"; } A: "pokemonpay" B: "okemonpay" C: "okemonay" D: This method call will error.

B

What is wrong with this method definition? EXAMPLE: public String outputString(String input) { System.out.println(input); } A: The method should not be public B: Improper return value C: Improper variable names D: All of the above

B

What kind of error does this method cause? buggyMethod("BUG"); EXAMPLE: public void buggyMethod(String x) { for(int i = 0; i <= x.length(); i++) { System.out.println(x.substring(i, i+1)); } } A: Compile Time Error: Unexpected return value B: Runtime Error: String index out of range C: Compile Time Error: Missing return value D: Compile Time Error: Syntax Error in method definition

B

What will the value of yourBankAccount be after the method call? depositMoney(yourBankAccount, 1000000); EXAMPLE: int yourBankAccount = 0; public void depositMoney(int bankAccount, int deposit) { bankAccount += deposit; } A: 1000000 B: 0 C: 100000 D: The code will error.

B

What will this method call output? myMethod(false,false) EXAMPLE: public int myMethod(boolean x, boolean y) { if(!x) { if(y) { return 1; } else { return 0; } } else { return -1; } } A: 1 B: 0 C: -1 D: This method call will error.

B

What will this method call output? yesOrNo(true) EXAMPLE: public String yesOrNo(boolean myBoolean) { if(myBoolean == true) { return "Yes"; } else { return "No"; } } A: "No" B: "Yes" C: 0 D: This program will error

B

What are parameters? A: The value that a method returns. B: The values that a method prints to the screen. C: The formal names given to the data that gets passed into a method. D: The type that is given to a variable.

C

What does this method call output? doubleOrNothing(9.9); EXAMPLE: public double doubleOrNothing(double myDouble) { return (double) (int) myDouble * 2; } A: 20 B: 18.8 C: 18.0 D: This method is improperly written.

C

What does void mean? A: Void means that a method returns any value. B: Void means that a method takes no parameters. C: Void means that a method returns no value. D: Void means that a method can take any parameter.

C

What will this method call output? mysteryMethod("Karel The Dog", 'e'); EXAMPLE: public int mysteryMethod(String x, char y) { int z = 1; for(int i = 0; i < x.length(); i++) { if(x.charAt(i) == y) { z++; } } return z; } A: 1 B: 2 C: 3 D: 4

C

Write a method that loops forever 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" Use readLine(String prompt) for user input A: public void secretPassword() { int count = 1; while(true) { if(count == 10) { System.out.println("You are locked out!"); return; } if(readLine("Password?").equals("secret")) { System.out.println("Welcome!"); return; } count++; } } B: public void secretPassword() { int count = 0; while(true) { if(count == 10) { System.out.println("You are locked out!"); } if(readLine("Password?").equals("secret")) { System.out.println("Welcome!"); } count++; } } C: public void secretPassword() { int count = 0; while(true) { if(count == 10) { System.out.println("You are locked out!"); return; } if(readLine("Password?").equals("secret")) { System.out.println("Welcome!"); return; } count++; } } D: public void secretPassword() { int count = 0; while(true) { if(count != 10) { System.out.println("You are locked out!"); return; } if(!readLine("Password?").equals("secret")) { System.out.println("Welcome!"); return; } count++; } }

C

What does this method call output? trickyMethod(3,1) EXAMPLE: public int trickyMethod(int x, int y) { int sum = 0; while (x <= 10) { sum += x % y; x++; y++; } return sum; } A: 9 B: 10 C: 11 D: 12

D

What does this method call return? doubleInt(5); EXAMPLE: public int doubleInt(int x) { x * 2; } A: 10 B: 5 C: This method returns nothing. D: This method is improperly written.

D

What is a Javadoc? A: The reference guide to Java that is in the back of a textbook. B: A program that cures the bugs in your Java program. C: Data that gets passed into a method D: A comment that clearly states the functionality and usage of a class or method.

D

What is a return value? A: The value that a method prints to the screen. B: The value that is inputted to a method. C: The value that a user inputs to a program. D: The value that a method outputs.

D

What is the return value of this method call? someWhereInTheMiddle(188603) EXAMPLE: public static String someWhereInTheMiddle(int number) { String stringNum = "" + number; int middle = stringNum.length()/2; if(stringNum.length() % 2 == 1) { return stringNum.substring(middle,middle+1); } else { return stringNum.substring(middle-1,middle+1); } } A: 8 B: 6 C: 7 D: 86

D

What will this method call output? someMethod(9990) EXAMPLE: public boolean someMethod(int number) { if(number % 2 == 0) { return true; } else { return false; } } A: false B: 0 C: "true" D: true

D

What will this method call print to the screen? someMethod(9,"Mar", 1250) EXAMPLE: public static void someMethod(int a, String b, int c) { System.out.println(b + " " + a + ", " + c); } A: 9 Mar 1250 B: Mar 9 1250 C: 9, Mar 1250 D: Mar 9, 1250

D

What would this method call output? myMethod(830) EXAMPLE: public int myMethod(int num) { while(num / 10 >= 10) { num /= 10; } return num; } A: 830 B: 30 C: 0 D: 8 E: 83

E


Ensembles d'études connexes

19.6: The Age of Napoleon (1799-1815)

View Set

Wiley Plus Chapter 24,26,28 Questions

View Set

ORGANISATIONS INTERNATIONALES ET INSTITUTIONS INTERNATIONALES

View Set

Nursing as a Career Final study guide

View Set

ALL SETS: NR 304 + NR 302 Practice questions

View Set

BIBL 104 Quiz 2 Liberty University

View Set