super java keyword questions and answers

Question on java keyword super and answers


(1)

class Square{
    static double pi=Math.PI;
   
    static void area(double r){
         System.out.print((int)pi*r*r);
    }
}

class Cube extends Square{
    static void area(double r){
         System.out.print(6*(int)pi*r*r);
    }
}

class SuperDemo extends Cube{
    public static void main(String[] args) {
         SuperDemo t=new SuperDemo();
         t.area(2);
    }

    void area(int r){
         super.area(r);
    }
}

What will output when you compile and run the above code?

(a)12.0
(b)68.45
(c)72.0
(d)Compiler error






Answer: (c)

(2)

class World{
    World where(){
         System.out.println("I think about our world");
         return this;
    }
}

class CounSuperDemo extends World{
    CounSuperDemo where(){
         System.out.println("I think about our country");
         return this;
    }
}

class State extends CounSuperDemo{
    State where(){
         System.out.println("I think about our State");
         return this;
    }
}

class SuperDemo extends State{
    public static void main(String[] args) {
         SuperDemo t=new SuperDemo();
         t.call();
    }

    void call(){
        World w=super.where();
         w.where();
    }
}

What will output when you compile and run the above code?

(a) I think about our State
    I think about our State
(b) I think about our country
  I think about our State
(c) I think about our State
    I think about our country
(d)Compiler error






Answer: (a)

 (3)

class World{
    World where(){
         System.out.println("I think about our world");
         return this;
    }
}

class CounSuperDemo extends World{
    World where(){
         System.out.println("I think about our counSuperDemo");
         return this;
    }
}

class State extends CounSuperDemo{
    CounSuperDemo where(){
         System.out.println("I think about our State");
         return this;
    }
}

class SuperDemo extends State{
    static SuperDemo t;
    public static void main(String[] args) {
         t=new SuperDemo();
         t.call();
    }

    void call(){
         ((t.where()).where()).where();
    }

}

What will output when you compile and run the above code?

(a) I think about our State
    I think about our counSuperDemo
    I think about our counSuperDemo
(b) I think about our State
    I think about our State
    I think about our State
(c) I think about our counSuperDemo
    I think about our State
    I think about our State
(d)Compiler error






Answer: (b)

 (4)

class World{
    World where(){
         System.out.println("I think about our world");
         return this;
    }
}

class CounSuperDemo extends World{
    World where(){
         System.out.println("I think about our country");
         Return new World();
    }
}

class State extends CounSuperDemo{
    World where(){
         System.out.println("I think about our State");
         return new CounSuperDemo();
    }
}

class SuperDemo extends State{
    public static void main(String[] args) {
         CounSuperDemo c=new State();
         c.where().where().where();
    }
}

What will output when you compile and run the above code?

(a) I think about our World
    I think about our country
    I think about our state
(b) I think about our Country
    I think about our state
    I think about our world
(c) I think about our State
    I think about our country
    I think about our world
(d)Compiler error






Answer: (c)

(5)

class Sea{
    int a,b;
    Sea(int a,int b){
         this.a=a;
         this.b=b;
    }
}

class SuperDemo extends Sea{
    int a,b;
    static SuperDemo t;
   
    public static void main(String[] args) {
    t=new SuperDemo(1,2,3,4);
    t.find();
    }

    SuperDemo(int a,int b,int c,int d){
         super(c,d);
         this.a=a;
         this.b=b;
    }

    void find(){
         System.out.print(a+t.b+this.a+super.b);
    }
}

What will output when you compile and run the above code?

(a)7
(b)8
(c)9
(d)Compiler error






Answer: (b)

1 comment:

leona fassi said...

That's really massive exposure post and I must admire you in this regard.

PIC Scheme