Module 02: Using Objects Flashcards
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
The value that a non-void method outputs is called
- 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;*
- }*
14.0
What does the method call tripleString(“APCSA”); return for the following method?
- public String tripleString(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;*
- }*
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”
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
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?
- quarter.getValue();
- double coinWorth = quarter.getValue();
- int coinWorth = quarter.getValue();
- double coinWorth = quarter;
- double coinWorth = quarter.getValue();
Consider the following class:
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
- 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
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:
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.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
- 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
I and III only
Consider the following class:
What is the difference between a primitive type and a reference type?
What are objects?
All reference types hold references to objects and provide a means of accessing those objects in memory:
- Sting name = “My name is Karel.”;*
- // name = object*
- Object variable of data type that is used defined
- The object has both state and behavior
- The state is data about the object
- The behavior of an object is the action that can be performed on that object
What are classes?
Objects are created from classes
A class is a template for creating an object
What are instance variables?
Used to store the state or data of the object instances
What is a constructor/signature?
Allows for the creation of a new object. Consists of the constructor name and parameter list
what is the “new” keyword?
Keyword for instantiating a class object
What is instantiate?
Create an instance of a class object
What are formal and actual parameters?
- Formal parameters are the parameters outlined in the parameter list in the constructor
- Actual parameters are the parameters that are input when a new instance of a clas object
What is a constructor?
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.
What is overloading?
When a class has more than one constructor with the same name, but different parameter lists
Overloading: (Java looks for constructor call with the appropriate parameter) Has the same name and different parameters
Purpose: It allows the user to set the values of different combinations of the instance variables when the object is created.
What is the reference variable?
A reference variable holds the address of an object, not the name of the object.
What are objects in memory?
What are scanner constructors and classes?
The scanner class is an example of a preexisting class that we have utilized for our own benefit When we use a class that someone has created, we are a client of that class.
Documentation for the Scanner constructor