Java Interview Questions
Java Interview Questions
Platform independence means that we can write and compile the java code in one platform (eg
Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3. What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
JVM's are not platform independent. JVM's are platform specific run time implementation provided
by the vendor.
JDK is Java Development Kit which is for development purpose and it includes execution
environment also. But JVM is purely a run time environment and hence you will not be able to
compile your source files using a JVM.
java.lang.Object
Java uses primitive data types and hence is not a pure object oriented language.
Path and Classpath are operating system level environment variales. Path is used define where the
system can find the executables(.exe) files and classpath is used to specify the location .class files.
9. I don't want my class to be inherited by any other class. What should i do?
You should declared your class as final. But you can't define your class as final, if it is
an abstract class. A class declared as final can't be extended by any other class.
We cannot declare top level class as static, but only inner class can be declared static.
public class Test
{
static class InnerClass
{
public static void InnerMethod()
{ System.out.println("Static Inner Class!"); }
}
public static void main(String args[])
{
Test.InnerClass.InnerMethod();
}
}
//output: Static Inner Class!
The program compiles properly but at runtime it will give "main() method not public." message.
No the program fails to compile. The compiler says that the main() method is already defined in
the class.
When a class defines a method using the same name, return type, and arguments as a method in
its superclass, the method in the class overrides the method in the superclass.
When the method is invoked for an object of the class, it is the new definition of the method that is
called, and not the method definition from superclass. Methods may be overridden to be more
public, not more private.
16. What is the difference between declaring a variable and defining a variable?
In declaration we just mention the type of the variable and it's name. We do not initialize it. But
defining means declaration + initialization.
Java only supports pass by value. With objects, the object reference itself is passed by value and
so both the original reference and parameter copy both refer to the same object.
Java provides specialized classes corresponding to each of the primitive data types. These are
called wrapper classes.
It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes
store objects and not primitive data types. And also the wrapper classes provide many utility
methods also.
Because of these reasons we need wrapper classes. And since we create instances of these classes
we can store them in any of the collection classes and pass them around as a collection. Also we
can pass them around as method parameters where a method expects an object.
These JVM errors and you can not repair them at runtime. While exceptions are conditions that
occur because of bad input etc. Example: FileNotFoundException will be thrown if the
specified file does not exist. Or aNullPointerException will take place if you try using a null
reference.
In most of the cases it is possible to recover from an exception (probably by giving user a
feedback for entering proper values etc.).
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions.
and
2. List the desired exceptions in the throws clause of the method and let the caller of the method
hadle those exceptions.
22. Is it necessary that each try block must be followed by a catch block?
A while statement checks at the beginning of a loop to see whether the next loop iteration should
occur.
A do statement checks at the end of a loop to see whether the next iteration of a loop should
occur. The do statement will always execute the body of a loop at least once.
stmt.exceuteUpdate();
Delegation means that you include an instance of another class as an instance variable, and
forward messages to the instance. It is often safer than inheritance because it forces you to think
about each message you forward, because the instance is of a known class, rather than a new
class, and because it doesn't force you to accept all the methods of the super class: you can
provide only the methods that really make sense. On the other hand, it makes you write more
code, and it is harder to re-use (because it is not a subclass).
Interpreter
A singleton class in java can have only one instance and hence all its methods and variables
belong to just one instance. Singleton class concept is useful for the situations when there is a
connection to a database due to some driver limitations or because of any licensing issues.
34. What are Loops in Java? What are three types of loops?
for, while and do-while.
35. What is an infinite Loop? How infinite loop is declared?
Ans: An infinite loop runs without any condition and runs infinitely. An infinite loop can be
broken by defining any breaking logic in the body of the statement blocks.
40. Can we have two methods in a class with the same name?
Ans: We can define two methods in a class with the same name but with different
number/type of parameters. Which method is to get invoked will depend upon the
parameters passed.
41. Can a constructor have different name than a Class name in Java?
Ans: Constructor in Java must have same name as the class name and if the name is
different, it doesn’t act as a constructor and compiler thinks of it as a normal method.
43. What is JDBC – JDBC is a set of Java API for executing SQL statements