Key Concepts in Java |
Q1) Why is main() method static? Ans) To access the static method the object of the class is not needed. The method can be access directly with the help of ClassName. So when a program is started the jvm search for the class with main method and calls it without creating an object of the class. |
Q2) What is the difference between static methods and instance methods? Ans) instance method belongs to the instance of a class therefore it requires an instance before it can be invoked, whereas static method belongs to the class itself and not to any class instance so it doesn’t need an instance to be invoked. |
Q3) Can static block throw exception? Ans) Yes, static block can throw only Runtime exception or can use a try-catch block to catch checked exception. |
Q4 What is difference between abstract class and interface? Ans)
1) A class is called abstract when it contains at least one abstract method. It can also contain n numbers of concrete method.Interface can contain only abstract( non implemented) methods. |
Q5) Explain with example to describe when to use abstract class and interface?
Ans) Consider a scenario where all Cars will have 4 tyres and other features can be different. public abstract class Car{ public abstract String getCarName(); public final int getNoOfTyres(){ return 4; } } Consider a scenario where Cars can have any number of tyres and other features can also be different. In this case interface will be created. public interface Car{ public abstract String getCarName(); public abstract int getNoOfTyres(); } |
Q6) Does java support multiple interitance? Why?
Ans) Java doesnt support multiple inheritance but it provide a way through
which it can enact it.
Class A{ public void add(){ // some text } } Class B{ public void add(){ // some text } } Class C extends A,B{ public static void main(String arg[]){ C objC = new C(); objC.add(); // problem, compiler gets confused and cant decide to call Class A or B method. }
This problem is called Diamond problem.
interface A{
add(); } interface B{ add(); } class C implements A,B{ add(){ // doesnt matter which interface it belong to } } |
Q7) Can this keyword be assigned null value? Ans) No |
Q8) What are the different types of references in java? Ans) Java has a more expressive system of reference than most other garbage-collected programming languages, which allows for special behavior for garbage collection. A normal reference in Java is known as a strong reference. The java.lang.ref package defines three other types of references—soft, weak, and phantom references. Each type of reference is designed for a specific use. A SoftReference can be used to implement a cache. An object that is not reachable by a strong reference (that is, not strongly reachable), but is referenced by a soft reference is called softly reachable. A softly reachable object may be garbage collected at the discretion of the garbage collector. This generally means that softly reachable objects will only be garbage collected when free memory is low, but again, it is at the discretion of the garbage collector. Semantically, a soft reference means "keep this object unless the memory is needed." A WeakReference is used to implement weak maps. An object that is not strongly or softly reachable, but is referenced by a weak reference is called weakly reachable. A weakly reachable object will be garbage collected during the next collection cycle. This behavior is used in the class java.util.WeakHashMap. A weak map allows the programmer to put key/value pairs in the map and not worry about the objects taking up memory when the key is no longer reachable anywhere else. Another possible application of weak references is the string intern pool. Semantically, a weak reference means "get rid of this object when nothing else references it."
|
Q9) How to change the heap size of a JVM? Ans)
The old generation's default heap size can be overridden by using the -Xms and -Xmx switches to specify the initial and maximum sizes respectively: |
Q10) What is difference between instanceof and isInstance(Object obj)? Ans) Differences are as follows: 1) instanceof is a reserved word of Java, but isInstance(Object obj) is a method of java.lang.Class. if (obj instanceof MyType) { 2) instanceof is used of identify whether the object is type of a particular class or its subclass but isInstance(obj) is used to identify object of a particular class. |
Q11) Java supports pass by value or pass by reference? Ans) Java supports only pass by value. The arguments passed as a parameter to a method is mainly primitive data types or objects. For the data type the actual value is passed.
public void tricky(Point arg1, Point arg2) { arg1.x = 100; arg1.y = 100; Point temp = arg1; arg1 = arg2; arg2 = temp; } public static void main(String [] args) { Point pnt1 = new Point(0,0); Point pnt2 = new Point(0,0); System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); System.out.println(" "); tricky(pnt1,pnt2); System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); }
OutPut:
X: 0 Y: 0 X: 0 Y: 0 X: 100 Y: 100 X: 0 Y: 0 The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references. |
Q12) What is memory leak? Ans) A memory leak is where an unreferenced object that will never be used again still hangs around in memory and doesnt get garbage collected. |
Q13) What is the difference between equals() and ==?
Ans) == operator is used to compare the references of the objects.
String str1 = "MyName";
String str2 = "MyName"; String str3 = str2; if(str1 == str2){ System.out.println("Objects are equal") }else{ System.out.println("Objects are not equal") } if(str1.equals(str2)){ System.out.println("Objects are equal") }else{ System.out.println("Objects are not equal") } Output: Objects are not equal Objects are equal String str2 = "MyName"; String str3 = str2; if(str2 == str3){ System.out.println("Objects are equal") }else{ System.out.println("Objects are not equal") } if(str3.equals(str2)){ System.out.println("Objects are equal") }else{ System.out.println("Objects are not equal") } OutPut: Objects are equal Objects are equal |
Q14) How to make sure that Childclass method actually overrides the method of the superclass? Ans) The @Override annotation can be added to the javadoc for the new method. If you accidently miss an argument or capitalize the method name wrong, the compiler will generate a compile-time error. |
Q15) How to find the size of an object? Ans)The heap size of an object can be found using - Runtime.totalMemory()-Runtime.freeMemory() . |
More Questions >> |