Table of Contents
- 1 How HashMap works in java?
- 2 Which methods you should implement for key Object in HashMap?
- 3. Guess the output of below program.
- 4. Guess the output of below code.
- 5. What are differences between Comparator and Comparable?
- 6. What is difference between Error and Exception?
- 7. What is ClassLoader in java?
- 8. What are types of ClassLoader in java?
- 9. Are you aware of Singleton design pattern? Can you create a Singleton class?
- 10. What is the difference between Statement and PreparedStatement?
In this post, we will see top Java technical interview asked in the Core Java interview.I have covered a lot of interview question on Java programming.
Here is list of other java interview question you might find useful.
- Core java interview questions
- Java Collections interview questions
- Java String interview questions
- OOPs interview questions in java
- Exceptional handling interview questions in java
- Java Serialization interview questions in java
- Method overloading and overriding interview questions
Here is the list of top 10 Java technical interview questions.
1 How HashMap works in java?
This is most asked interview question in java interview. You need to have understanding of internal working of HashMap to answer this questions.
You can refer How HashMap works in java for details.
2 Which methods you should implement for key Object in HashMap?
You need to implement Hashcode and equals method for Key object in HashMap.
You can refer Hashcode and equals method for details.
3. Guess the output of below program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package org.arpit.java2blog; public class Customer { String name; int age; public Customer(String name,int age) { this.name=name; this.age=age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Customer other = (Customer) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Main class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.HashMap; import java.util.Map; public class HashMapMain { public static void main(String[] args) { Customer customer1=new Customer("John",27); Map<Customer,String< hm=new HashMap<Customer,String<(); hm.put(customer1, "Verified"); customer1.setAge(28); System.out.println(hm.get(customer1)); } } |
4. Guess the output of below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; public class ByteMain { public static void main(String[] args) { byte a=10; byte b=20; a+=b; a=a+b; System.out.println(a); } } |
What will be output of above program.
5. What are differences between Comparator and Comparable?
Comparable and Comparator are used to sort list of object.
Please refer to the difference between Comparator and Comparable for details.
6. What is difference between Error and Exception?
Both java.lang.Error and java.lang.Exception classes are sub classes of java.lang.Throwable class.
You can not recover from Error but you can recover from Exception from using try catch block.
Examples for Errors are: java.lang.StackOverflowError, java.lang.OutOfMemoryError
Examples of Exceptions are: IOException, NullPointerException.
7. What is ClassLoader in java?
ClassLoader is the program that loads byte code class to memory whenever you want to need it.
8. What are types of ClassLoader in java?
There are three types of Class Loader in java.
Bootstrap class loader: It loads classes related to Java platform.
Extension class loader: Here the classes which will use Java extension mechanism will be loaded. These classes will reside in extensions directory as .jar files.
Application class loader: these classes are defined by users. These classes will be found by using class path variable.
9. Are you aware of Singleton design pattern? Can you create a Singleton class?
Singleton means you can create only one instance of that class.You can create a static method which will give you singleton object and you can make constructor private to restrict more than one object for below class.
This is a very basic version of Singleton class and there are various concepts such as Serialization,multithreading which can break below Singleton class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; public class MySingleton { private static MySingleton mySingleton; static{ mySingleton = new MySingleton(); } private MySingleton(){ } public static MySingleton getInstance(){ return mySingleton; } public void test(){ System.out.println("Calling method inside singelton class"); } public static void main(String a[]){ MySingleton ms = getInstance(); ms.test(); } } |
10. What is the difference between Statement and PreparedStatement?
Statement : is used to execute normal SQL queries.You can not pass parameters to Statement.
PreparedStatement : PreparedStatement is used to execute dynamic or parameterized queries.PreparedStatement extends Statement.If you are going to execute queries multiple queries then you should use PreparedStatement as PreparedStatement are precompiled and query plan is executed only once no matter how many time you will execute the query.
That’s all about Java technical Interview questions.