<< Chapter < Page Chapter >> Page >
This module contains review questions and answers keyed to the module titled Java4080: The Comparable Interface, Part 2.

Revised: Wed Apr 06 22:35:48 CDT 2016

This page is included in the following Books:

Table of contents

Preface

This module is one in a collection of modules on Java Collections designed for teaching ITSE2321 - Object-Oriented Programming (Java) at Austin Community College in Austin, TX.

This module contains review questions and answers keyed to the module titled Java4080: The Comparable Interface, Part 2 .

Once you study that module, you should be able to answer the review questions in this module.

The questions and the answers in this module are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again.

Questions

Question 1 .

True or False? The elements stored in a TreeSet collection must be references to objects instantiated from a class that implements the Comparable interface or makes use of an alternative approach involving the Comparator interface.

Answer 1

Question 2

What output is produced by the program shown in Listing 1 ?

  • Compiler Error
  • Runtime Error
  • 44321
  • 4321
  • 1234
  • 12344
  • None of the above.

Answer 2

Question 3

True or False? The program in Listing 1 throws a runtime error because the class named MyClass fails to implement the CompareTo interface.

Answer 3

Question 4

What output is produced by the program shown in Listing 2 ?

  • Compiler Error
  • Runtime Error
  • 44321
  • 4321
  • 1234
  • 12344
  • None of the above.

Answer 4

Question 5

True or False? A class that implements the Comparable interface must provide a concrete definition of the compareTo method.

Answer 5

Listings

What is the meaning of the following two images?

These images were inserted here simply to insert some space between the questions and the answers to keep them from being visible on the screen at thesame time.

Spacer image of a rabbit and a penguin.

This image was also inserted for the purpose of inserting space between the questions and the answers.

Spacer image of a penguin and some houses.

Answers

Answer 5

True.

Back to Question 5

Answer 4

1234

Back to Question 4

Answer 3

False. The program in Listing 1 throws a runtime error because the class named MyClass fails to implement the Comparable interface.

Back to Question 3

Answer 2

Runtime Error

Back to Question 2

Answer 1

True.

Back to Question 1

Complete program listings

Listing 1 . Comparable04.java.
//File Comparable04.java import java.util.*;public class Comparable04{ public static void main(String args[]){ new Worker().doIt();}//end main() }//end class Comparable04class Worker{ public void doIt(){Iterator iter; Collection ref;ref = new TreeSet(); Populator.fillIt(ref);iter = ref.iterator(); while(iter.hasNext()){System.out.print(iter.next()); }//end while loopSystem.out.println(); }//end doIt()}// end class Worker class Populator{public static void fillIt(Collection ref){ ref.add(new MyClass(4));ref.add(new MyClass(4)); ref.add(new MyClass(3));ref.add(new MyClass(2)); ref.add(new MyClass(1));}//end fillIt() }//end class Populatorclass MyClass{ int data;MyClass(){ data = 0;}//end noarg constructor MyClass(int data){this.data = data; }//end parameterized constructorpublic String toString(){ return "" + data;}//end overridden toString() }//end MyClass
Listing 2 . Comparable05.java.
//File Comparable05.java import java.util.*;public class Comparable05{ public static void main(String args[]){ new Worker().doIt();}//end main() }//end class Comparable05class Worker{ public void doIt(){Iterator iter; Collection ref;ref = new TreeSet(); Populator.fillIt(ref);iter = ref.iterator(); while(iter.hasNext()){System.out.print(iter.next()); }//end while loopSystem.out.println(); }//end doIt()}// end class Worker class Populator{public static void fillIt(Collection ref){ ref.add(new MyClass(4));ref.add(new MyClass(4)); ref.add(new MyClass(3));ref.add(new MyClass(2)); ref.add(new MyClass(1));}//end fillIt() }//end class Populatorclass MyClass implements Comparable{ int data;MyClass(){ data = 0;}//end noarg constructor MyClass(int data){this.data = data; }//end parameterized constructorpublic String toString(){ return "" + data;}//end overridden toString() public int compareTo(Object o){if(!(o instanceof MyClass)) throw new ClassCastException();if(((MyClass)o).data<data) return 1;if(((MyClass)o).data>data) return -1;else return 0; }//end compareTo()public boolean equals(Object o){ if(!(o instanceof MyClass))return false; if(((MyClass)o).data == data)return true; else return false;}//end overridden equals() }//end MyClass

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Java4080r: Review
  • File: Java4080r.htm
  • Published: 12/01/13
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download aPDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, youshould be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version ofthe module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale onAmazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If youpurchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made andpublished without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask