Quizzes
Site Language: English
Українська
English
Русский
Programming Tests
Login
Sign Up
Programming Tests
Theory
Snippets
Papers
Landing
Android
Prices
FAQ
Cosmo Story
Terms and Conditions
Privacy Policy
Cookies Policy
Send Feedback
reflection
:
Content language: English
Русский
What will be the output of the following code? package question; import java.lang.reflect.Method; class Test { public void methodPublic() { System.out.println("Test.methodPublic"); } protected void methodProtected() { System.out.println("Test.methodProtected"); } private void methodPrivate() { System.out.println("Test.methodPrivate"); } } public class PrivateTest { public static void main(String[] args) throws Exception { Test test = new Test(); callMethods(test, "methodPublic"); callMethods(test, "methodProtected"); callMethods(test, "methodPrivate"); } static void callMethods(Object a, String methodName) throws Exception { Method m = a.getClass().getDeclaredMethod(methodName); m.setAccessible(true); m.invoke(a); } }
reflection
What will be the result of compilation and execution of the following code? public class Test<T>{ static class MyTest{ public MyTest(int k) { System.out.println("MyTest created"); } } T obj1, obj2; public Test(T t, Class<T> cls) throws Exception { obj1 = t; // 1 obj2 = cls.newInstance(); // 2 } public static void main(String[] args) throws Exception { MyTest mt = new MyTest(10); Test t = new Test(mt, MyTest.class); } }
reflection
← Prev
1
Next →
Sign Up Now
or
Subscribe for future quizzes