GupShup Study
Java Interview Question For Freshers, Frequently Asked By Interviewer
Ravi Chopra

Java Interview Question For Freshers, Frequently Asked By Interviewer

23-Jan-2016 | | Total View : 340 |

This article is about preparing for Java Interviews. we post some frequently asked question with answer that normally asked by every interviewer.

I am sure many Java programmer already has a list of questions handy, if not this is a good time to find and make your own list. These are the questions which you simply can't afford to miss, especially at freshers level

1 - Difference between String, StringBuffer and StringBuilder in Java?

Ans - String is immutable while both StringBuffer and StringBuilder is mutable, which means any change e.g. converting String to upper case or trimming white space will produce another instance rather than changing the same instance. On later two, StringBuffer is synchronized while StringBuilder is not, in fact its a ditto replacement of StringBuffer added in Java 1.5.

2 - What is ClassLoader in Java? 

Ans - The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders. Delegation is an important concept to understand when learning about classloaders.
Each Java class must be loaded by a class loader. Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries.

When the JVM is started, three class loaders are used:

  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader


 3 - What is the most Important feature of java?

Ans - The most important feature of java is that "Java is platform independent language". but there are also some other important feature like-

1) Simple,Small and familiar 
2) Object oriented 
3) Distributed 
4) Robust 
5) Secure 
6) Platform independent 
7) Portable 
8) Compiled and Interpreted 
9) High performance 
10) MultiThreading and interactive 
11) Dynamic and extensible 


4 - What is constructor chaining and how is it achieved in Java ?

Ans - A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.


5 - What is a JVM?

Ans - JVM is Java Virtual Machine which is a run time environment for the compiled java class files.


6 - How do you create thread-safe Singleton in Java?

Ans - There are more than one ways to do it. You can  create thread safe Singleton class in Java by creating the one and only instance during class loading. static fields are initialized during class loading and Classloader will guarantee that instance will not be visible until its fully created.


7 - What is the difference between a JDK and a JVM?

Ans - JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.


8 - What are instance variables?

Ans - Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.


9 - When to use transient variable in Java?

Ans - Transient in Java is  used to indicate that the variable should not be serialized. Serialization is a process of saving an object's state in Java. When we want to persist and object's state by default all instance variables in the object is stored. In some cases, if you want to avoid persisting some variables because we don’t have the necessity to transfer across the network. So, declare those variables as transient. If the variable is declared as transient, then it will not be persisted.


10 - What is difference between wait and notify in Java?

Ans - Both wait and notify methods are used for inter thread communication, where wait is used to pause the thread on a condition and notify is used to send notification to waiting threads. Both must be called from synchronized context e.g. synchronized method or block.


11 - How do you handle error condition  while writing stored procedure or accessing stored procedure from java?

Ans - stored procedure should return error code if some operation fails but if stored procedure itself fail than catching SQLException is only choice.


12 - Can we override private method in Java?

Ans - No, we cannot override private methods in Java as if we declare any variable ,method as private that variable or method will be visible for that class only and also if we declare any method as private than they are bonded with class at compile time not in run time so we cant reference those method using any object so we cannot override private method in Java.


13 - Why is the main() method declared static?

Ans - main() method is called by the JVM even before the instantiation of the class hence it is declared as static.


14 - Can a Byte object be cast to a double value?

Ans - No, an object cannot be cast to a primitive value.


15 - What is the % operator?

Ans - It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.


16 - Difference between Hashtable and HashMap in Java?

Ans - Main difference between HaspMap and Hashtable are following :

  • HashMap allows null values as key and value whereas Hashtable doesn't allow nulls.
  • Hashtable is thread-safe and can be shared between multiple threads whereas HashMap cannot be shared between multiple threads without proper synchronization.
  • Because of synchronization, Hashtable is considerably slower than HashMap, even in case of single threaded application.
  • Hashtable is a legacy class, which was previously implemented Dictionary interface. It was later retrofitted into Collection framework by implementing Map interface. On the other hand, HashMap was part of framework from it's inception.
  • You can also make your HashMap thread-safe by using Collections.synchronizedMap() method. It's performance is similar to Hashtable.


17 - What is the diffrence between inner class and nested class?

Ans - When a class is defined within a scope od another class, then it becomes inner class. If the access modifier of the inner class is static, then it becomes nested class.


18 - Difference between Serializable and Externalizable in Java?

Ans - Serializable is a marker interface with no methods defined it but Externalizable interface has two methods defined on it e.g. readExternal() and writeExternal() which allows you to control the serialization process. Serializable uses default serialization process which can be very slow for some application.


19 - How does HashSet works in Java?

Ans - HashSet is internally implemented using HashMap in Java and this is what your interviewer wants to hear. He could then quiz you with some common sense based question e.g. how can you use HashMap because its needs two object key and value? what is the value in case of HashSet? Well, in case of HashSet a dummy object is used as value and key objects are the actual element on Set point of view. Since HashMap doesn't allow duplicate key it also follows contract of set data structure to not allow duplicates.


20 - What is numeric promotion?

Ans - Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.


If you have any question or doubt then please ask in comment section below.

Share With Friends :  

No any Comment yet!