Java Interview Questions - 3



Java Interview Questions



1. Which is the base class for all classes in java?

Object class.


2. What are methods in object class in java?

clone() - Creates and returns a copy of this object.

equals(Object obj) - 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 run-time 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.

wait(long timeout) - Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

wait(long timeout, int nanosec) - Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.


3. Program to remove any given character from a String?


public class RemoveChar {
 
 public static void main(String[] args) {
  String line = "A simple implementation of Bindings backed";
  char chr = 'i';
  String finalStr = new RemoveChar().removecharater(line, chr);
  
  System.out.println("INPUT:  "+line);
  System.out.println("CHAR TO REMOVE: "+chr);
  System.out.println("\nOUTPUT: "+finalStr);
 }
 
 public String removecharater(String line, char chr){
  char[] charArr = line.toCharArray();
  StringBuilder strBuilder = new StringBuilder();
  
  for (char c : charArr) {
   if(c != chr){
    strBuilder.append(c);
   }   
  }  
  return strBuilder.toString();
 }
}


OUTPUT:


INPUT:  A simple implementation of Bindings backed
CHAR TO REMOVE: i

OUTPUT: A smple mplementaton of Bndngs backed




4. Find no. of occurances of each character in a given string?


public class CharOccurance {

 public static void main(String[] args) {
  String line = "implementation";
  
  HashMap<Character, Integer> charCounts = getCharCounts(line);
  
  System.out.println("INPUT:  "+line);
  System.out.println("RESULT: "+charCounts);  
 } 
 
 public static HashMap<Character, Integer> getCharCounts(String line){
  HashMap<Character, Integer> charCounts = new HashMap<Character, Integer>();
  char[] chr = line.toCharArray();
  for (char c : chr) {
   if(charCounts.containsKey(c)){
    int cnt = charCounts.get(c);
    charCounts.put(c, cnt+1);
   }else{
    charCounts.put(c, 1);
   }
  }  
  return charCounts;
 } 
}

OUTPUT:


INPUT:  implementation
RESULT: {t=2, e=2, a=1, p=1, n=2, o=1, l=1, m=2, i=2}


5. Find second highest number in an integer array?


public class SecondHighest {

 public static void main(String[] args) {
  int[] values = {8,4,1,3,7,6,5,2,9};
  
  // Sorting array into TreeSet
  Set<Integer> tree = new TreeSet<Integer>();
  for (int i : values) {
   tree.add(i);
  }
  
  // Copy TreeSet values into ArrayList
  List<Integer> list = new ArrayList<Integer>();
  list.addAll(tree);
  
  // Print last 2nd value for 2nd Hignest value
  System.out.println("SECOND HIGHEST : "+list.get( list.size()-2 ));  
 }  
}

OUTPUT:


SECOND HIGHEST : 8


6. How to read/write/append/delete a file?

Please refer to the earlier post for complete all file operations. - http://javadiscover.blogspot.com/2013/03/how-to-create-writeappend-read-delete.html 


7. What are the ways we can create threads in java?

By 2 ways we can create thread 
a. Extending Thread class
b. Implementing Runnable interface 


8. How to create a Thread using extending a Thread class?



public class MyThread extends Thread{
 
 @Override
 public void run() {
  int i=0;
  while(i<5){
   System.out.println(Thread.currentThread().getName());
   i++;
  }
 }
 
 public static void main(String[] args) {
  MyThread t1 = new MyThread();
  MyThread t2 = new MyThread();
  t1.start();
  t2.start();
 }
}


OUTPUT:


Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0




9. How to create a Thread using implemeting Runnable interface ?


public class MyThread implements Runnable{
 
 public void run() {
  int i=0;
  while(i<5){
   System.out.println(Thread.currentThread().getName());
   i++;
  }
 }
 
 public static void main(String[] args) {
  MyThread obj = new MyThread();
  Thread t1 = new Thread(obj);
  Thread t2 = new Thread(obj);
  t1.start();
  t2.start();
 }
}

OUTPUT:


Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1


10. What are different types of cloning in Java?

There are 2 types of cloning in Java, Deep and shallow cloning (Copy). By default shallow copy is used in Java. As we seen in 2nd question by Object class clone() will do this operation by default. 







No comments :

Post a Comment