1/30
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Why do we use methods in Java programming?
All of the above
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 is a Javadoc
A comment that clearly states the functionality and usage of a class or a method
Does the following code snippet have a run time error, compile time error, both, or neither?
int x = 3
int y = 4;
int sum = x + y;Run time error
Correct Answer
Compile time error
both
neither
Complete time error
Which exception will be thrown as a result of running this code snippet?
String hello = "hello";
char last = hello.charAt(10);IndexOutOfBoundsException
What are parameters
The formal names given to the data that gets passed into a method
What is a method?
A procedure that is defined by the user
What is a Javadoc
A comment that clearly states the functionality and usage of a class or method
What is a return value?
The value that a method outputs
What does void mean?
Void means that a method returns no value
What does this method call output?
public double doubleOrNothing(double myDouble)
{
return (double) (int) myDouble * 2;
}
doubleOrNothing(9.9);
18.0
What does this method call return?
public int doubleInt(int x)
{
x * 2;
}
doubleInt(5);
The method is improperly written
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, 1000000);
0
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 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();
}
}
What would this method call output?
public int myMethod(int num)
{
while(num / 10 >= 10)
{
num /= 10;
}
return num;
}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 returned by this method call: translator("pokemon")?
public String translator(String word)
{
return word.substring(1) + word.charAt(0) + "ay";
}“okemonpay”
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 could the method signature for a Karel command look like?
public void move()
{
//some code
}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);
}Mar 9, 1250
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 String yesOrNo(boolean myBoolean)
{
if(myBoolean == true)
{
return "Yes";
}
else
{
return "No";
}
}“Yes”
What will this method call output?
public boolean someMethod(int number)
{
if(number % 2 == 0)
{
return true;
}
else
{
return false;
}
}true
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;
}3
What is wrong with this method definition?
public String outputString(String input)
{
System.out.println(input);
}Improper return value
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));
}
}
Runtime Error: String index out of range
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);
}12345
Why do we use methods in Java?
To make code easier to understand
To avoid repeated code
To simplify code
All of the above
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;
}12