Java Mastery Quiz Quiz

  1. Volatile Arrays

    In Java, can you declare an array as volatile, ensuring that all elements of the array are handled atomically across different threads?

    1. Yes, the entire array becomes volatile and is thread-safe.
    2. No, volatile only applies to the array reference, not the elements within.
    3. Only primitive arrays can be made volatile.
    4. Volatile has no effect on arrays.
    5. Volatile applies to the first element.
  2. Atomic Operations

    Can the volatile keyword guarantee that a non-atomic operation becomes atomic in a multithreaded Java program?

    1. Yes, volatile ensures atomicity in all operations.
    2. Volatile can sometimes provide atomicity, but not always.
    3. Volatile doesn't relate to atomicity.
    4. Yes, volatile always makes long and double atomic.
    5. Volatile operations are always atomic.
  3. Wait Method

    In Java, how should the wait method typically be called to avoid spurious wakeups?

    1. Within an if block.
    2. Within a while loop.
    3. It doesn't matter, just call wait.
    4. Wait should be avoided.
    5. Within a try-catch block.
  4. Swing Thread Safety

    Is it safe to update Swing components directly from any thread in Java?

    1. Yes, Swing is inherently thread-safe.
    2. No, Swing components should only be updated from the Event Dispatch Thread.
    3. It depends on the Swing component.
    4. Yes, as long as you synchronize the updates.
    5. Updates can be made from any thread if invoke later is used.
  5. Immutable Object Creation

    Is it possible to create an immutable object in Java that contains a mutable object?

    1. No, immutable objects cannot contain mutable objects.
    2. Yes, as long as you don't share the mutable object's reference directly.
    3. Only if the mutable object is final.
    4. Impossible to create an immutable object.
    5. Impossible to create mutable objects.
  6. Price Representation

    In Java, what is generally the most appropriate data type to represent monetary values or prices when precision is crucial?

    1. Float
    2. Double
    3. BigDecimal
    4. Integer
    5. String
  7. Integer vs. Int Memory

    Which data type typically takes up more memory in Java, the primitive type int or the object wrapper type Integer?

    1. int takes more space.
    2. Integer takes more space.
    3. They take the same amount of space.
    4. Depends on the compiler.
    5. Depends on the operating system.
  8. String Immutability

    Why is the string object immutable in Java?

    1. For improved performance.
    2. To allow caching of strings.
    3. For security and thread safety.
    4. Easier shareing same String object between multiple clients.
    5. All of the above.
  9. Switch Statement Strings

    Starting from which Java version can you use strings in a switch statement?

    1. Java 5
    2. Java 6
    3. Java 7
    4. Java 8
    5. Java 9
  10. Int Size

    What is the size of an int in a 64-bit JVM environment in Java?

    1. 64 bits
    2. 32 bits
    3. 16 bits
    4. 8 bits
    5. Depends on the operating system.