Friday 25 September 2015

miscellaneous java interview Question



1- public class IndiaToday {
             public static void main(String[] args) {
                        HashMap hash =  new HashMap();
                        hash.put(1, "A");
                        hash.put(2, "B");
                        hash.put(3, "C");
                        hash.put(4, "D");
                      
                        System.out.println(hash.keySet());
            }
}
 Output:
[1,2,3,4]




2 :
public class IndiaToday {
             public static void main(String[] args) {
                        String str = "Amit";
                        str = str.substring(0,1);//sublist create object using new keyword
                        if(str == "Ä") {
                                    System.out.println(true);
                        } else {
                                    System.out.println(false);
                        }
                      
            }
}
Output: false

3- Which SOP gives 180
public class IndiaToday {

            public static void main(String[] args) {
                      
                        String str = "CS180";
                        System.out.println(str.substring(2,4));
                        System.out.println(str.substring(3,4));
                        String temp ="P";
                        System.out.println(temp.length()+"80");
                        System.out.println(temp.length()+80);
            }
}
 i. 80, ii. 0, iii. 180, iv. 81

4-
public class IndiaToday {
             public static void main(String[] args) {
                        Object i = new ArrayList<>().iterator();
                        System.out.println(i instanceof List);
                        System.out.println(i instanceof ListIterator);
                        System.out.println(i instanceof Iterator);
                      
            }
}
 i. false, ii, false, iii. true


5 – Priority of current Thread or main Thread
public class IndiaToday {

       public static void main(String[] args) {
     
              Thread c = Thread.currentThread();
              System.out.println(c.getPriority());
       }
}
 i.5(Default priority is always 5, max priority is 10, and min priority is 1)

6)- Internal implementation of JVM


7)-
 Compile and runtime polymorphism.

Ans
Method overriding - run time polymorphism
Method Overloading  - Compile time polymorphism
 
8)-
Why we can't override or overload static method

Ans
Because static methods implementation bind to the class at compile time not to the object.


9)-
How eclipse find error in your program or suggest.
Ans
Reflection


10)-
Use of classpath in java

11)-
   what is inline and outline comment


12)- Precompiled query

Ans
 When you use prepared statement(i.e pre-compiled statement), As soon as DB gets this statement, it compiles it and caches it so that it can use the last compiled statement for successive call of same statement. So it becomes pre-compiled for successive calls.
You generally use PreparedStatement with bind variables where you provide the variables at run time. Now what happens for successive execution of prepared statements, you can provide the variables which are different from previous calls. From DB point of view, it does not have to compile the statement every time, will just insert the bind variables at run time. So becomes faster.


13)-
How substring work.

Ans
SubString method takes two parameter one is beginIndex and other is endIndex and gives you result. but internally it again call one String constructor to return new String which accept data like below
int offset, int count, char value []

The char array here is the original string on which you want to perform subString, So it will stop original String to get garbage collected.

To avoid this type of issue , You can use different constructor of String class like :
//calling String(string) constructor

String test =  “aaaaaaaaaaaaaaaasssssssssssssssd”;
String temp = new String (test.subString(0,1));


14)-
Write a SQL query to get 3rd maximum salary from table.
Ans
select salary from salTable order by salary DESC LIMIT 2,1;

15)-
Write a SQL query to get 3rd maximum salary from table department wise
Ans
select salary, dept  from salTable order by DESC LIMIT 2,1, GROUP BY dept;


16)-
How will you remove duplicate records from a database table ? Will group by clause help ?


Ans
yes Group by can help to extract data by grouping columns
DELETE FROM your_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM your_table
GROUP BY column1, column2, column3);

If you want to check on behalf of particular  column then you can use
SELECT  data
      , COUNT(data) nr
FROM    table
GROUP BY data
HAVING  COUNT(data) > 1


17)-
What is purpose of Classloader provided by Java ? What is effect of multiple classloaders on Singleton Class?.


18)- What is hierarchy of class loader in java?

19)-
Integer.valueOf(), Integer.parseInt()


20)-
 Select 3 field values from Database using hibernate.
    Consider a class Company{
    int id;
    String name;
    String address;
    String numOfEmployee;
    String companyStartDate;
}

you need to select only name, address and numOfEmployee from Company using hibernate.

21)-
Company Table
C_ID   C_NAME    C_PRODUCT     C_UNIT_PRICE

Select query returned ResultSet rs.
You need to store all records from result set to a map against company id so that we can get company details from map using company id. What would be your approach?

22)-
Difference between i = i+1 and i++.

23)-
 Identify major modules to design EBay Like system.

24)-
What may be the possible parameters for calculating Delivery Cost?

25)-
Declare a variable into interface and modified value in implementing class?

26)-
can we declare/define static method in interface?

27)-
public class TestExample
{

  static {
    System.out.println("1");
  }
  {
    System.out.println("2");
  }

  public TestExample()
  {
    System.out.println("3");
  }
  public static void main(String[] args)
  {
    new TestExample();
   
  }

  public static void hello()
  {
    System.out.println("4");
   
  }


}
Output?

28)-
    What is Type Erasure .

29)-
 how can we make clone of any object?

30)-
When to do shallow copy and deep copy?
               
if the object has only primitive fields, then obviously you will go for shallow copy but if the object has references to other objects, then based on the requirement, shallow copy or deep copy should be chosen. What I mean here is, if the references are not modified anytime, then there is no point in going for deep copy. You can just opt shallow copy. But if the references are modified often, then you need to go for deep copy. Again there is no hard and fast rule, it all depends on the requirement.    

31)-
What is lazy copy?
Ans
      its combination of deep and shallow copy,when we clone any object it makes first shallow copy and contain a counter to track record if data are shared between objects ,if data are shared between objects it dynamically starts to use deep copy           


32)-
public String findLargestPalindrome(String str)
  {     }

    HeadStrong 121 - 128

33)-
Write a query to get data last swipe-in(of per day) of particular employee for whole month from employee table.

34)- Explain architecture of your current project ?

35)- Explain Java Memory Management in details?

36)- How do you handle error condition while writing stored procedure or accessing stored procedure from java?

37)- What is the difference between factory and abstract factory pattern.
38)- How would you prevent a client from directly instantiating your concrete classes? For example, you have a Cache interface and two implementation classes MemoryCache and Disk Cache, How do you ensure there is no object of this two classes is created by client using new() keyword.

39)- hql for writing query of [employee name]second max salary from list of employee.
40)- get  unique object from list of 100 student objects[id,name,age]id can be duplicate.

41)- Create custom annotation.

42)- Write Singleton class?
43)- String VS StringBuffer?
44)- Write code for immutable class?
45)- Factory Pattern?
46)- sleep VS wait?
47)- what is the use of multiple catch in java
48)- checked and unchecked exception
49)- custom exception

50)- How you can refresh the cache in multiple jvm environment.
51)- Write an api which returns unique I'd always without storing them.
52)- how spy works in junit
53)- What is collateral api  in java
54)- Difference between DataElement & DTD ?
55)- Difference between Entity beans in EJB 2.0 & EJB 3.0 ?
56)- If there are 100 beans in JNDI & the context.lookup() method takes time to lookup. How do you handle the situation to make it easy retrieval of the bean ?
57)- Servlet is Java class. Then why there is no constructor in Servlet ? Can we write the constructor in Servlet ?

58)- What is the difference between using a HttpSession & a Stateful Session bean ? Why can't HttpSession be used instead of Session bean ?

59)- Can we override init() method of a Servlet ?

60)- Can we call destroy() method forcefully ?

61)- what is class loader .   how many types of class loader

62)- Classnotfound and noClassDef

63)- How implicit objects are available in jsp.

64)- SimpleDateFormat  is based on which design pattern
65)- what is overloading in java. If we have two method method1(Integer a) and method1(Sting a) and if we invoke the method as method1(null), then which method would get invoked and why?

66)- What are the restriction of method overloaing?

67)- what is Immutable in java and uses? How do you test it through JUnit

68)- what is Proxy pattern?

69)- How do you do performance tuning of database

70)- Suppose we have an integer array A[] of n length and its next index iterator is currentIndex+A[currentIndex].
We have to write down a method which return unreached/untraversed value while iterating over the aaray

71)- Without using #, how can you use OGNL?

72- Types of constructors? explicit / implicit ? diff . example.
73)- What all possibilities to create object in java?

74)- how to estimate a project ? module? task ?
75)- diff between waterfall and agile ?
76)- how can you initiate junit in a existing project where there is no junit. What things should be there to be able to introduce junit?
77)- what is TDD ?
78)- What UI technologies you know? Jquery? Node.js ? Javascript? Rate yourself?
79)- How to say no to client in diplomatic /  polite way?

80)- How can you call a argument-ed constructor for an abstract class?
injection