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
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:
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
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.
Ans - JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
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.
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.
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.
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.
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.
Ans - stored procedure should return error code if some operation fails but if stored procedure itself fail than catching SQLException is only choice.
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.
Ans - main() method is called by the JVM even before the instantiation of the class hence it is declared as static.
Ans - No, an object cannot be cast to a primitive value.
Ans - It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.
Ans - Main difference between HaspMap and Hashtable are following :
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.
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.
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.
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.
No any Comment yet!