1/22
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
what is a field variable and class variable?
field variable and class variables are the SAME THING, and they are both static and belong to the class itself
public class Cat() {
private static int age;
private String name;
public Cat(String name) {
this.name = name;
age++
}
}If you had this code, what would happen to the class variable “age” each time you create a new object of the class?
The age would keep incrementing because it belongs to the class and not any object.
What are instance variables/methods
Variables and methods that belong to a class but are not static, because it is “instance” they are associated with objects
What are local variable? What is significant about local variables?
Local variables are variables that belong to a method. Once you exit the method, or exit the “{}“, you aren’t able to access these variables anymore so you CANNOT assign access modifiers like public/private to these variables
True/False: methods can access constructor parameters
False, because parameters or local variables only exist within curly braces (scope)
How to directly access a method without creating an object?
Method has to be static. Also classes can never be static
What are static methods not allowed to access in the same class? Also then how would you return it?
They are not allowed to access non-static variables that are in the same class. If you wanted to return the non-static variable, you would have to first create an object of the class and use “.“ to access the variable. For example:
public class Student
{
private int grade;
public Student(int g)
{
grade = g;
}
public static void printGrade(Student s)
{
System.out.println(s.grade);
}
}You can see that for the parameter of printGrade “s“ is type Student, and we are using OBJECT.INSTANCE_VAR
True/False: normal methods (instance methods) can access any variable
True, only static methods are not allowed to access instance variables (first create an object and use object.instance_name)
public class BoolTest {
private int one;
public BoolTest(int newOne){
one = newOne;
}
public int getOne(){
return one;
}
public boolean isGreater(BoolTest other){
/* missing code */
}What can the instance “other“ access?
It is allowed to access the private variable “one” because it is an object of a method that is in the same class as the class that contains the private variable
How to access an element of an array?
ArrayName[element]
what is the right way to find length of String, ArrayList, and Array?
String: .length()
ArrayList: .size()
Array: .length
When you print an arrayList what does it return?
The real arrayList, and not the address because “System.out.println“ automatically has “toString()“
how to initialize an array
dataType[] name = new dataType(amount);
If you have this line:
newList.add(oldList.remove(1))
what happens to both lists?
the value 1 from oldList is removed, while the value is also added to newList.
What does .get(index) return
the index
what does .set(index, element) return
old element that was replaced
what does .add(element) return
boolean true (usually)
what does .add(index, element) return
nothing (void)
what is the difference between void and null
void is a return type, while null is an actual value (like int, double)
what is autoboxing
when you convert primitive to Object type
Integer age = 5;
how are print statements executed in a recursion?
they are “on hold“ and start printing from the latest statement to the first ever statement because it is in a stack
if the print statement was before the recursion call, what would happen?
cause infinite statements to be printed
if the print statement was after the recursion call, what would happen?
no statements are printed