Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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) { 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 } } 

Refer to Ch2 code generic types. Match the code example to the result.

// 1

Pair p = new Pair<>("cat", 3);

Object x = p.first();

// 2

Pair p = new Pair<>("cat", 3) String x = p.first();

// 3

Pair p = new Pair<>("cat", 3) String x = (String) p.first();

// 4

Pair p = new Pair<>("cat", 3) Integer x = (Integer) p.first();

// 5

Pair p = new Pair<>(new Object(), 3) Object x = p.first();

// 6

Pair p = new Pair<>(new Object(), 3); String x = (String) p.first();

// 7

Pair p = new Pair<>(new Object(), 3) Object x = p.first();

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

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

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions