Skip to main content

Posts

Showing posts from January, 2019

Multi-threading Interview Questions in Java

Multi-threading 1. What is a Thread in Java? A thread in Java is a lightweight process that runs within another process or thread. It is an independent path of execution in an application. JVM gives each thread its own method-call stack. When we start JVM, Java starts one thread. This thread calls the main method of the class passed in argument to java call. 2. What is the priority of a Thread and how it is used in scheduling? In Java, every Thread has a priority. This priority is specified as a number between 1 to 10. Scheduler in Java schedules different threads based on the priority of a thread. It is also known as pre-emptive scheduling. The thread with higher priority gets preference in execution over a thread with lower priority. 3. What is the default priority of a thread in Java? In Java, a new thread gets the same priority as the priority of the parent thread that creates it. Default priority of a thread is 5 (NORM_PRIORITY). 4. What are the three

Java Collection Interview Questions part- 2

Java Collection 1.What are the major differences between a HashSet and a HashMap? The main difference between a HashSet and a HashMap are: 1.      Base class : A HashSet class implements the Set interface. Whereas a HashMap class implements the Map interface. 2.      Storage : A HashSet is used to store distinct objects. A HashMap is used for storing key & value pairs, so that these can be retrieved by key later on. 3.      Duplicate Elements : A HashSet does not allow storing duplicate elements. A HashMap also does not allow duplicate keys. But we can store duplicate values in a HashMap. 4.      Null Elements : In a HashSet we can store a single null value. In a HashMap we can store single null key, but any number of null values. 5.      Element Type : A HashSet contains only values of objects as its elements. Whereas a HashMap contains entries(key value pairs). 6.      Iteration : By using an Iterator we can iterate a HashSet. But a HashMap has to be con