18 Most Important Java Interview Questions and Answers Define Synchronization in Java Synchronization in Java is a process in which the accessibility of shared resources are controlled by making use of multiple threads in such a way that only a single resource could be accessed at one time. A shared object could be modified using a single thread whereas another thread would be involved in the process of updating or using the value of the object in non-synchronized multi-threaded application. What is meant by JVM and why Java is considered as a Platform Independent Programming Language? JVM otherwise known as Java Virtual Machine is considered as an interpreter that is capable of accepting and executing the ‘Bytecode’. Because Java works on the basis of ‘compile once, run everywhere’, it has been termed as a platform independent language. The sequential steps that give an insight in to the platform independent characteristics are as follows. • Non-executable codes known as ‘Bytecode’ will be provided as an output by Java Compiler • Bytecode is nothing but a set of highly optimized computer instructions that could be executed by Java Virtual Machine
• The programs could be executed easily across a wide range of platforms by means of translation in to bytecode. This is because all that is required is a JVM being tailored for that particular platform. • There could be a difference in configuration for different platforms in JVM and the same set of bytecodes could be understood easily. All this makes Java program a platform independent one. Differentiate between JDK and JRE This is a typical Java interview question that has confused most of the Java developers and it is important for Java developers to understand the difference between JDK and JRE which would help take their career to the next level. JDK stands for Java Development Kit which is a comprehensive package of software which can be used for creating Java based software. On the other hand, JRE stands for Java Runtime Environment. It is nothing but an implementation of Java Virtual Machine that is capable of executing Java programs. Each of the JDK would be comprised of one or more JRE’s in addition to different kind of development tools such as Java source compilers, debuggers, development libraries and bundling as well as deployment of tools etc. What is the difference between Constructors, Copy Constructor and Constructor Overloading in Java? For beginners, Constructors is considered to be basics of OOPS. Constructor – Creating an instance of a class remains the most important objective of having Constructors. Whenever an object of a class has been created, the constructors would be invoked. Some of the key characteristics of Java Constructors are as follows. • Constructors could be public, private and protected etc. • Default no-argument constructor could not be used whenever a constructor with arguments has been defined in a class. • Whenever the classes are being instantiated, they would be called only once. • Constructors would have the same name as that of the class. • Value is not returned and therefore they need not have to specify the keyword void.
• Java helps in creating a so called default no-argument constructor whenever a constructor for the class is not created. Constructor Overloading – Constructor overloading can be defined as the passing of various number as well as type of variables as arguments out of which most of them are regarded as private variables of the class. Copy Constructor – A type of constructor that is capable of constructing the object of class from another object of the same class is defined as Copy Constructor. The reference of its own class is considered as a parameter by the copy constructor. Define Java Exception Handling and differentiate between Unchecked Exception, Checked Exception and Errors? Exception is a method to point out the occurring of an abnormal condition in Java. Exceptions are considered as objects in Java and whenever an exception is thrown, the object is also thrown. It has to be understood that it is not possible to simply throw any objection as an exception. The throwable is considered as a base class for the entire family of classes that has been declared in java.lang which your program can instantiate and throw. Unchecked Exception – It is inherited from RunTimeException and the RunTimeException is treated differently by JVM as the application code is not required to deal with them explicitly. Checked Exception – It is basically inherited from the Exception-class. The checked exceptions have to be handled by a client code either through a try-catch clause or should be thrown for the Super class to catch the same. Errors – Errors are popularly thrown for problems that deserve more seriousness. The problems usually include OutOfMemoryError or OOM that is not that easy to handle as well as manage. It is desirable to set aside some time in improving the Java skills as exception handling requires some special attention while designing of huge applications. Mention the key differences between Byte Streams and Character Streams Most of the Java programmers are very much familiar with the file operations. The list of file operations ranges from generating user reports,
sending of attachments through mails as well as filling out the data files from Java programs. Having an in-depth knowledge regarding file operations is very essential for Java developers who are dealing with Java interview questions. Byte Stream It is used for the reading as well as writing of binary data. The byte streams are used by programs for performing the byte input as well as byte output. Performing InputStreams operations as well as OutputStream operations means the presence of a loop that is capable of reading the input stream as well as writing the output stream one byte at a time. The buffered I/O streams could be also used for an overhead reduction if any. Character Streams Character streams work along with characters other than bytes. Unicode conventions are considered while storing the characters in Java. During such type of storage, characters usually become platform independent, program independent as well as language independent. Differentiate between the functions of Over-Riding and Over- Loading in Java? It is regarded as an important aspect in Object-Oriented Programming and the concepts as well as functionalities of Over-Riding and Over- Loading in Java has to be understood by every Java programmer. Over-Riding It is a function type that occurs in a class that is inherited by another class. The functions that are inherited from the base class are replaced by means of an Over-Ride function. However it is implemented in such a way that it is called even though an instance of its class is pretending to be different type through polymorphism. Over-Loading Whenever multiple methods with the same name along with different parameters are defined through an action, then it can be defined as overloading. However it is not at all related to overriding or polymorphism. Two mechanisms are usually used for overloading the functions in Java. The two mechanisms include ‘varying the number of arguments’ as well as ‘varying the data type’.
List the most common data types supported by Java This is one of the most popular and fundamental questions that are asked as part of Java interview questions. The list of eight primitive data types supported by Java is as follows. • Byte • Short • Int • Long • Float • Double • Char • Boolean What is the difference between Autoboxing and Unboxing? Autoboxing is defined as the automatic transformation of primitive types in to its object equivalents or wrapper types for making the compilation easier. The automatic transformation is performed by a Java Compiler. Unboxing can be defined as the automatic conversion of wrapper types in to its primitive equivalent. What are the key differences between interface and an abstract class? Abstract class is a partially implemented class by a programmer and it could be comprised of more than one abstract techniques. However interface is considered similar to an abstract class and it occupies the same namespaces as that of classes as well as abstract classes. Therefore it is not possible to define an interface with the same name as that of classes. The interface is more of a fully abstract class. Similarly abstract classes could be comprised of constants, members, defined methods and method stubs at the same time interfaces consists of only constants as well as method stubs. Methods of an interface should be defined as public where as the methods of an abstract class can be defined with any visibility. Whether garbage collection is an automatic process?
Yes, garbage collection is an automatic process and it could be requested by means of calling System.gc (). The immediate starting of Garbage Collection however cannot be guaranteed by JVM. Garbage collection is also considered to be the most relevant feature of Java and is also known as automatic memory management. This is because the unused variables or objects from the memory can be automatically removed by the JVM. The object in the memory cannot be freed directly by the user program whereas it is up to the garbage collector for automatically freeing the objects that are no longer referenced by a program. Define Memory Leak Whenever an unreferenced object that is not of any use is still not garbage collected and thereby still hangs around within the memory is often referred to as a memory leak. Define Instance Variables in Java The variables that are defined at a class level are known as Instance Variables. There is no need to initialize instance variables before making use of them as it could be automatically initialized towards their default values. Differentiate between Path and Class-path Both Path and Class-path are considered as operating system level environment variables. Classpath is used for specifying the location .class files whereas Path is used for defining where the executable files (.exe files) could be used within the system. Define Java Reflection Java Reflection is commonly used by programs that require the ability to examine as well as modify the runtime behaviour of applications that are powered by the Java Virtual Machine. When the interface or abstract class has to be used? To declare the methods as well as members a class must have, Interface is used. The methods listed by the interface have to be declared and implemented by users implementing the interface. Abstract class is used whenever users require a default implementation. Thus users can implement any number of interfaces as they require, however they would be able to extend only one class. Contract or Standard is defined by
Interfaces and those contracts or standard has to be followed during the implementation of the interface. What is the major difference between StringBuffer and StringBuilder? The most important difference between StringBuffer and StringBuilder is that the StringBuffer is synchronized where as the StringBuilder is unsynchronized. If the text could be changed and the accessibility is possible only from a single thread, then it is desirable to make use of StringBuilder as it is unsynchronized. At the same time if the text can change and it has to be accessed from multiple threads, then it is desirable to make use of StringBuffer as it is synchronous. Does Java support Pointers? Pointer can be defined as a reference handle towards a memory location. If the pointer is not handled properly, it can result in memory leaks as well as reliability issues. This is the main reason why the usage of pointers is not supported by Java.

Java questions and answers jan bask.net

  • 1.
    18 Most ImportantJava Interview Questions and Answers Define Synchronization in Java Synchronization in Java is a process in which the accessibility of shared resources are controlled by making use of multiple threads in such a way that only a single resource could be accessed at one time. A shared object could be modified using a single thread whereas another thread would be involved in the process of updating or using the value of the object in non-synchronized multi-threaded application. What is meant by JVM and why Java is considered as a Platform Independent Programming Language? JVM otherwise known as Java Virtual Machine is considered as an interpreter that is capable of accepting and executing the ‘Bytecode’. Because Java works on the basis of ‘compile once, run everywhere’, it has been termed as a platform independent language. The sequential steps that give an insight in to the platform independent characteristics are as follows. • Non-executable codes known as ‘Bytecode’ will be provided as an output by Java Compiler • Bytecode is nothing but a set of highly optimized computer instructions that could be executed by Java Virtual Machine
  • 2.
    • The programscould be executed easily across a wide range of platforms by means of translation in to bytecode. This is because all that is required is a JVM being tailored for that particular platform. • There could be a difference in configuration for different platforms in JVM and the same set of bytecodes could be understood easily. All this makes Java program a platform independent one. Differentiate between JDK and JRE This is a typical Java interview question that has confused most of the Java developers and it is important for Java developers to understand the difference between JDK and JRE which would help take their career to the next level. JDK stands for Java Development Kit which is a comprehensive package of software which can be used for creating Java based software. On the other hand, JRE stands for Java Runtime Environment. It is nothing but an implementation of Java Virtual Machine that is capable of executing Java programs. Each of the JDK would be comprised of one or more JRE’s in addition to different kind of development tools such as Java source compilers, debuggers, development libraries and bundling as well as deployment of tools etc. What is the difference between Constructors, Copy Constructor and Constructor Overloading in Java? For beginners, Constructors is considered to be basics of OOPS. Constructor – Creating an instance of a class remains the most important objective of having Constructors. Whenever an object of a class has been created, the constructors would be invoked. Some of the key characteristics of Java Constructors are as follows. • Constructors could be public, private and protected etc. • Default no-argument constructor could not be used whenever a constructor with arguments has been defined in a class. • Whenever the classes are being instantiated, they would be called only once. • Constructors would have the same name as that of the class. • Value is not returned and therefore they need not have to specify the keyword void.
  • 3.
    • Java helpsin creating a so called default no-argument constructor whenever a constructor for the class is not created. Constructor Overloading – Constructor overloading can be defined as the passing of various number as well as type of variables as arguments out of which most of them are regarded as private variables of the class. Copy Constructor – A type of constructor that is capable of constructing the object of class from another object of the same class is defined as Copy Constructor. The reference of its own class is considered as a parameter by the copy constructor. Define Java Exception Handling and differentiate between Unchecked Exception, Checked Exception and Errors? Exception is a method to point out the occurring of an abnormal condition in Java. Exceptions are considered as objects in Java and whenever an exception is thrown, the object is also thrown. It has to be understood that it is not possible to simply throw any objection as an exception. The throwable is considered as a base class for the entire family of classes that has been declared in java.lang which your program can instantiate and throw. Unchecked Exception – It is inherited from RunTimeException and the RunTimeException is treated differently by JVM as the application code is not required to deal with them explicitly. Checked Exception – It is basically inherited from the Exception-class. The checked exceptions have to be handled by a client code either through a try-catch clause or should be thrown for the Super class to catch the same. Errors – Errors are popularly thrown for problems that deserve more seriousness. The problems usually include OutOfMemoryError or OOM that is not that easy to handle as well as manage. It is desirable to set aside some time in improving the Java skills as exception handling requires some special attention while designing of huge applications. Mention the key differences between Byte Streams and Character Streams Most of the Java programmers are very much familiar with the file operations. The list of file operations ranges from generating user reports,
  • 4.
    sending of attachmentsthrough mails as well as filling out the data files from Java programs. Having an in-depth knowledge regarding file operations is very essential for Java developers who are dealing with Java interview questions. Byte Stream It is used for the reading as well as writing of binary data. The byte streams are used by programs for performing the byte input as well as byte output. Performing InputStreams operations as well as OutputStream operations means the presence of a loop that is capable of reading the input stream as well as writing the output stream one byte at a time. The buffered I/O streams could be also used for an overhead reduction if any. Character Streams Character streams work along with characters other than bytes. Unicode conventions are considered while storing the characters in Java. During such type of storage, characters usually become platform independent, program independent as well as language independent. Differentiate between the functions of Over-Riding and Over- Loading in Java? It is regarded as an important aspect in Object-Oriented Programming and the concepts as well as functionalities of Over-Riding and Over- Loading in Java has to be understood by every Java programmer. Over-Riding It is a function type that occurs in a class that is inherited by another class. The functions that are inherited from the base class are replaced by means of an Over-Ride function. However it is implemented in such a way that it is called even though an instance of its class is pretending to be different type through polymorphism. Over-Loading Whenever multiple methods with the same name along with different parameters are defined through an action, then it can be defined as overloading. However it is not at all related to overriding or polymorphism. Two mechanisms are usually used for overloading the functions in Java. The two mechanisms include ‘varying the number of arguments’ as well as ‘varying the data type’.
  • 5.
    List the mostcommon data types supported by Java This is one of the most popular and fundamental questions that are asked as part of Java interview questions. The list of eight primitive data types supported by Java is as follows. • Byte • Short • Int • Long • Float • Double • Char • Boolean What is the difference between Autoboxing and Unboxing? Autoboxing is defined as the automatic transformation of primitive types in to its object equivalents or wrapper types for making the compilation easier. The automatic transformation is performed by a Java Compiler. Unboxing can be defined as the automatic conversion of wrapper types in to its primitive equivalent. What are the key differences between interface and an abstract class? Abstract class is a partially implemented class by a programmer and it could be comprised of more than one abstract techniques. However interface is considered similar to an abstract class and it occupies the same namespaces as that of classes as well as abstract classes. Therefore it is not possible to define an interface with the same name as that of classes. The interface is more of a fully abstract class. Similarly abstract classes could be comprised of constants, members, defined methods and method stubs at the same time interfaces consists of only constants as well as method stubs. Methods of an interface should be defined as public where as the methods of an abstract class can be defined with any visibility. Whether garbage collection is an automatic process?
  • 6.
    Yes, garbage collectionis an automatic process and it could be requested by means of calling System.gc (). The immediate starting of Garbage Collection however cannot be guaranteed by JVM. Garbage collection is also considered to be the most relevant feature of Java and is also known as automatic memory management. This is because the unused variables or objects from the memory can be automatically removed by the JVM. The object in the memory cannot be freed directly by the user program whereas it is up to the garbage collector for automatically freeing the objects that are no longer referenced by a program. Define Memory Leak Whenever an unreferenced object that is not of any use is still not garbage collected and thereby still hangs around within the memory is often referred to as a memory leak. Define Instance Variables in Java The variables that are defined at a class level are known as Instance Variables. There is no need to initialize instance variables before making use of them as it could be automatically initialized towards their default values. Differentiate between Path and Class-path Both Path and Class-path are considered as operating system level environment variables. Classpath is used for specifying the location .class files whereas Path is used for defining where the executable files (.exe files) could be used within the system. Define Java Reflection Java Reflection is commonly used by programs that require the ability to examine as well as modify the runtime behaviour of applications that are powered by the Java Virtual Machine. When the interface or abstract class has to be used? To declare the methods as well as members a class must have, Interface is used. The methods listed by the interface have to be declared and implemented by users implementing the interface. Abstract class is used whenever users require a default implementation. Thus users can implement any number of interfaces as they require, however they would be able to extend only one class. Contract or Standard is defined by
  • 7.
    Interfaces and thosecontracts or standard has to be followed during the implementation of the interface. What is the major difference between StringBuffer and StringBuilder? The most important difference between StringBuffer and StringBuilder is that the StringBuffer is synchronized where as the StringBuilder is unsynchronized. If the text could be changed and the accessibility is possible only from a single thread, then it is desirable to make use of StringBuilder as it is unsynchronized. At the same time if the text can change and it has to be accessed from multiple threads, then it is desirable to make use of StringBuffer as it is synchronous. Does Java support Pointers? Pointer can be defined as a reference handle towards a memory location. If the pointer is not handled properly, it can result in memory leaks as well as reliability issues. This is the main reason why the usage of pointers is not supported by Java.