Java Threading and Generics
Test your knowledge of Java multithreading concepts including thread states, synchronization, and race conditions, as well as Java generics including type parameters, raw types, and generic method declarations.
Questions
What are the benefits of generics?
- Generic collections accept only Object types
- Type errors are caught at compile time
- Generics result in readily understandable code
- Generics sometimes require type casting
What are the features of raw types?
- Can be used with Generic code
- Raw types take Type parameters
- Raw types must be cast to appropriate objects at runtime
- Type safety
Identify the type parameter which can be used to use all numeric types in a generic call.
- Integer
- Float
- Number
- Double
Which one is the superclass of all generic objects?
- <?>
- <Object>
- <? extends Object>
- None of these
To create a public generic method named "update" that has no return type. In its argument list, the method accepts an argument named "updateValue" which is of the same type as the type parameter - T. Which is a valid declaration?
- public T update()
- public void update(T updateValue)
- void update(T updateValue)
- public void update()
Will it run without error: Thread th = new Thread(new Thread());
- It will run without error
- Runtime Error
- Compilation Error
- None of all
What is true
- We can invoke run() method directly to start thread
- If we call start() method twice, two threads will be created
- t.sleep(50) - it will give minimum guarantee of at least 50ms this thread will not run
- Thread can be still alive even after the completion of run().
When you create a thread instance the state will be
- Ready
- Runnable
- New
- Waiting
We can synchronize the variable to avoid race conditions.
- True
- False
If a thread goes to sleep in synchronized block. (Which one is false)
- it is still alive
- It will release all hold locks
- It comes to runnable state after sleep time expired
- No other thread can enter into the synchronized.
Running thread can't come back to runnable state directly.
- True
- False