Core java interview questions

Share via:
Core java interview questions
Dear Readers,
In this article you will get to know the Core java interview questions

1: Name some OOPS Concepts in Java?

Answer: Java is based on Object Oriented Programming Concepts, following are some of the OOPS concepts implemented in java programming.

Abstraction

Abstraction is the concept of hiding the internal details and describing things in simple terms. For example, a method that adds two integers. The internal processing of the method is hidden from the outer world. There are many ways to achieve abstraction in object-oriented programming, such as encapsulation and inheritance.

A Java program is also a great example of abstraction. Here java takes care of converting simple statements to machine language and hides the inner implementation details from the outer world.

Encapsulation

Encapsulation is the technique used to implement abstraction in object-oriented programming. Encapsulation is used for access restriction to class members and methods.

Access modifier keywords are used for encapsulation in object-oriented programming. For example, encapsulation in java is achieved using private, protected, and public keywords.

Polymorphism

Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism – compile time polymorphism and runtime polymorphism.

Inheritance

Inheritance is the object-oriented programming concept where an object is based on another object. Inheritance is the mechanism of code reuse. The object that is getting inherited is called the superclass and the object that inherits the superclass is called a subclass.

We use extends keyword in java to implement inheritance. Below is a simple example of inheritance in java.

package com.journaldev.java.examples1;

Association

Association is the OOPS concept to define the relationship between objects. The association defines the multiplicity between objects. For example, Teacher and Student objects. There is a one-to-many relationship between a teacher and students. Similarly, a student can have a one-to-many relationship with teacher objects. However, both student and teacher objects are independent of each other.

Aggregation

Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is ownership. Whenever we have “HAS-A” relationship between objects and ownership then it is a case of aggregation.

Composition

The composition is a special case of aggregation. The composition is a more restrictive form of aggregation. When the contained object in “HAS-A” relationship cannot exist on its own, then it’s a case of composition. For example, House has-a Room. Here the room cannot exist without the house.

2.What do you mean by platform independence of Java?

Platform independence means that you can run the same Java Program in any Operating System. For example, you can write java program in Windows and run it in Mac OS.

3.What is JVM and is it platform independent?

Java Virtual Machine (JVM) is the heart of java programming language. JVM is responsible for converting byte code into machine-readable code. JVM is not platform-independent, that is why you have different JVM for different operating systems. We can customize JVM with Java Options, such as allocating minimum and maximum memory to JVM. It’s called virtual because it provides an interface that does not depend on the underlying OS.

4.What is the difference between JDK and JVM?

Java Development Kit (JDK) is for development purpose and JVM is a part of it to execute the java programs.

JDK provides all the tools, execu tables and binaries required to compile, debug, and execute a Java Program. The execution part is handled by JVM to provide machine independence.

5.What is the difference between JVM and JRE?

Java Runtime Environment (JRE) is the implementation of JVM. 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. If you want to execute any java program, you should have JRE installed.

6.Why Java doesn’t support multiple inheritance?

Java does not support multiple inheritance in classes because of “Diamond Problem”.

However multiple inheritances are supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So, there is no issue of the diamond problem with interfaces.

7.What is difference between path and class path variables?

PATH is an environment variable used by the operating system to locate the executable’s. That’s why when we install Java or want any executable to be found by OS, we need to add the directory location in the PATH variable.

Class path is specific to Java and used by java executable’s to locate class files. We can provide the class path location while running java application and it can be a directory, ZIP files, JAR files, etc.

9.What is the importance of main method in Java?

main () method is the entry point of any standalone java application. The syntax of main method is public static void main (String args[]).

Java’s main method is public and static so that Java runtime can access it without initializing the class. The input parameter is an array of String through which we can pass runtime arguments to the java program.

10.What is overloading and overriding in java?

When we have more than one method with the same name in a single class, but the arguments are different, then it is called method overloading.

The overriding concept comes in picture with inheritance when we have two methods with the same signature, one in the parent class and another in child class. We can use @Override annotation in the child class overridden method to make sure if the parent class method is changed, so as child class.

11.Can we have multiple public classes in a java source file?

We can’t have more than one public class in a single java source file. A single source file can have multiple classes that are not public.

 

Thank you for giving your valuable time to read the above information.
Follow us on 
Website  www.ktexperts.com
Facebook Page KTExperts Facebook
Linkedin Page : KT EXPERTS Linkedin

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment