Modifier Types


  1. What is the output of this program?

    class box_area
    {
    int w;
    int l;
    int h;
    box_area()
    {
    w = 7;
    l = 4;
    h = 2;
    }
    void volume()
    {
    int vol = w * h * l;
    System.out.println(vol);
    }
    }
    public class cons_method
    {
    public static void main(String args[])
    {
    box_area obj = new box_area();
    obj.volume();
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    Output: 56


  1. What is the output of this program?

    public class Result
    {
    static void main(String args[])
    {
    int p , q = 1;
    p = 20;
    if(p != 20 && p / 0 == 0)
    System.out.println(q);
    else
    System.out.println(++q);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    main() method must be made public. Without main() being public java run time system will not be able to access main() and will not be able to execute the code.



  1. What is the output of this program?

    class modifier
    {
    static int p;
    static int q;
    void addition(int n1 , int n2)
    {
    p = n1 + n2;
    q = p + n2;
    }
    }
    public class static_use
    {
    public static void main(String args[])
    {
    modifier obj1 = new modifier();
    modifier obj2 = new modifier();
    int n1 = 5;
    obj1.addition(n1, n1 + 3);
    obj2.addition(7, n1);
    System.out.println(obj1.p + " " + obj2.q);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Output: 12 17


  1. What is the output of this program?

    class modifier
    {
    static int p;
    void method()
    {
    p++;
    }
    }
    public class static_keyword
    {
    public static void main(String args[])
    {
    modifier obj1 = new modifier();
    modifier obj2 = new modifier();
    obj1.p = 5;
    obj1.method();
    obj2.method();
    System.out.println(obj1.p + " " + obj2.p);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    All objects of class share same static variable, all the objects share same copy of static members, obj1.p and obj2.p refer to same element of class which has been incremented twice and its value is 5.



  1. What is the output of this program?

    class Modifier
    {
    public int p;
    static int q;
    void cal(int n1, int n2)
    {
    p += n1;
    q += n2;
    }
    }
    public class static_keyword
    {
    public static void main(String args[])
    {
    Modifier obj1 = new Modifier();
    Modifier obj2 = new Modifier();
    obj1.p = 2;
    obj1.q = 3;
    obj1.cal(4, 5);
    obj2.p = 4;
    obj2.cal(4, 6);
    System.out.println(obj1.p + " " + obj2.q);
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Output: 6 14