Java Interview Questions With Answers
Java Interview Questions With Answers
Default Values:
Memory Size
Data Type Default Value Wrapper Class
(Byte)
byte 1 0 Byte
Short 2 0 Short
int 4 0 Integer
long 8 0 Long
float 4 0.0f Float
double 8 0 Double
char 2 - Character
boolean 1 bit False Boolean
5. Access modifiers?
Public:
Global Level Access. (inside and outside the package)
Example: public class student()
Private:
Class Level Access. (Inside the class only)
Example: private class student {}
Protected:
Same like public but to be used with “Extends” keyword
Example: protected class student {}
Default:
Package Level Access. (only inside the package)
Ans: It is the method of implementation in which our program is oriented in the form of,
Class:
- Combination of method & object
Method:
- Set of actions to be performed
Object:
- Instance of the class,
- It allocates memory
- Using object, we can call the method
Concepts:
Encapsulation
Inheritance
Polymorphism
Abstraction
7. Encapsulation?
Ans: Accessing one class property into another class using “extends” keyword.
Types:
Single Inheritance
- Accessing one parent class property into one child class.
Multiple Inheritance
It is impossible in Java
It can be overcome by Interface
More than two parent class properties accessed by one child class.
Multilevel Inheritance
- One class property is accessed by one child class which is being accessed by
another child class.
Hierarchical Inheritance
- One parent class property accessed by two (or) more child classes.
Hybrid
- Combination of single and multiple Inheritances.
More than two parent class properties accessed by one child class.
It is impossible in Java
But it can be overcome by Interface
Types:
Method Overriding
-Class names are different,
-Method names and parameters will be same.
Types:
Default/Non-parameterized
Parameterized
Types:
Local Variable
Class/Instance variable
Static variable
15. Static?
Static is a keyword which can be used in class variable and method level, to call
directly without using an object.
When a class variable is assigned as public static, the variable can be used inside
and outside the package. (class.objectvariable)
16. This vs. Super?
This:
Super:
17. Final-Finally-Finalize?
- Final class can’t be inherited It will execute whether the This method is used
- Final method can’t be overridden exception occurs or not to clear the memory
- Final variable values can’t be
changed
Throw Throws
It can handle only one Exception it can handle more than one Exception
While Do-while
The while loop in java executes one or more The do-while loop, however, tests
statements after testing the loop continuation the loop continuation condition after the first
condition at the start of each iteration. iteration has completed.
Break Continue
It will terminate the loop itself It will skip the particular iteration
Declaration:
Int a [] = {1, 2, 3, 4, 5 };
Features:
Mutable Mutable
Synchronized Asynchronized
Slower than string buffer heap Faster than string buffer heap
Immutable Mutable
If we add a duplicate value it will share the If we add the duplicate value it will create
memory. a new memory.
If we concatenate the values it will create If we append the values it will share the
new memory. memory.
Collection Collections
Types namely,
1.List
1. It will support dissimilar data types.
2.Set
2.It is dynamic memory allocation.
3. Queue
4.Map
27. List vs. Set vs. Map?
Iterator can traverse elements in a collection List Iterator can traverse in both
only in forward direction. forward and backward directions
33. Exception vs. Error?
Exception Error
Exceptions are the problems which can occur Errors mostly occur at runtime that's they
at runtime and compile time belong to an unchecked type
If you are creating your own Exception that is known as custom exception or user-
defined exception.
Java custom exceptions are used to customize the exception according to user need.
By the help of custom exception, you can have your own exception and message.
Pre-Increment Post-Increment
++i i++
Before assigning the value to the variable, After assigning the value to the variable,
the value is incremented by one the value is incremented
37. Explain main method syntax? And, Can we change it?
public: It is an access specifier. We should use a public keyword before the main() method so
that JVM can identify the execution point of the program. If we use private, protected, and
default before the main() method, it will not be visible to JVM.
static: You can make a method static by using the keyword static. We should call the main()
method without creating an object. Static methods are the method which invokes without
creating the objects, so we do not need any object to call the main() method.
void: In Java, every method has the return type. Void keyword acknowledges the compiler that
main() method does not return any value.
main(): It is a default signature which is predefined in the JVM. It is called by JVM to execute a
program line by line and end the execution after completion of this method. We can also
overload the main() method.
String args[]: The main() method also accepts some data from the user. It accepts a group of
strings, which is called a string array. It is used to hold the command line arguments in the form
of string values.
Ans:
Static methods cannot be overridden because they are not dispatched on the object
instance at runtime. The compiler decides which method gets called.
Static methods can be overloaded (meaning that you can have the same method name for
several methods as long as they have different parameter types).
39. Enum or Enumerator?
Enum Enumerator