Menu
Topics Index
...
`

Classes inside the class1566


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes inside the class1566
What will be the output of the following program?
import java.lang.reflect.*;
import java.util.*;
public class ReflectionMethods {
    public static void main(String[] args) {
        ZYX a = new ZYX();
        printMethods(a);
    }
    public static void printMethods(Object obj) {
        String str = "";
        List<String> m1s = new ArrayList<String>();
        for (Method method : Object.class.getMethods()) {
            m1s.add(method.getName());
        }
        List<String> m2s = new ArrayList<String>();
        for (Method method : obj.getClass().getMethods()) {
            m2s.add(method.getName());
        }
        List<String> m3s = new ArrayList<String>();
        for (Method method : XYZ.class.getDeclaredMethods()) {
            m3s.add(method.getName());
        }
        m2s.removeAll(m1s);
        m3s.addAll(m2s);
        SortedSet<String> ts = new TreeSet<String>(m3s);
        System.out.print(ts);
    }
}
class XYZ {
    XYZ() {    }
    private int apple()             {    return 1;    }
    protected int banana(int i)     {    return 2;    }
    public int orange(int x, int y) {    return 9;    }
    private Double clementine()     {    return null;    }
    Integer strawberry()            {    return 6;    }
}
class ZYX extends XYZ {
    private void kiwi()        {    int i = 0;    }
    protected boolean Guava()    {    return true;    }
    void mango(int j)            {            }
    public int banana(int j)      {    return 3;    }
    String grapes()                  {    return "";    }
    char watermelon()             {    return 'A';    }
    long Fig()                    {    return 0;    }
    public Number cherry()        {    return 34;    }
}

A. [banana, apple, clementine, strawberry, orange, banana, cherry, orange]
B. [apple, banana, cherry, clementine, orange, strawberry]
C. [Fig, Guava, apple, banana, cherry, clementine, grapes, kiwi, mango, orange, strawberry, watermelon]
D. [banana, apple, clementine, strawberry, orange, kiwi, mango, banana, grapes, watermelon, Fig, cherry, Guava]
E. Some other output
F. Compilation Error Runtime Error
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

B. is the answer as.firstly the class ZYX is inherited from XYZ and then we are creating the ArrayList adding the methods  and using TreeList sorting we r printing the methods.

Posted by Shabaaz Shaikz    2014-02-12 13:12:55


ans E m2s.removeAll(mls) gives only methods in class xyz and m3s.addAll(m2s) adds all methods except mango, grapes watermelon,fig,banana because it is undeclared and treeset will printed in a sorted order

Posted by Bheema Reddy    2014-02-12 13:38:44


Congratulations Shabaaz Shaikz. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-13 04:12:00


thankqq....:-)

Posted by Shabaaz Shaikz    2014-02-13 08:16:28


© meritcampus 2019

All Rights Reserved.

Open In App