Professional Documents
Culture Documents
JAVAinterview Questions-Part1
JAVAinterview Questions-Part1
Static Block in JAVA :- Java supports a special block, called static block (also called static clause) which can be used for
static initializations of a class. The code inside static block is executed only once: the first time the class is loaded into
memory. Static blocks are executed before constructors.
Eg . class Test {
static int i;
int j;
// start of static block
static {
i = 10;
System.out.println("static block called ");
}
// end of static block
}
class Main {
public static void main(String args[]) {
// Although we don't have an object of Test, static block is
// called because i is being accessed in following statement.
System.out.println(Test.i);
}
}
Output:
static block called
10
Static initializers in JAVA : Java provides a feature called a static initializer that’s designed specifically to let you initialize
static fields. A static initializer block resembles a method with no name, no arguments, and no return type. There is no need
to refer to it from outside the class definition. The code in a static initializer block is executed by the virtual machine when
the class is loaded.
Eg. static
{
//CODE
}
Via this() we can call one contructor of same class from other constructor. o chain to a particular superclass constructor
instead of one in the same class, use super instead of this. Note that you can only chain to one constructor, and it has to be
the first statement in your constructor body
In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a
specific implementation of a method that is already provided by one of its super-classes or parent classes.
When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a
method in its super-class, then the method in the subclass is said to override the method in the super-class. Method
overriding is one of the way by which java achieve Run Time Polymorphism
The most basic difference is that overloading is being done in the same class while for overriding base and child classes are
required. Overriding is all about giving a specific implementation to the inherited method of parent class.
Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.
Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.
private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one
private/final methods of same name but a child class cannot override the private/final methods of their base class.
Return type of method does not matter in case of method overloading, it can be same or different. However in case of
method overriding the overriding method can have more specific return type
Argument list should be different while doing method overloading. Argument list should be same in method Overriding.
JAVA is Concurrent i.e. it can execute many statement at once instead of executing them sequentially. Java is class based and
object oriented programming language. It is a platform independent language i.e. the compiled code can be run on any java
supporting platform. It runs on the logic of “Write once, run anywhere”.
Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument
lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor
having different argument lists. Method overloading is an example of Static Polymorphism.
C++ uses compiler only. C++ is compiled and run using the compiler which converts source code into machine code so, C+
+ is platform dependent. WHEREAS JAVA uses compiler and interpreter both. Java source code is converted into bytecode
at compilation time. The interpreter executes this bytecode at runtime and produces output. Java is interpreted that is why it
is platform independent.
Bytecode is program code that has been compiled from source code into low-level code designed for a software interpreter.
It may be executed by a virtual machine (such as a JVM) or further compiled into machine code, which is recognized by
the processor.
A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that
are common to all objects of one type. In general, class declarations can include these components, in order:
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc.
It is a basic unit of Object Oriented Programming and represents the real life entities. A typical Java program creates many
objects, which as you know, interact by invoking methods. An object consists of :
State : It is represented by attributes of an object. It also reflects the properties of an object.
Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
Identity : It gives a unique name to an object and enables one object to interact with other objects.
Example of an object : dog
15. What is method in JAVA ?
A Java method is a collection of statements that are grouped together to perform an operation.
16. What is encapsulation ?
Encapsulation in Java is a mechanism to wrap up variables(data) and methods(code) together as a single unit. It is the
process of hiding information details and protecting data and behavior of the object. It is one of the four important OOP
concepts. The encapsulate class is easy to test, so it is also better for unit.
17. Explain about main() method in Java
Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args).
102.