Menu
Question Index
...

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;    }
}


[banana, apple, clementine, strawberry, orange, banana, cherry, orange]
[apple, banana, cherry, clementine, orange, strawberry]
[Fig, Guava, apple, banana, cherry, clementine, grapes, kiwi, mango, orange, strawberry, watermelon]
[banana, apple, clementine, strawberry, orange, kiwi, mango, banana, grapes, watermelon, Fig, cherry, Guava]
Some other output
Compilation Error Runtime Error

Doubts

Problems

Topic: Unknown

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App