1/28
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Animal = new Dog();
if the method catch() is in the Dog class and the Animal class, which class does it run the method from?
The Dog method
If using super() to call a parent class constructor, where do you put it in the subclass constructor?
It has to be the first line
Do you have to initialize instance variables that are primitive data types?
No, if you forget to initialize them in a constructor, because they are primitive, they will set themselves to a default value
Can you use methods to initialize instance variables to a value in a constructor? (The methods and constructor are part of the same class)
yes
Do subclasses inherit the parent class’s constructor?
no
What kind of methods are equals(), lessThan() and toString()?
Accessor method
System.out.println(this) == toString() ?
yes
What is a NullPointer Exception?
runtime exception, when an object does not point to anything (null)
When are two objects considered aliases?
when. they both reference the same object
what are algorithms?
step by step process that describes how to solve a problem in a way that always gives a correct answer
What is the running part of a class called?
public void static main(String[] args)
for ap cs a, classes and constructors are private/public and only instance variables are private/public
public, private
Can you call Math.sqrt(x) on x if it is a Double (wrapper class) instead of double (primitive)? Can it be called on an Integer or int?
No, it has to be called on a double, the wrapper class doesn’t count. It can be called on an int, because it will implicitly convert it but not the Integer wrapper class.
How do you get the primitive value of a Wrapper class object?
.intValue() or the wrapper class equivalent
How would you box the Double 3.0 to a double?
double x = Double.doubleValue();
Double x = 3.4;
double y = 2.3;
Is System.out.println(x) == System.out.println(y);
yes, it prints the same thing
If you add 1 to an Integer at its max or subtract 1 from an Integer at its min, what happens?
The int will wrap around to the min or max
Autoboxing
Automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes (ex: int → Integer)
Unboxing
Automatic conversion that the Java compiler makes between object wrapper classes and their corresponding primitive types (ex: Integer → int)
When does Java apply Autoboxing?
When primitive types are psased as parameters of their corresponding wrapper class
when primitives are assigned to a variable of the type of their corresponding wrapper class
When does Java apply unboxing?
When wrapper class types are passed as parameters of their corresponding primitive type
when wrapper class types are assigned to a variable of their corresponding primitive type
Reference types only store ________
references (ex: String, ArrayList, Integer)
Pair p1 = new Pair(2, 3)
Pair p2 = new Pair(2, 3)
p1.equals(p2) = t/f?
false
.equals() has to be overridden for the Pair class, otherwise it just checks to see if p1 and p2 reference the same thing
Overloaded constructor
set default values to something else, this constructor has parameters
What happens if you don’t write a constructor for a class?
Java will write a no-argument constructor for it
What are the standard Java default values if the instance variables are not initialized?
int = 0
double = 0.0
boolean = false
Strings and other objects = null
What if an accessor or mutator method tries to run on a String or other object that hasn’t been initialized?
might be a nullPointer exception for accessor, but def for mutator
What is the difference between methods that return primitive types vs. methods that return reference types?
primitive types: returns a copy, so the original is not changed
reference types: Java returns a copy of the object reference, not the value itself (saves space)
What are the 5 parts of a method header? Name an example.
Access level (public/private), return type (primitive/reference/void), identifier (name of method), parameter list
ex: public void main (String[] args)