Introduction To Methods & Streams

1

What is the output of this program?

    class A {
        int i;
        public void display() {
            System.out.println(i);
        }    
    }    
    class B extends A {
        int j;
        public void display() {
            System.out.println(j);
        } 
    }    
    class Dynamic_dispatch {
        public static void main(String args[])
        {
            B obj2 = new B();
            obj2.i = 1;
            obj2.j = 2;
            A r;
            r = obj2;
            r.display();     
        }
   }   
A. 1
B. 2
C. 3
D. 4

2

What is the output of this program?

    class binary {
         public static void main(String args[]) {
             int num = 17;
             System.out.print(Integer.toBinaryString(num));
        }
    }
A. 1001
B. 10011
C. 11011
D. 10001

3

What is the output of this program?

    class A {
        public void display() {
            System.out.println("A");
        }    
    }    
    class B extends A {
        public void display() {
            System.out.println("B");
        } 
    }    
    class Dynamic_dispatch {
        public static void main(String args[])
        {
            A obj1 = new A();
            B obj2 = new B();
            A r;
            r = obj1;
            r.display();
            r = obj2;
            r.display();     
        }
   }
A. A B
B. B A
C. Runtime Error
D. Compilation Error

4

What is the output of this program?

    class area {
        int width;
        int length;
        int volume;
        area() {
        width = 5;
        length = 6;
        }
        void volume() {
             volume = width * height * length;
        } 
    }    
    class cons_method {
        public static void main(String args[]) {
            area obj = new area();
            obj.volume();
            System.out.println(obj.volume);
        } 
    }
A. 0
B. 1
C. 25
D. 30

5

What is the output of this program?

    class area {
        int width;
        int length;
        int volume;
        area() {
        width=5;
        length=6;
        }
        void volume() {
             volume = width*length*height;
        } 
    }    
    class cons_method {
        public static void main(String args[])
        {
            area obj = new area();
            obj.volume();
            System.out.println(obj.volume);        
        } 
    }
A. 0
B. 1
C. 30
D. error