Anda di halaman 1dari 8

Net-informations.

com

Java Interview Questions and Answers


The practical guide to the Core Java interview process

http://net-informations.com/java/cjava/default.htm
Is there any need to import java.lang package?

No, you don't need to import java.lang.*; All classes in the java.lang package
are imported by default and it includes the core Java language classes.

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

No, JDK (Java Development Kit) isn't required on each machine to run a Java
program. Only JRE is required, it is an implementation of the Java Virtual
machine (JVM), which actually executes Java programs. JDK is development
Kit of Java and is required for development only. It is a bundle of software
components that is used to develop Java based applications.

What is a package?

Packages are used to organizes a set of related classes and interfaces. Programs
are organized as sets of packages. Each package has its own set of names for
types, which helps to prevent name conflicts.

How can I convert my Java program to an .exe file?

there are many options available to make a Java program to a .exe file like
Executable Jar File, JSmooth, JexePack, LaunchAnywhere etc. But there is a
simple way, to make a .bat with the following code:

start javaw -jar YourJarFile.jar


then convert the bat to an exe using any .bat to .exe converter.

More Java Interview Questions: http://net-informations.com/java/cjava/default.htm


How many types of memory areas are allocated by JVM?

There are five type of memory allocated by JVM

Class(Method) Area
Heap
Stack
Program Counter Register
Native Method Stack

Java is Pass by Value or Pass by Reference?

Java is always pass-by-value, not reference. Java does manipulate objects by


reference, and all object variables are references. So Objects, Arrays, Primitive
Types etc. these are all passed by value in Java.

Is null a keyword?

No, null is not a keyword. The null is a literal similar to true and false in java,
but it is a character string that is treated specially by the compiler if the
compiler encounters it in a java source file. It treated as a reserved word in java
language.

Are true and false keywords?

No, true, false, and null are not Java keywords, they are literals. Literals are
something which are actual values not the variable names. If you try to use them
as variables you will get compile error saying - invalid VariableDeclaratorId.
They treated as reserved words in java language and you cannot use them as
identifiers in your programs.

More Java Interview Questions: http://net-informations.com/java/cjava/default.htm


How to get the path of a running JAR file?

String jarPath =
Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(jarPath, "UTF-8");

Can Enum extend any class in Java?

No, Enum types are final by design. An enum cannot extends another class
because an enum already extends Enum < T > . That class provides all the
enum functionality. All enums implicitly extend java.lang.Enum. Because a
class can only extend one parent and the Java language does not support
multiple inheritance of state, therefore an enum cannot extend anything else.

Should a main() method be compulsorily declared in all java classes?

No, the main method is the default entry point for a programme. That doesn't
mean that, every java class should contain main method. It is driver program
which handles all other java files. It is important to note that all classes of java
should be connected with main() method directly or indirectly.

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

No. The main method's return type must be void, because the java language
specification enforces it. You can't change the return type of main(). Once the
main is finished the program is dead, so you don't have any benefit from that.

More Java Interview Questions: http://net-informations.com/java/cjava/default.htm


Can we call a non-static method from inside a static method?

Yes, you can call a non-static method from a static method is to have an
instance of the class containing the non-static method.

Can you override a private or static method in Java?

No, you cannot override private method. A private method cannot be overridden
because it is not accessible to the derived class.

What is dot(.) operator in Java?

The Dot operator will give access to visible methods and variables of an Object,
and static variables and methods of a Class.

How to get current time in milliseconds?

long timeInMillis = Calendar.getInstance().getTimeInMillis();

Difference between a static and a non-static inner class?

Nested classes are divided into two categories: static and non-static. Nested
classes that are declared static are simply called static nested classes. Non-static
nested classes are called inner classes. A non-static nested class has full access
to the members of the class within which it is nested. A static nested class does
not have a reference to a nesting instance, so a static nested class cannot invoke
non-static methods or access non-static fields of an instance of the class within
which it is nested.

More Java Interview Questions: http://net-informations.com/java/cjava/default.htm


Difference between a break statement and a continue statement?

Break statement: If a particular condition is satisfied then break statement exits


you out from a particular loop or switch case etc.
Continue statement: When the continue statement is encountered in a code, all
the actions up till this statement are executed again without execution of any
statement after the continue statement.

Difference between an if statement and a switch statement?

The if statement is used to select among two alternatives only. It uses a boolean
expression to decide which alternative should be executed. While the switch
statement is used to select among multiple alternatives. It uses an int expression
to determine which alternative should be executed.

We can't operate float or double values using switch case, but we can do using if
else statements. A switch statement works much faster than equivalent if-else
ladder. It is because compiler generates a jump table for a switch during
compilation. Consequently, during execution, instead of checking which case is
satisfied, it only decides which case has to be executed.

What is object cloning?

The object cloning is a way to create exact copy of an object. The clone()
method provides this functionality. Every object has also a clone method which
can be used to copy the object. For this purpose, clone() method of Object class
is used to clone an object.

More Java Interview Questions: http://net-informations.com/java/cjava/default.htm


Is there any difference between nested classes and inner classes?

Nested classes that are declared static are simply called static nested classes.
Non-static nested classes are called inner classes.

Can an Interface implement another Interface?

The word implements means implementation, when interface is meant to


declare just to provide interface not for implementation. Only a class can do
this. You can't implement an interface from another interface. It specifies what
needs to be implemented. But interfaces use inheritance, so you can extend an
interface and define a subinterface that can require additional methods.

Can a class be defined inside an Interface?

Yes, you can have classes inside interfaces. Specifying a class inside an
interface ties that class directly to that interface. Users which use that interface
will have access to that class and all the functionality that it provides.

Can an Interface be defined inside a class?

Java programming language allows defining inner classes and interfaces. This is
typically useful if you want to limit visibility of this class or interface by scope
of current outer class. If there is a situation, inside your class you may need
multiple implementations of an interface, which is only relevant to this
particular class. In that case make it an inner interface.

More Java Interview Questions: http://net-informations.com/java/cjava/default.htm


Can we define private and protected modifiers for variables in interfaces?

Interface is like a blueprint of any class, where you declare your members. Any
class that implement that interface is responsible for its definition. Having
private or protected members in an interface doesn't make sense conceptually.
Only public members matter to the code consuming the interface. The public
access specifier indicates that the interface can be used by any class in any
package.

More Java Interview Questions and Answers:


http://net-informations.com/java/cjava/default.htm

Anda mungkin juga menyukai