WorksheetsCollections_Generics_ExceptionHandling
Total questions: 18
Worksheet time: 7mins
Why are ArrayLists better than arrays ?
You don't have to decide the size of an ArrayList when you first make it.
You can put more items into an ArrayList than into an array.
ArrayLists can hold more kinds of objects than arrays.
You don't have to decide the type of an ArrayList when you first make it.
You get a NullPointerException. What is the most likely cause?
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of communications.
Your code has used up all available memory.
The object you are using has not been instantiated.
What is the output of this code?
[abc, 0, 2, 10]
The code does not compile.
[abc, 2, 10, 0]
[0, 10, 2, abc]
Which code snippet is valid ?
ArrayList<String> words = new ArrayList<String>(){"Hello", "World"};
ArrayList words = Arrays.asList("Hello", "World");
ArrayList<String> words = new ArrayList<>(Arrays.asList("Hello", "World"));
ArrayList<String> words = {"Hello", "World"};
Which characteristic does not apply to instances of java.util.HashSet?
uses hashcode of objects when inserted
contains unordred elements
contains unique elements
contains sorted elements
You have an instance of type Map<String, Integer> named instruments containing the following key-value pairs: guitar=1200, cello=3000, and drum=2000. If you add the new key-value pair cello=4500 to the Map using the put method, how many elements do you have in the Map when you call instruments.size()?
2
3
4
When calling the put method, Java will throw an exception
What is the result of this code?
Hello World
It will not compile because the second catch statement is unreachable
Hello World!
It will throw a runtime exception
Which class acts as the root class for the Java Exception hierarchy?
Clonable
Throwable
Object
Serializable
Which class does not implement the java.util.Collection interface ?
java.util.Vector
java.util.ArrayList
java.util.HashSet
java.util.HashMap
Which language feature ensures that objects implementing the AutoCloseable interface are closed when it completes?
try-catch-finally
try-finally-close
try-with-resources
try-catch-close
What language feature allows types to be parameters on classes, interfaces, and methods in order to reuse the same code for different data types?
Regular Expressions
Reflection
Generics
Concurrency
Which letters will print when this code is run?
A, B, and D
A, C, and D
C and D
A and D
What is the problem with this code ?
String should be compared using == method instead of equals.
Modifying a collection while iterating through it can throw a ConcurrentModificationException.
The List interface does not allow an argument of type String to be passed to the remove method.
ArrayList does not implement the List interface.
What is HashMap and Map ?
HashMap is Interface and map is a class that implements that
Map is Interface and Hashmap is the class that implements that
HashMap is a class and map is an interface that implements that
Map is a class and Hashmap is an interface that implements that
Which of the following declarations don't compile ?
List<?> l1 = new ArrayList<>();
List<String> l2 = new ArrayList();
List<? super Object> l3 = new ArrayList<String>();
List<? extends Object> l4 = new ArrayList<String>();
Which of the following is a benefit of using Java generics?
Which of the following statements about wildcard types in Java generics is true?
Wildcard types can be used to specify an upper bound for a type parameter
Wildcard types can be used to specify a lower bound for a type parameter
Wildcard types can be used to represent an unknown type
Wildcard types can be used to specify a specific type for a type parameter
For the below Test class which of the following way of object creation is invalid ?
class Test<T extends Number> { }
Test<Integer> t1 = new Test<Integer>();
Test<String> t2 = new Test<String>();
Test<Double> t3 = new Test<Double>();
Test<Number> t4 = new Test<Number>();
Test<Object> t5 = new Test<Object>();
