Friday, 12 October 2018

Core Java Interview Questions

  1. Java 8  : How to Sort a List using lambdas?
    1. Integer[] arr = { 1, 7, 3, 9, 4, 67, 100, 23, 26, 76, 8 }; List list = Arrays.asList(arr); list.sort((a1, a2) -> a1.compareTo(a2)); System.out.print(list);
  2. Is singleton is lazy initialisation?
    1. Yes. As in the below code singleton is lazy initialisation.
    2. We will not initialise on static instance property. And initialise only on getInstance method, so it will initialise on required time only and lazy loading. If we need to make it as eager initialise instance on static field, then it will initialise on the time of class loading.
    3. public class Singleton { private static Singleton instance; private LazyInitializedSingleton(){ } public static Singleton getInstance(){ if(instance == null){ synchronized (DclSingleton.class) { instance = new Singleton(); } } return instance; } }
  3. How to find the nth element In QUEUE?
    1. The fact that accessing elements by index is not part of the concept of a queue.If you need to access elements by index, you want a list, not a queue.
  4. What is a partially checked exception in Java?
    1. A checked exception is said to be partially checked exception if and only if some of its child classes are unchecked 
      1. Ex: Exception
    2. The only possible partially checked exception in java are  
      1. Exception 
      2. Throwable 
  5. String intern()?
    1. The java string intern() method returns the interned string. It returns the canonical representation of string.
    2. It can be used to return string from memory, if it is created by new keyword. It creates exact copy of heap string object in string constant pool.
    3. public class InternExample{ public static void main(String args[]){ String s1=new String("hello"); String s2="hello"; String s3=s1.intern();//returns string from pool, now it will be same as s2 System.out.println(s1==s2);//false because reference variables are pointing to different instance System.out.println(s2==s3);//true because reference variables are pointing to same instance } }
  6. Can an abstract class have main method and run it?
    1. Yes. It have main method and run. But canot create its own object,.
    2. public abstract class AbstractMainEx { public static void main(String[] args) { System.out.println("Hi"); } public abstract boolean test(); }
  7. How to create a custom Exception?
    1. To create you own exception extend the Exception class or any of its subclasses.
    2. class New1Exception extends Exception { } // this will create Checked Exception class NewException extends IOException { } // this will create Checked exception class NewException extends NullPonterExcpetion { } // this will create UnChecked exception
  8. Can we have private constructor for parent constructor in inheritance?
    1. No
    2. Implicit super constructor Animal() is not visible for default constructor. Must define an explicit 
       constructor


No comments:

Post a Comment

Search This Blog

Contact us

Name

Email *

Message *