cool hit counter
Cafe4Java
Cafe4Java
 

SCJP Mock Exam 4 - Q2
Exam Objective: 6.5 Use capabilities in the java.util package to write code to manipulate a list by sorting, performing a binary search, or converting the list to an array. Use capabilities in the java.util package to write code to manipulate an array by sorting, performing a binary search, or converting the array to a list. Use the java.util.Comparator and java.lang.Comparable interfaces to affect the sorting of lists and arrays. Furthermore, recognize the effect of the "natural ordering" of primitive wrapper classes and java.lang. String on sorting.
Q2)
Examine the following code:

import java.util.*;
public class Cafe4JavaCollection {
	public static void main(String[] args) {
		Book[] bookArray = {new Book ("Java", "John"),
				new Book ("AJAX", "Raman"),
				new Book ("FighterJet", "Bill")};
		// INSERT CODE HERE
	}
}

public class Book {
	private String title;
	private String author;
	public Book (String varTitle, String varAuthor) {
		title = varTitle; author = varAuthor;
	}
	public String getTitle() { return title; }
	public String getAuthor() { return author; }
}

Which of the following code snippets, when replaced with '// INSERT CODE HERE', will sort the array 'bookArray' in the reverse alphabetical order of its author (Select all that apply)
  • A)
    
    class BookComparator implements Comparator<Book> {
    	public int compare(Book one, Book two) {
    		return one.getAuthor().compareTo(two.getAuthor());
    	}
    }
    Arrays.sort(bookArray, new BookComparator());
    
    
  • B)
    
    class BookComparator implements Comparator<Book> {
    	public int compare(Book one, Book two) {
    		return two.getAuthor().compareTo(one.getAuthor());
    	}
    }
    Comparator<Book> comparator = new BookComparator<Book>();
    Arrays.sort(bookArray, comparator);
    
    
  • C)
    
    Comparator<Book> comparator = new Comparator<Book> () {
    	public int compare(Book one, Book two) {
    		return two.getAuthor().compareTo(one.getAuthor());
    	}
    };
    Arrays.sort(bookArray, comparator);
    
    
  • D)
    
    class BookComparator implements Comparator<Book> {
    	public int compare(Book one, Book two) {
    		return two.getAuthor().compareTo(one.getAuthor());
    	}
    }
    Comparator<Book> comparator = new BookComparator();
    Arrays.sort(bookArray, comparator);
    
    
  • E)
    
    class BookComparator implements Comparator<Book> {
    	public int compare(Book one, Book two) {
    		return two.getAuthor().compareTo(one.getAuthor());
    	}
    }
    Arrays.sortArray(bookArray, new BookComparator());
    
    
  • F)
    
    class BookComparator implements Comparator<Book> {
    	public int compare(Book one, Book two) {
    		return two.getAuthor().compareTo(one.getAuthor());
    	}
    }
    Arrays.sort(bookArray, new BookComparator());
    
    
  • G)
    
    class BookComparator implements Comparator<Book> {
    	public int compare(Book one, Book two) {
    		return two.getAuthor().compareTo(one.getAuthor());
    	}
    }
    Comparator<Book> comparator = new BookComparator();
    Arrays.sortCollection(bookArray, comparator);
    
    
  • H) None of the above
Answer to Q2


    <<<< Back to Q1 <<<<         >>>> Go to Q3 >>>>

Submit your feedback on this mock exam