Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Easy
Question 9
Given code of Test.java file:
- package com.udayan.ocp;
- public class Test {
- private static final <X extends Integer, Y extends Integer> void add(X x, Y y) {
- System.out.println(x + y);
- }
- public static void main(String[] args) {
- add(10, 20);
- }
- }
What will be the result of compiling and executing Test class?
- A Runtime Exception
- B Compilation error
- C 1020
- D 30
Reveal correct answer
Correct answer: D
Explanation
If a generic method is defined in a non-generic class then type parameters must appear before the return type of the method.
Integer is also a final class so parameters X and Y can only be of Integer type.
add(10, 20); => Auto-boxing converts int literals to Integer objects. 30 is printed on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
