Core Java Interview Questions Set -1

Q1. What is Java?

Java is a popular programming language, created in 1995. Java is a high-level, object-oriented, robust, secure, platform-independent, high-performance, Multithreaded, and portable programming language. It can also be known as the platform as it provides its own JRE and API.

Q2. List the features of the 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 it easier to write the program in it.

Object-Oriented: Java follows the object-oriented paradigm which allows us to maintain our code as a combination of different types of objects that incorporates both data and behavior.

Portable: Java supports the 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. Java comes with a 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 secure.

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 powerful.

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. 

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 the dynamic loading of classes. It means classes are loaded on demand.

Q3. What do you understand by JVM?

JVM is an acronym for Java Virtual Machine; it is an abstract machine that provides the runtime environment in which Java bytecode can be executed. It is a specification that specifies the working of Java Virtual Machine. Its implementation has been provided by Oracle and other companies. Its implementation is known as JRE.

Q4. What do you understand by JRE?

JRE stands for Java Runtime Environment. It is the implementation of JVM. The Java Runtime Environment is a set of software tools that are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

Q5. What do you understand by JDK?

JDK is an acronym for Java Development Kit. It is a software development environment that is used to develop Java applications and applets. It physically exists. It contains JRE + development tools.

Q6. What is a JIT compiler?

Just-In-Time(JIT) compiler: 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.

Q7. What is the platform?

A platform is the hardware or software environment in which a piece of software is executed. There are two types of platforms, software-based and hardware-based. Java provides a software-based platform.

Q8. What is a classloader?

Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java.

Bootstrap ClassLoader: This is the first classloader which is the superclass of the Extension classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes, etc.

Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.

System/Application ClassLoader: This is the child classloader of the Extension classloader. It loads the class files from the classpath. By default, the classpath is set to the current directory. You can change the classpath using "-cp" or "-classpath" switch. It is also known as the Application classloader.

Q9. Is Empty .java file name a valid source file name?

Yes, Java allows us to save our java file by .java only, we need to compile it by javac .java and run by java classname.

Q10. If I don't provide any arguments on the command line, then what will the value stored in the String array passed into the main() method, empty or NULL?

It is empty, but not null.

Q11. What if I write static public void instead of the public static void?

The program compiles and runs correctly because the order of specifiers doesn't matter in Java.

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

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

Q13. What are the various access specifiers in Java?

1) Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.

2) Protected: Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.

3) Default: Default is accessible within the package only. By default, all the classes, methods, and variables are of default scope.

4) Private: The private class, methods, or variables defined as private can be accessed within the class only.

Q14. What is the purpose of static methods and variables?

The methods or variables defined as static are shared among all the objects of the class. The static is the part of the class and not of the object. The static variables are stored in the class area, and we do not need to create an object to access such variables. Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class.

Q15. What are the advantages of Packages in Java?

Packages avoid name clashes.

The Package provides easier access control.

We can also have the hidden classes that are not visible outside and used by the package.

It is easier to locate the related classes.

Q16. Name some important features of the Java 14 release?

Java 14 was released on March 17, 2020. It is a Non-LTS version. Some of the developer-specific Java 14 Features are:

Switch Expressions – JEP 361

Pattern matching improvements of instanceof operator – Preview Feature, JEP 305

Helpful NullPointerException Messages – JEP 358

Text Blocks – Second Preview, JEP 368

Records – data class, preview feature, JEP 359.

Q17. Name some important features of the Java 13 release?

Java 13 was launched on Sept 17, 2019. It is a Non-LTS version. Some of the developer-specific Java 13 Features are:

Text Blocks – Preview Feature, JEP 355

Switch Expressions – Preview Feature, JEP 354

Reimplementation of Java Sockets API – JEP 353

Dynamic CDS Archive – JEP 350

FileSystems.newFileSystem() Method

DOM and SAX Factories with Namespace support

Unicode 12.1 support

ZGC improvements to return Unused Memory – JEP 351

Q18. Name some important features of the Java 12 release.

Java 12 was launched on March 19, 2019. It is a Non-LTS version. Some of the Java 12 Features are:

JVM Changes – JEP 189, JEP 346, JEP 344, and JEP 230.

Switch Expressions

File mismatch() Method

Compact Number Formatting

Teeing Collectors in Stream API

New Methods in String Class

JEP 334: JVM Constants API

JEP 305: Pattern Matching for instanceof

Raw String Literals is Removed From JDK 12.

Q19. What are the important features of the Java 11 release?

Java 11 is the second LTS release after Java 8. They’ve changed the licensing and support model which means if you download the Java 11 Oracle JDK, it will be paid for commercial use. If you want to use a free version, you can download it from the OpenJDK website.

Some of the important features of Java 11 are:

We can run a java program directly through the java command. The source file will be implicitly compiled and executed. It’s part of the JEP 330 feature implementation.

There are 6 new methods added in String class – isBlank(), lines(), strip(), stripLeading(), stripTrailing(), and repeat(). You can read all about them at our article on the Java String class.

Files class got two new methods to read/write string data – readString() and writeString().

We can use “var” with lambda expressions too. It’s part of JEP 323 implementation.

Epsilon: A No-Op Garbage Collector for test environments. It’s an experimental feature and part of JEP 318.

Java EE and CORBA Modules have been removed from the standard JDK build. Nashorn JavaScript Engine has also been deprecated.

New HTTP Client (JEP 321) and Flight Recorder (JEP 328)

For a complete overview, please read Java 11 Features.

Q20. What are the important features of the Java 10 release?

Java 10 is the first every six months from Oracle corporation, so it’s not a major release like earlier versions. However, some of the important features of Java 10 are:

Local-Variable Type Inference

Enhance java.util.Locale and related APIs to implement additional Unicode extensions of BCP 47 language tags.

Enable the HotSpot VM to allocate the Java object heap on an alternative memory device, such as an NV-DIMM, specified by the user.

Provide a default set of root Certification Authority (CA) certificates in the JDK.

Java 10 is mostly a maintenance release, however, I really liked the local variable type inference feature. For a detailed overview of Java 10, please go through Java 10 Features.

Q21. What are the important features of the Java 9 release?

Java 9 was a major release and brought a lot of features. Some of the important features are:

Java 9 REPL (JShell)

Java 9 Module System

Factory Methods for Immutable List, Set, Map, and Map.Entry

Private methods in Interfaces

Reactive Streams

GC (Garbage Collector) Improvements

Q22. What are the important features of the Java 8 release?

Java 8 has been released in March 2014.

Java 8 has been one of the biggest releases after Java 5 annotations and generics. Some of the important features of Java 8 are:

Interface changes with default and static methods

Functional interfaces and Lambda Expressions

Java Stream API for collection classes

Java Date Time API

Q23. Which class is the superclass of all classes?

java.lang.Object is the root class for all the java classes and we don’t need to extend it.

Q24. Why Java doesn’t support multiple inheritances?

Java doesn’t support multiple inheritances in classes because of the “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.

Q25. Why Java is not a pure Object Oriented language?

Java is not said to be pure object-oriented because it supports primitive types such as int, byte, short, long, etc. I believe it brings simplicity to the language while writing our code. Java could have wrapper objects for the primitive types but just for the representation, they would not have provided any benefit.

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

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

Java’s main method is public and static so that the 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.

Q27. What is overloading and overriding in Java?

Overloading: 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.

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

Q28. Can we overload the main() method?

Yes, we can have multiple methods with the name “main” in a single class. However, if we run the class, the java runtime environment will look for the main method with syntax as public static void main(String args[]).

Q29. What is the final keyword?

The final keyword is used with Class to make sure no other class can extend it. For example, the String class is final and we can’t extend it.

We can use the final keyword with methods to make sure child classes can’t override it.

Java’s final keyword can be used with variables to make sure that it can be assigned only once. However the state of the variable can be changed, for example, we can assign a final variable to an object only once but the object variables can change later on.

Java interface variables are by default final and static.

Q30. What is a static keyword?

The static keyword can be used with class-level variables to make it global i.e all the objects will share the same variable.

We can use static keyword with methods also. A static method can access only static variables of the class and invoke only static methods of the class.

Q31. What is finally in java?

finally: The finally block is used with try-catch to put the code that you want to get executed always, even if an exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block.

Q32. What is finalize in java?

finalize: The finalize() is a special method in the Object class that we can override in our classes. This method gets called the garbage collector when the object is getting garbage collected. This method is usually overridden to release system resources when the object is garbage collected.

Q33. What is static import?

If we have to use any static variable or method from another class, usually we import the class and then use the method/variable with the class name.

import java.lang.Math;

//inside class

double test = Math.PI * 5;

We can do the same thing by importing the static method or variable only and then using it in the class as if it belongs to it.

import static java.lang.Math.PI;

//no need to refer to class now

double test = PI * 5;

The use of static import can cause confusion, so it’s better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.

Q34. What is a static block?

Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when a class is loaded.

Q35. What is an interface?

Interfaces are core concepts of java programming language and are used a lot not only in JDK but also in java design patterns, most of the frameworks, and tools. Interfaces provide a way to achieve abstraction in java and are used to define the contract for the subclasses to implement.

Q36. What is an abstract class?

Abstract classes are used in java to create a class with some default method implementation for subclasses. An abstract class can have an abstract method without the body and it can have methods with implementation also.

The abstract keyword is used to create an abstract class. Abstract classes can’t be instantiated and are mostly used to provide a base for sub-classes to extend and implement the abstract methods and override or use the implemented methods in abstract classes.

Q37. What is the difference between abstract class and interface?

The abstract keyword is used to create an abstract class whereas interface is the keyword for interfaces.

Abstract classes can have method implementations whereas interfaces can’t.

A class can extend only one abstract class but it can implement multiple interfaces.

We can run an abstract class if it has a main() method whereas we can’t run an interface.

Q38. Can an interface implement or extend another interface?

Interfaces don’t implement another interface, they extend it. Since interfaces can’t have method implementations, there is no issue with the diamond problem. That’s why we have multiple inheritances in interfaces i.e an interface can extend multiple interfaces.

Q39. What is a Marker interface?

A marker interface is an empty interface without any method but is used to force some functionality in implementing classes by Java. Some of the well-known marker interfaces are Serializable and Cloneable.

Q40. What are Wrapper classes?

Java wrapper classes are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final. Java 5 autoboxing and unboxing allow easy conversion between primitive types and their corresponding wrapper classes.

Q41. What is Enum in Java?

Enum was introduced in Java 1.5 as a new type whose fields consist of a fixed set of constants. For example, in Java, we can create Direction as an enum with fixed fields such as EAST, WEST, NORTH, and SOUTH.

enum is the keyword to create an enum type similar to the class. Enum constants are implicitly static and final.

Q42. What are Java Annotations?

Java Annotations provide information about the code and they have no direct effect on the code they annotate. Annotations are introduced in Java 5. Annotation is metadata about the program embedded in the program itself. It can be parsed by the annotation parsing tool or the compiler. We can also specify annotation availability to either compile-time only or till runtime. Java Built-in annotations are @Override, @Deprecated, and @SuppressWarnings.


Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2

GIT Interview Questions

Redis Interview Questions

Core Java Interview Questions Set -1

Docker interview question Set -1

Docker interview question Set -2

Kubernetes Interview Question Set -1

Kubernetes Interview Question Set -2

No comments:

Post a Comment