1.
Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.)
Correct Answer(s)
B. IllegalStateException
C. NumberFormatException
D. IllegalArgumentException
Explanation
These exceptions are typically thrown by an API developer or an application developer because they are related to invalid or illegal arguments or states in the code. ClassCastException and ExceptionInInitializerError, on the other hand, are more likely to be thrown by the JVM itself when there are issues with class casting or initializing static variables.
2.
Which about the three java.lang classes String, StringBuilder, and StringBuffer are true? (Choose all that apply.)
Correct Answer(s)
A. All three classes have a length() method.
B. Objects of type StringBuffer are thread-safe.
Explanation
All three classes have a length() method because the length() method is a common method in the String, StringBuilder, and StringBuffer classes that returns the length of the string.
Objects of type StringBuffer are thread-safe because StringBuffer is designed to be thread-safe, meaning that multiple threads can safely access and modify a StringBuffer object without causing any data corruption or inconsistencies.
The other statements are not true. The "+" operator is only overloaded for the String class, not for StringBuilder or StringBuffer. According to the API, StringBuilder is generally faster than StringBuffer because it is not synchronized, while StringBuffer is synchronized, making it slower in multi-threaded environments. Finally, all three classes provide methods to modify the value of an instance, such as append(), delete(), and replace().
3.
Which are true? (Choose all that apply.)
Correct Answer(s)
B. Both DateFormat and NumberFormat objects can be constructed to be Locale specific.
C. Both Currency and NumberFormat objects must be constructed using static methods.
D. If a NumberFormat instance's Locale is to be different than the current Locale, it must be specified at creation time.
E. A single instance of NumberFormat can be used to create Number objects from Strings and to create formatted numbers from numbers.
Explanation
Both DateFormat and NumberFormat objects can be constructed to be Locale specific. This means that the formatting of dates and numbers can be customized based on the language and cultural conventions of a specific locale.
Both Currency and NumberFormat objects must be constructed using static methods. This is because these classes provide a set of predefined formats that are commonly used for currencies and numbers, and the static methods allow for easy instantiation of these formats.
If a NumberFormat instance's Locale is to be different than the current Locale, it must be specified at creation time. This means that if you want to format numbers according to a specific locale, you need to explicitly specify the desired locale when creating the NumberFormat instance.
A single instance of NumberFormat can be used to create Number objects from Strings and to create formatted numbers from numbers. This means that you can use the same NumberFormat object to parse a string representation of a number into a Number object, as well as to format a number into a desired format.
4.
Which collection class(es) allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized? (Choose all that apply.)
Correct Answer
D. Java.util.ArrayList
Explanation
The java.util.ArrayList class allows you to grow or shrink its size and provides indexed access to its elements. Its methods are not synchronized, meaning that it is not thread-safe. This class is a part of the Java Collections Framework and is commonly used when you need a dynamic array-like data structure.
5.
Which statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden? (Choose all that apply.)
Correct Answer(s)
B. If the equals() method returns false, the hashCode() comparison == might return true.
D. If the hashCode() comparison == returns true, the equals() method might return true.
Explanation
If the equals() method returns false, the hashCode() comparison == might return true: This statement is true because even if two instances of the same class are not equal, it is still possible for their hash codes to be the same. The hashCode() method is used to generate a hash code for an object, and it is possible for different objects to have the same hash code.
If the hashCode() comparison == returns true, the equals() method might return true: This statement is also true because while it is generally expected that if two objects have the same hash code, they should also be equal, it is not a strict requirement. It is possible for two objects with the same hash code to have different values and therefore not be equal.
6.
Which are true about a method-local inner class? (Choose all that apply.)
Correct Answer(s)
B. It can be marked abstract.
E. It can access private members of the enclosing class.
Explanation
A method-local inner class can be marked abstract because it can be subclassed and overridden by other classes. It can access private members of the enclosing class because it has access to all members of its enclosing class, including private members. However, it does not need to be marked final, public, or static, as these modifiers are not applicable to method-local inner classes.
7.
Which are true? (Choose all that apply.)
Correct Answer(s)
A. The notifyAll() method must be called from a synchronized context.
D. When a thread is waiting as a result of wait(), it release its lock.
Explanation
The notifyAll() method must be called from a synchronized context because it is used to wake up all threads that are waiting on the same object's monitor. To ensure thread safety and avoid race conditions, it is necessary to call notifyAll() within a synchronized block or method.
When a thread is waiting as a result of wait(), it releases its lock. This is because the wait() method causes the thread to temporarily give up its lock and enter a waiting state until it is notified or interrupted. This allows other threads to acquire the lock and continue executing their code.
8.
Which are methods of the Object class? (Choose all that apply.)
Correct Answer(s)
A. Notify();
B. NotifyAll();
F. Wait(long msecs);
Explanation
The methods of the Object class are used for synchronization and inter-thread communication in Java. The methods notify() and notifyAll() are used to wake up threads that are waiting on the object's monitor. The method wait(long msecs) is used to make a thread wait for a specified amount of time or until it is notified. The other methods mentioned, isInterrupted(), synchronized(), and interrupt(), are not methods of the Object class.
9.
Which interface provides the capability to store objects using a key-value pair?
Correct Answer
A. Java.util.Map
Explanation
The correct answer is java.util.Map. This interface provides the capability to store objects using a key-value pair. It allows you to associate a value with a unique key, and then retrieve that value later using the same key. This is useful for scenarios where you need to store and access data in a way that is efficient and allows for quick retrieval based on a specific key.
10.
Which statement is true? (Choose all that apply)
Correct Answer(s)
C. Some reference variables live on the stack, and some live on the heap.
E. It’s possible to request the GC via methods in either java.lang.Runtime or java.lang.System classes.
Explanation
Some reference variables live on the stack, and some live on the heap. In Java, primitive types and local variables are stored on the stack, while objects and reference variables are stored on the heap. It’s possible to request the GC via methods in either java.lang.Runtime or java.lang.System classes. These classes provide methods like System.gc() and Runtime.getRuntime().gc() to request garbage collection, although the actual garbage collection process is controlled by the JVM.