What are parameters?
The value that a method returns.
The formal values that a method prints to the screen.
The formal names given to the data that gets passed into a method.
The type that is given to a variable.
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.
keyword.
variable.
variable type.
method.
All non-void methods must have which of the following?
a print statement.
an argument.
a parameter.
a return value.
a return value.
What is true of a void method?
It returns any value.
It takes no parameters.
It returns no value.
It can take any parameter.
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;
}
16
15.6
14.0
This method is improperly written.
15.0
14.0
What does the method call mystery("APCSA");
return for the following method?
public String mystery(String x)
{
return x * 3;
}
APCSAAPCSAAPCSA
APCSA APCSA APCSA
APCSA3
This method is improperly written.
APCSA 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;
}
694
122
572
The code will error.
122
What is returned by this method call: translator("pokemon")
?
public String translator(String word)
{
return word.substring(1) + word.substring(0,1) + "ay";
}
“pokemonpay”
“okemonpay”
“okemonay”
This method call will error.
“okemonpay”
Question: 9
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")
Dec 17, 2018
Dec 17 2018
17 Dec 2018
17 Dec, 2018
17 Dec, 2018
What is wrong with this method definition?
public int printPayAmount(int amount)
{
System.out.println(amount);
}
This method should be private
Nothing is returned from this method
This is a String type method
The method is never called
Nothing is returned from this method
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 private
I - V all
III, IV, and V
I, II, III, and IV
I, III, and IV
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 latte
is DrinkOrder
.
An attribute of latte
is DrinkOrder
.
An attribute of name
is latte
.
An instance of the DrinkOrder
class is latte
.
An attribute of DrinkOrder
is latte
.
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; } }
public class DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String name, int ounces, boolean isIced) { name = name; ounces = ounces; isIced = isIced; } }
public class DrinkOrder { public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }
DrinkOrder { private String name; private int ounces; private boolean isIced; public DrinkOrder(String theName, int theOunces, boolean hasIce) { name = theName; ounces = theOunces; isIced = hasIce; } }
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
-1
7
10
0
4
-1
10
7
3
10
3
-1
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
?
quarter.getValue();
double coinWorth = quarter.getValue();
int coinWorth = quarter.getValue();
double coinWorth = quarter;
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)
Insect(String theName, int legNumber, boolean isExoskeleton)
Insect(String theName)
Insect()
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);
Math.min(Math.pow(x, y));
Math.pow(Math.min(x, y));
Math.pow(2, Math.min(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?
This code would result in an error, as name
has yet to be initialized.
This code would result in an error, as there is no constructor present in the Dog
class.
" "
null
false
4.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
13.0
12.0
12.5
Double
values cannot be used in double
methods before being converted.
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 Math.random() * 100;
return Math.random() * 100 + 1;
return (int) (Math.random() * 100);
return (int) (Math.random() * 100 +1);
return (int) (Math.random() * 101);
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));
14
9
11.0
5
9.0
11
5
9
11.0
14
11.0
9
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 am103years old
I am 13 years old
I am13years old
I am 103 years old
years oldI am 13
3. 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 only
III only
I and II only
I and III only
I, II, & III
4.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"
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();
II only
I, III and IV
IV only
I and IV
I and III
4.I and IV
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: Oxygen
Atomic Weight: 15.999
Atomic Number: 8
Which of the following instance variables makes the most sense based on these attributes?
private int atomicWeight; private String name; private int atomicNum;
private int atomicWeight; private String name; private double atomicNum;
private double atomicWeight; private String name; private int atomicNum;
private double atomicWeight; private String name; private double atomicNum;
int atomicWeight; String name; int atomicNum;
3.private double atomicWeight; private String name; private int atomicNum;