Due to the rapid boost in jobs which involve the use of Java, it is definitely worth devoting a whole section to this language of programming that is so popular. Though this might not be complete and a proper guide, there is some basic knowledge of Java. Here are a few interview questions in java for freshers which cover a whole range of topics. If you are about to attend a Java programming interview, here are some java fresher interview questions you might have to answer. Some of them are tricky so read them carefully before practicing. We really hope this post will help you.
Java Programming Interview Questions and Answers:
The following mentioned are few interview questions and answers on java for freshers.
1. What happens when you place a return statement or System exit () on and try to hold a block? Will that block be executed?
This happens to be a very popular and tricky question in Java and it can be quite tricky because there are several programmers who believe no matter what, the final block shall always be executed. If you have been thinking the same, then let us inform you this answer could backfire. The question is challenging and does challenge the concept itself by pulling off a return statement in the try or even try to catch block by calling exit. You can answer the java tricky questions by saying that the block can be executed even when you manage to pull a statement of return or even catch the block. Nonetheless it will not run if you call the system.exit.
2. Is it possible to override a static method?
This is also a tricky question in Java language you should know how to answer. Over riding is always a fine topic but could be a trick question to take care of. Anyway you shouldn’t override any of the static methods inside Java itself. You can very easily create another method with the same type of return as well as the same method arguments in every child class and then it will be able to conceal the method of super class. This is popularly called method hiding. Similarly, you will never be able to over ride any private method in sub classes since it might not always be accessible. What you can do is find another method which is private with a similar name.
3. What does 1.0 / 0.0 mean? What does it return? Shall it give any exception? Are there are any compile time errors?
This is quite a tricky question from double class. Though the developer of Java knows about any primitive double type or the double class while doing any arithmetic, they still don’t care much or lay much emphasis on Double. Infinity. NaN or 0.0. Under this rule, there are other rules which govern the calculations involving this. A simple way to answer the question is that it might not throw and return double the infinity. Also you should note a proper comparison. Keep in mind that double NAN leads to false despite x being a NaN itself. If you want to test x as NaN, then you should use the method and call it double. Also check if the number is NaN or something else. In case you have a good idea about SQL, it is pretty close to NULL.
4. Does Java in any way support several inheritances?
A tricky question found in Java is whether Java can support any of the inheritances. When you answer such questions, remember to be subtle. Java in reality does show support towards several inheritances of Type by simply allowing a method of interface so that other interfaces can be extended. What Java doesn’t support is the several inheritances of the methods of implementation. The difference also becomes a little blur due to the method of default of Java which at the moment provides Java with several inheritances too.
5. How can Java achieve independence on platform?
When we talk about Java and say that it is platform independent, we mean that these java programs never depend on any of the platforms or the operating system like windows, Linux or the architecture. Java can be achieved by using a virtual machine usually when Java programs that have been compiled, they are always converted to class files which is collected and understandable by JVM. So the same program of Java can be run in any system of operating. But JVM does differ depending on the OS and all it does is understand and converts byte code and that is exactly how Java can get independence on platform.
6. Define Class Loader:
A very advanced question in the field of Java happens to be this. In the past couple of years, this question has been asked several times and is quite common. If you want to ace the interview, knowing about class loader is important for you. When a Java program has been converted into class files by the java compiler. This happens to be a series of bytes and code class loaders which is responsible for loading the class files from the system, networks or any important location. The class loader happens to be nothing but a class from any kind of location that they are loading. Class loaders happen to be of three kinds known as extension, bootstrap and system class loader.
7. How can you differentiate between HashSet and ArrayList In Java?
Another important question you will be asked during java based interviews happens to be this one. The difference between HashSet and ArrayList is quite common. Here are a couple of differences noticed between two important classes. While answering the question, here are some important points you should remember. ArrayList will implement the List Interface whereas the other will set interface in the field of Java. Secondly, HashSet happens to be an unorganized collection that doesn’t have any order as such and ArrayList is the exact opposite comparatively. ArrayList can be backed by any Array whereas HashSet has to be backed by HashMap example. Another important difference between ArrayList and HashSet is that since it is based on index, you can get hold of objects by calling get or remove by calling remove. HashSet. It doesn’t come with any get method.
8. How will you describe double checked locking?
Interviewers always tend to ask this question during interview. Not only is this question popular but also one that is asked very often in the world of Java. Singleton means that you can never create any instance of classes in terms of Singleton DCL and that it is the only way to make sure that, the only instance created in an environment that is multi threaded is by the two thread tying that can create any instance of singleton class. We can’t make sure whether that one example will be enough to avoid such situations by using the method of double check locking which is done with the help of synchronized blocks where you have been creating any objects.
9. When will be the right time to use volatile variables?
Volatile keywords are those which are only used with one variable in the field of Java and that it guarantees the whole value of volatile variable which may be read from the main memory and not always from the local cache of Thread. So we will always be able to use volatile by simply achieving synchronization since it did guarantee that all readers thread will see them in updated value of the volatile variable once the operation of writing has been completed without any volatile keyword reader thread, that will read values which are different. Volatile modifier also helps in preventing the reordering of codes by offering more guarantee and visibility.
10. When can you use variables that are transient?
Transients in Java have in the past used this to show that variables must not ever be serialized. Serialization happens to a process which saves an objects state in the field of Java. When we would like to persist and object the state by using default, all instances and variables in the objects will be stored. There are some cases in which you might want to avoid persisting some of the variables because you usually wont face the necessity of transferring all over the network. So what you can do is declare the variables which are transient. If the variables have been declared as transient, then it shall never be persisted. This could be a huge reason of transient and to learn a lot about the variables in Java, you can check more websites online.
11. What is the difference between volatile and transient variables?
A follow up question you will find during Java interviews is knowing the difference between volatile and transient. Here are a few important differences you should know between the two. Transient variables can be defined as transient keywords which are used in instance variables that may not always participate in the process of serialization as they happen to be an important part of instance variables. Volatile variables at least have used the only variables in Java and it always guarantee and values volatile variables that will be read from the main memory and never from the local cache of Thread which could be static at times.
12. Differentiate between HashMap and Hashtable?
You must also know how to differentiate between these two if you want to ace a java interview. Some of the main differences between Hashtable and HashMap include Hashtable that never allows any nulls whereas Hashmap will allow the null values as keys. Hashtable happens to be thread safe and may be shared between several threads whereas HashMap has to be shared between several threads without any synchronization. Hashtable is also a class of legacy which had been implemented by the dictionary previously and the dictionary interface. It was also retrofitted by a framework by implementing Map. HashMap again on the other hand is a part of framework from the beginning.
13. What is the difference between Set and List?
Another important interview question for java fresher, you must never forget to prepare is learning how to differentiate between Set and List. List and Set are both quite useful interfaces of Java and its collections and the difference between the two will allow duplicate elements but set at the same time doesn’t allow any of the duplicate elements. It does maintain the insertion order but set doesn’t have a collection that is unordered. List on the other hand has several null objects and can set permit by using one null element. The question is at times also asked to differentiate between List, Set and Map to make things more comprehensive as the three major data form collections of framework.
14. Final Tip:
Here are some final tips you should always remember before attending a java interview. First of all, remember some basic concepts such as infinite loop, three types of loops in java, singleton class etc. Keep in mind that your recruiting manager will start the interview by asking basic questions so you have to be able to answer them correctly if you want to make a good impression and reach the next few rounds. It will be easy only if you prepare.
With that we would like to bring the post to a close. Java interviews are not all that hard to nail if you know what kind of java interview questions for freshers shall be asked and how you should answer them. If you have enjoyed reading the post and would like to give us your feedback or post a few questions, kindly do so in the comment box below. We would love to hear from you. Attending a java interview shall definitely not be that difficult for you now. Just prepare enough and you are good to go. On that note, good luck.