What is a boolean?
A primitive data type that can either be true or false.
What does a constructor do?
It creates an object and initializes all the instance variables.
What does the following return:
"hello".length();
5. This is because length() returns the number of characters a String has.
What are 3 different ways to increment this by one:
int x = 7;
x = x + 1;
x++;++x;
x += 1;
int x = 4;
String str = "hi"
What is the type of x + str?
What types of constructors are these?
public class Dragon {
//Data members
public Dragon () {
//...
}
public Dragon (Dragon other) {
//...
}
}
Default and copy constructors
"watermelon".substring(4, 8);
What is overloading?
A feature that allows a class to have more than one method having the same name, if their argument lists are different.
int x = 5;
int y = 2;
int z = x/y;
2
What is the purpose of the equals method?
To compare Objects based on their instance/field variables, rather than by their place in memory.
What does this return:
"Hello".equals("hello")
false
What is the difference between a primitive and an Object?
Objects have field variables, their own methods, have to be "constructed", and can contain multiple pieces of information. Often, they represent something in real life. These can either be written by you or are given by Java.
What does this print out?
int x = 5;
double y = 2.0;
System.out.println(x/y);
2.5
What does the keyword "this" refer to?
The keyword "this" refers to the current object that you are writing code for. For example, you can refer to the current object's instance variables as: this.fieldName. This is used often in the constructor and the object's methods.
What does this return:
"strawberry".indexOf("r");
"start".indexOf("c");
"household".indexOf("hold");
2
-1
5
What does this do:
int s = 0;
int total = 0;
while (s < 25) {
if (s % 5 == 0) {
total = total + s;
}
s++;
}
It adds up all the multiples of 5 starting at 0 and going up to (but not including) 25.
What is a Wrapper class?
ex. Integer for int
It is a class whose object contains/wraps primitive data types. It has methods you can use for primitive data types. You can think of it like an Object version of a primitive data type.
Say we wrote a class Laptop without an equals method. What does the following print out?
Laptop x = new Laptop();
Laptop y = new Laptop(x);
Laptop z = x;
System.out.println(x.equals(y));
System.out.println(x.equals(z));
true
What do these return (positive/negative/zero):
"apricot".compareTo("banana");
"tree".compareTo("TREE".toLowerCase());
"cat".compareTo("apricot".substring(4));
negative
zero
negative
What is wrong with this code:
int i = 4;
int j = 5;
while (i % j <= 5) {
if (j % 5 == 2) {
j += 6;
}
j++;
}
The loop will infinitely run since i is never incremented, so i % j will always be less than or equal to 5.