Java Interview Questions
Java Interview Questions
com
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.
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.
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:
Class(Method) Area
Heap
Stack
Program Counter Register
Native Method Stack
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.
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.
String jarPath =
Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(jarPath, "UTF-8");
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.
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.
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.
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.
No, you cannot override private method. A private method cannot be overridden
because it is not accessible to the derived class.
The Dot operator will give access to visible methods and variables of an Object,
and static variables and methods of a 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.
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.
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.
Nested classes that are declared static are simply called static nested classes.
Non-static nested classes are called inner classes.
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.
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.
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.