Java Interview
Java Interview
Ques: What modifiers may be used with an inner class that is a member of an
outer class?
Answer: A (non-local) inner class may be declared as public, protected, private,
static, final, or abstract.
Ques: Which java. util classes and interfaces support event handling?
Answer: The Event Object class and the Event Listener interface support event
processing.
Ques: What’s new with the stop(), suspend() and resume() methods in JDK 1.2 ?
Answer:
These methods have been deprecated in JDK 1.2.
Ques: What value does read Line() return when it has reached the end of a file?
Answer: The readLine() method returns null when it has reached the end of a file.
Int can be assigned directly to long .Automatic type conversion takes place if int is
assigned to long because long is larger datatype than int.
Widening Conversion comes under Automatic type conversion.
Ques: What is the difference between the prefix and postfix forms
of the ++ operator?
Answer: The prefix form performs the increment operation and returns the value
of the increment operation. The postfix form returns the current value all of the
expression and then performs the increment operation on that value.
Ques: Can we define a package statement after the import statement in java?
Answer: We can’t define a package statement after the import statement in java.
a package statement must be the first statement in the source file. We can have
commented before the package statement.
Ques: Define How many objects are created in the following piece of code?
MyClass c1, c2, c3; c1 = new MyClass (); c3 = new MyClass ();
Answer: Only 2 objects are created, c1 and c3. The reference c2 is only declared
and not initialized.
Ques: How can we find the actual size of an object on the heap?
Answer: In Java, there is no way to find out the actual size of an object on the
heap.
Ques: What is the difference between access specifiers and access modifiers in
java?
Answer: In C++ we have access specifiers as public, private, protected and
default and access modifiers as static, final. But there is no such division of
access specifiers and access modifiers in java. In Java, we have access to
modifiers and nonaccess modifiers. Access Modifiers: public, private, protected,
default Non Access Modifiers: abstract, final, strip.
Ques: String and StringBuffer both represent String objects. Can we compare
String andStringBuffer in Java?
Answer: Although String and StringBuffer both represent String objects, we can’t
compare them with each other and if we try to compare them, we get an error.
Ques: Can we cast any other type to Boolean Type with type casting?
Answer: No, we can neither cast any other primitive type to Boolean data type
nor can cast Boolean data typeto any other primitive data type.
Ques: Can we catch more than one exception in a single catch block?
Answer: From Java 7, we can catch more than one exception with a single
catch block. This type of handling reduces the code duplication.
Note: When we catch more than one exception in a single catch block, catch
parameter is implicitly final.
We cannot assign any value to catch parameter.
Ex : catch(ArrayIndexOutOfBoundsException || ArithmeticException e)
In the above example, e is final we cannot assign any value or modify e in the
catch statement.
Ques: What are abstract methods in java?
Answer: An abstract method is a method which doesn’t have anybody. An
abstract method is declared with keyword abstract and semicolon in place of the
method body.
Signature : public abstract void ();
Ex : public abstract void get details();
It is the responsibility of subclass to provide implementation to an abstract
method defined in the abstract class.
Ques: What is the difference between inner class and nested class?
Answer: When a class is defined within a scope of another class, then it becomes
inner class. If the access modifier of the inner class is static, then it becomes
nested class.
Ques: Can you call one constructor from another if a class has multiple
constructors?
Answer: Yes, use this() syntax.