1/32
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
What are the primitive data types?
boolean, char, byte, int, short, long, float, double
What are the two meanings of the '+' operator discussed in the chapter and how does Java determine which meaning to use?
Arithmetic addition or string concatenation. If two numbers are being added, '+' refers to addition. If one of the values is a string, '+' refers to string concatenation.
Why are escape characters needed?
To allow the use of the "" in the middle of a string, if necessary
Which of the following are illegal identifiers in Java?
a. toDec
b. two+Four
c. forgetIt
d. letItBeMe
e. 3littlePigs
f. l_o_n_g
g. whatFun
h. whyMe?
b, e, h
You have a variable generator that is of type Random. What expression using a generator would generate a random integer with the inclusive range -100 to 100?
generator.nextInt(201) - 100
Since you cannot take the square root of a negative number, you might use which of the following instructions to find the square root of the variable x, where x might be negative?
a. Math.sqrt(x*x),
b. Math.sqrt((int) x),
c. Math.sqrt(Math.abs(x)),
d. Math.abs(Math.sqrt(x)),
e. Math.sqrt(-x)
c.
Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character?
a. (s.charAt(0) s.charAt(s.length( ))),
b. (s.charAt(1) s.charAt(s.length( ))),
c. (s.charAt(0) s.charAt(s.length( ) - 1)),
d. (s.charAt(0) s.charAt(s.length( ) + 1)),
e. (s.charAt(0) == s.charAt(last))
c
A cast is required in which of the following situations?
a. using charAt to take an element of a String and store it in a char,
b. storing an int in a double,
c. storing a double in a double,
d. storing a double in an int, e. all of the above require casts
d
Which of the following is not a method on the String class?
a. equals(),
b. toUpperCase(),
c. truncate(),
d. substring(),
e. length()
c
What role and purpose does the Constructor fulfill for a class? What happens if you do not include a Constructor?
Initializes the data of the class to a known state. All variables are initialized to 0 or null.
Why should every class, that is not a driver class, include a toString() method?
Helps with debugging and ensuring the class is human-readable when printed.
Why is encapsulation important in Java? What methods does Java use to attempt to enforce proper encapsulation?
Hides the data to keep it safe and ensure that mutations are always done safely.
Explain the difference between instance data and local data? Why are different scopes useful in developing programs?
Instance data belongs to an instance of a class, local data belongs to an individual method. Different scopes are necessary to allow the data to be used either for a single method or for the life of an instance.
Explain the difference between an IF statement and an IF-ELSE statement. Why might you want to use an IF-ELSE?
An IF statement only executes the block if the condition is true. An IF-ELSE has two blocks: one which executes if the condition is true and one if the condition is false.
If a WHILE and a FOR loop can perform the same task, why are both types of loops useful?
A FOR loop reduces some of the boilerplate for doing a common loop and makes it easier to run a loop a specified number of times.
What is the difference between a WHILE and a DO-WHILE loop?
A DO-WHILE loop will always execute the loop at least once. A WHILE loop may never execute.
What makes the conditional operator, or ternary operator, unique? Does this uniqueness make reading statements that use the operator harder or easier to understand?"
The ternary is unique because it has three operands. If used well, it may be easier to understand. Otherwise, it can increase complexity and be harder to understand."
What role do interfaces play in object-oriented design?
Interfaces are a contract between an implementor and a consumer. This allows a class to implement multiple interfaces and be used to satisfy the contracts to desired consumers.
Why is the "this" reference useful?
Code can use ""this"" to reference instance variables that may be shadowed in the current method.
What does it mean when we say a method is "overloaded"?
There are multiple methods that have the same name. Java will determine which method to call based on the parameters provided at the call site.
What different class relationships does the chapter discuss?
dependency, aggregation, and inheritance
If I have an array called data, what does the notation data[i] signify if i is an integer? What is the valid range for i? If I declare an array with the expression data = new int[20], what would data.length return?
It references the value at position i in the array. The valid range is 0 to data.length - 1. 20.
How is an array of primitive datatypes different than an array of objects?
The array of primitive datatypes holds the values directly, while the array of objects holds a sequence of references to each object.
How would you declare a 2D array of integers? How is working with a 2D array differ from working with a 1D array?
int[][] data = new int[10][10]. Working with a 2D array is like working with an array of arrays, so the first [] navigates one array to access the array at that location, while the second [] accesses the data at that index of the inner array.
Does Java natively support arrays with more than two dimensions? If so, describe how you would declare and use the array. If not, describe a workaround that could be used to accomplish the task
Yes, extra [] are used to define and access each dimension of the array
What keyword do we use to indicate that a class is a subclass of another class?
extends
How do the access modifiers private and protected differ? Why is protected needed when we are using inheritance?
private means only the current class can access the data or method. protected means this class and any subclass can access the data or method.
How many classes can a class be a direct subclass of? What is the name for this limitation. Are there any workarounds to these limitations?
One. Single Inheritance. Interfaces
Can a subclass have methods with the same signature as methods in the superclass? What about attributes? What terms do we use to encompass these techniques? Are there any downsides to this?
Yes, this is an example of overloading. Yes, this is an example of shadowing. Overriding is a useful way to expand the capabilities of a base class, but shadowing leads to confusion and should not be used.
What is polymorphism? Why is the concept of late binding important to making it work?
Polymorphism is when the instance a variable holds could potentially be one of multiple different classes. Late binding is important because the actual method called is not determined until runtime based on which class the instance actually is.
How can you use inheritance to implement polymorphism?
Multiple subclasses can inherit from the same base class, so a variable of the type of the base class can potentially be any of the subclasses.
How can you use interfaces to implement polymorphism?
Multiple classes can implement an interface, so a variable of the type of the interface can hold an instance of any class that implements the interface.
Which method of achieving polymorphism did the sorting and search examples use? What is the advantage in writing those classes in that manner?
Interface polymorphism. This is useful because the sorting methods do not have to know how to compare two instances of a class, only that they can be compared and what the relative ordering is between them.