Java Oop Interview Questions
Java Oop Interview Questions
StringBuilder StringBuffer
string builder objects are string buffer objects are
not synchronized and not thread synchronized and thread safe.
safe.
what is a class
1. class is a virtual entity.
2. class acts as blueprint for objects.
3. class is used for storing related variables & methods
eg:
public class Student
{
public int sno = 1;
public String sname = “ramesh”;
public String subject = “java”;
}
what is an object
1. object is a real world entity.
2. we use new keyword for creating object
eg: Student s1 = new Student();
what is constructor
1. by using constructor we can initialize objects.
2. constructor name should be same as class name
3. constructor should not have return type
eg :
eg:
public class A
{
}
public class B extends A
{
}
what is polymorphism?
when an entity is appearing with the same name in different
forms, then that entity is said to exhibit polymorphism.
public class D {
public void m2() {
s.o.pln(“this is parent class m2 function”);
}
}
public class E extends D {
public void m2() {
s.o.pln(“this is child class m2 function”);
}
}
what is the use of this keyword?
this keyword is used to refer current instance
or current object.
eg: public class Doctor
{
public string name;
public int exp;
}
what is super keyword?
by using super keyword we can access parent
class members from child class.
public class F
{
public int x = 10;
}
public class G extends F
{
public int x = 20;
public void m3()
{
System.out.println(this.x);
System.out.println(super.x);
}
}
what is the difference between
instance variables and static variables?
Instance variable Static variable
public class H
{ H h1 = new H();
public int x = 10; System.out.println( h1.x );
public static int y = 20; System.out.println( H.y );
}
what is the difference between
instance methods and static methods?
Instance method (non static method) Static method
try
{
// code which may give error
}
catch( exception ex )
{
// error storing code
}
can we have multiple catch blocks for a try?
1. yes, a try block can be followed by multiple catch
blocks.
2. exceptions of child class should be handled first,
then parent class exceptions should be handled.
what is the difference between throw and throws?
throw throws
2. unchecked exceptions.
2. lists
3. maps
What is map?
1. map is used to store pairs of elements.
2. Keys should be unique, and values can be
duplicate, in map.
3. there are 3 types of maps.
eg:
HashMap<String, String> m =
new HashMap<String, Sting> ();
eg:
ArrayList<Integer> x = new ArrayList<Integer> ();
x.add(10);
x.add(20);
System.out.println(x);
What is the difference between ArrayList and
LinkedList?
Array List LinkedList
eg:
HashSet<String> h = new HashSet<String> ();
m.add( “java”);
m.add( “j2ee”);
System.out.println(h);
What is the difference between HashMap and
TreeMap?
HashMap TreeMap
List stores elements in insertion order Set stores elements in random order
(first come first store order)
List allows duplicate elements. Sets doesn’t allow duplicate elements.
Lists are slow compared to sets. Sets are faster than lists.
explain System.out.println()?
System is a predefined class of java.
out is a static object present in System class.
println() is a method of out.