Modifiers Related Questions

  1. Which class declaration is wrong, for top level class?
    public class A
    private class A
    class A
    static class A
    final class A
    abstract class A
    transient class A
    volatile class A
    Ans: Top level class only can public, final and abstact, so others are wrong.
  2.  What are wrong about variable declaration?
          a transient int i;
          b volatile int j;
         c private int x;
         d public int y;
         e final int z;
         f abstact int a;
    Ans: a,b,c,d correct, e and f wrong because final variable always initalised so we have to write final int z=7;
    abstact modifier can not asign to variable.
  3.  What are the wrong statement about method declaration?
    public void methodA()
    private void methodA()
    final void methodA()
    static void methodA()
    volatile void methodA()
    transient void methodA()
    abstact void methodA();
    Ans: Except volatile and transient other are correct, but if we are declaring method as abstact that class must be abstact
  4. Can I write main method as  private static void main(String a[])?
    Ans: You won't get any compilation error, but JVM can not consider as main method. It will be a new method.
  5. Can we write main method like this?
     public static final void main(String ar[])
    Ans: yes we can
  6.  Can we wtire a calss like?
    public final abstract class A

    Ans :no
    Because, class is final means we can't extend that calss else where, but abstract means, it needs to be subclassed
  7.  Can I write one method like this
     final static void methodA()

    Ans: we can
  8. What is difference between interface and abstact class?
    Interface Abstract Class
    Interface is by default abtract in nature, means all the methods declare in interface is implicitily abstact, no nead to write abstact Here we have to explicitily declare as abstact.
    Contains only abstact methods and final variable Contais all type of method and and variable
    We can implements no of interfaces in a class We can extend one abstact class
    If we are implementing interface we have to override all implementation methods Not necessary
    We can not keep constructor We can keep one default constructor
  9. What are similarities between abstact class and interface?
    Ans Both we can't instantiate.
  10.  In which case we should use interface and which case abstact class?
  11. Which lines are wrong?
     final static void methodA(){
             public int i=8;//1
             private int j=8;//2
             int x=6;//3
             static int y=9;//4
             final int z=8;//5
        }
    Ans: only 3 and 5 are true and rest of lines is now allowed inside method.
  12. What is the use of volatile modifier?
    Ans:The volatile modifier applies only to instance variables .
  13. What is transient modifiier in java?
    Ans:Transient variable can not be serialized.
  14. What is Java Serialization?
    Ans:Serialization is a process to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again
    For that we have to implements Serializable interface.
  15. What is marker interface?
    Ans:Interface which doesn't contain any method or variable called as marker interface. Exemple Serializable