Volatile Arrays
In Java, can you declare an array as volatile, ensuring that all elements of the array are handled atomically across different threads?
- Yes, the entire array becomes volatile and is thread-safe.
- No, volatile only applies to the array reference, not the elements within.
- Only primitive arrays can be made volatile.
- Volatile has no effect on arrays.
- Volatile applies to the first element.
Atomic Operations
Can the volatile keyword guarantee that a non-atomic operation becomes atomic in a multithreaded Java program?
- Yes, volatile ensures atomicity in all operations.
- Volatile can sometimes provide atomicity, but not always.
- Volatile doesn't relate to atomicity.
- Yes, volatile always makes long and double atomic.
- Volatile operations are always atomic.
Wait Method
In Java, how should the wait method typically be called to avoid spurious wakeups?
- Within an if block.
- Within a while loop.
- It doesn't matter, just call wait.
- Wait should be avoided.
- Within a try-catch block.
Swing Thread Safety
Is it safe to update Swing components directly from any thread in Java?
- Yes, Swing is inherently thread-safe.
- No, Swing components should only be updated from the Event Dispatch Thread.
- It depends on the Swing component.
- Yes, as long as you synchronize the updates.
- Updates can be made from any thread if invoke later is used.
Immutable Object Creation
Is it possible to create an immutable object in Java that contains a mutable object?
- No, immutable objects cannot contain mutable objects.
- Yes, as long as you don't share the mutable object's reference directly.
- Only if the mutable object is final.
- Impossible to create an immutable object.
- Impossible to create mutable objects.
Price Representation
In Java, what is generally the most appropriate data type to represent monetary values or prices when precision is crucial?
- Float
- Double
- BigDecimal
- Integer
- String
Integer vs. Int Memory
Which data type typically takes up more memory in Java, the primitive type int or the object wrapper type Integer?
- int takes more space.
- Integer takes more space.
- They take the same amount of space.
- Depends on the compiler.
- Depends on the operating system.
String Immutability
Why is the string object immutable in Java?
- For improved performance.
- To allow caching of strings.
- For security and thread safety.
- Easier shareing same String object between multiple clients.
- All of the above.
Switch Statement Strings
Starting from which Java version can you use strings in a switch statement?
- Java 5
- Java 6
- Java 7
- Java 8
- Java 9
Int Size
What is the size of an int in a 64-bit JVM environment in Java?
- 64 bits
- 32 bits
- 16 bits
- 8 bits
- Depends on the operating system.