Saturday, 25 January 2014

Tavant interview questions

1. Sort integer array ?
  
               public class SortedArray
{
    public static void main(String[] args) {

        int a[]={30,7,9,20};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
}
}

2. Sort integer array without using Array.sort by ascending

  public class ArraySort {
public static void main(String[] args) {
int a[] = {6,500,700,200,1000,1};   
int temp=0;
/*** Ascending Order ***/
for(int i=0;i<a.length;i++)
{   
for(int j=a.length-1;j>i;j--)
{   
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;   
}
}
}
for(int i=0;i<a.length;i++)
System.out.println("a["+i+"] = "+a[i]);
}
}

                  
3. Sort integer array without using Array.sort by decending

            public class Test {
    public static void main(String[] args)
    {
        int a[] = {6,500,700,200,1000,1};   
        int temp=0;
        /*** Descending Order***/
        for(int i=0;i<a.length;i++)
        {   
        for(int j=0;j<a.length;j++)
        {   
        if(a[i]>a[j]){
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;   
        }
        }
        }
        for(int i=0;i<a.length;i++)
            System.out.println("a["+i+"] = "+a[i]);
    }
    }

4. why need to use overriding ?

             Consider Manager is a super class and Employee is a child class.
             Both class has work() .
             But in Manager,he only manage and in Employee, he will do coding. But basically Manager and Employee are worker.
            Their work is different based on class.

5. What is Generic ?
             A class or interface that operates on parameterized type is called Generic.
             Generic also provide type safety.
             we cannot use other class object .

6.How to sort employee details using comparable ?

                 class Student implements Comparable<Student> {
    int rollno;
    String name;
    int age;
    public int getRollno() {
        return rollno;
    }
    public void setRollno(int rollno) {
        this.rollno = rollno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
        public int compareTo(Student obj)
    {
        return this.rollno - obj.rollno;
    }
}

public class Simple {
   
    public static void main(String[] args) {
        ArrayList al = new ArrayList();
        Student a = new Student();
        a.setAge(2);
        a.setName("bala");
        a.setRollno(5);
        Student b = new Student();
        b.setAge(1);
        b.setName("sures");
        b.setRollno(67);
        Student c = new Student();
        c.setAge(8);
        c.setName("ramesh");
        c.setRollno(43);
        al.add(a);
        al.add(b);
        al.add(c);
        Collections.sort(al);
        Iterator itr = al.iterator();
        while(itr.hasNext())
        {
            Student st=(Student)itr.next();
            System.out.println(st.age+" "+st.name+" "+st.rollno);
        }
       
    }

}

7. Reverse the string ?

            public class StringReverseExample{
       public static void main(String[] args){
          String string="How to name it";
          String reverse = new StringBuffer(string).
          reverse().toString();
          System.out.println("\nString before reverse:" +string);
          System.out.println("String after reverse:"+reverse);
       }
    }
                     

8.Difference between executeUpdate and executeQuery ?

  1. int executeUpdate(String SQL) : Returns the numbers of rows affected by the execution of the SQL statement. Use this method to execute SQL statements for which you expect to get a number of rows affected - for example, an INSERT, UPDATE, or DELETE statement.
  2. ResultSet executeQuery(String SQL) : Returns a ResultSet object. Use this method when you expect to get a result set, as you would with a SELECT statement.

No comments:

Post a Comment