Core Java Interview Questions and Answers

Here are 50+ commonly asked core Java interview questions and answers which will definitely help you out to crack one of the toughest interviews.

1. What is JVM?
JVM stands for Java Virtual Machine and it makes Java a Portable and a Platform Independent Language. JVM is called Virtual as it is an Abstract System that executes Compiled Java Programs. JVM executes the Java ByteCode File or .class file.

2. Enlist Different Packages of Swings.
The different Packages of Swings are as follows:
1. javax.swing: Provides a set of Light-weight Java Language Components.
2. javax.swing.plaf: Provides One interface and Many abstract classes to provide look-and-feel capabilities.
3. javax.swing.plaf.multi: Provides User Interface Objects built for Multi Platform Look and Feel.
4. javax.swing.event: Provides Events Fired by Swing Components.
5. javax.swing.tree: Provides Classes and Interfaces for dealing with javax.awt.swing.JTree
6. javax.swing.undo: Provides support for Undo/Redo Capabilities in Text Editor Applications.

3. Enlist the difference between C++ and Java.
1. Java is a True Object Oriented Language whereas C++ supports both Object Oriented and Procedure Oriented paradigm.
2. Java does not support Operator Overloading whereas C++ supports it.
3. C++ contains Header Files whereas Java doesn’t contain header files.
4. Java supports Multi-Threading whereas C++ doesn’t support Multi-Threading.
5. Java does not support Pointers whereas C++ makes use of Pointers.
6. Java doesn’t supports Multiple Inheritance whereas C++ supports it.

4. Enlist Versions of Java.
1. Java Enterprise Edition(J2EE): Used to Develop Server-Side applications such as Java Servlets and Java Server Pages.
2. Java Micro Edition(J2ME): Used to develop applications for Mobile Devices such as Cell Phones, Tablets.
3. Java Standard Edition(J2SE): Develop Client Side Applets or Standalone Applications.

5. What is Object Oriented Programming Approach?
Object Oriented Programming is an approach that provides a way of modularizing programs by creating partitioned memory area for data as well as functions that can be used as templates for creating copies of such modules on demand. OOP allows decomposition of a problem into a number of entities called Objects and then builds data and functions around these objects.

6. Explain Vector Class in Java.
A Vector Class can be used to achieve a Generic Dynamic Array known as Vectors. Such Vector Class helps to store Objects of any quantity and Data Type. These Objects accessed into vector Class need not contain similar elements. Arrays are impemented as vectors. Vectors therefore, can be used to store a list of Objects that may differ in size.

7. What is jdb?
It is the Java Debugger. Jdb basically helps you out to find Java Bugs. It is a Command Line Debugger Tool for Java Classes. It provides inspection and debugging of Local and Remote Java Virtual Machine.

8. What is AppletViewer?
AppletViewer is a Java command line program to execute Java Applets. Its included in Java SDK. Java applet viewer helps to create Dynamic Applets as well as facilitates to view Java Applets.

9. What is an Object?
An Object is a Software Bundle of related State and Behaviour. Object is basically an instant of Class to which it belongs to. It is therefore possible to represent real world objects using Software Objects.

10. What is Inheritance?
Inheritance in Java is basically used by an Object to acquire the properties of another Object. Classes can therefore now be defined in terms of another previously defined Classes which thereby inherits the properties of previous Classes.

11. Enlist the Types of Errors in Java.
The Types of Errors in Java are as follows:

Compile Time Errors
This includes primarily Syntax Errors indicated by Java Compiler at Compile Time.

Run Time Errors
This includes primarily the Logical Errors that are not catched by the Java Compiler by may result into Abnormal Termination of the Program. It includes Divide by Zero Error, Opening a File that doesn’t exist, etc.

12. What is InstanceOf Operator?
This operator is used to Reference Objects Variables. It tests if the first operand is an instance of its second. Its written as:

(Object Reference Variable) instanceOf (Class/ Interface Type)

13. Enlist Types of Constructors.
Types of Constructors are:

1. Default Constructor: This is a constructor without any parameters. A Default constructor is automatically created if you do not define any constructor for a Class.

2. Parameterized Constructor: A Constructor invoked with parameters is called a Parameterized constructor.

3. Overloading Constructor: Java allows Polymorphism i.e., a Method Overloading can be achieved by defining Two or more methods with the same name in a class.

14. Explain Static Methods.
Static methods in Java are used to create Methods or Functions that exists independent of Instances created for a Class. These methods doesn’t use any instance variables of any object of the class that they are defined in. Static methods can only access static data. They cannot call any other methods except Static Methods. Static Methods can not refer to This or Super Keywords.

15. Explain this Keyword.
There are times when a Function needs to refer to an Object that is invoked by it. This keyword facilitates reference to an Object within that particular methods. This keyword can be referenced to an object of current class type.

16. What is jar?
Jar stands for Java Archive. It is a utility that combines several files into a Single jar file or expand a jar file. It is basically an extended version of zip file.

17. Enlist Types of Inheritance.
Types of Inheritance supported in Java are as follows:
1. Single Inheritance
2. Hierarchical Inheritance
3. Multi-Level Inheritance
4. Multiple Inheritance

18. What is awt in Java?
AWT stands for Abstract Window Toolkit. It basically contains Widgets related to graphical User Interface designing that can be positioned using Layout Manager. AWT is platform independent. It provides Components, Containers and Layout Managers.

19. Explain Subclass constructor in Java.
A Subclass constructor is used to construct the instance variables of the Sub Class as well as the Super Class. A Sub Class can explicitly make a call to a Constructor of its very next Super Class. The Sub Class therefore, uses Super Keyword to invoke the Constructor methods of the Sub Class which results in execution of relevant constructor from Super Class based on the arguments passed to it.

20. What is the use of Final Keyword?
The value of a variable that has been declared as final cannot be changed later in the program. Java allows to declare a variable to be final irrespective of the Scope of the variable.
Final int variable = 15;
A Final local variable that has been declared but not initialised with any value is called as a Black Final.

21. Enlist come common Java Built-In API Packages.
The commonly used Packages in Java are as follows:
Java.lang: It includes support for Threads, Strings, Exceptions and much more.
java.util: Support for Date, Hash Tables, Vectors and others. Input and Output support for Classes. It provides support for Networking
Java.applet: Support for creation of Applets.

22. What is Polymorphism?
Polymorphism is used to allow routines or methods to execute different behaviour in different instances. An operator or a Function can be made to react differently in different positions. In C, we have Asterisk * that by default makes use of Polymorphism. It works as a Multiply Operator as well as a Pointer or a De-Reference Operator automatically.

23. Explain InputStream Class in Java.
An InputStream class is used to read 8-bit bytes that includes a Super Class known as InputStream and it also includes a number of Sub Classes that support Input related methods. It can perform actions like reading bytes, closing streams, marking positions in streams, skipping ahead in a stream and much more.

24. Explain Final Classes in Java.
A Class is declared as Final to avoid it from being Sub Classed. It therefore, helps to prevent the application of inheritance on that particular class. A Final Class is therefore a class that cannot be sub classed. If you try to extend the Classes from a Class declared as Final, a Compile Time Error will be generated.

25. What is javah?
javah is a utility to generate Source Files and Header Files required to implement Native Methods. These Header files are thus used for referencing an Object’s Instance variables from Native Source Codes.

26. Explain Methods Overloading in Java?
It is an application of Polymorphism. An overloaded method is the method that have the same name but number of arguments many differ or even the datatype may differ in some cases. Two methods that have the same name but different arguments or parameters within the same Class, it is known as Method overloading.

27. Enlist Features of Swing.
1. Customization and Other Features: Change Border, Align Text, Custom Cursors, Dockable Toolbars, etc.
2.Large Set of Controls: Text Areas To display HTML or RTF, Trees, Tabbed Panes, Toolbars, Image Buttons
3. Look and Feel: It can modify the look and feel of the GUI at Runtime and you can even customize Design.
4. Lightweight: It is not built on Native Windows Based System.

28. What is JDK?
JDK stands for Software Development Kit and it includes Tools such as JVM Compiler, Debugger, Interpreter and few others to develop Java Applets and Applications.

29. Enlist Features of Java.
1. Distributed
2. Interpreted
3. Multi-Threaded
4. Platform Independent
5. Object Oriented
6. Portable

30. Explain Method Overriding in Java?
If a Class inherits a Method from its Upper Class, there are chances that it can make the Method Over Riding successful. However, that method must not be declared with final keyword. Function overriding basically means to override the functionality and capability of any existing method with a new type of operation.

31. What is StringBuffer Class used for?
A StringBuffer Class is included in String Class. Normal String creates Strings of Fixed Length characters whereas StringBuffer Class helps in creating Strings of Flexible Length that has the capability to modify according to Content and Length. Hence, StringBuffer Class is memory efficient compared to String Class.

32. Enlist Applet Life Cycle Methods.
The common methods used in an Applet Life Cycle are:
1. init(): This method is invoked to initialize an applet.
2. start(): This method is basically called after init() and is basically used to load the Applet into the System.
3. stop(): This method is invoked by the Web Browser itself and indicates that the applet is not in use now.
4. destroy(): This method is invoked when the Browser is closed.

33. What are Wrapper Classes?
Wrappers are Objects that are used to add them to a collection because Primitive Datatypes cannot be added directly. The Java.lang package provides standard library classes that are closely related to Primitive Datatypes and these are known as Wrappers. Wrappers are Byte, Integer, Short, Long, Character, Boolean, Double and Float.

34. What is a Constructor?
A Constructor is a special member function whose primary task is to initialize objects of its class. The constructor is invoked whenever an object of its class with which it is associated to is created. It is named as constructor because it constructs the values of class data members. It is special because its name is the same as its class name.

35. What is New Operator?
A New Operator is used to create a New Object or a New Array.
Integer variableInteger = new Integer(25);

36. Enlist Visibility Modifiers in Java.
The visibility modifiers in Java are as follows:
1. Public: Visible from methods in any class in the same program.
2. Private: Accessible only from Functions inside the class. Unable to access outside the Class.
3. Protected: Accessible from functions in any class in the same package and from any subclass anywhere in the program.

37. What is javap?
javap is a command that works as a Disassembler. It basically disassembles a Class file and prints its Output to Standard Output. It prints Protected, Public Fields and Methods and the Packages of the Classes passed as arguments to it.

38. What is an Interface in Java?
An Interface in Java is basically a collection of Abstract Methods (Functions). The functions in an Interface are by default Public. An Interface contains behaviors that a Class implements. Once an interface is defined, any number of Classes can be used to implement that particular Interface. An Interface is implicitly an Abstract type.

39. What are Abstract Classes in Java?
Abstract Classes in Java are used to declare common characteristics of subclass. An abstract class cannot be instantiated. However, it can be sub-classed. An Abstract Class helps to provide a template or Design concrete subcalsses. If a Class has any abstract methods declared within it, the entire Class must be declared as Abstract.
Syntax: Abstract type-name(argument);

40. What are Packages in Java?
A Package is a combination of Related Types that offers Access Protection and Name Space management. It is basically a container for classes which are used to separate the Name Spaces. In Java, packages are of two types viz. API Packages and Built-in Packages. Packages are used to Control Access, prevent Naming Conflicts, Make use of Class, Interface easier and to Search or Locate data.

41. Explain Finalize() method in Java.
The finalize() function or method should explicitly define the tasks to be performed. It is therefore possible to define a method that will be invoked before an Object’s final destruction by the garbage collection mechanism. Protected keywords prevents the access of finalize() code from outside class. Java does not support destructor.

42. Explain Exception Handling in Java.
An Exception is basically an Error that occurs at Run Time. Java allows to rectify the Exception at Run Time. Therefore, the process of solving an Exception at Run Time is called as Exception Handling. It thereby provides a means to detect and report Exceptional Circumstances so that required measures can be taken.

43. What are Streams in Java?
A Stream in Java is an Abstraction that either Consumes or Produces Information. A Stream is linked to a physical device such as Input/Output. There are primarily following Type of Streams in Java viz. Byte Stream, Input Stream, Output Stream and Character Stream. An Input Stream is used to extract data from the Source File and sends it to the program whereas an Output file extracts data from the program and sends it to the destination file.

44. What is javac?
javac is the Java Compiler that helps to Translate Java Source Code into Java Byte Code or the .class file. This Bytecode can then be Interpreted and executed by Java Interpreter and converts into Machine Specific Binary Code.

45. Explain OutputStream Class in Java.
This class is derived from the base class OutputStream which is an Abstract Class and therefore it is not possible to instantiate it. Output Stream Classes can be used to Write Bytes, Close the Stream and Flush Streams.

46. Explain ByteStream Class and CharacterSteam Class in Java.
ByteStreams Classes helps to provide a convenient way to handle Output and Input of Bytes. It is useful for Reading and Writing Binary data. ByteStreams in Java are unidirectional and hence they can transmit information in one direction only. Characters Streams are designed for handling the Input and Output of Characters into Files or Input/Output.

47. What are Java Applets?
An Applet is a Java program that can be executed in a Web browser. They are primarily developed for Internet purpose. It can therefore be embedded into HTML Web pages thereby making the website more dynamic. Applet runs remotely on the client Web Browser and cannot access any system resources on the Local Computer.

48. Enlist AWT Controls.
The AWT controls in Java are as follows:
1. Buttons
2. Canvas
3. Checkbox
4. List
5. Label

49. What are event listeners in Java?
The Events that are invoked by the Graphical User Interface components are managed by Listener Class. It is basically an Interface. It is from the Package java.awt.event. It therefore handles the events that invoked on the User Interface. There are multiple Listener Interfaces which are WindowListener, ActionListener,MouseListener, ItemListener, ComponentListener, ContainerListener and much more.

50. What is javadoc?
Javadoc is a tool to generate HTML Documents from Java Source Code Files. It therefore provides Online documentation of Packages and Classes. All javadoc tags start with @ character symbol. It basically parses Source Files for Class Methods and Comments.

51. What is a Class?
A Class is basically a template for defining behavior and states of the Objects of that particular Class. An entire set of data and code of an object can be made user-defined data type using a class. A Class is a Prototype that define Variables and Methods common to all the Objects of that particular Class. Objects are variables of Class type. Once a Class has been defined, we can create numerous Objects of its type.

52. What are the applications of Object Oriented Programming?
The areas of applications for OOP includes:
1. Artificial Intelligence Systems
2. Simulation and Modelling
3. Real-Time Systems
4. Neural networks and Parallel Programming
5. Object Oriented Databases

53. What is Encapsulation?
Encapsulation is basically Wrapping up of data and functions into a single unit called as a Class. This protection of data from direct access by the program is called Data Hiding. Hence, The data is inaccessible to the external parties, only the functions wrapped inside class can access it.

54. What is JRE?
JRE helps in execution of Java Programs. It comprises of JVM, Deployment Technologies, Run-Time Class Libraries and User Interface Toolkits.

55. What is Swing in Java?
Swing is basically a set of customizable graphical components whose look and feel can be determined at run time. Swing is an extension library to the AWT that includes New and Enhanced components that co-ordinates functionality of Graphical User Interface. Swing is a part of JFC.

56. Enlist Types of Java Applets.
The Types of Applets in Java are as follows:
1. Local Applet: It is an applet that is locally developed in a computer system and is stored on that system itself. Hence, the web page doesn’t need any internet connection to load that web applet within the same system.

2. Remote Applet: It is an applet that is developed by a Third-Party and stored on another computer. It can accessed by millions of people worldwide through their own computers. It therefore needs access to internet.

So this was the list of some important core Java interview questions and answers. If you found any information incorrect or missing in above list then please mention it by commenting below.

4 thoughts on “Core Java Interview Questions and Answers”

Leave a Comment

Your email address will not be published. Required fields are marked *