Fri Jul 13 2018

Java interview questions

Tips and Tricks766 views

Java related interview questions

Java is a programming language expressly designed for use in the distributed environment of the Internet. Java language and computing platform first released by Sun Microsystems in 1995. It was designed to have the "look and feel" of the C++ language, but it is simpler to use than C++ and enforces an object-oriented programming model. A programmer uses it to write programs that run in a client/server configuration and over remote networks. Day by day there are more and more applications created using Java. It is a very simple, powerful and robust, programming language which provides a fast, secure, and reliable platform to work on. Java is being used everywhere, from laptops to data centers, game consoles to scientific supercomputers, and cell phones to the Internet. So, there is a huge number of companies who searching for good Java programmers and ready to pay a higher amount for right candidates. They take interviews and check out the qualities of the candidates. In the interviews, there may so many questions that will ask by the employers. In this article, we will suggest some sample of questions that will give you an idea of the type of questions which may be asked in an interview.

Let's take a look at Java related Interview Questions -

Q. What is Java?

Ans. Java is an object-oriented computer language. It is a high-level programming language developed by James Gosling in Sun Microsystem in 1995. It's a fast, secure and reliable language used for many games, devices, and applications.

Q. What is a class in Java?

Ans. Java encapsulates the codes in various classes which define new data types. These new data types are used to create objects.

Q. Explain JDK, JRE, and JVM?

Ans. JVM stands for Java Virtual Machine which provides a runtime environment for Java Bytecodes to be executed. JRE (Java Runtime Environment) that includes sets of files required by JVM during runtime. JDK (Java Development Kit) consists of JRE along with the development tools required to write and execute a program.

Q. Compare Overloading and Overriding?

Ans. Overloading refers to the case of having two methods of the same name but different properties, but overriding occurs when there are two methods of same name and properties, but one is in the child class and one is in the parent class.

Q. How can we restrict inheritance for a class?

Ans. We can restrict inheritance for class by using final keyword; if we make all method final, then we cannot override that. By using private constructors and also by using Javadoc comment (//).

Q. Define JSON?

Ans. Expansion of JSON is “JavaScript Object Notation”, It is a much lighter and readable alternative to XML. It is an independent and easily parse-able in all programming languages. It is primarily used for Communicating between client-server or server-server communication. It is a much lighter and readable alternative to XML.

Q. What is content negotiation?

Ans. If we have visited the website to search the information, we will get the information in different languages and in different formats when a client makes an HTTP request to a server, the client can also specify the media types here. The client can specify what it can accept back from the host and on the basis of availability, the host will return to the client. This is known as content negotiation because client and server negotiated on the language and format of the content to be shared.

Q. What are Loops in Java? Explain types of loops?

Ans. Looping is used in programming to execute a statement or a block of statement repeatedly.

There are three types of loops in Java:

For Loops - For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when the number of times to execute the statements is known to the programmer.

While Loops - While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, the condition is checked first before execution of statements.

Do While Loops - Do While Loop is same as While loop with the only difference that condition is checked after execution of a block of statements. Hence in case of doing while loop, statements are executed at least once.

Q. What is a JIT compiler?

Ans. Just-In-Time(JIT) compiler is used to improve the performance. JIT compiles parts of the bytecode that has similar functionality which in turn reduces the amount of time needed for compilation. The term “compiler” here refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

Q. What is an immutable object?

Ans. Java classes whose objects cannot be modified once they are created are known as Immutable classes. Any modification of Immutable object results formation of the new object.

Q. What is an Abstract class?

Ans. A class which contains the abstract keyword in its declaration is known as an abstract class. It can have abstract and non-abstract methods (method with the body). This class can have public, private, protected or constants and default variables. It needs to be extended and its method implemented. It cannot be instantiated. If a class has at least one abstract method, then the class must be declared abstract.

Q. What is the purpose of garbage collection in Java, and when is it used?

Ans. The main purpose of garbage collection is to analyze a program, identify and discard the objects that are no longer needed. It is helpful as the resources can be reclaimed and reused. A Java object is subjected to garbage collection when the object is not reachable to the program in which it is used.

Q. What is the first argument of the String array in main() method?

Ans. Unlike in C/C++ where the first element by default is the program name, the string array in main() has no element, the String array is empty.

Q. What is the multi-catch block in Java?

Ans. Multi-catch block makes the code shorter and cleaner when every catch block has similar code. We can catch multiple exceptions in a single catch block using this feature.

Q. Have any difference between double and float variables in Java?

Ans. In Java, float takes 4 bytes in memory while Double takes 8 bytes in memory. The float is single precision floating point decimal number while Double is a double precision decimal number.

Q. What are Java Packages? What’s the significance of packages?

Ans. In Java, the package is a collection of classes and interfaces which are bundled together as they are related to each other. Use of packages helps developers to modularize the code and group the code for proper re-use. Once the code has been packaged in Packages, it can be imported into other classes and used.

Q. Why Java doesn't use pointers?

Ans. Pointers are susceptible and slight carelessness in their use may result in memory problems and hence Java basically manages their use.

Q. What is an enumeration?

Ans. Usually, we called enumeration as an enum. An enumeration is an interface containing methods for accessing the original data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in the collection.

Q. Which API is provided by Java for operations on a set of objects?

Ans. Java provides a Collection API which provides many useful methods which can be applied to a set of objects. Some of the important classes provided by Collection API include ArrayList, HashMap, TreeSet, and TreeMap.

Q. Is there any need to use Vector class?

Ans. Yes! The Vector class provides the ability to execute a growable array of objects. Vector proves to be very useful if you don’t know the size of the array in advance, or we need one that can change sizes over the lifetime of a program.

Q. What is the default size of load factor in hashing based collection?

Ans. Default Size = 0.75. The default capacity is computed as initial capacity * load factor.

Q. Explain Expression Language in JSP?

Ans. The expression language is used in JSP to simplify the accessibility of objects and It provides many objects that can be used directly like param, request Scope, session Scope, application Scope, request, session etc.

Q. What is HTTP Tunneling?

Ans. Tunneling refers to encapsulating private network data/information and transmitting it through a public network. Instead of sending data as a packet, tunneling encrypts the data and encapsulates in a connection. It allows the outside clients to collect all the data sent by client-side Object Request Broker (ORB) that needs to be sent to the server-side ORB. HTTP tunneling masks other protocol requests as HTTP requests.

Q. What is the front controller class of Spring MVC?

Ans. The Dispatcher Servlet class works as the front controller in Spring MVC.

Q. How an object is serialized in java?

Ans. In Java, to convert an object into a byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing serializable interface get serialized and their state is saved in the byte stream.

Q. When we should use serialization?

Ans. Serialization is used when data needs to be transmitted over the network. Using serialization, object’s state is saved and converted into a byte stream. The byte stream is transferred over the network and the object is re-created at the destination.

Q. Why Runnable Interface is used in Java?

Ans. The Runnable interface is used in Java for implementing multi-threaded applications. Java.Lang. The Runnable interface is implemented by a class to support multithreading.

Q. What is multi-threading?

Ans. Multithreading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share the same process stack and running in parallel. It helps in performance improvement of any program.

Q. What’s the benefit of using inheritance?

Ans. A key benefit of using inheritance is reusability of code as inheritance enables sub-classes to reuse the code of its superclass. Polymorphism (Extensibility ) is another great benefit which allows new functionality to be introduced without affecting existing derived classes.

Q. What is Encapsulation?

Ans. Encapsulation means the localization of the information or knowledge within an object. Encapsulation is also called “Information Hiding”.

Q. What is the difference between Error and Exception?

Ans. Error - Mostly a system issue. It always occurs at the runtime and must be resolved in order to proceed further. Exception - Mostly an input data issue or wrong logic in code. Can occur at compile time or runtime.

Q. What is throws keyword?

Ans. If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method’s signature.

Q. Can we have multiple catch blocks following a single try block?

Ans. Yes, we can have multiple catch blocks in order to handle more than one exception.

Q. What is a Java Bean?

Ans. A JavaBean is a Java class that follows some simple conventions including conventions on the names of certain methods to get and set state called Introspection. Because it follows conventions, it can easily be processed by a software tool that connects Beans together at runtime. JavaBeans are reusable software components.

Q. What is a deadlock?

Ans. Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.

Q. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

Ans. Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18-bit patterns. UTF-16 uses 16-bit and larger bit patterns.

Q.  What are Wrapper classes?

Ans. These are classes that allow primitive types to be accessed as objects. Example: Integer, Character, Double, Boolean etc.

Q. How do you differentiate between a Window and a Frame?

Ans. The Frame class extends Window to define a main application window that can have a menu bar.

Q.  Which Java operator is right associative?

Ans. The = operator is right associative.

Q.  How can a dead thread be restarted?

Ans. A dead thread cannot be restarted.


 

These are not only questions that you will be asked by the employers, it only depends on them; what they are looking for and how they can testing you. Hope this article successfully guided you and helped you to understand the level of interview questions about Java. Best of luck for the future! Thank you!

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.