1/24
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What are parameters?
The formal names given to the data that gets passed into a method.
A procedure that is defined by the user is called a
method
The value that a non-void method outputs is called
return value
What is true of a void method?
It returns no value.
What does this method call doubleNumber(7.8); return, given the following method?
public double doubleNumber(double myNumber)
{
return (double) (int) myNumber * 2;
}
14.0
What does the method call tripleString("APCSA"); return for the following method?
public String tripleString(String x)
{
return x * 3;
}
This method is improperly written.
What will the value of myBankAccount be after the method call depositMoney(myBankAccount, 572);?
int myBankAccount = 122; public void depositMoney(int bankAccount, int deposit)
{
bankAccount += deposit;
}
122
What is returned by this method call: translator("pokemon")?
public String translator(String word)
{
return word.substring(1) + word.substring(0,1) + "ay";
}
"okemonpay"
What will this method call print to the screen?
public static void formatText(int a, int b, String c)
{
System.out.println(b + " " + c + ", " + a);
}
formatText(2018, 17, "Dec")
17 Dec, 2018
What is wrong with this method definition?
public int printPayAmount(int amount)
{
System.out.println(amount);
}
Nothing is returned from this method
Why do we use methods in Java?
I. To make code easier to understandII. To define global variablesIII. To avoid repeated codeIV. To simplify codeV. To avoid needing to define them as public or private
I, III, and IV
A coffee shop has created a DrinkOrder class. The class contains variables to represent the following:
A String variable called name to represent the name of the drink.
An int variable called ounces to indicate how many ounces the drink should be.
A boolean variable called isIced to indicate whether the drink is iced.
An object latte has been declared as type DrinkOrder after someone has ordered a Latte.
Which of the following descriptions is accurate?
An instance of the DrinkOrder class is latte.
A coffee shop has created a DrinkOrder class. The class contains variables to represent the following:
A String variable called name to represent the name of the drink.
An int variable called ounces to indicate how many ounces the drink should be.
A boolean variable called isIced to indicate whether the drink is iced.
An object Latte has been declared as type DrinkOrder after someone has ordered a Latte.
Based on the information provided, which would be the most accurate class declaration for the DrinkOrder class?
public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }
After the execution of the following lines of code, what will be output onto the console?
String letters = "ABCde"; String name = "Karel the Dog"; String letter = "D";
System.out.println(name.indexOf(letter)); System.out.println(letters.indexOf(name.substring(3,4))); System.out.println(letters.indexOf(letter));
10
4
-1
Consider the following class:
public class Coin
{ private String name; private double value; public Coin(String theName, double theValue) { name = theName; value = theValue; } public double getValue() { return value; } }
Assume that a Coin object quarter has been properly declared and initialized. Which of the following code snippets will successfully assign the value of quarter to a new variable coinWorth?
double coinWorth = quarter.getValue();
Consider the following class:
public class Insect { private String name; private int numLegs; private boolean hasWings; private boolean hasExoskeleton; public Insect(String theName, int legNumber, boolean isWinged, boolean isExoskeleton) { name = theName; numLegs = legNumber; hasWings = isWinged; hasExoskeleton = isExoskeleton; } public Insect(String theName, int legNumber, boolean isWinged) { name = theName; numLegs = legNumber; hasWings = isWinged; hasExoskeleton = true; } }
Which of the following is NOT a possible header for a new constructor for the Insect class?
Insect(String theName, int legNumber, boolean isExoskeleton)
Which of the following code segments would successfully square the minimum value between two int variables x, y?
Math.pow(Math.min(x, y), 2);
Consider the following class:
public class Dog { private String name; private String breed; public String getName() { return name; } }
An object Karel is created using the Dog class. What would the result of the command System.out.println(Karel.getName()); be?
null
Consider the following method:
public double doubleVal(double x) { return x *2; }
The following code segment calls the method doubleVal:
Double val = 6.5; System.out.println(doubleVal(val));
What is printed when the code segment is executed?
13.0
Consider the following class:
public class RaffleTicket { private String ownerName; private int ticketNum; public RaffleTicket(String name) { ownerName = name; ticketNum = getTicketNum(); } private int getTicketNum() { / Code to be implemented / }
Which of the following can be used to replace /Code to be implemented / so that the code segment produces a random number between 1-100?
return (int) (Math.random() * 100 +1);
Consider the following class:
public class RandomCalculator { private int x; private int y; public RandomCalculator(int one, int two) { x = one; y = two; } public int add(int num) { return x + y + num; } public int add(double num) { return (int)(num) + x / y; } public int add(double num, int num2) { return (int)(num2 + num + x - y); } public double add(int num, double num2) { return num + num2 - x + y; }
What would the output be of the following code segment:
5
9
11.0
Consider the following code segment:
String str = "I am"; str += 10 + 3; String age = "years old"; System.out.println(str + age);
What is printed as a result of executing the code segment?
I am13years old
Which of the following code segments would correctly assign word from the String sentence = "Grab the last word" to the variable lastWord?
I. String lastWord = sentence.substring(sentence.length()-4, sentence.length()); II. String lastWord = sentence.substring(sentence.indexOf("w"), sentence.indexOf("d")); III. String lastWord = sentence.substring(14, 16) + sentence.substring(1, 2) + sentence.substring(sentence.length()-1, sentence.length());
I and III only
Consider the following class:
public class Greetings { private String greetings; public Greetings(String greeting) { greetings = greeting; System.out.println(greetings); } public void hello() { System.out.println("Hello"); } public void translate() { greetings = "Hola"; } public void changeGreeting(String greeting) { greetings = greeting; } public void greeting() { System.out.println(greetings); } }
Which of the following code segments will produce this output in the console:
"Hello" "Hola"
Greetings hi = new Greetings("Hello"); hi.translate(); hi.greeting();
Greetings hola = new Greetings("Hello"); hola.changeGreeting("Hola"); hola.greeting();
A science teacher wants to create an Elements class for the Periodic Table. They want to include several attributes to the Elements class: the atomic weight, the element name, and the atomic number for the element.
For example, if you wanted to create an entry for Oxygen you would use the following data:Name: OxygenAtomic Weight: 15.999Atomic Number: 8
Which of the following instance variables makes the most sense based on these attributes?
private double atomicWeight;
private String name;
private int atomicNum;