Wednesday, October 30, 2013

Java Interview Questions

Q 1: What happens if you don’t override equals method?

A: If you don’t override equals method then you won’t be able to use those objects as key in hashtable and you probably won’t get accurate Sets such that there are no conceptual duplicates.

Q2: How do you implement equals method?

A:    Suppose you have an Employee Class which looks like below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Employee{
      private long employeeId;
      private String name;
      private int salary;
      //... public getter and setter methods
      public boolean equals(Object other){
        if((other instanceof Employee) &&
           ((Employee)other).getEmployeeId() == this.employeeId){
           return true;
        }else{
           return false;
        }
     }
}

Q3:  What is equals contract?

A: Equals contract says that:
  1. It is reflexive. For any any reference value x, x.equals(x) should return true.
  2. It is symmetric. For any reference values x and y, x.equals(y) should return true if and only if y.equals(x) is true.
  3. It is transitive. For any reference values x, y and z, if x.equals(y) returns true and y.equals(z) returns true then x.equals(z) must return true.
  4. It is consistent. For any reference values x and y, multiple invocations of x.equals(y) consistently returns true or consistently returns false, provided no information used in equals comparision on the object is modified.
  5. For any non-null reference value x, x.equals(null) should return false.

Q4:In How Many Ways can you create an object for a class in java?

A: There are four ways in which we can create an object for a class in java
  1. Using New Operator: like Test t = new Test();
  2. Using newInstance(): like
Test t = (Test) Class.forName("").newInstance();
  1. Using Clone: like
Test x=new Test();
Test t=x.clone();
  1. Using Object Deserialization : like ObjectInputStream istream=new ObjectInputStream(some inputStream);

Q5:Can we create constructor for an abstract class?

A: Yes, we can create Constructor for abstract class. The constructor will be invoked while instantiating its subclass object. The constructor can be invoked explicitly by using super keyword.

Q3:Is it possible to override static method?

A: No it is not possible to override a static method. This because there is only 1 instance of a static method at class level.

Q4:Is it possible to access non-static method from a static method

A: No static method can not access non-static methods Java has two types of methods, instance methods and static methods. A static method can be accessed without creating an instance of the class. If you try to use a non-static method and variable defined in this class then the compiler will say that non-static variable or method cannot be referenced from a static context. Static method can call only other static methods and static variables defined in the class.

Q5: Why is Java not fully object oriented?

A: In order to be fully object oriented a language must support classes, objects, inheritance and polymorphism. C++ is fully object oriented as it supports all the types of inheritances i.e. single, multilevel, multiple, hierarchical and multipath inheritances and if talk about polymorphism, C++ supports static binding and operator overloading which come under static polymorphism.
Java is not fully object oriented as Java does NOT support operator overloading and multiple inheritances Java supports multiple inheritance through interfaces though.

Q6: What is the difference between class variable, member variable and automatic(local) variable.

  • Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
  • Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
  • Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.

Q7: Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD com compile successfully?

A: Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying,can not resolve symbol
1
2
3
symbol : class ABCD
location: package io
import java.io.ABCD;

Q: What are the methods in Object class?

There are 11 methods in Object class listed below
  1. protected Object clone()
  2. boolean equals(Object obj)
  3. protected void finalize()
  4. Class getClass()
  5. int hashcode()
  6. void wait()
  7. void wait(long timeout)
  8. void wait(long timeout, int nanos)
  9. void notify()
  10. void notifyAll()
  11. String toString()

Q8:How to make your class/Object immutable?

A: Follow below rules to make an object immutable
  1. Mark all fields private and final
  2. No methods that modify state
  3. Ensure that class can’t be extended (mark it final)
  4. Ensure exclusive access to any mutable components

Q9: There is a singleton object which is searialized – so what do you think in deserialization will happen – will it be the same object back?

Q10: What is the difference between sleep and wait?

WaitSleep
Wait method release the acquired monitor while the thread waitsSleep method holds the monitor while the thread goes to sleep
Wait method should be called from inside a synchronized blockThere is not such mandate for sleep method
Thread.sleep() method is a static method and applies on current threadwait() is an instance specific method and only resumes execution only if some other thread calls notify method on same object
In case of sleep, sleeping thread immediately goes to Runnable state after waking upIn case of wait, waiting thread first acquires the lock and then goes into Runnable state
Use wait() method when you want to implement inter-thread communication.Use sleep method if you want your thread to pause for a fixed/known amount of time

Q11: How will you resolve concurrency issues in your code?

Q: You create a pool of 10 threads. How will you ensure that they all start at the same time?

Q: In collections if i am using map and i need to have unique objects as key – how will i do this?

No comments: