Java Quest Online Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Stefstoilov
S
Stefstoilov
Community Contributor
Quizzes Created: 1 | Total Attempts: 86
Questions: 10 | Attempts: 86

SettingsSettingsSettings
Java Quest Online Quiz - Quiz



Questions and Answers
  • 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.)

    • A.

      ClassCastException

    • B.

      IllegalStateException

    • C.

      NumberFormatException

    • D.

      IllegalArgumentException

    • E.

      ExceptionInInitializerError

    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.

    Rate this question:

  • 2. 

    Which about the three java.lang classes String, StringBuilder, and StringBuffer are true? (Choose all that apply.)

    • A.

      All three classes have a length() method.

    • B.

      Objects of type StringBuffer are thread-safe.

    • C.

      All three classes have overloaded append() methods.

    • D.

      The "+" is an overloaded operator for all three classes.

    • E.

      According to the API, StringBuffer will be faster than StringBuilder under most implementations.

    • F.

      The value of an instance of any of these three types can be modified through various methods in the API.

    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().

    Rate this question:

  • 3. 

    Which are true? (Choose all that apply.)

    • A.

      The DateFormat.getDate() is used to convert a String to a Date instance.

    • 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.

    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.

    Rate this question:

  • 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.)

    • A.

      Java.util.HashSet

    • B.

      Java.util.LinkedHashSet

    • C.

      Java.util.List

    • D.

      Java.util.ArrayList

    • E.

      Java.util.Vector

    • F.

      Java.util.PriorityQueue

    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.

    Rate this question:

  • 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.)

    • A.

      If the equals() method returns true, the hashCode() comparison == might return false.

    • B.

      If the equals() method returns false, the hashCode() comparison == might return true.

    • C.

      If the hashCode() comparison == returns true, the equals() method must return true.

    • D.

      If the hashCode() comparison == returns true, the equals() method might return true.

    • E.

      If the hashCode() comparison != returns true, the equals() method might return true.

    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.

    Rate this question:

  • 6. 

    Which are true about a method-local inner class? (Choose all that apply.)

    • A.

      It must be marked final.

    • B.

      It can be marked abstract.

    • C.

      It can be marked public.

    • D.

      It can be marked static.

    • E.

      It can access private members of the enclosing class.

    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.

    Rate this question:

  • 7. 

    Which are true? (Choose all that apply.) 

    • A.

      The notifyAll() method must be called from a synchronized context.

    • B.

      To call wait(), an object must own the lock on the thread.

    • C.

      The notify() method is defined in class java.lang.Thread.

    • D.

      When a thread is waiting as a result of wait(), it release its lock.

    • E.

      The notify() method causes a thread to immediately release its lock.

    • F.

      The difference between notify() and notifyAll() is that notifyAll() notifies all waiting threads, regardless of the object they're waiting on.

    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.

    Rate this question:

  • 8. 

    Which are methods of the Object class? (Choose all that apply.)

    • A.

      Notify();

    • B.

      NotifyAll();

    • C.

      IsInterrupted();

    • D.

      Synchronized();

    • E.

      Interrupt();

    • F.

      Wait(long msecs);

    • G.

      Sleep(long msecs);

    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.

    Rate this question:

  • 9. 

    Which interface provides the capability to store objects using a key-value pair?

    • A.

      Java.util.Map

    • B.

      Java.util.Set

    • C.

      Java.util.List

    • D.

      Java.util.Collection

    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.

    Rate this question:

  • 10. 

    Which statement is true? (Choose all that apply)

    • A.

      The invocation of an object’s finalized method is always the last thing that happens before an object is garbage collected.

    • B.

      When a stack variable goes out of scope it is eligible for GC.

    • C.

      Some reference variables live on the stack, and some live on the heap.

    • D.

      Only objects that have no reference variables referring to them can be eligible for GC.

    • E.

      It’s possible to request the GC via methods in either java.lang.Runtime or java.lang.System classes.

    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.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Oct 19, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 04, 2013
    Quiz Created by
    Stefstoilov
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.