AdSense

AdSense3

Thursday, 2 July 2015

Java Important Interview Questions - 1

1. Can Java thread object invoke start method twice?

Code:
package com.kundan;

public class Example extends Thread{

 public void run(){
  System.out.println("Run");
 }
 
 public static void main(String a[]){
  Thread t1 = new Thread(new Example());
  t1.start();
  t1.start();
 }
}
Answer:
No, it throws IllegalThreadStateException

2. Give the list of Java Object class methods.


Answer:
 clone() - Creates and returns a copy of this object.
 equals() - Indicates whether some other object is "equal to" this one.
 finalize() - Called by the garbage collector on an object when garbage collection
   determines that there are no more references to the object.
 getClass() - Returns the runtime class of an object.
 hashCode() - Returns a hash code value for the object.
 notify() - Wakes up a single thread that is waiting on this object's monitor.
 notifyAll() - Wakes up all threads that are waiting on this object's monitor.
 toString() - Returns a string representation of the object.
 wait() - Causes current thread to wait until another thread invokes the notify() method
   or the notifyAll() method for this object.

3. Can we call servlet destory() from service()?


Answer:
As we know, destroy() is part of servlet life cycle methods, it is used to kill the
servlet instance. Servlet Engine is used to call destroy(). In case, if you call destroy
method from service(), it just execute the code written in the destroy(), but it wont
kill the servlet instance. destroy() will be called before killing the servlet instance
by servlet engine.

4. Can we override static method?


Answer:
We cannot override static methods. Static methods are belongs to class, not belongs
to object. Inheritance will not be applicable for class members

5. Can you list serialization methods?


Answer:
Serialization interface does not have any methods. It is a marker interface.
It just tells that your class can be serializable.

6. What is the difference between super() and this()?


Answer:
super() is used to call super class constructor, whereas this() used to call
constructors in the same class, means to call parameterized constructors.

7. How to prevent a method from being overridden?


Answer:
By specifying final keyword to the method you can avoid overriding
in a subcalss. Similarlly one can use final at class level to
prevent creating subclasses.

8. Can we create abstract classes without any abstract methods?


Answer:
Yes, we can create abstract classes without any abstract methods.

9. How to destroy the session in servlets?


Answer:
By calling invalidate() method on session object, we can destroy the session.

10. Can we have static methods in interface?


Answer:
By default, all methods in an interface are declared as public, abstract. It will never be static. But this
concept is changed with java 8. Java 8 came with new feature called "default methods" with in interfaces

11. What is transient variable?


Answer:
Transient variables cannot be serialized. During serialization process,
transient variable states will not be serialized. State of the value will
be always defaulted after deserialization.

12. Incase, there is a return at the end of try block, will execute finally block?


Answer:
Yes, the finally block will be executed even after writing return statement
at the end of try block. It returns after executing finally block.

13. What is abstract class or abstract method?


Answer:
We cannot create instance for an abstract class. We can able to create
instance for its subclass only. By specifying abstract keyword just before
class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is
just method signature, it does not contains any implementation. Its subclass
must provide implementation for abstract methods. Abstract methods are looks
like as given below:

public abstract int getLength();

14. What is default value of a boolean?


Answer:
Default value of a boolean is false.

15. When to use LinkedList or ArrayList?


Answer:
Accessing elements are faster with ArrayList, because it is index based.
But accessing is difficult with LinkedList. It is slow access. This is
to access any element, you need to navigate through the elements one by
one. But insertion and deletion is much faster with LinkedList, because
if you know the node, just change the pointers before or after nodes.
Insertion and deletion is slow with ArrayList, this is because, during
these operations ArrayList need to adjust the indexes according to
deletion or insetion if you are performing on middle indexes. Means,
an ArrayList having 10 elements, if you are inserting at index 5, then
you need to shift the indexes above 5 to one more.

16. What is daemon thread?


Answer:
Daemon thread is a low priority thread. It runs intermittently in the back ground, 
and takes care of the garbage collection operation for the java runtime system. 
By calling setDaemon() method is used to create a daemon thread.

17. Does each thread in java uses seperate stack?


Answer:
In Java every thread maintains its own separate stack. It is
called Runtime Stack but they share the same memory.

18. What is the difference between Enumeration and Iterator?


Answer:
The functionality of Enumeration and the Iterator are same. You can get remove() 
from Iterator to remove an element, while while Enumeration does not have remove()
method. Using Enumeration you can only traverse and fetch the objects, where as using
Iterator we can also add and remove the objects. So Iterator can be useful if you want
to manipulate the list and Enumeration is for read-only access.

19. Find out below switch statement output.

Code:
public static void main(String a[]){
 int price = 6;
 switch (price) {
  case 2: System.out.println("It is: 2");
  default: System.out.println("It is: default");
  case 5: System.out.println("It is: 5");
  case 9: System.out.println("It is: 9");
 }
}

Answer:
It is: default
It is: 5
It is: 9
If there is a break in between then the switch will return with the defined case. If there is no break, switch will execute from the first matched case till the end.

20. Does system.exit() in try block executes code in finally block?

Code:
 try{
  System.out.println("I am in try block");
  System.exit(1);
 } catch(Exception ex){
  ex.printStackTrace();
 } finally {
  System.out.println("I am in finally block!!!");
 }

Answer:
It will not execute finally block. The program will be terminated
after System.exit() statement.

21. What is fail-fast in java?


Answer:
A fail-fast system is nothing but immediately report any failure that
is likely to lead to failure. When a problem occurs, a fail-fast system
fails immediately. In Java, we can find this behavior with iterators.
Incase, you have called iterator on a collection object, and another
thread tries to modify the collection object, then concurrent modification
exception will be thrown. This is called fail-fast.

22. What is final, finally and finalize?


Answer:
final:
 final is a keyword. The variable decleared as final should be
 initialized only once and cannot be changed. Java classes
 declared as final cannot be extended. Methods declared as final
 cannot be overridden.
 
finally:
 finally is a block. The finally block always executes when the
 try block exits. This ensures that the finally block is executed
 even if an unexpected exception occurs. But finally is useful for
 more than just exception handling - it allows the programmer to
 avoid having cleanup code accidentally bypassed by a return,
 continue, or break. Putting cleanup code in a finally block is
 always a good practice, even when no exceptions are anticipated.
 
finalize:
 finalize is a method. Before an object is garbage collected, the
 runtime system calls its finalize() method. You can write system
 resources release code in finalize() method before getting garbage
 collected.

23. In java, are true and false keywords?


Answer:
true, false, and null might seem like keywords, but they are actually
literals. You cannot use them as identifiers in your programs.

24. What are the different session tracking methods?


Answer:
Cookies:
 You can use HTTP cookies to store information. Cookies will be
 stored at browser side.

URL rewriting:
 With this method, the information is carried through url as
 request parameters. In general added parameter will be sessionid,
 userid. 

HttpSession:
 Using HttpSession, we can store information at server side. Http
 Session provides methods to handle session related information.
 
Hidden form fields:
 By using hidden form fields we can insert information in the webpages
 and these information will be sent to the server. These fields are not
 visible directly to the user, but can be viewed using view source
 option from the browsers. The hidden form fields are as given below:
 
 <input type='hidden' name='siteName' value='kundanblogs'/>

25. What is the purpose of garbage collection?


Answer:
The garbage collection process is to identify the objects which are
no longer referenced or needed by a program so that their resources can be
reclaimed and reused. These identified objects will be discarded.

No comments:

Post a Comment