CodeGym /Java Course /Java Multithreading /Interview questions - Level 1

Interview questions - Level 1

Java Multithreading
Level 1 , Lesson 15
Available

"I almost forgot. Here are some potential interview questions that we covered in this level:"

 

Interview questions
1 List the Object class's methods
2 Why do we need the equals & hashCode methods?
3 What will happen if you override equals but don't override hashCode?
4 Why do we need the wait, notify, and notifyAll methods?
5 What's the right way to clone an object?
6 Why do we need the finalize() method and how does it work?
7 What is the difference between final, finally, and finalize?
8 What is a try-with-resources statement?
9 What's the difference between wait(1000) and sleep(1000)?
10 What's the difference between i++ and ++i?
Comments (12)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Gabriela Knapik Level 31, Poland
1 February 2022
I would appreciate correct answers attached here to check ourselves :D
Fadi Alsaidi Level 34, Carrollton, TX, USA
2 August 2020
7- final is a class access modifier. finally is the code to be executed after a try and catch statement in either scenario. finalize is deprecated method that supposedly releases resource bu deleting them once they are no longer referenced by any other code or objects 8- it a good replacement for finalize where you can open resources and close them as soon as they are done. 9- read number 4 10- i++ will get the latest i number then increment i by 1, while ++i will increment last known i by one.
Andrei Level 41
26 March 2021
for final I would phrase it like this: final - non-access modifier keyword that does not allow: changing the variable after declaration overriding the method by subclasses the inheritance of a class
Andrei Level 41
26 March 2021
also, for number 9, referring to number 4 does not explain fully. Here is my opinion: wait - sleeps current thread but grants access to synchronised block to other threads sleep - sleeps current thread but does not grant access to sync block to other threads. "wait() is used for inter-thread communication while sleep() is used to introduce pause on execution, generally." - google
Fadi Alsaidi Level 34, Carrollton, TX, USA
2 August 2020
1- Object class's Methods are : clone(), equal(), hashcode(), finalize(), getClass(), toString(), notify(), notifyAll(), wait() 2- To be able to compare objects on what matter to us for an object to be equal , equal(). Hashcode method is a mechanism used in many java methods and objects. When we @Override equal() we must override hashcode() on the fields that makes the equal() works 3- causes major issues when running your code especially when using collections or hascode based objects. which is called violation of general contract of Object.hashcode() 4- inside a synchronized block, where you can use wait() or notify(), if an object is locked for synchronization but that object is dependent on another object's existent then instead of locking your resources until that dependent object exist, you can release the synchronization to other object and suspend the one that is waiting. the notify will simply tell the waiting object to get the synchronization going because the dependent object exists now. wait(), notify() can only be used inside the synchronized() block 5- for primitive fields type objects , a simple clone method will do. for everything else we must do deep cloning which is basically creating a new instance of the object being cloned and pass the fields we intend to clone. Deep cloning creates a new reference to the fields which will help us avoid changes to the cloned object if the original object changed. 6- Supposedly finalize closes resources by telling the garbage collector that we are done with them. the garbage collector check is there is any more references to the object, and if there are none, then the garbage collector deletes the object and free resources. In reality that doesn't happen as we wish and we should avoid using finalize() guys feel free to correct me and i will edit
Chris Hilborne Level 26, Sanlucar de Barrameda, Spain
7 June 2020
I wish we'd been given these kinds of questions to check our understand throughout the whole course...
Johannes Level 27, Centurion, Pretoria, South-Africa
26 April 2020
I might be wrong, but wait(1000) and sleep(1000) does the exact same thing ? (Except that you can probably only use wait() in a synchronized block ?
Johannes Level 27, Centurion, Pretoria, South-Africa
30 April 2020
Ah yes. Thank you. Forgot about the releasing of the monitors.
Azja Level 32, Krakow, Poland
9 May 2019
Cool!
Andrew Level 29, Seattle, United States
3 April 2019
I like this! Would love for this to be included in all topics.
Hashirama Level 26, Port-Harcourt, Nigeria
26 June 2019
Yeah