Java Interview Questions

1. What is JAVA?

Answer:-Java is a widely used programming language expressly designed for use in the distributed environment of the internet. It is the most popular programming language for Android smartphone applications and is also among the most favored for the development of edge devices and the internet of things.


2. Major Differences between JAVA and C++?

Answer:- There are lot of differences, some of the major differences are:

1)  C++ is mainly used for system programming.Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications.
2)  C++ supports multiple inheritance.Java doesn't support multiple inheritance through class. It can be achieved by interfaces in java.
3)  C++ supports pointers. You can write pointer program in C++. Java supports pointer internally. But you can't write the pointer program in java. It means java has restricted pointer support in java.
4)  C++ is platform-dependent.Java is platform-independent.
5)  C++ uses compiler only. C++ is compiled and run using the compiler which converts source code into machine code so, C++ is platform dependent. Java uses compiler and interpreter both. Java source code is converted into bytecode at compilation time. The interpreter executes this bytecode at runtime and produces output. Java is interpreted that is why it is platform independent.
6)  C++ supports structures and unions. Java doesn't support structures and unions.
7)  C++ doesn't support documentation comment. Java supports documentation comment (/** ... */) to create documentation for java source code.


3. What are the features in JAVA?

Answer:- Java is a general-purpose programming language developed with the aim to bring portability and a higher level of security. Other than these two main java features, there are many other features of Java that make it such a unique and popular language.

List of features of Java programming languages —
1)  Simple
2)  Object-Oriented
3)  Portable
4)  Platform Independent
5)  Secured
6)  Robust
7)  Architecture Neutral
8)  Dynamic
9)  High Performance
10)  Multi-Threaded
11)  Distributed


4. What do you understand by Java virtual machine?

Answer:- JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).

1)  A specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies.
2)  An implementation Its implementation is known as JRE (Java Runtime Environment).
3)  Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is created.


5. What is the difference between JDK, JRE, and JVM?

Answer:- Following are the differences between JDK, JRE, and JVM:

  JDK :-Java Development Kit is the core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program. JDK is a platform-specific software and that’s why we have separate installers for Windows, Mac, and Unix systems.
  JVM :-JVM is the heart of Java programming language. When we run a program, JVM is responsible for converting Byte code to the machine specific code. JVM is also platform dependent and provides core java functions like memory management, garbage collection, security etc.
  JRE :-JRE is the implementation of JVM, it provides a platform to execute java programs. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn’t contain any development tools like java compiler, debugger etc.


6.How many types of memory areas are allocated by JVM?

Answer:- There are six main types of memory areas that are allocated by JVM. These include:

1.  Class loader – a subsystem of JVM that is used to load class files.
2.  Class (Method) Area – stores per-class structures such as the runtime constant pool, field and method data, the code for methods.
3.  Heap - is the runtime data area in which objects are allocated.
4.  Stack - stores frames and holds local variables and partial results. Participates in method invocation and return.
5.  Program Counter (PC) Register - contains the address of the JVM instruction that is currently being executed.
6.  Native Method Stack - contains all native methods used in the application.


7. What is JIT compiler?

Answer:- It is used to improve performance. JIT compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.


8. What are the main differences between the Java platform and other platforms?

Answer:- The three major differences between Java platform and most other platforms are:

1)  It is a software-based platform while others can be hardware based or hardware software platform.
2) Java supports WORA (Write once run anywhere),which means it is platform independent, but many of other platforms does not provide such feature.
3)  The Java API enable users to write java program, which are converted to byte code by interpreter and then into machine language by complier, whereas other platform provide either complier or interpreter.


9. What is classloader?

Answer:- We know that Java Program runs on Java Virtual Machine (JVM). When we compile a Java Class, it transforms it in the form of bytecode that is platform and machine independent compiled program and stores it as a .class file. After that when we try to use a Class, Java ClassLoader loads that class into memory.
1)  Bootstrap Class Loader :It loads JDK internal classes, typically loads rt.jar and other core classes for example java.lang.* package classes.
2)  Extensions Class Loader :It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
3)  System Class Loader :It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options.


10. What is the default value of the local variables?

Answer:- The local variables are not initialized to any default value, neither primitives nor object references.


11. What are the various access specifiers in Java?

Answer:- You must have seen public, private and protected keywords while practising java programs, these are called access modifiers. An access modifier restricts the access of a class, constructor, data member and method in another class. In java we have four access modifiers:

1)  Default:When we do not mention any access modifier, it is called default access modifier. The scope of this modifier is limited to the package only. This means that if we have a class with the default access modifier in a package, only those classes that are in this package can access this class. No other class outside this package can access this class.
2) Private:Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.Private access modifier is the most restrictive access level. Class and interfaces cannot be private.Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.
3) Public:A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.
4) Protected:Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.


12. Can we Overload or Override static methods in java ?

Answer:-

Can we overload static methods?:- The answer is ‘Yes’. We can have two ore more static methods with same name, but differences in input parameters.
Can we Override static methods in java? :- We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’. Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by their name) are looked up statically (i.e. at compile-time).


13. What are the advantages of Packages in Java?

Answer:-There are various advantages of defining packages in Java.
1) Packages avoid the name clashes.
2) The Package provides easier access control.
3) We can also have the hidden classes that are not visible outside and used by the package.
4) It is easier to locate the related classes.


14. What is object-oriented paradigm?

Answer:- It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm.

1)  Follows the bottom-up approach in program design.
2)  Focus on data with methods to operate upon the objects data
3) Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
4) Implements the real-time approach like inheritance, abstraction, etc.
5) The examples of the object-oriented paradigm are C++, Simula, Smalltalk, Python, C#, etc.


15. What is the constructor?

Answer:-A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.


16. Does constructor return any value?

Answer:-yes, The constructor implicitly returns the current instance of the class.
(You can't use an explicit return type with the constructor)


17. How many types of constructors are used in Java?

Answer:- Based on the parameters passed in the constructors, there are two types of constructors in Java.

1)  Default Constructor: A Constructor with no parameters is called as Default Constructor or no-arg constructor. In the below code we have created the no-arg constructor which gets called during the time of object creation.
Example: (Abc a = new Abc())
2) Parameterized Constructor:A Constructor which has parameters in it called as Parameterized Constructors, the Parameterized constructor is used to assign different values for the different objects. In the below example we have a parameterized constructor for the car class which set the value for the parameter “Color”
Example: Abc(String Color)


18. What is the static block?

Answer:- Static block is a set of statements, which will be executed by the JVM before execution of main method.
At the time of class loading if we want to perform any activity we have to define that activity inside static block because this block execute at the time of class loading.


19. What is the static method?

Answer:-

1) A static method belongs to the class rather than the object.
2) There is no need to create the object to call the static methods.
3) A static method can access and change the value of the static variable.


20. What are the main uses of this keyword?

Answer:-There are the following uses of this keyword.

1) this can be used to refer to the current class instance variable.
2)  this can be used to invoke current class method (implicitly)
3)  this() can be used to invoke the current class constructor.
4) this can be passed as an argument in the method call.
5)  this can be passed as an argument in the constructor call.
6)  this can be used to return the current class instance from the method.


21. Which class is the superclass for all the classes?

Answer:-The object class is the superclass of all other classes in Java.


22. What is aggregation?

Answer:-Aggregation in Java is a relationship between two classes that is best described as a "has-a" and "whole/part" relationship. It is a more specialized version of the association relationship. The aggregate class contains a reference to another class and is said to have ownership of that class. Each class referenced is considered to be part-of the aggregate class.


23. What is composition?

Answer:-Composition in java is the design technique to implement has-a relationship in classes. We can use java inheritance or Object composition in java for code reuse.Java composition is achieved by using instance variables that refers to other objects.
For example:- A Person has a Job.


24. Why does Java not support pointers?

Answer:-The pointer is a variable that refers to the memory address. They are not used in Java because they are unsafe(unsecured) and complex to understand.


25. What are the main uses of the super keyword?

Answer:-The main uses of the super keyword is :-

1) super.< variable_name > refers to the variable of variable of parent class.
2) super() invokes the constructor of immediate parent class.
3) super.< method_name > refers to the method of parent class.


26. What is the difference between the final method and abstract method?

Answer:-Final method is a method that is marked as final, i.e. it cannot be overridden anymore. Just like final class cannot be inherited anymore.
Abstract method, on the other hand, is an empty method that is ought to be overridden by the inherited class. Without overriding, you will quickly get compilation error.


27. What is the difference between compile-time polymorphism and runtime polymorphism?

Answer:-There are the following differences between compile-time polymorphism and runtime polymorphism.

compile-time polymorphism:-
1)In compile-time polymorphism, call to a method is resolved at compile-time.
2) It is also known as static binding, early binding, or overloading.
3) Overloading is a way to achieve compile-time polymorphism in which, we can define multiple methods or constructors with different signatures.
4) It provides fast execution because the type of an object is determined at compile-time.

Run-time polymorphism:-
1)In runtime polymorphism, call to an overridden method is resolved at runtime.
2)It is also known as dynamic binding, late binding, overriding, or dynamic method dispatch.
3) Overriding is a way to achieve runtime polymorphism in which, we can redefine some particular method or variable in the derived class. By using overriding, we can give some specific implementation to the base class properties in the derived class.
4) It provides slower execution as compare to compile-time because the type of an object is determined at run-time.


28. What is the difference between static binding and dynamic binding?

Answer:-There are the following differences between static binding and dynamic binding.

Static binding:-
1) Static binding in Java occurs during Compile time.
2) private, final and static methods and variables uses static binding and bonded by compiler.
3) Static binding uses Type(Class in Java) information for binding.


Dynamic binding:-
1) Dynamic binding occurs during Runtime.
2)virtual methods are bonded during runtime based upon runtime object.
3) Dynamic binding uses Object to resolve binding.


29. What is the abstraction?

Answer:-Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user.
Example: A car is viewed as a car rather than its individual components.


30. What is the abstract class?

Answer:-Abstract class in Java is similar to interface except that it can contain default method implementation. An abstract class can have an abstract method without body and it can have methods with implementation also.
Abstract class in Java Important Points
1) abstract keyword is used to create an abstract class in java.
2) Abstract class in java can’t be instantiated.
3) We can use abstract keyword to create an abstract method, an abstract method doesn’t have body.
4) If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.




Also check :


Discussion


* You must be logged in to add comment.