Question
public class Pair { A first; B second; public Pair(A a, B b) { // constructor first = a; second = b; } public A
public class Pair { A first; B second; public Pair(A a, B b) { // constructor first = a; second = b; } public A getFirst() { return first; } public B getSecond() { return second;} public String toString() { return "[" + first + ", " + second + "]"; } public static void main(String[] args) { Pairbid; bid = new Pair<>("ORCL", 32.07); // rely on type inference // using Java6 style without type inference: bid = new Pair ("ORCL", 32.07); // give explicit types // using classic style without generics bid = new Pair("ORCL", 32.07); // classic style String stock = bid.getFirst(); double price = bid.getSecond(); Pair [] holdings; /* holdings = new Pair [25]; // illegal; compile error */ holdings = new Pair[25]; // correct, but warning about unchecked cast holdings[0] = new Pair<>("ORCL", 32.07); // valid element assignment } }
Refer to Ch2 code generic types. Match the code example to the result.
// 1
Pair
Object x = p.first();
// 2
Pair
// 3
Pair
// 4
Pair
// 5
Pair
// 6
Pair
// 7
Pair
Group of answer choices
1
[ Choose ] code has a runtime error (ClassCastException) code has a compiler error code is okay
2
[ Choose ] code has a runtime error (ClassCastException) code has a compiler error code is okay
3
[ Choose ] code has a runtime error (ClassCastException) code has a compiler error code is okay
4
[ Choose ] code has a runtime error (ClassCastException) code has a compiler error code is okay
5
[ Choose ] code has a runtime error (ClassCastException) code has a compiler error code is okay
6
[ Choose ] code has a runtime error (ClassCastException) code has a compiler error code is okay
7
[ Choose ] code has a runtime error (ClassCastException) code has a compiler error code is okay
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started