Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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) { Pair bid; 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 x = new Pair<>(4, "dog");

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions