Question
class IntegerStringPair { Integer first; String second; public IntegerStringPair(Integer first, String second) { this.first = first; this.second = second; } public Integer getFirst() { return
class IntegerStringPair { Integer first; String second; public IntegerStringPair(Integer first, String second) { this.first = first; this.second = second; } public Integer getFirst() { return first; } public String getSecond() { return second; } }
public class ObjectPair { Object first; Object second; public ObjectPair(Object a, Object b) { // constructor first = a; second = b; } public Object getFirst() { return first; } public Object getSecond() { return second;} public String toString() { return "[" + first + ", " + second + "]"; } public static void main(String[] args) { ObjectPair bid = new ObjectPair("ORCL", 32.07); /* String stock = bid.getFirst(); // illegal; compile error */ String stock = (String) bid.getFirst(); // narrowing cast: Object to String } }
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 } }
For each of the four code snippets below, match to the description.
// 1
IntegerStringPair x = new IntegerStringPair(4, "dog");
Integer left = x.first();
String right = x.second();
// 2
ObjectPair x = new ObjectPair(4, "dog");
Integer left = x.first();
String right = x.second();
// 3
ObjectPair x = new ObjectPair(4, "dog");
Integer left = (Integer) x.first();
String right = (String) x.second();
// 4
ObjectPair x = new ObjectPair(4, "dog");
String left = (String) x.first();
Integer right = (Integer) x.second();
// 5
Pair
Integer left = x.first();
String right = x.second();
Group of answer choices
1
[ Choose ] compiles but there is a runtime exception: ClassCastException compiles and runs successfully, and it is good code doesn't compile compiles and runs successfully, but it's poor code
2
[ Choose ] compiles but there is a runtime exception: ClassCastException compiles and runs successfully, and it is good code doesn't compile compiles and runs successfully, but it's poor code
3
[ Choose ] compiles but there is a runtime exception: ClassCastException compiles and runs successfully, and it is good code doesn't compile compiles and runs successfully, but it's poor code
4
[ Choose ] compiles but there is a runtime exception: ClassCastException compiles and runs successfully, and it is good code doesn't compile compiles and runs successfully, but it's poor code
5
[ Choose ] compiles but there is a runtime exception: ClassCastException compiles and runs successfully, and it is good code doesn't compile compiles and runs successfully, but it's poor code
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