Question. Why
do we use transient variable?
Ans:Transient
member variable does not serialized with the object. When the class gets
de-serialized, this variable gets initialized with a default value of its data
type (i.e. zero for integers).
Question. How
many types of casting are there in java.
Ans:
a)Explicit casting(When u assign superclass object to subclass object)
Object o = "hello";
String x;
x= (String) o;
b)implicit casting(when u assign subclass object to superclass)
String x1 = "hi";
Object o1;
o1 = x1;
Question:
What's the difference between constructors and other methods?
Answer:
Constructors must have the same name as the class and can not return a value.
They are only called once while regular methods could be called many
times.
Question:
What's the difference between constructors and methods?
Answer:
Constructors:
must have the same name as the class name.
does not return any value.
called once while making instance of the class
methods:
method name differ from class name
it may return/not return any value.
can be called many times.
Question:
Explain the usage of Java packages.
Answer:
way of
organizing the files
helps
resolving naming conflicts when classes with the same names.
protect data
from being used by the non-authorized classes.
Question: what
is difference between "==" and the method "equals()"?
Answer:
'==' : Check the actual object instances are same or not.
.equals: check if the contents are equal or not.
Question: what
is difference between && and &
Answer:
&&:if first condition fails then 2nd condition is not checked.
&:if first condition fails even it checks 2nd condition.
Question: How
can a subclass call a method or a constructor defined in a superclass?
Answer: for
method :super.myMethod();
for constructor:super();
Question: How
is shallow copy implemented in Java?
Answer:
Object cloning.
Question: How
would you make a copy of an entire Java object with its state?
Answer:
Implement Cloneable interface and override clone() method.
Question: If
you're overriding the method equals() of an object, which other method you also need to
override?
Answer:
hashCode()
Question:What are access modifiers in java?
Answer:
private: accessible only inside the class
default: accessible in same package only
protected: accessible in same package and all subclasses of
this class
public: accessible everywhere
Question: What
is polymorphism in java?
Answer:
Polymorphism means one name many form. Java implement it in 2 ways.
1) Method
overloading(Compile time polymorphism):
In same class, if name of the method remains common but the number and
type of parameters are different, then it is called method overloading in
Java.
public class alpha{
public int add(int a,int b) {
return a+b;
}
public int add(int a,int b,int c){
return a+b+c;
}
public static void main(String[] args){
alpha a = new alpha();
System.out.println(a.add(1,2));
System.out.println(a.add(1,2,3));
}
}
2) Method
overriding:
A
method is said to be overridden when one is in parent class and
another is in child class with the same name, same return type, same
parameter. It is implemented in two ways:
a)through inheritence:
public class Base{
public void show(){
System.out.println("base")
}
}
public class Derive extends Base{
public void show{
System.out.println("derive");
}
public static void main(String[] args){
Base b = new Derive();
b.show();
}
}
b)through java interface :
what is
enhanced for loop in java?
for loop
before java5(old form):
for (int i=0; i <
array.length; i++) {
System.out.println("Element: " +
array[i]);
}
for loop in
newer form
for (String element : array) {
System.out.println("Element: " +
element);
}
1)with old form there is
initialization,termination and increment.
with enhanced for loop we
dont require to go for these steps
2) new form increases the
clarity and readability.
Why do we go for enhanced for
loop even we have iterator?
when we use iterator
there is chances of getting it wrong.
during nested for loop,
with iterator we have to first take the outer variable and
then we can use it in inner variable.
for (Iterator i =
empList.iterator(); i.hasNext(); ) {
Emp
emp = (Emp) i.next();
for
(Iterator j = deptList.iterator(); j.hasNext(); )
db_opr.add(emp,j.next);
}
but with enhanced for
loop we can go straight forward as:
for (Emp emp : empList)
for
(Dept dept : deptList)
db_opr.add(emp,dept);
enhanced for loop has
limitations:
1)The program needs access to
the iterator in order to remove the current element.
2)it is not usable
for loops where you need to replace elements in a list or array
as you traverse it.
3)it is not usable for
loops that must iterate over multiple collections in parallel.
No comments:
Post a Comment