AP Computer Science CodeHS Unit 2 Quiz

What are parameters?
A) The value that a method returns.
B) The formal 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.
Click the card to flip 👆
1 / 25
Terms in this set (25)
Why do we use methods in Java? I. To make code easier to understand II. To define global variables III. To avoid repeated code IV. To simplify code V. To avoid needing to define them as public or privateI, III, and IVA 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 -1Consider 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?NullConsider 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.0Consider 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: RandomCalculator calc = new RandomCalculator(4, 5); System.out.println(calc.add(5.0)); System.out.println(calc.add(5.0, 5)); System.out.println(calc.add(5, 5.0));5 9 11.0Consider 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 oldWhich 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 onlyConsider 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" I. Greetings hi = new Greetings("Hello"); hi.translate(); hi.greeting(); II. Greetings.hello(); Greetings.translate(); Greetings.greeting(); III. Greetings hello = new Greetings(); hello.hello(); hello.translate(); hello.greeting(); IV Greetings hola = new Greetings("Hello"); hola.changeGreeting("Hola"); hola.greeting();I and IVA 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: Oxygen Atomic Weight: 15.999 Atomic 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;