www.bullraider.com

You are here: Home Java Core Java

Java Programming Interview Questions

AddThis Social Bookmark Button

A set of questions, especially  programming questions asked in most of the interviews.Some of the questions are trivial and some are tricky. So get prepared before getting embarrassed in interview.

  1. How to identify a given positive decimal number as even/odd without using % or / operator ?
    A plain trap, even for those experienced people. You may be very good at coding,but if you questioning how on earth you could solve this problem.Then here is a solution. If you remember the good old days of our primary school then the solution is easy,"division is a matter of iterative subtraction".
    public class TestEvenOdd {
    public static void main(String arg[]){
    int num=6;
    int result=num;
    while(result>=2){
    result=result-2;
    }
    if(result==1){
    System.out.println("The number is odd");
    }else{
    System.out.print("The number is even");
    }
    }
    }
    
  2. Convert a given string as "11/12/2010" to a Date object.
    For those who are new to Java development may think this would be a big programming to solve this.But remember Java is famous for its libraries
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class StringToDate {
        public static void main(String args[]) throws ParseException{
            SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
            String dateString="11/12/2010";
            Date d=sdf.parse(dateString);
            System.out.println(d);
        }
    }
    
  3. Find out the number of days in between two  given dates ?
  4. How to divide a number by 2 without using / operator ?
  5. How to multiply a number by 2 without using * operator ?
  6. How to swap two variables,by using pass by reference method ?
  7. How to make a list immutable ? 
  8. Write a Immutable class.
  9. Write a program that proves Strings are immutable.
    Ans:
    Though there is no way to find if Strings are immutable through coding, but the code below is an attempt to show if Strings are mutable.Remember the only way to say if Strings are immutable is to see the API.
     
    public class ImmutableTest {
     public static void main(String ags[]){
      String initial = "ABCDEFG";  
      String after = initial.replace('A', 'Z');  
      System.out.println("initial = " + initial);  
      System.out.println("after= " + after);
     }
    } 
    
  10. How do you determine if a given class or object implements a given interface through coding.(Without looking at the java file of the class) ?
  11. Given an complex object and it's method which returns an array of strings but method return type is Object[], write code not more than a single line and without using any String related method , assigns the first String in the returned String array to a String variable ?
    Lets suppose the method looks something very trivial like below
    public Object[] returnStringAsArray(){
    return new String[]{"abc","def"};
    }
    
    then the code that would call the method and assign the first string to a String variable is
    String s=(String)returnStringArray()[0];
    
  12. If you assign null to the String variable, and then you print the variable what will be the output ? Justify your answer.
    No, the answer is not NullPointerException,It will print null.Many beginners confuse that case like this would lead to the NullPointerExeception.Such exception will arise only if the object reference (which is not poiting to any object ),is used ,to try to access the object's behavior or properties.
  13. What is Tripleton design pattern , and write a tripleton class ?
Powered by Bullraider.com