What’s the result of "hello".substring(1, 4)?
"ell"
What is an instance variable?
A variable that belongs to an object.
private int age;
What is the index of the first element in a Java array?
0
What’s the difference between a void and a non-void method?
A void method performs an action but doesn’t return a value; non-void methods return something.
What keyword is used to create a new object from a class?
new
Why can’t you modify a String after creating it?
Strings are immutable. Methods like .concat() make new Strings.
What’s a class variable (static variable)?
A variable shared by all objects of a class.
static int count;
What happens if you try to access arr[arr.length]?
ArrayIndexOutOfBoundsException
What happens if two methods have the same name but different parameters?
They are overloaded — Java chooses the one matching the argument list.
What is the default value of an uninitialized boolean instance variable?
false
How do you compare two Strings for equality?
Use .equals() instead of ==.
Why make instance variables private?
To protect them from direct modification (data integrity).
How can you check if two arrays have the same elements in the same order?
Arrays.equals(a, b)
Can't use a==b
or loop through to see if each element is the same
What does the return statement do?
Sends a value back to the caller and ends the method.
What’s the purpose of the this keyword?
(ex. this.name = name)
The current object (the instance that called the method)?
What’s the output of "banana".indexOf("na", 3)?
4 (search starts at index 3).
What’s the difference between a class and an object?
A class is a blueprint or template; an object is an instance created from that blueprint.
What’s wrong with int[] a = new int[-3];?
Array length can’t be negative, causes NegativeArraySizeException.
What’s the difference between a parameter and an argument?
Parameters are variables in the method definition; arguments are actual values passed in when calling it.
When calling a constructor that has parameters, what must you provide?
The correct number and types of arguments.
What is the result of:
university.substring(2, 7).indexOf('s')
3
What happens if you don’t write a constructor in a class?
Java provides a default constructor that takes no parameters and sets all variables to default values (0, false, or null).
How do you loop through an array with an enhanced for loop?
for (int element : values){}
Why can two methods with the same name in different classes behave differently?
Because of method overriding — subclasses redefine inherited methods with new behavior.
Why can’t instance variables be accessed directly from a static method?
Static methods belong to the class, not an instance, instance variables only exist in specific objects.