Key Concepts in Java |
|
Q16) Can an abstract class have a static method? Ans) Yes an abstract class have a static method and it can be accessed by any other class(even not a concrete class). |
|
Q17) Can an abstract class have a constructor? Ans) Yes an abstract class have a default and parameterized constructors. |
Q18) Why static methods cannot access non static variables or methods? Ans) A static method cannot access non static variables or methods because static methods doesnt need the object to be accessed. So if a static method has non static variables or non static methods which has instantiated variables they will no be intialized since the object is not created and this could result in an error. |
Q19) What is difference between stringbuffer and stringbuilder? Ans) The only difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer. Criteria to choose among StringBuffer and StringBuilder 1)If your text can change and will only be accessed from a single thread, use a StringBuilder 2)because StringBuilder is unsynchronized. If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous. |
Q20) Consider a scenario in which the admin want to sure that if some one has written System.exit() at some part of application then before system shutdown all the resources should be released. How is it possible?
Ans) This is possible using Runtime.getRuntime().addShutdownHook(Thread hook).
2. The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.
A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method. |
|
Q21) What is the difference between final, finally and finalize() in Java?
Ans) |
Q22) How does Java allocate stack and heap memory?
Ans) Each time an object is created in Java it goes into the area of memory known as heap. The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked and stack pointer is decremented when a method call is completed. |
Q23) Explain re-entrant, recursive and idempotent methods/functions?
Ans) A method in stack is re-entrant allowing multiple concurrent invocations that do not interfere with each other. |
Q24)Can a private variable or method of a class can be accessed? Ans) Yes its possible using reflection. |
Q25)What is difference between static block and the init block? Ans) The static block is loaded when the class is loaded by the JVM for the 1st time only whereas init {} block is loaded every time class is loaded. Also first the static block is loaded then the init block.
public class LoadingBlocks {
static{ System.out.println("Inside static"); } { System.out.println("Inside init"); } public static void main(String args[]){ new LoadingBlocks(); new LoadingBlocks(); new LoadingBlocks(); } } Output: Inside static Inside init Inside init Inside init |
Q26)Why inner class can access only final variable? Ans) Local classes can most definitely reference instance variables. The reason they cannot reference non final local variables is because the local class instance can remain in memory after the method returns. When the method returns the local variables go out of scope, so a copy of them is needed. If the variables weren't final then the copy of the variable in the method could change, while the copy in the local class didn't, so they'd be out of synch. |
Q27)What is fully abstract class? Ans) An abstract class which has all methods as abstract and all fields are public static final. |
Q28)What is dynamic binding and static binding? Ans)
Method invocation |
Q29) What is Java Reflection? Ans)Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. |
Q30) When an obj is passed through a function , u can set the properties but u cannot set a new memory location? Ans) It is because when u pass an object the address value is passed and stored in some new address . like if address 1234 is passed , it is stored in 4567 location. So if u change in the value of an object it will take the address from 4567 and do 1234.setXXX(). If u set the object to null it will set 4567=null. |