Java Developer Interview Questions and Answers

Java is an object-oriented, cross platform, multi-purpose programming language produced by Sun Microsystems. First released in 1995, it was developed to be a machine independent web technology. It was based on C and C++ syntax to make it easy for programmers from those communities to learn.

JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. It was developed by James Gosling and Patrick Naughton. It is a simple programming language. Writing, compiling and debugging a program is easy in java. It helps to create modular programs and reusable code.

The original and reference implementation Java compilers, virtual machines, and class libraries were originally released by Sun under proprietary licenses. Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once, Run Anywhere (WORA), providing no-cost run-times on popular platforms. On 13 November, 2006, Sun released much of Java as free and open source software under the terms of the GNU General Public License (GPL). As of May 2007, in compliance with the specifications of the Java Community Process, Sun relicensed most of its Java technologies under the GNU General Public License. Others have also developed alternative implementations of these Sun technologies, such as the GNU Compiler for Java (bytecode compiler), GNU Class path (standard libraries), and IcedTea-Web.

The new version is Java SE 9, released on September 21, 2017, and is one of the two versions currently supported for free by Oracle. The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively.The latest version is Java SE 11 (September 25, 2018)

Introduction to Machine Language:

Machine language, the numeric codes for the operations that a particular computer can execute directly. The codes are strings of 0s and 1s, or binary digits (bits), which are frequently converted both from and to hexadecimal (base 16) for human viewing and modification. Machine language consists of very simple instructions that can be executed directly by the CPU of a computer. Almost all programs, though, are written in high-level programming languages such as Java, Pascal, or C++. A program written in a high-level language cannot be run directly on any computer. First, it has to be translated into machine language. This translation can be done by a program called a compiler. A compiler takes a high-level-language program and translates it into an executable machine-language program. Once the translation is done, the machine-language program can be run any number of times, but of course it can only be run on one type of computer (since each type of computer has its own individual machine language). If the program is to run on another type of computer it has to be re-translated, using a different compiler, into the appropriate machine language.

There is an alternative to compiling a high-level language program. Instead of using a compiler, which translates the program all at once, you can use an interpreter, which translates it instruction-by-instruction, as necessary. An interpreter is a program that acts much like a CPU, with a kind of fetch-and-execute cycle. In order to execute a program, the interpreter runs in a loop in which it repeatedly reads one instruction from the program, decides what is necessary to carry out that instruction, and then performs the appropriate machine-language commands to do.

Java Virtual Machine (JVM) is a specification that provides runtime environment in which java bytecode can be executed. As the name implies, the JVM acts as a “virtual” machine or processor. Java’s platform independence consists mostly of its Java Virtual Machine (JVM) Java was designed to allow application programs to be built that could be run on any platform without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the platform. The JVM performs following operation:

Loading code

Verifies code

Executes code

Bytecode:

Bytecode (Intermediate code) is object-oriented programming (OOP) code compiled to run on a virtual machine (VM) instead of a central processing unit (CPU). The VM transforms program code into readable machine language for the CPU because platforms utilize different code interpretation techniques. A VM converts bytecode for platform interoperability, but bytecode is not platform-specific. Bytecode is in a compiled Java programming language format and has the .class extension executed by Java Virtual Machine (JVM)

Java Development Kit (JDK):

JDK (Java SE Development Kit) includes a complete JRE (Java Runtime Environment) plus tools for developing, debugging, and monitoring Java applications. JDK is required to develop and run Java applications and applets.

Java Runtime Environment (JRE):

The Java Runtime Environment (JRE), also known as Java Runtime, is part of the Java

Development Kit (JDK), a set of programming tools for developing Java applications. The Java Runtime Environment provides the minimum requirements for executing a Java application; it consists of the Java Virtual Machine (JVM), core classes, and supporting files.

Java is a programming language. Java is an object-oriented language derived from C++ with strong support for networking, multithreading, and component-oriented development. Java is commonly used to refer to the Java platform, a set of tools allowing for easy cross-platform application development, as well as the Java programming language, which is a general-purpose programming language often used to develop programs for this platform.

Java files are stored in files suffixed with .java. These files are then compiled into Java byte code using the Java compiler, and the byte code is then executed using the Java Virtual Machine (JVM). The Java compiler and the JVM are part of the Java Development Kit.

Simple: Java is a simple language because of its various features, Java Doesn’t Support Pointers, Operator Overloading etc. Java provides bug free system due to the strong memory management.

Object-Oriented: Object-Oriented Programming Language (OOPs) is the methodology which provide software development and maintenance by using object state, behavior, and properties.

Secure: Java is Secure Language because of its many features it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption. Java does not support pointer explicitly for the memory. All Program Run under the sandbox.

Robust: Java was created as a strongly typed language. Data type issues and problems are resolved at compile-time, and implicit casts of a variable from one type to another are not allowed.

Platform-independent: Java Language is platform-independent due to its hardware and software environment. Java code can be run on multiple platforms ex: windows, Linux, sun Solaris, Mac/Os etc. Java code is compiled by the compiler and converted into byte code. This byte code is a platform independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere (WORA).

Architecture neutral: It is not easy to write an application that can be used on Windows, UNIX and a Macintosh. And it’s getting more complicated with the move of windows to non-Intel CPU architectures.

Portable: Java code is portable. It was an important design goal of Java that it be portable

so that as new architectures (due to hardware, operating system, or both) are developed, the java environment could be ported to them.

Dynamic: Because it is interpreted, Java is an extremely dynamic language, at runtime, the java environment can extend itself by linking in classes that may be located on remote

Servers on a network.

Interpreted: We all know that Java is an interpreted language as well. With an interpreted language such as Java, programs run directly from the source code. The interpreter program reads the source code and translates it on the fly into computations. Thus, Java as an interpreted language depends on an interpreter program.

High performance: For all but the simplest or most infrequently used applications, performance is always a consideration for most applications, including graphics-intensive ones such as are commonly found on the World Wide Web, the performance of java is more than adequate.

Multithreaded and Interactive: Writing a computer program that only does a single thing at a time is an artificial constraint that we’ve lived with in most programming languages. With java, we no longer have to live with this limitation. Support for multiple, synchronized threads is built directly into the Java language and runtime environment.

Distributed: Java facilitates the building of distributed application by a collection of classes for use in networked applications. By using java?s URL (Uniform Resource Locator) class, an application can easily access a remote server. Classes also are provided for establishing socket-level connections.

Simple Small and Familiar: It is a simple Language because it contains many features of other Languages like c and C++ and Java Removes Complexity because it doesn’t use pointers, Storage Classes and Go to Statements and java doesn’t support Multiple Inheritance

The Just-In-Time (JIT) compiler is a component of the JRE (Java Runtime Environment) that improves the performance of Java applications at run time. It helps improve the performance of Java programs by compiling bytecodes into native machine code at runtime.

Java Class Loader loads a java class file into java virtual machine. It is as simple as that. It is not a huge complicated concept to learn and every java developer must know about the java class loaders and how it works.

Java class loaders can be broadly classified into 3 types.

Bootstrap Class Loader: Bootstrap class loader loads java’s core classes like java.lang, java.util etc. These are classes that are part of java runtime environment. Bootstrap class loader is native implementation and so they may differ across different JVMs.

Extensions Class Loader: Extensions Class Loader is JAVA_HOME/jre/lib/ext contains jar packages that are extensions of standard core java classes. Extensions class loader loads classes from this ext folder. Using the system environment propery java.ext.dirs you can add ‘ext’ folders and jar files to be loaded using extensions class loader.

System Class Loader: System Class Loader Java classes that are available in the java classpath are loaded using System class loader.

Java Servlet is server-side technologies to extend the capability of web servers by providing support for dynamic response and data persistence. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets.

Class Variable: Class Variables only have a single copy of the variable and are declared with static modifiers. Every object of the class shares a single copy of the class variable, so if any changes are made to a class variable they will be seen by all the objects of a class. A class variable is allocated memory when the class is loaded first time.

Instance variable: Instance variables belong to an object and are specified without a static modifier. Every object of the class will have its own personal copy of the instance variable unlike class variables where single copy of the variable is shared by different objects of the class.

Sleep (): Sleep () is a static method. Sleep () is a blocking operation that keeps a hold on the monitor / lock of the shared object for the specified number of milliseconds. Sleep () is most commonly used for polling, or to check for certain results, at a regular interval. Sleep () is defined in java.lang.Thread class.

Wait (): Wait () is an instance method. wait (), on the other hand, simply pauses the thread until either the specified number of milliseconds have elapsed or it receives a desired notification from another thread (whichever is first), without keeping a hold on the monitor/lock of the shared object. Wait () is generally used in multithreaded applications, in conjunction with notify () / notifyAll (), to achieve synchronization and avoid race conditions. Wait () is defined in java.lang.Object class.

ArrayList and Vector both use Array as a data structure internally. However, there are few differences in the way they store and process the data.

ArrayList: ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. ArrayList grow by half of its size when resized. As ArrayList is unsynchronized, it operates faster than Vector. ArrayList uses Iterator interface to traverse the objects stored in ArrayList.

Vector: Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Vector doubles the size of itself by default when grows. As Vector is synchronized, it operates slower than ArrayList. Vector uses Enumeration as well as Iterator interface to traverse the objects stored in Vectors.

Fail-Fast iterators doesn’t allow modifications of a collection while iterating over it.These iterators throw ConcurrentModificationException if a collection is modified while iterating over it.They use original collection to traverse over the elements of the collection. These iterators don’t require extra memory.

Ex: Iterators returned by ArrayList, Vector, HashMap.

Fail-Safe Iterators

Fail-Safe iterators allow modifications of a collection while iterating over it.These iterators

don’t throw any exceptions if a collection is modified while iterating over it.They use copy of the original collection to traverse over the elements of the collection. These iterators require extra memory to clone the collection.

No, we cannot override private methods in Java as if we declare any variable, method as private that variable or method will be visible for that class only and also if we declare any method as private than they are bonded with class at compile time not in run time so we can’t reference those method using any object.

Wrapper class wraps (encloses) around a data type and gives it an object appearance. They are used to convert any primitive type into an object. The primitive data types are not objects, they do not belong to any class and they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language.

Type Erasure is the technique using which the Java compiler translates generic / parameterized type to raw type in Java generics. Type erasure is applicable in the context of generics. Type erasure can be used as an easy way to define typing over implicitly typed languages. The reverse operation is called type inference.

Object’s Locking is a mechanism used to synchronize a non-static content of code, so that only one object can hold the monitor and can access the non-static content. Developers can use this lock when two or more threads are sharing the same object of the class that implements Runnable or extends Thread. All objects and classes have locks.

Wikipedia defines the spring framework as “an application framework and inversion of control container for the Java platform. The framework’s core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform.” Spring is essentially a lightweight, integrated framework that can be used for developing enterprise applications in java.

A stream is a way of sequentially accessing a file. In Streams you can process the data one

at a time as, bulk operations are unavailable with them. But, streams support a huge range

of source and destinations including disk file, arrays, other devices, other programs etc. In Java, a byte is not the same thing as a char. Therefore, a byte stream is different from a character stream.

Java defines two types of streams: Byte Streams and Character Streams.

Byte Streams: A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, however not quite appropriate for text files.

Character Streams: A character stream will read a file character by character. Character Stream is a higher-level concept than Byte Stream. A Character Stream is, effectively, a Byte Stream that has been wrapped with logic that allows it to output characters from a specific encoding. That means, a character stream needs to be given the file’s encoding in order to work properly. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc. All character stream classes are descended from Reader and Writer.

@Request Mapping: This annotation can be used configuring URI mapping in controller handler methods. This is a very important annotation, so you should go through Spring MVC RequestMapping Annotation Examples

@Qualifier: This annotation can be used along with @Autowired to do autowiring by name. You can specify the name of the bean as @Qualifier(“beanName”).

@Primary: It allows to give preference to a bean when multiple beans match for autowiring.

@ResponseBody: sending Object as response, usually for sending XML or JSON data as response.

@Component: It is used over a class to tell spring to consider it as a bean component.

@ComponentScan: This annotation can be used to specify the packages that need to be scanned for components and is usually used along with @Configuration classes. You may also specify the same behaviour using <context: component-scan in spring xml configuration.

@Repository, @Service and @Controller: This annotation is used instead of @Component based on actual application layer you are developing. However technically all behave the same. If in doubt, use @Component.

@Profile: It allows to specify a profile for your bean.

@ActiveProfiles: It can be used from a JUnit test to specify the active profiles

Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc. The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.

NoClassDefFoundError means that the class is present in the class path at Compile time, but it doesn’t exist in the class path at Runtime. After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the class loader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you’re trying to use.

The following are the three-reason cause for this error:

ClassNotFoundException- .class not found for that referenced class irrespective of whether it is available at compile time or not (i.e. base/child class).

Insertions are easy and fast in Linked List as compared to Array List because there is no risk of resizing array and copying content to new array if array gets full which makes adding into Array List of O (n) in worst case, while adding is O (1) operation in Linked List in Java. Array List also needs to be update its index if you insert something anywhere except at the end of array.

Removal also better in Linked List than Array List due to same reasons as insertion.

Linked List has more memory overhead than Array List because in Array List each index only holds actual object (data) but in case of Linked List each node holds both data and address Of next and previous node.

Both Linked List and Array List require O (n) time to find if an element is present or not. However we can do Binary Search on Array List if it is sorted and therefore can search in O (Log n) time.