Core Java Interview Questions￼
- May 9, 2022
- Posted by: Laraonline2020
- Category: Interview Question and Answers
Top Core Java Interview Questions and Answers
1. What are the principle concepts of OOPS?
There are four principle concepts upon which object-oriented design and programming rest. They are:
- (i.e. easily remembered as A-PIE).
2. What is Abstraction?
Abstraction refers to the act of representing essential features without including the background details or explanations.
3. What is Encapsulation?
Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.
4. What is the difference between Abstraction and Encapsulation?
Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing its inside view, where the behavior of the abstraction is implemented.
Abstraction solves the problem on the design side while Encapsulation is the Implementation.
Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer’s needs.
5. What is Inheritance?
Inheritance is the process by which objects of one class acquire the properties of objects of another class. A class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Inheritance is done by using the keyword extends.
The two most common reasons to use inheritance are:
- To promote code reuse
- To use polymorphism
Core Java Interview Questions for Freshers
6. What is Polymorphism?
Polymorphism is briefly described as “one interface, many implementations.” Polymorphism usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
7. How does Java implement polymorphism?
Inheritance, Overloading, and Overriding are used to achieve Polymorphism in Java. Polymorphism manifests itself in Java in the form of multiple methods having the same name.
In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods). In other cases, multiple methods have the same name, same return type, and the same formal argument list (overridden methods).
8. Explain the different forms of Polymorphism.
There are two types of polymorphism, one is Compile time polymorphism and the other is run time polymorphism. Compile-time polymorphism is method overloading. Runtime time polymorphism is done using inheritance and interface.
Note: From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java:
- Method overloading
- Method overriding through inheritance
- Method overriding through the Java interface
9. What is runtime polymorphism or dynamic method dispatch?
In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
10. What is Dynamic Binding?
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
11. What is method overloading?
Method Overloading means having two or more methods with the same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.
- Overloaded methods MUST change the argument list.
- Overloaded methods CAN change the return type.
- Overloaded methods CAN change the access modifier.
- Overloaded methods CAN declare new or broader checked exceptions.
- A method can be overloaded in the same class or in a subclass.
12. What is method overriding?
Method overriding occurs when a subclass declares a method that has the same type of arguments as a method declared by one of its superclasses. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.
Note: The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can’t override a method marked public and make it protected). You cannot override a method marked final. You cannot override a method marked Static.
13. What is the difference between method overloading and method overriding?
- Arguments – Must Change
- Return type: Can Change
- Exceptions – Can Change
- Access – Can Change
- Invocation – Reference type determines which overloaded version is selected. Happens at compile time.
- Arguments – Must not Change
- Return type: Can’t change except for covariant returns
- Exceptions – Can reduce or eliminate. Must not throw new or broader checked exceptions
- Access – Must not make more restrictive (can be less restrictive)
- Invocation – Object type determines which method is selected. Happens at runtime
14. List the features of Java Programming language.
There are the following features in Java Programming Language.
- Simple: Java is easy to learn. The syntax of Java is based on C++ which makes easier to write the program in it.
- Object-Oriented: Java follows the object-oriented paradigm which allows us to maintain our code as the combination of different type of objects that incorporates both data and behavior.
- Portable: Java supports read-once-write-anywhere approach. We can execute the Java program on every machine. Java program (.java) is converted to bytecode (.class) which can be easily run on every machine.
- Platform Independent: Java is a platform independent programming language. It is different from other programming languages like C and C++ which needs a platform to be executed. Java comes with its platform on which its code is executed. Java doesn’t depend upon the operating system to be executed.
- Secured: Java is secured because it doesn’t use explicit pointers. Java also provides the concept of ByteCode and Exception handling which makes it more secured.
- Robust: Java is a strong programming language as it uses strong memory management. The concepts like Automatic garbage collection, Exception handling, etc. make it more robust.
- Architecture Neutral: Java is architectural neutral as it is not dependent on the architecture. In C, the size of data types may vary according to the architecture (32 bit or 64 bit) which doesn’t exist in Java.
- Interpreted: Java uses the Just-in-time (JIT) interpreter along with the compiler for the program execution.
- High Performance: Java is faster than other traditional interpreted programming languages because Java bytecode is “close” to native code. It is still a little bit slower than a compiled language (e.g., C++).
- Multithreaded: We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn’t occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc.
- Distributed: Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet.
- Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.
15. What are the main differences between the Java platform and other platforms?
There are the following differences between the Java platform and other platforms.
- Java is the software-based platform whereas other platforms may be the hardware platforms or software-based platforms.
- Java is executed on the top of other hardware platforms whereas other platforms can only have the hardware components.
16. What gives Java its ‘write once and run anywhere’ nature?
The bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform specific and can be executed on any computer.
17. What are the differences between the constructors and methods?
There are many differences between constructors and methods. They are given below.
|Java Constructor||Java Method|
|A constructor is used to initialize the state of an object.||A method is used to expose the behavior of an object.|
|A constructor must not have a return type.||A method must have a return type.|
|The constructor is invoked implicitly.||The method is invoked explicitly.|
|The Java compiler provides a default constructor if you don’t have any constructor in a class.||The method is not provided by the compiler in any case.|
|The constructor name must be same as the class name.||The method name may or may not be same as class name.|
18. What are the restrictions that are applied to the Java static methods?
Two main restrictions are applied to the static methods.
- The static method can not use non-static data member or call the non-static method directly.
- this and super cannot be used in static context as they are non-static.
19. Why does Java not support pointers?
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.
20. Difference between method Overloading and Overriding.
|Method Overloading||Method Overriding|
|1) Method overloading increases the readability of the program.||Method overriding provides the specific implementation of the method that is already provided by its superclass.|
|2) Method overloading occurs within the class.||Method overriding occurs in two classes that have IS-A relationship between them.|
|3) In this case, the parameters must be different.||In this case, the parameters must be the same.|
Hope you liked Laraonlinetraining Java Interview Questions