Java Interview Questions

Ques:1 What is JVM ?

Ans- JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed.

Ques:2 What is the main feature of Java?

Ans-Java is a platform independent language.

Ques:3 What is the Difference between JDK and JRE ?

Ans-JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.

Ques:4 Are JVM’s platform independent?
Ans-JVM’s are not platform independent. JVM’s are platform specific runtime implementation provided by the vendor.

Ques:5 What do you know about Java?

Ans-Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

Ques:6 List any five features of Java?

Ans-features are:

Object Oriented
Platform Independent
Robust
Interpreted
Multi-threaded

Ques:7 What is the base class of all classes?

Ans- java.lang.Object

Ques:8 What are packages?

Ans-A package is a collection of related classes and interfaces providing access protection and namespace management.

Ques:9 Why is Java Architectural Neutral?

Ans- It’s compiler generates an architecture-neutral object file format, which makes the compiled code to be executable on many processors, with the presence of Java runtime system.

Ques:10 What is JIT (Just In Time) Compiler?

Ans-Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor.

Ques:11 Why is Java considered dynamic?

Ans-11 It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

Ques:12 What is the difference between superclass and subclass?

Ans-A superclass is a class that is inherited whereas sub class is a class that does the inheriting.

Ques:13 What is the main difference between Java platform and other platforms?

Ans- The Java platform differs from most other platforms in the sense that it’s a software-based platform that runs on top of other hardware-based platforms.It has two components:

Runtime Environment
API(Application Programming Interface)

Ques:14 Difference between Function Overriding and Overloading?

Ans- Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters. On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.

Ques:15 What is an abstract class?

Ans-An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete.

Ques:16 What is a pointer and does Java support pointers?

Ans-Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues, hence Java doesn’t support the usage of pointers.

Ques:17 What provides Java the ‘write once and run anywhere’ feature?

Ans-The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

Ques:18 What is a Constructor, Constructor Overloading in Java and Copy-Constructor ?

Ans-A constructor gets invoked when a new object is created. Every class has a constructor. In case the programmer does not provide a constructor for a class, the Java compiler (Javac) creates a default constructor for that class. The constructor overloading is similar to method overloading in Java. Different constructors can be created for a single class. Each constructor must have its own unique parameter list. Finally, Java does support copy constructors like C++, but the difference lies in the fact that Java doesn’t create a default copy constructor if you don’t write your own.

Ques:19 Does Java support multiple inheritance?

Ans- Java doesn’t support multiple inheritance.

Ques:20 How HashMap works in Java ?

Ans-A HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods, in order to put and retrieve elements to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists, its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.

Ques:21 What is the importance of hashCode() and equals() methods ?

Ans- In Java, a HashMap uses the hashCode and equals methods to determine the index of the key-value pair and to detect duplicates. More specifically, the hashCode method is used in order to determine where the specified key will be stored. Since different keys may produce the same hash value, the equals method is used, in order to determine whether the specified key actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and efficiency of the HashMap.

Ques:22 What differences exist between HashMap and Hashtable ?

Ans- Both the HashMap and Hashtable classes implement the Map interface and thus, have very similar characteristics. However, they differ in the following features:

A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.
A Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hash Table is suitable for multi-threaded environments.
A HashMap provides its set of keys and a Java application can iterate over them. Thus, a HashMap is fail-fast. On the other hand, a Hash Table provides an Enumeration of its keys.
The Hashtable class is considered to be a legacy class.

Ques:23 What is an applet?

Ans-Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser.

Ques:24 What is the lifecycle of an applet?

Ans-
init() method – Can be called when an applet is first loaded.
start() method – Can be called each time an applet is started.
paint() method – Can be called when the applet is minimized or maximized.
stop() method – Can be used when the browser moves off the applet.
destroy() method – Can be called when the browser is finished with the applet.

Ques:25 Is Java a pure object oriented language?

Ans- Java uses primitive data types and hence is not a pure object oriented language.

Ques:26 What are instance variables?

Ans-Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

Ques:27 Can we instantiate an abstract class?

Ans-An abstract class can never be instantiated. Its sole purpose is to be extended (subclassed).

Ques:28 What do you mean by Object?

Ans-Object is a runtime entity and its state is stored in fields and behavior is shown via methods. Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication.

Ques:29 What do you mean by class?

Ans-A class is a blueprint from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

Ques:30 What is JDBC?

Ans-JDBC is a set of Java API for executing SQL statements. This API consists of a set of classes and interfaces to enable programs to write pure Java Database applications.

Ques:31 What do you mean by Constructor?

Ans-Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.
Ques:32 What is a static variable?

Ans- Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

Ques:33 What do you mean by Access Modifier?

Ans-Java provides access modifiers to set access levels for classes, variables, methods and constructors. A member has package or default accessibility when no accessibility modifier is specified.

Ques:34 What is the Java API?

Ans-The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.

Ques:35 Why is StringBuffer called mutable?

Ans-The String class is considered as immutable, so that once it is created a String object cannot be changed. If there is a necessity to make alot of modifications to Strings of characters then StringBuffer should be used.

Ques:36 What is the difference between StringBuffer and StringBuilder class?

Ans-Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.

Ques:37 Which package is used for pattern matching with regular expressions?

Ans- java.util.regex package is used for this purpose.

Ques:38 java.util.regex consists of which classes?

Ans- java.util.regex consists of three classes: Pattern class, Matcher class and PatternSyntaxException class.

Ques:39 What is finalize() method?

Ans-It is possible to define a method that will be called just before an object’s final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.

Ques:40 What is an Exception?

Ans-An exception is a problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the thread’s method invocation stack.

Ques:41 What do you mean by Checked Exceptions?

Ans- It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

Ques:42 Explain Runtime Exceptions?

Ans- It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.

Ques:43 When is throws keyword used?

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.

Ques:44 When is throw keyword used?

Ans-An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

Ques:45 What is method overloading and method overriding?

Ans- Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading.
Method overriding : When a method in a class having the same method name with same arguments is said to be method overriding.

Ques:46 What is the difference between this() and super()?

Ans-this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a superclass constructor.

Ques:47 What is RMI architecture?

Ans-RMI architecture consists of four layers and each layer performs specific functions:
a) Application layer – contains the actual object definition.
b) Proxy layer – consists of stub and skeleton.
c) Remote Reference layer – gets the stream of bytes from the transport layer and sends it to the proxy layer.
d) Transportation layer – responsible for handling the actual machine-to-machine communication.

Ques:48 What is a Java Bean?

Ans-A Javabean is a software component that has been designed to be reusable in a variety of different environments.

Ques:49 What is the purpose of finalization?

Ans-The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected. For example, closing an opened file, closing an opened database Connection.

Ques:50 What is a Socket?

Ans-Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server.

Ques 51 : What gives Java its ‘write once and run anywhere’ nature?

Ans-The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

Ques 52 :What is this in java?

Ans-It is a keyword that that refers to the current object.

Ques 53 :Explain Runtime Exceptions?

Ans-It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.

Ques 54 :What is the difference between Swing and AWT components?

Ans- AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example, java.awt.Button is a heavyweight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.

Ques 55 :What is the difference between the >> and >>> operators?

Ans- The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

Ques 56 :How to run a JAR file through command prompt?

Ans-We can run a jar file using java command but it requires Main-Class entry in jar manifest file. Main-Class is the entry point of the jar and used by java command to execute the class.

Ques 57 :Java Compiler is stored in JDK, JRE or JVM?

Ans-The task of java compiler is to convert java program into bytecode, we have javac executable for that. So it must be stored in JDK, we don’t need it in JRE and JVM is just the specs.

Ques 58 : Can main() method in Java can return any data?

Ans- In java, main() method can’t return any data and hence, it’s always declared with a void return type.

Ques 59 :What’s the benefit of using inheritance?

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

Ques 60 :Is JDK required on each machine to run a Java program?

Ans-JDK is development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn’t required. Only JRE is required.

Ques 61 :What’s the base class of all exception classes?

Ans-In Java, Java.Lang.throwable is the superclass of all exception classes and all exception classes are derived from this base class.

Ques 62 :Explain TreeSet?

Ans-It is a Set implemented when we want elements in a sorted order.

Ques 63 :Difference between throw and throws?

Ans- It includes:

Throw is used to trigger an exception where as throws is used in declaration of exception.Without throws, Checked exception cannot be handled whereas checked exception can be propagated with throws.

Ques 64 :What is the most important feature of Java?

Ans-Java is a platform independent language.

Translate »