cool hit counter
Cafe4Java
Cafe4Java
 

SCJP Mock Exam 3 - Q2
Exam Objective: 6.1 Given a design scenario, determine which collection classes and/or interfaces should be used to properly implement that design, including the use of the Comparable interface.
Q2)
What is the output of the following code?

//Cafe4JavaSCJP3TreeMap
import java.util.*;

public class Cafe4JavaSCJP3TreeMap {
	public static void main (String args[]) {
		TreeMap treeMap = new TreeMap();

		ShoppingCartItem item = new ShoppingCartItem("BOOK234");
		treeMap.put (item.getSerialNumber(), item);

		item = new ShoppingCartItem("VCD1464");
		treeMap.put (item.getSerialNumber(), item);

		item = new ShoppingCartItem("PT652");
		treeMap.put (item.getSerialNumber(), item);

		for (ShoppingCartItem iterItem: treeMap.values())
			System.out.println (iterItem.getSerialNumber());
	}
}

class ShoppingCartItem {
	private String serialNumber;
	private String description;
	public ShoppingCartItem (String sNo) {
		setSerialNumber(sNo);
	}
	public String getSerialNumber() {
		return serialNumber;
	}
	public void setSerialNumber(String val) {
		serialNumber = val;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String val) {
		description = val;
	}
}


  • A)
    BOOK234
    PT652
    VCD1464
    
  • B)
    BOOK234
    VCD1464
    PT652
    
  • C)
    PT652
    VCD1464
    BOOK234
    
  • D) The order cannot be determined. Each execution of this code may print out a random order.
Answer to Q2


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

Submit your feedback on this mock exam