Basics – 2

      Question

      1. How do you sort Collection of custom objects in java?
        We need to implement the comparable interface to custom object class and then implement compareTo(Object o) method which will be used for sorting. It will provides default way of sorting custom objects. Answer
      2. What is Enum in java?
        Java Enum is special data type which represents list of constants values. It is a special type of java class. It can contain constant, methods and constructors etc.
        You can refer Enum in java for more details.
        Answer
      3. How do you create custom exception in java?
        You just need to extend Exception class to create custom exception. If you want to create Unchecked exception, then you need extend Runtime Exception. Answer
      4. What is difference between Checked Exception and Unchecked Exception?
        Checked Exception: Checked exceptions are those exceptions which are checked at compile. If you do not handle them , you will get compilation error.
        For example: IOException
        Unchecked Exception : Unchecked exceptions are those exceptions which are not checked at compile time. Java won’t complain if you do not handle the exception.
        For example: ArrayIndexOutOfBoundsException
        Answer
      5. Can we have try without catch block in java ?
        Yes, we can have try without catch block by using finally block. You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit(). Answer
      6. What are ways to create a thread in java ?
        I) By extending thread class
        II) By implementing the Runnable interface.
        Answer
      7. Define states of thread in java?
        There are 5 states of thread in java
        New : When you create a thread object and it is not alive yet.
        Runnable: When you call start method of thread, it goes into Runnable state.
        Running : When thread is being executed, it goes to running state.
        Blocked : When thread waits for some resources or some other thread to complete, it goes to blocked state.
        Dead : When thread’s run method returns, thread goes to dead state.
        Answer
      8. The difference between nested public static class and a top level class in Java?
        You can have more than one nested public static class inside one class, but you can only have one top-level public class in a Java source file and its name must be same as the name of Java source file. Answer
      9. Difference between Composition, Aggregation and Association in OOP?
        If two objects are related to each other, they are said to be associated with each other. Composition and Aggregation are two forms of association in object-oriented programming. The composition is stronger association than Aggregation. In Composition, one object is OWNER of another object while in Aggregation one object is just USER of another object. If an object A is composed of object B then B doesn’t exist if A ceased to exists, but if object A is just an aggregation of object B then B can exists even if A ceased to exist. Answer
      10. Can you write a regular expression to check if String is a number?
        A numeric String can only contain digits i.e. 0 to 9 and + and – sign that too at start of the String, by using this information you can write following regular expression to check if given String is number or not Answer






      <Previous> < Next >