Java Interview Questions and Answers
Question - 31 : - What would you use to compare two String variables - the operator == or the method equals()?
Answer - 31 : - I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.
Question - 32 : - What is thread?
Answer - 32 : - A thread is an independent path of execution in a system.
Question - 33 : - What is multi-threading?
Answer - 33 : - Multi-threading means various threads that run in a system.
Question - 34 : - How does multi-threading take place on a computer with a single CPU?
Answer - 34 : - The operating system's task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.
Question - 35 : - How to create a thread in a program?
Answer - 35 : - You have two ways to do so. First, making your class "extends" Thread class. Second, making your class "implements" Runnable interface. Put jobs in a run() method and call start() method to start the thread.
Question - 36 : - Can Java object be locked down for exclusive use by a given thread?
Answer - 36 : - Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.
Question - 37 : - Can each Java object keep track of all the threads that want to exclusively access to it?
Answer - 37 : - Yes. Use Thread.currentThread() method to track the accessing thread.
Question - 38 : - Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
Answer - 38 : - Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.
Question - 39 : - What invokes a thread's run() method?
Answer - 39 : - After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.
Question - 40 : - What is the purpose of the wait(), notify(), and notifyAll() methods?
Answer - 40 : - The wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to communicate each other.