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
What is an instance method?
Instance Method: method that belongs to instances of a class, not to the class itself. An instance method is a piece of code called on a specific instance of the class. It is called a receiver object.
What are methods?
Methods are procedures that allow us to control and define the behavior of an object. (Change behavior)
How do you call methods?
- To use a method = must call it
- To call a method: objectName.method()
What is procedural abstraction?
Procedural Abstraction: Even though we don’t know how nextLine(() was written, we can still use it effectively in our programs as long as we know what is does
What are the different parts of a writing method declaration?

What is method overloading?
Method Overloading: Methods can have multiple signatures. Java will use the correct signature based on the actual parameters used in a program
What are parameters and why is parameter order important?
🖊️ Parameters: A value that you can pass to a method in Java. Parameters are the names of the variables in the method signature.
When there are multiple parameters, the order that parameters are input matters:
- public void setWidthAndHeight(int rectWidth, int rectHeight)*
- {*
- width = rectWidth;*
- height = rectHeight;*
- }*
What are return types?
The return type determines the type that will be returned from a method
We can return a value from a method by using the keyword return
public void printArea() //Can only print the value of area
{
int area = width * height; System.out.print(area);
}
public int getArea() //Returns the value of area which can be used in a variety of ways
{
int area = width * height; return area;
}
Define immutability:
Unable to be changed or manipulated. String are immutable
Define concatenation:
The process of adding two Strings values together.
This creates a new String object. Primitives can be concatenated with String objects
- Strings can be concatenated with one another to create a new String value:
- String firstName = “Karel”;*
- String lastName = “ The Dog”;*
- String fullname = firstName + last Name;*
- System.out.println(fullName); // → Karel The Dog*
- We use the += shortcut on String values:
- String firstName = “Karel”;*
- firstName += “!”;*
- System.out.println(firstName); // → Karel!*
Define Implicit Conversation:
Automatic process of transforming a variable data type. This occurs when a primitive and String object are concatenated by changing the primitive value to a String object type
What is an escape sequences?
Enables users to use special characters and actions within String objects
How do you indicate that something is a reference type?
Starting a data type with a capital letter indicates that it’s a reference data type!
What does the method do?
name.concat(“value”)
Returns a new String with “value” appended to the end of the name
What does the method do?
name.replace(“a”, “p”)
Returns a new String with all instances of “a” in name replaced with “p”
What does the method do?
name.toUpperCase()
Returns name as Upper Case letters (same can be done to lower case letters)
How can primitive types can be concatenated with String objects?
- Implicit Conversion converts int age to a String, and concatenates it to the end of currentAge!
int currentAge = 20;
int age = 10;
System.out.println(“In ten years, I will be” + currentAge + age);
// → In ten years, I will be 2020
- Putting the equation within the parenthesis will evaluate it before converting it to String:
int currentAge = 20;
int age = 10;
System.out.println(“In ten years, I will be” + (currentAge + age));
// → In ten years, I will be 30
What are escape sequences?

What does String(String, str) constructor do?
Constructs a new String object that represents the same sequence of characters as str
int length ()
Returns the number of characters ins a String objects
String substring (int from, int to)
Returns the substring beginning at index from and ending at index to -1
String substring (int from):
Returns substring(from, substring())
IndexOutOfBoundsException
A String object has index values from 0 to length -1.
Attempting to access indicates outsides this range will result in this error
int indexOf(String str)
Returns the index of the first occurrence of str: returns -1 if not found
int compareTo(String other)
Returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other
Application Programming Interference:
APIs and libraries simplify complex programming tasks by providing sets of clearly defined methods of communications among various computing components
What is Interning?
Interning: When a String is created without the new operator, a single copy of the String is allocated into memory

Why are these not the same?
String s2 = new String(“Karel);
String s4 = new String(“Karel);
Not the same because they refer to a DIFFERENT location in memory.
The JVM (Java Virtual Machine) is forced to create a new string reference, even if “karel” is in the reference pool
s2 and s4 refer to two different objects in the heap, neither of which have been interned as is the case with string literals
Different objects always have different memory references
What is an index?
- Each character in a string has an index.
- *INDEX**: Position of the character in the String
- Each String starts with index 0. The final index of a String is one less than the number of the characters in the String
charAT(int index)
Method charAT(int index) to access specific characters in a String:
String str = “hello”;
System.out.println(str.charAt(1);
//→ 1 e
name.length( )
Returns the number of characters in a String object
String str = new String(“Good day!”);
// the number of characters includes the spaces and exclamation point
int length = str.lenght(); System.out.println(length); // → 9
name.substring(2, 6)
Returns the substring beginning at index 2 and ending at index 5
- 2 is inclusive
- 5 is excluding
name.indexOf(“c”)
Returns the index of the first occurrence of d; returns -1 if not found
name.equals(“karel”)
Returns true if the name is equal to Karel; returns false otherwise
- We can compare two Strings using one.equals(two).
- Method returns true if both Strings have the same characters
name.compareTo(“Karel”)
Returns a value < 0 if name is less than Karel; returns zero if name is equal to Karel; returns a value > 0 if name is grater than Karel
- Compares the value of the first character that is NOT equal in both strings
- Its returns a numerical value based on the comparison because character values are actually numerical values
- Negative value means that the original String in the comparison is lexicographically lesser than the second
- Compares the first two characters that are not equal
What is Java.lang and packages?
- String class and all associated String methods are part of the java.lang package.
-
Package is a group of related classes, along with their methods, fields, and constructors
- part of a programming library
-
Libraries: Collection of written works make by other authors
- collection of packages and classes (packages are made up of classes)
- Classes has an Application Programming Interface (API) or a set of instructions on how to use it.
- API’s allow us to use methods and classes without knowing how they work through documentation
- Documentation is the reference for how to use different methods and classes
- Oracle Documetation: Google Java
What are integer and double classes?
The classes are part of the java.lang package and Object class and have a number of useful methods
Integer(int value) and Double(double value)
Constructs a new Integer object or a new double object that represents the specified int or doubles
Autoboxing?
Automatic conversion between primitive types and their corresponding objective wrapper classes
Unboxing :
Reverse of autoboxing; automatic conversion from the wrapper class to the primitive type
What are wrapper class?
Convert primitive types to object types using wrapper classes
A Wrapper class in Java is a class that contains or “wraps” primitive data types as an object
How do Wrapper Classes Help?
- Each wrapper class contains special values (like the minimum and maximum values of the type) and methods that are useful for converting between types.

Integer.Min_VALUE and Integer.Max_VALUE
Returns the minimum and maximum possible int or Integer value
Integer.intValue()
Converts integer value into an int value
intValue() allows us to extract the primitive value from the object Integer value
Double.doubleValue()
Converts Double value into a double value
What are the advantages of autoboxing and unboxing?
Autoboxing and unboxing lets programmers write clearer code, so its easier to read
Allows us to use primitive types and wrapper class objects interchangeably without having to perform any casting explicitly
What is autoboxing? Autoboxing: Converting a primitive value into an object of the corresponding wrapper class
Autoboxing: Converting a primitive value into an object of the corresponding wrapper class
- Instead of writing:
- Integer myInt = new Integer(53);*
- Double myDouble = new Double(7.3);*
- We can write:
- Integer myInt = 53;*
- Double myDouble = 7.3;*
Java automatically converts primitive type values to integers and double objects if values are declared as Wrapper class objects
Complier will also apply autoboxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class
What is unboxing?
📌 Unboxing: Converting an object of a wrapper type to its corresponding primitive value
Write: (Shows unboxing when an int object is assigned back to a primitive int type. The Java compiler takes care of the unboxing conversation at runtime)
/* autoboxing - wrap 53 */
Integer myInt = 53;
/* unboxing the object - back to primitve type */
int myInt2 = myInt;
What are instance methods?
- Instance (non-static) methods need to be called with an object of the class as a receiver
- Instance methods are also non-static methods
Ex:
- Rectangle is an instance of the Rectangle Class*
- getArea( ) is an instance method*
- rectangle.getArea()*
What are static methods?
Static Methods: Methods in Java that can be called without creating an object of a class. Referenced by the class name itself.
- public static void rectEquation ();*
- /* When a method is declared with “static keyword” it is a known static method */*
- {*
- System.out.println(“The formula for area is: L * W”);*
- System.out.println(“The formula for perimeter is: 2(L + W)”);*
- }*
Math.abs(x)
Returns the absolute value of x
Math.pow(base, exponent)
Returns the value of baise raised to the power of exponent
Math.sqrt(x)
Return the positive square root of x
Math.random()
Returns a double value greater than or equal to 0.0 and less than 1.0
General Formula from Generating any range from (start to start + range) is:
int rangeInteger = (int)(Math.random() * (range + 1) + start);