JavaBeat Certifications Certifications Kits Articles Interview Questions OCAJP 7 OCPJP 5 OCPJP 6 OCEJWCD 6 SCBCD 5.0 SCEA SCJA
350 Mock Questions on SCJP 1.5 - JUST Rs.250 or 7 USD
Send us mail to sales@javabeat.net
more details
01. MockExam - 01 02. MockExam - 02 03. MockExam - 03 04. AutoBoxing - 1 (10 questions)
05. AutoBoxing - 2 (10 questions) 06. Generics 1 - 6 Questions 07. Generics 2 - 7 Questions 08. Generics 3 - 10 Questions
09. Generics 4 - 10 Questions 10. Generics 5 - 10 Questions 11. Generics 6 - 10 Questions 12. Enum 1 - 10 Questions
13. Enums 2 - 10 Questions 14. Enums 3 - 15 Questions 15. Var Args 1 - 10 Questions 16. Var Args 2 - 10 Questions
17. SCJP 5.0 Mock Questions -1 18. Declaration,Access Control-1 19.Java File IO Package - 1 20.Java File IO Package - 2
21.Language Features - 1 22.New Api's - 1 (10 questions) 23.New Api's - 2 (12 questions) 24.Objective - 1 (20 questions)
25.Objective - 2 (10 questions) 26.Objective - 3 (10 questions) 27.Objective - 4 (10 questions) 28.Objective - 5 (10 questions) 29.Objective - 6 (10 questions) 30.Objective - 7 (10 questions)

SCJP 5.0 Objective - 6(10 Questions)

2) What will be the output of the following program?


package scjp5_0.chap06;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Ques01 {

	public static void main(String[] args) {

		List<Box> boxes = Arrays.asList(new Box(10), new Box(-1), new Box(5), new Box(2));
		Collections.sort(boxes, new BoxComparator());

		for (Object box : boxes){
			System.out.print(((Box)box).getSize() + " ");
		}
	}
}

class Box{

	public int size;

	public Box(int size){
		this.size = size;
	}

	public int getSize(){
		return size;
	}
}

class BoxComparator implements Comparator<Box>{

	@Override
	public int compare(Box one, Box two) {

		if (one.getSize() == two.getSize()){
			return 0;
		}else if (one.getSize() < two.getSize()){
			return 1;
		}
		return -1;
	}
}
  1. The program will throw a run-time ClassCastException.
  2. The program will compile to produce the output '10 -1 5 2'.
  3. The program will compile to produce the output '-1 2 5 10'.
  4. The program will compile to produce the output '10 5 2 -1'.

Get more questions on SCJP 1.5 topics

Answer

2)d
The objects of type Box have been arranged in descending order and hence the output will be '10 5 2 -1'.

350 Mock Questions on SCJP 1.5 - JUST Rs.250 or 7 USD
Send us mail to sales@javabeat.net
more details

javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2013), JavaBeat