These are used to avoid name clashes, provide easier access control and make it easier to find relevant classes
What are packages?
This is the output of the following code
public class Main {
public static void main(String args[]) {
StringBuilder s = new StringBuilder(10 + 2 + "ABC" + 4 + 5);
s.append(s.delete(3, 6));
System.out.println(s);
}
}
What is 12A512A5?
The + operator adds two numbers but concatenates the last two numbers.
When the + operator encounters a String object, it treats all the remaining operands as String objects.
s.delete(3, 6) modifies the existing value of the StringBuilder to 12A5.
It then appends the same value to itself when calling s.append(), resulting in the value 12A512A5.
____ is used to access parent class member variables and methods
What is super?
These method calls will return 2 in the following code
public int howMany(boolean b, boolean... b2) {
return b2.length;
}
A. howMany();
B. howMany(true);
C. howMany(true, true);
D. howMany(true, true, true);
E. howMany(true, {true});
F. howMany(true, {true, true});
G. howMany(true, new boolean[2]);
What is D. howMany(true, true, true);
and G. howMany(true, new boolean[2]);?
D passes the initial parameter plus two more to turn into a vararg array of size 2. G passes the initial parameter plus an array of size 2. A does not compile because it does not pass the initial parameter. E and F do not compile because they do not declare an array properly. It should be new boolean[] {true}. Option B creates a vararg array of size 0 and option C creates a vararg array of size 1.
This exception is thrown when running the following code
Object obj = new Integer(3);
String str = (String) obj;
System.out.println(str);
What is ClassCastException?
This can be defined as the special type of method that is used to initialize the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object.
What is a constructor?
This is the output of the following code
public class Main{
private int i = 1;
public static void main(String argv[]){
int i = 2;
Main s = new Main ();
s.someMethod();
}
public static void someMethod(){
System.out.println(i);
}
}
What is a compile time error?
You cannot access an instance variable from a static method.
_____ is used to call a constructor from another constructor in the same class.
What is this()?
These method calls will return 2 in the following code
public int howMany(boolean b, boolean... b2) {
return b2.length;
}
A. howMany();
B.howMany(true);
C. howMany(true, true);
D. howMany(true, true, true);
E. howMany(true, {true});
F. howMany(true, {true, true});
G. howMany(true, new boolean[2]);
What are A. final static void method4() { }
and D. static final void method3() { }?
A and D are correct because the optional specifiers are allowed in any order. Options B and C are incorrect because they each have two return types. Options E and F are incorrect because the return type is before the optional specifier and access modifier, respectively.
This happens if you add the statement System.out.println(1 / 0); to a working main() method
What is an ArithmeticException?
This is a type of method that cannot be overridden
What is Static?
Given the following code, this expression will return "Equal"
String s1="Java";
String s2="java";
if(expression) {
System.out.println("Equal");
} else {
System.out.println("Not equal");
}
What is s1.equalsIgnoreCase(s2)?
_____ is a keyword used to apply restrictions on class methods and variables. If a class is marked ____ it cannot be inherited by any other class
What is final?
These lambda expressions can fill in the blank
public static void main(){
List<String> list = new ArrayList<>();
list.removeIf(___________________);
}
A. s -> s.isEmpty()
B. s -> {s.isEmpty()}
C. s -> {s.isEmpty();}
D.s -> {return s.isEmpty();}
E.String s -> s.isEmpty()
F. (String s) -> s.isEmpty()
What are A. s -> s.isEmpty()
D. s -> {return s.isEmpty();}
and F. (String s) -> s.isEmpty() ?
removeIf() expects a Predicate interface. A Predicate interface takes a parameter list of one parameter using the specified type. B and C are incorrect since they do not use the return keyword. The return is required inside braces for lambda bodies. E is missing the parentheses around the parameter list. The parentheses are optional for a single parameter with an inferred type.
This needs to be inserted into the following code for it to compile
public void aMethod() throws Exception {
______ _____ Exception();
}
What is throw new?
Serial, parallel, CSM, and G1 are examples of this.
What are garbage collectors?
This is the output of the following code
public class Main {
public static void main(String[] args) {
int i = 10;
if((i>10 ? i++: --i)<10) {
System.out.print("Foo");
} if(i<10) System.out.print("Bar");
}
}
What is FooBar?
Only one of the right-hand ternary expressions will be evaluated at runtime. Since i is not less than 10, the second expression, --i, will be evaluated, and since the pre-increment operator was used, the value returned will be 9, which is less than 10. The first if-then statement will be visited and Foo will be output. Since i is still less than 10, the second if-then statement will also be reached and Bar will be output.
____ is used to compare contents
What is equals()?
Of the following this can replace line 2 to make this code compile
1: import java.util.*;
2: // INSERT CODE HERE
3: public class Main {
4: public void method(List<String> list) {
5: sort(list);
6: }
7: }
A. import static java.util.Collections;
B. import static java.util.Collections.*;
C. import static java.util.Collections.sort(List<String>);
D. static import java.util.Collections;
E. static import java.util.Collections.*;
F. static import java.util.Collections.sort(List<String>);
What is B. import static java.util.Collections.*;?
A is incorrect since you cannot do a static import on non-static members. C is wrong since method parameters are not needed. D, E, and F reverses the syntax of import static.
These are required to use a finally block
What are try and catch blocks?
These belong to the class itself not to objects of the class and different objects can not have different values for them.
What are static variables?
This is the output of the following code:
public class Main{
public static void main(String[] argv){
int[] array = {6,9,8};
List<Integer> list = new ArrayList<>();
list.add(array[0]);
list.add(array[2]);
list.set(1, array[1]);
list.remove(0);
System.out.println(list);
}
}
What is [9]?
After adding the two elements, list contains [6, 8]. The code then replaces the element at index 1 with 9, resulting in [6, 9]. Finally, we remove the element at index 0, leaving [9].
____ ____ is used to save space in memory and is stored in Java's heap memory
What is string pool?
This can fill in the blank to make the code compile
public class Ant {
_____ void method() { }
}
A. default
B. final
C. private
D. Public
E. String
F. zzz:
What are B. final and C. private?
void is a return type. Only the access modifier or optional specifiers are allowed before the return type.Option B is correct, creating a method with default access and the optional specifier final. Option C is correct, creating a method with private access. Option A is incorrect because default access omits the access modifier rather than specifying default. Option D is incorrect because Java is case sensitive. Option E is incorrect because the method already has a void return type. Option F is incorrect because labels are not allowed for methods.
ArrayIndexOutOfBoundsException, IllegalArgumentException, and NumberFormatException are examples of this type of exception
What are unchecked exceptions?