Show Menu
Cheatography

Java Interview Questions Cheat Sheet by

A cheat sheet for the most frequently asked Java Interview Questions

Main Features of Java

Object Oriented
Simple
Platform Indepe­ndent
Secure
Robust
Portable
Multit­hreaded
Distri­buted

Principles of OOP

Abstra­ction
Abstra­ction means using simple things to represent complexity using object, classes, and variables.
Encaps­ulation
This is the practice of keeping fields within a class private, then providing access to them via public methods. It’s a protective barrier that keeps the data and code safe within the class itself.
Inheri­tance
It lets progra­mmers create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinve­nting the wheel.
Polymo­rphism
Polymo­rphism refers to any entity whether it is a method or a constr­uctor or an operator which takes many forms or can be used for multiple tasks.

Multit­hreaded Progra­mming

Multit­hreaded progra­mming is one of the key features of java which allows multiple threads to execute their task simult­ane­ously.

There are two ways to create threads in Java
a) by extending the Thread class
b) by implem­enting the Runnable interface

Final, Finally, Finalize

Final
Makes a variable, method, or class unchan­geable.
Finally
Exception handling after success or failure during a try catch block.
Finalize
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
 

Inheri­tance

Inheri­tance is one of the key principle of object oriented progra­mming. Through inheri­tance, one class can inherit the properties of another class. The class from which properties are inherited is called super class and the class to which properties are inherited is called sub class.

Does Java support multiple inheri­tance?
Java supports multiple inheri­tance but only through interf­aces. That means a class can implement more than one interfaces but can not extend more than one class.

Constr­uctor & Method Overlo­ading

A class can have any number of constr­uctors. These constr­uctors will have different list of arguments. It is called constr­uctor overlo­ading. Constr­uctor overlo­ading provides different ways to instan­tiate a class.

If a class has more than one method with same name but with different list of arguments, then it is called method overlo­ading.

Overriding

If a super class method is modified in the sub class then it is called method overri­ding.

Abstract Classes

An abstract class is a class which cannot be instan­tiated. An abstract class is used by creating an inheriting subclass that can be instan­tiated. An abstract class does a few things for the inheriting subclass:

1.Define methods which can be used by the inheriting subclass.
2.Define abstract methods which the inheriting subclass must implement.
3.Provide a common interface which allows the subclass to be interc­hanged with all other subcla­sses.
 

Exception Handling

try
The code to be monitored for exceptions will be kept in this block.
catch
If any exceptions occurred in try block, those exceptions will be caught by this block.
finally
This block will be always executed whether exception is raised or not and raised exceptions are caught or not.

Checked & Unchecked Exceptions

Checked
Known and checked at compile time.
Unchecked
Known at Runtime, unknown at compile time.

Static & Non-Static Methods

Static method is common to all instances of a class. Static methods are stored in the class memory. Where as non-static methods are stored in the object memory. Each instance of a class will have their own copy of non-static methods.

Synchr­oni­zation

Synchr­oni­zation is a way of contro­lling the access of a method or a block by multiple threads. Only one thread can enter into a method or a block which has been declared as synchr­onized. Synchr­oni­zation is one of the way to achieve thread safety.

Garbage Collection

Removing unwanted objects or abandoned objects from the memory is called garbage collec­tion. Garbage collection is done automa­tically in java. You need not to remove the unwanted objects explic­itly. Garbage collector thread does this for you.

Cloning

Cloning is a process of creating an exact copy of an existing object in the memory. Cloning may be shallow or deep. In java, clone() method is used to create a clone of an object.

Interfaces

A Java interface is a bit like a class, except a Java interface can only contain method signatures and fields. An Java interface cannot contain an implem­ent­ation of the methods, only the signature (name, parameters and except­ions) of the method.

You can use interfaces in Java as a way to achieve polymo­rphism.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by sdm7306