Home >> Java
Java Interview Question 1:
What is the use of final keyword in Java ?
Java Interview answer
final keyword, if used with a variable(in its declaration), then the variable
can be initialized for once, and its value can never be changed. All final
variables should be initialized at the time of declaration. Values of final
variables cannot be changed.
A class, if declared as final, can not be a super class, a method, if declared
as final, cannot be overridden.
Java Interview Question 2:
Can values of variables defined within a final class, be changed after
instantiation? and if those variables declared as final, then?
Java Interview answer
Yes, values of variables within final class can be changed after instantiation,
but cannot be changed if those variables are declared as final.
Java Interview Question 3:
Can Java Interface be declared final? Justify your answer?
Java Interview Answer
No, as implementation of methods of interface is in a different class and works
in a dynamic polymorphism paradigm and variable of type interface will have to
be initialized by a different class, which is against the rules of final keyword.
Java Interview Question 4:
If super class method has access modifier as public, What are the possible
access modifier (private ,public, protected, default/no modifier)
allowed in the child class overridden method ?
Java Question Answer
Only public is allowed as access modifier in the sub class overridden method.
As there is always a same or stronger access privilege is allowed in method
overriding.
Java Interview Question 5:
if super class method access modifier is declared as private, then what
is the access modifier allowed in sub class overridden method?
Java Interview Answer
Method overriding is not possible in this case, as overriding is possible only
for those method which are accessible from outside class scope, or which are
accessible from instance variable.
Java Interview Question 6:
What is the result of following code? Justify?
class A
{
int i=20;
void show()
{
System.out.println("A");
}
}
public class SubClass extends A
{
int i=30;
public void show()
{
System.out.println("B");
}
}
Client code as:
A a = new SubClass();
a.show();
System.out.println(a.i);
Result:
B - overridden method B from SubClass
20 - instance variable i from A.
As show method is overridden, then the class method (super class or sub class)
to be called is decided based on the object (new SubClass()), not on the instance
variable (A a). But in case of variable (i), as it is not overridden, then i
from A class is called.
Question 7:
What is the result of following code? Justify?
interface I
{
void show(I i);
}
class A implements I
{
int i=20;
void show(I i)
{
System.out.println("A");
}
}
public class SubClass extends A
{
int i=30;
public void show(A a)
{
System.out.println("B");
}
}
What will be the result if following code executes in the client code:
A a = new SubClass();
a.show(new A());
The result is:
A
As this show method is not overridden, due to change in the method signature.
Question 8:
While overridden method throws exception, what is the rule applicable?
Sub class method should throw an exception that is same exception as that
of the super class method throws clause or a sub class/descendent of the super
class method throws clause exception or no throws clause at all.
like,
In super class:
...
public void show() throws java.lang.Exception
{
}
....
In sub class:
...
public void show() throws java.io.IOException
{
}
....
Secondly, if super class method that is to be overridden, has no throws clause
then the overridden method in sub class should not have any throws exception,
if exists then compilation error occurs.
Question 9:
How to create user defined unchecked exception? What is the difference
between checked and unchecked exception?
User defined unchecked exception should extend java.lang.RuntimeException,
but practically there is no use of user defined exception, as unchecked
or runtime exception don't propagate to the caller through throws clause.
unchecked exception should be handled within the method body only, where
as checked exception travels through the call stack trace.
Checked exception extends java.lang.Exception , whereas unchecked exception
extends java.lang.RuntimeException.
Question 10:
Suppose try block throws an exception, catch block catches this exception
and re-throws the same exception, but finally block returns a boolean.
What could be the result/output?
finally block overwrites all the activities in try and catch block, so the output
will be the boolean returned by finally block. If finally block throws some
exception, then that will be the final exception that is propagated to the caller.
I think, purpose of the interview is to find a right candidate for
the requirement in hand, for this it is always good to have the
resume reflect only those Technologies those are well practiced
/aware of, and not just mentioned as per project/project software
used. Sometimes I have seen resumes showing Apache ANT, even if it
is used for the build purpose and used by the SCM (Software
Configuration Management) or build/release teams, and developer has
not done any contribution to it, but by mentioning it in resume,
one has to do some homework or understanding of Apache ANT, so that
he can answer at least basic questions on this.
Like some common Java Interview Questions with answer are as follows:
Java Interview Question 11.
What is basic principles of OOP?
Java Interview Answer :
Abstraction, Encapsulation, Inheritence, Polymorphism are the basic
principles of Object Oriented Programming.
Java Interview Question 12.
What is difference between abstraction and encapsulation?
Java Interview Answer :
Abstraction is looking at anything from a specific context, like
for example Televison, from viewer's context, it is just an entertainment
box, but from actor's context, it is a medium to get popular. So in this
example entertainment and popularity are two abstraction of Television.
Encapsulation is the way of providing a wrapper over the implementation.
Different colorful mobile cases are the encapsulation over mobile phone,
by this we are able to hide the internal mechanism of mobile and be able
to provide some of the switches to user to properly and easily use mobile
phone.
If anything missed out , please let me know at
techienjoy at yahoo . com