We have collected huge set of java interview questions with answers that will be very fruitful for beginners as well as experienced programmers who want to face a java interview.
Instead of keeping answers sort, I've prefered it to explain so that you can answer in better way. Our collection contains java interview questions, OOPs interview questions, String Handling interview questions, Multithreading interview questions, collection interview questions, JDBC interview questions etc.
An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub class is a class which inherits from another class called super class. Sub class can access all public and protected methods and fields of its super class.
In Java, access specifiers are the keywords used before a class name, method name, variable name which defines the access scope. The types of access specifiers in java are:
The static keyword is used in java mainly for memory management. It is used with variables, methods, blocks and nested class. It is a keyword that are used for share the same variable or method of a given class. This is used for a constant variable or a method that is the same for every instance of a class.
Static variables or static methods can be access without creating instance of that class, just put the class name before the static variable or method to use them.
Static method can not call non-static method.
Encapsulation is one of the four fundamental OOP concepts.Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
Continue statement: The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.
A labeled continue statement skips the current iteration of an outer loop marked with the given label.
Break statement: The break statement has two forms: labeled and unlabeled. You can also use an unlabeled break to terminate a for, while, or do-while loop.
An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.
In java, float takes 4 bytes in memory while double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.
Final class: A class that is declared as final, it cannot be subclassed. Example String,Integer and other wrapper classes.
Final method: A method that is declared as final, can't be overridden by the subclasses of that class where final method is declared.
Final variable: The value of a variable that is declared as final, can't be changed.
Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It’s denoted as ?
Using Math.random() we can generate random numbers in the range of 0.1 to 1.0
Random class is in package java.util
In a switch statement, default case is executed when no other switch condition matches. Default case is an optional case.
In Java, 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 reuse. Once code has been packaged in Packages, it can be imported in other classes and used.
Yes we can.
Also, if a class has atleast one abstract method, then, the class must be declared as abstract otherwise it will give compile time error.
Key difference between an Abstract class and an Interface are:
Interfaces are slower in performance as compared to abstract classes as extra indirections are required for interfaces. Another key factor for developers to take into consideration is that any class can extend only one abstract class while a class can implement many interfaces.
Anytime an interface is implemented in a class; developer is forced to implement each and every method of interface.
In java, when a package is imported, its sub-packages aren’t imported and developer needs to import them separately if required.
In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won’t get any compilation error however, it will not get executed and will give a runtime error.
In java, we can pass argument to a function only by value and not by reference.
JVM: The Java Virtual machine (JVM) is the virtual machine that run the Java bytecodes. The JVM doesn't understand Java source code, that's why you compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM. It's also the entity that allows Java to be a "portable language" (write once, run anywhere).
JRE: The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. In addition, two key deployment technologies are part of the JRE: Java Plug-in, which enables applets to run in popular browsers; and Java Web Start, which deploys standalone applications over a network. It is also the foundation for the technologies in the Java 2 Platform, Enterprise Edition (J2EE) for enterprise software development and deployment. The JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications.
JDK: Java Development Kit is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.
In java, a just-in-time (JIT) compiler is a program that turns Java bytecode into instructions that can be sent directly to the processor.
A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides software-based platform.
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:
All JVM has atleast one class loader. The classloader are responsible of loading classes and interfaces.
There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.
OOPs stands for Object Oriented Programming. The concepts of oops in java are:
In object oriented programming, abstraction is one of the three central principles (along with encapsulation and inheritance). Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as as single unit. In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding.
Association establish relationship between any two objects. It can be defined as the multiplicity between any objects so you must know about the one to one, many to one, one to many and many to many as all these are the association between objects.
Association: A uses B, A is related to B
Aggregation is one of the special case of Association. The directional association present in between the objects can also be determined. When a single object ‘has-a’ another object and you will get the aggregation. The directions between the object will be specified with the object containing other object. Aggregation can also be called as “Has-a” relationship. For example, Employee "Has a" address.
Java composition is a design correlation between two classes that is based on the “has-a” and “whole/part” association or aggregation relationship. Composition takes the relationship one step further by ensuring that the containing object is responsible for the lifetime of the object it holds. If Object B is contained within Object A, then Object A is responsible for the creation and destruction of Object B. Unlike aggregation, Object B cannot exist without Object A.
Polymorphism is the ability of a class instance to behave as if it were an instance of another class in its inheritance tree, most often one of its ancestor classes. For example, in Java all classes inherit from Object. Therefore, you can create a variable of type Object and assign to it an instance of any class.
The best way to express polymorphism is via an abstract base class (or interface). Consider, a Human abstract class with an abstract method goPee(). This method is abstract because it can't be defined for human but can be defined for Male and Female.
Now, consider the implementation of Male and Female class.
And Female class is
Now, we can tell an entire room full of Humans to go pee.
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
String array will be emply not null
In java, there is no default values for local variables either it is primitive or object reference.
Ordering of static, public will not affect the result.
Object oriented programming language: Object oriented programming supports all features of oops and it doesn't has in built object. Object oriented programming languages are C++, C#, Java etc.
Most of the beginners get confused with this question. Lets look 2 code samples very closely:
In the first code sample, a is a main method local variable. Method local variables need to be initialized before using them, so it will give compile time error.
In the second code sample, a is class member variable, hence it will be initialized to default value that is 0
So, answer of the question is null
Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation.
The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class
Main method is static because object is not required to call static method if It were non static method, jvm creates object first then call main() method that will lead to the problem of extra memory allocation.
Yes, we can execute a program by using static block
Program compiles. But at runtime throws an error "NoSuchMethodError".
It is a java keyword that points to the current object.
If a class can inherit properties of many parent then process is called multiple inheritance.
To reduce complexity, Java doesn't support multiple inheritance.
Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).
Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.
Both this() and super() are constructor calls, and constructor call must be the first (and only first) call in a constructor. Otherwise, the Object constructor will be called more than once when instantiating a single object. So, we can't use this() and super() both in a constructor.
The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create.
Method overloading in Java occurs when two or more methods shares same name and fulfill at least one of the following condition:
Yes, we can have many main() methods in a class by overloading the main method.
Yes, we can have many main() methods in a class by overloading the main method.