Question 51
MediumWhat is the difference between HashSet and TreeSet in Java?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Programming Languages
What is the difference between HashSet and TreeSet in Java?
They are the same
TreeSet allows duplicates
HashSet has O(1) average operations with no order; TreeSet keeps elements sorted with O(log n) operations
HashSet is sorted
Sign up free to see the answer and a step-by-step explanation.
Which Java feature, added in Java 8, lets you write list.forEach(x -> System.out.println(x))?
Generics
Enums
Annotations
Lambda expressions
Sign up free to see the answer and a step-by-step explanation.
What does the 'transient' keyword do on a field?
Makes it thread-safe
Makes it constant
Excludes the field from Java serialisation
Makes it public
Sign up free to see the answer and a step-by-step explanation.
What is the default value of an int field and of an object field in a Java class?
0 and null
1 and an empty object
Compilation error if not set
Garbage values
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
public class Main {
public static void main(String[] args) {
int i = 1;
int j = i++;
System.out.println(i);
}
}
Error
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
public class Main {
static void f() {
try {
System.out.print("try ");
return;
} finally {
System.out.print("finally");
}
}
public static void main(String[] args) {
f();
}
}
finally
try finally
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
public class Main {
public static void main(String[] args) {
int x = Integer.MAX_VALUE;
System.out.println(x + 1);
}
}
Error
-2147483648
Sign up free to see the answer and a step-by-step explanation.
What happens when you modify an ArrayList while iterating over it with a for-each loop?
Nothing special happens
The list is copied automatically
A ConcurrentModificationException is usually thrown
The loop skips the new element silently
Sign up free to see the answer and a step-by-step explanation.
Which statement about Java's garbage collection is correct?
Garbage collection runs only when the program ends
Calling System.gc() always frees memory immediately
Objects with no reachable references become eligible for collection, but the time of collection isn't guaranteed
Java programmers must free memory manually
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
public class Main {
public static void main(String[] args) {
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println((a == b) + " " + (c == d));
}
}
false false
false true
Sign up free to see the answer and a step-by-step explanation.
1
2
finally try
try
2147483648
0
true false
true true