Advanced Java Questions & Answers – Reflection API

This set of Advanced Java Multiple Choice Questions & Answers (MCQs) focuses on “Reflection API”.

1. What are the components of a marker interface?
a) Fields and methods
b) No fields, only methods
c) Fields, no methods
d) No fields, No methods
View Answer

Answer: d
Explanation: Marker interface in Java is an empty interface in Java.

2. Which of the following is not a marker interface?
a) Serializable
b) Cloneable
c) Remote
d) Reader
View Answer

Answer: d
Explanation: Reader is not a marker interface. Serializable, Cloneable and Remote interfaces are marker interface.

3. What is not the advantage of Reflection?
a) Examine a class’s field and method at runtime
b) Construct an object for a class at runtime
c) Examine a class’s field at compile time
d) Examine an object’s class at runtime
View Answer

Answer: c
Explanation: Reflection inspects classes, interfaces, fields and methods at a runtime.
advertisement
advertisement

4. How private method can be called using reflection?
a) getDeclaredFields
b) getDeclaredMethods
c) getMethods
d) getFields
View Answer

Answer: b
Explanation: getDeclaredMethods gives instance of java.lang.reflect.Method.

5. How private field can be called using reflection?
a) getDeclaredFields
b) getDeclaredMethods
c) getMethods
d) getFields
View Answer

Answer: a
Explanation: getDeclaredFields gives instance of java.lang.reflect.Fields.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What is used to get class name in reflection?
a) getClass().getName()
b) getClass().getFields()
c) getClass().getDeclaredFields()
d) new getClass()
View Answer

Answer: a
Explanation: getClass().getName() is used to get a class name from object in reflection.

7. How method can be invoked on unknown object?
a) obj.getClass().getDeclaredMethod()
b) obj.getClass().getDeclaredField()
c) obj.getClass().getMethod()
d) obj.getClass().getObject()
View Answer

Answer: c
Explanation: obj.getClass().getMethod is used to invoke a method on unknown object obj.
advertisement

8. How to get the class object of associated class using Reflection?
a) Class.forName(“className”)
b) Class.name(“className”)
c) className.getClass()
d) className.getClassName()
View Answer

Answer: a
Explanation: forName(String className) returns the Class object associated with the class or interface with the given string name.

9. What does Class.forName(“myreflection.Foo”).getInstance() return?
a) An array of Foo objects
b) class object of Foo
c) Calls the getInstance() method of Foo class
d) Foo object
View Answer

Answer: d
Explanation: Class.forName(“myreflection.Foo”) returns the class object of Foo and getInstance() would return a new object.
advertisement

10. What does foo.getClass().getMethod(“doSomething”, null) return?
a) doSomething method instance
b) Method is returned and we can call the method as method.invoke(foo,null);
c) Class object
d) Exception is thrown
View Answer

Answer: b
Explanation: foo.getClass().getMethod() returns a method and we can call the method using method.invoke();

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.