CodeHS Methods Questions
What will this method call output? public String yesOrNo(boolean myBoolean) { if(myBoolean == true) { return "Yes"; } else { return "No"; } } yesOrNo(true)
"Yes"
What will this method call 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(); } } patternGrid(3,4,'#');
#### #### ####
What is returned by this method call: translator("pokemon")? public String translator(String word) { return word.substring(1) + word.charAt(0) + "ay"; }
"okemonpay"
What will the value of yourBankAccount be after the method call? int yourBankAccount = 0; public void depositMoney(int bankAccount, int deposit) { bankAccount += deposit; } depositMoney(yourBankAccount, <a href="tel:1000000">1000000</a>);
0
What will this method call output? public int myMethod(boolean x, boolean y) { if(!x) { if(y) { return 1; } else { return 0; } } else { return -1; } } myMethod(false,false)
0
What does this method call output? public int trickyMethod(int x, int y) { int sum = 0; while (x <= 10) { sum += x % y; x++; y++; } return sum; } trickyMethod(3,1)
12
What will this method call print to the screen? public void numberMadness(double myDouble) { int myInt = (int) myDouble; String myString = ""; while(myInt != 0) { myString = myInt % 10 + myString; myInt /= 10; } System.out.println(myString); } numberMadness(12345)
12345
What does this method call output? public double doubleOrNothing(double myDouble) { return (double) (int) myDouble * 2; } doubleOrNothing(9.9);
18.0
What will this method call output? 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; } mysteryMethod("Karel The Dog", 'e');
3
What would this method call output? public int myMethod(int num) { while(num / 10 >= 10) { num /= 10; } return num; } myMethod(830)
83
What is the return value of this method call? 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); } } someWhereInTheMiddle(188603)
86
What is a Javadoc?
A comment that clearly states the functionality and usage of a class or method.
What is a method?
A procedure that is defined by the user.
Why do we use methods in Java programming?
Break down our program into smaller parts/to simplify code Avoid repeating code Make our program more readable/easier to understand
What is wrong with this method definition? public String outputString(String input) { System.out.println(input); }
Improper return value
What will this method call print to the screen? public static void someMethod(int a, String b, int c) { System.out.println(b + " " + a + ", " + c); } someMethod(9,"Mar", 1250)
Mar 9, 1250
What kind of error does this method cause? public void buggyMethod(String x) { for(int i = 0; i <= x.length(); i++) { System.out.println(x.substring(i, i+1)); } } buggyMethod("BUG");
Runtime Error: String index out of range
What are parameters?
The formal names given to the data that gets passed into a method.
What is a return value?
The value that a method outputs.
What does this method call return? public int doubleInt(int x) { x * 2; } doubleInt(5);
This method is improperly written.
What does void mean?
Void means that a method returns no value.
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. public int loopTillNo() { int count = 0; while(!readLine("Again?").equals("no")) { count++; } return count; } public int loopTillNo() { int count = 0; while(readLine("Again?").toLowerCase().equals("no")) { count++; } return count; } public int loopTillNo() { int count = 0; while(readLine("Again?").equals("no")) { count++; } return count; } public int loopTillNo() { int count = 0; while(!readLine("Again?").toLowerCase().equals("no")) { count++; } return count; }
public int loopTillNo() { int count = 0; while(!readLine("Again?").toLowerCase().equals("no")) { count++; } return count; }
What could the method signature for a Karel command look like? public void move() { //some code } public String move() { //some code } public int move() { //some code } public boolean move() { //some code }
public void move() { //some code }
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 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++; } } 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++; } } 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++; } } 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++; } }
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++; } }
What will this method call output? public boolean someMethod(int number) { if(number % 2 == 0) { return true; } else { return false; } } someMethod(9990)
true