SCJP Java Collections Mock Exam Questions
SCJP Java Collections Mock Exam Questions
About Us
Powered by QuizOver.com The Leading Online Quiz & Exam Creator Create, Share, and Discover Quizzes & Exams http://www.quizover.com
(2) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
Disclaimer
All services and content of QuizOver.com are provided under QuizOver.com terms of use on an "as is" basis, without warranty of any kind, either expressed or implied, including, without limitation, warranties that the provided services and content are free of defects, merchantable, fit for a particular purpose or non-infringing. The entire risk as to the quality and performance of the provided services and content is with you. In no event shall QuizOver.com be liable for any damages whatsoever arising out of or in connection with the use or performance of the services. Should any provided services and content prove defective in any respect, you (not the initial developer, author or any other contributor) assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty constitutes an essential part of these "terms of use". No use of any services and content of QuizOver.com is authorized hereunder except under this disclaimer. The detailed and up to date "terms of use" of QuizOver.com can be found under: http://www.QuizOver.com/public/termsOfUse.xhtml
(3) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
Oracle, Sun, Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates.
Creative Commons License Attribution-NonCommercial-NoDerivs 3.0 Unported (CC BY-NC-ND 3.0) http://creativecommons.org/licenses/by-nc-nd/3.0/ You are free to: Share: copy and redistribute the material in any medium or format The licensor cannot revoke these freedoms as long as you follow the license terms. Under the following terms: Attribution: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. NonCommercial: You may not use the material for commercial purposes. NoDerivatives: If you remix, transform, or build upon the material, you may not distribute the modified material. No additional restrictions: You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
(4) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
Table of Contents
Quiz Permalink: http://www.quizover.com/pdf/java-collections-certification-questions Author Profile: http://www.quizover.com/user/profile/Java.Champ
1. Collections
(5) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4. Chapter: Collections
1. Collections Questions
(6) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.1. What is the result of compiling and running the following code?
import java.util.Arrays; import java.util.Set; import java.util.TreeSet; public class Tester extends Thread { public static void main(String[] args) { Integer[] arr = {7,5,7,3}; Set<Integer> set = new TreeSet<Integer>(Arrays.asList(arr)); set.add(4); for(Integer value: set){ System.out.print(value); } } } Please choose only one answer: Compilation error 75734 3457 34577
Check the answer of this question online on QuizOver.com: Question: how to sort java Treeset? Permalink: http://www.quizover.com/pdf/how-to-sort-java-treeset
(7) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.2. What is the result of compiling and running the following code?
import java.util.*; public class Test { public static void main(String[] args) { TreeSet s = new TreeSet(); s.add(1); s.add(99.9); s.add(99.9); s.add(96.9); for (int i = 0; i < s.size(); i++) { System.out.print(s.pollFirst()+" "); } } } Please choose only one answer: 1 96.9 99.9 1 96.9 99.9 99.9 1 compilation error an exception is thrown at run time
Check the answer of this question online on QuizOver.com: Question: inserting into a non generic TreeSet in java Permalink: http://www.quizover.com/pdf/inserting-into-a-non-generic-treeset-in-java
(8) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.3. What is the result of compiling and running the following code?
import java.util.HashSet; import java.util.Set; public class Test{ public static void main(String[] args) { Set<Human> humans = new HashSet<Human>(); humans.add(new Human(13)); humans.add(new Human(33)); humans.add(new Human(21)); humans.add(new Human(21)); System.out.print(humans.size()+" "); System.out.print(humans); } } class Human implements Comparable<Human> { Integer age; public Human(int age) { this.age = age; } public int compareTo(Human h) { return h.age.compareTo(this.age); } public String toString() { return ""+this.age; } } Please choose only one answer: 4 [33, 21, 13, 21] 3 [21, 33, 13] compilation error exception is thrown at run time
Check the answer of this question online on QuizOver.com: Question: inserting into a HashSet in java Permalink: http://www.quizover.com/pdf/inserting-into-a-hashset-in-java
(9) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.4. What is the output of compiling and running the following code?
public class Test{ public static void main(String[] args) { Foo foo1 = new Foo(1); Foo foo2 = new Foo(2); System.out.print(foo1.equals(foo2)); } } class Foo { Integer code; Foo(Integer c) { code = c; } public boolean equals(Foo f) { return false; } public boolean equals(Object f) { return true; } } Please choose only one answer: true false
Check the answer of this question online on QuizOver.com: Question: overriding equals() method in java Permalink: http://www.quizover.com/pdf/overriding-equals-method-in-java
(10) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.5. What is the result of compiling and running the following code?
import java.util.TreeMap; public class Test { public static void main(String[] args) { TreeMap<Integer,String> map = new TreeMap<Integer,String>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); map.put(4, "four"); System.out.print(map.higherKey(2)); System.out.print(map.ceilingKey(2)); System.out.print(map.floorKey(1)); System.out.print(map.lowerKey(1)); } } Please choose only one answer: 3211 321null 321-1 231null 2310
Check the answer of this question online on QuizOver.com: Question: TreeMap methods in java Permalink: http://www.quizover.com/pdf/treemap-methods-in-java
(11) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.6. What is the result of compiling and running the following code?
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test{ public static void main(String[] args) { List<Human> humans = new ArrayList<Human>(); humans.add(new Human(13)); humans.add(new Human(33)); humans.add(new Human(21)); humans.add(new Human(21)); Collections.sort(humans); System.out.print(humans.get(0).age); System.out.print(humans.size()); } } class Human implements Comparable<Human> { int age; public Human(int age) { this.age = age; } public int compareTo(Human h) { return h.age.compareTo(this.age); } } Please choose only one answer: 333 334 133 134 Compilation fails
Check the answer of this question online on QuizOver.com: Question: How to implement Comparable in java Permalink: http://www.quizover.com/pdf/how-to-implement-comparable-in-java
(12) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.7. In this program, the set humans allows for objects of type Human wi...
In this program, the set humans allows for objects of type Human with the same age to be inserted. What can be done to make the set reject any Human object with value age equals to age value for an already inserted human object?
import java.util.HashSet; import java.util.Set; public class Test{ public static void main(String[] args) { Set<Human> humans = new HashSet<Human>(); humans.add(new Human(13)); humans.add(new Human(33)); humans.add(new Human(21)); humans.add(new Human(21)); } } class Human { Integer age; public Human(int age) { this.age = age; } } Please choose only one answer: only override method equals() in Human as follows: public boolean equals(Object h) { return this.age==((Human)h).age; } only override method hashCode() in Human as follows: public int hashCode(){ return 17; } must override both methods hashCode() and equals() as illustrated in the above answers
Check the answer of this question online on QuizOver.com: Question: How the Set collection allows no duplicates in java ? Permalink: http://www.quizover.com/pdf/how-the-set-collection-allows-no-duplicates-in-java
(13) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
(14) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.8. What is the output of compiling and running the following program?
import java.util.NavigableMap; import java.util.TreeMap; public class Tester { public static void main(String[] args) { TreeMap tree = new TreeMap(); tree.put("aa", 1); tree.put("cc", 2); tree.put("ee", 3); tree.put("gg", 4); NavigableMap nvMap = tree.headMap("ee", false); nvMap.put("nn", 5); // line 16 System.out.println(nvMap); } } Please choose only one answer: {aa=1, cc=2, ee=3} {aa=1, cc=2, ee=3, nn=5} Compilation error at line 16 Exception is thrown at line 16
Check the answer of this question online on QuizOver.com: Question: How to use java headMap? Permalink: http://www.quizover.com/pdf/how-to-use-java-headmap
(15) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.9. What can be inserted, independently, at line // insert code here to...
What can be inserted, independently, at line // insert code here to cause the program to print out : [3, 4, 7, 0]
import java.util.*; public class Test { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<Integer>(); queue.add(1); queue.add(3); queue.add(4); queue.add(7); // insert code here System.out.println(queue); } } Please choose all the answers that apply: queue.poll();queue.offer(0);queue.peek(); queue.peek();queue.add(0);queue.peek();queue.poll(); queue.add(0);queue.remove();queue.peek(); queue.add(0);queue.poll();queue.remove();
Check the answer of this question online on QuizOver.com: Question: PriorityQueue methods in java Permalink: http://www.quizover.com/pdf/priorityqueue-methods-in-java
(16) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.10. What is the result of compiling and running the following code?
import java.util.*; public class Test { public static void main(String[] args) { String[] arr = { "Java", "Champ", "." }; List<String> list = (List<String>) Arrays.asList(arr); // line 1 arr[2] = ".com"; // line 2 for (String word : list) { System.out.print(word); } } } Please choose only one answer: JavaChamp. JavaChamp.com compilation error at line 1, the correct method is toList not asList compilation error at line 2, cannot modify array after converting to a List compilation error in other lines than lines 1 and 2
Check the answer of this question online on QuizOver.com: Question: asList in java Permalink: http://www.quizover.com/pdf/aslist-in-java
(17) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.11. Based on equals() and hashCode() contract in java, which of the fol...
Based on equals() and hashCode() contract in java, which of the following is true? Please choose all the answers that apply: if o1.equals(o2) is true, then hashCode(o1)== hashCode(o2) must return true if o1.equals(o2) is true, then hashCode(o1)== hashCode(o2) might return true if o1.equals(o2) is false, then hashCode(o1)== hashCode(o2) must return false if o1.equals(o2) is false, then hashCode(o1)== hashCode(o2) might return true if hashCode(o1)== hashCode(o2) returns true then o1.equals(o2) must return true
Check the answer of this question online on QuizOver.com: Question: equals and hashCode contract in java Permalink: http://www.quizover.com/pdf/equals-and-hashcode-contract-in-java
(18) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.12. What is the output of compiling and running the following program?
import java.util.NavigableMap; import java.util.TreeMap; public class Tester{ public static void main(String[] args) { TreeMap tree = new TreeMap(); tree.put("aa", 1); tree.put("cc", 2); tree.put("ee", 3); tree.put("gg", 4); NavigableMap nvMap = tree.headMap("ee", false); System.out.print(nvMap); } } Please choose only one answer: {aa=1, cc=2} {aa=1, cc=2,ee=3} {aa=1, cc=2, ee=3, gg=4} No output is produced Compilation error
Check the answer of this question online on QuizOver.com: Question: What does java headMap() do? Permalink: http://www.quizover.com/pdf/what-does-java-headmap-do
(19) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
What does the method ceilingKey in TreeMap do? Please choose only one answer: Returns the least key greater than or equal to the given key, or null if there is no such key. Returns the least key strictly greater than the given key, or null if there is no such key. Returns the least key in the tree
Check the answer of this question online on QuizOver.com: Question: ceilingKey in TreeMap in java Permalink: http://www.quizover.com/pdf/ceilingkey-in-treemap-in-java
(20) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.14. What is the result of compiling and running the following code?
import java.util.SortedSet; import java.util.TreeSet; public class Test { public static void main(String[] args) { TreeSet<Integer> map = new TreeSet<Integer>(); map.add(1); map.add(2); map.add(4); map.add(7); SortedSet<Integer> smap = map.subSet(2,7); map.add(5); map.add(9); System.out.println(smap); } } Please choose only one answer: [2, 4, 5, 7] [2, 4, 7] [2, 4, 5, 7, 9] [2, 4, 5] [2, 4]
Check the answer of this question online on QuizOver.com: Question: subSet in java Permalink: http://www.quizover.com/pdf/subset-in-java
(21) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.15. What is the result of compiling and running the following code?
import java.util.SortedMap; import java.util.TreeMap; public class Test { public static void main(String[] args) { TreeMap<Integer,String> map = new TreeMap<Integer,String>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); map.put(4, "four"); SortedMap<Integer, String> smap1 = map.tailMap(2); SortedMap<Integer, String> smap2 = smap1.headMap(4); SortedMap<Integer, String> smap3 = smap2.subMap(2, 3); System.out.println(smap3); } } Please choose only one answer: {2=two, 3=three, 4=four} {2=two, 3=three} {2=two} no output is printed
Check the answer of this question online on QuizOver.com: Question: tailMap, headMap and subMap in java Permalink: http://www.quizover.com/pdf/tailmap-headmap-and-submap-in-java
(22) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.16. What is the result of compiling and running the following code?
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Tester{ public static void main(String[] args) { List<Human> humans = new ArrayList<Human>(); humans.add(new Human(13)); humans.add(new Human(33)); humans.add(new Human(21)); humans.add(new Human(21)); // line 1 HumanComparator c = new HumanComparator(); // line 2 Collections.sort(humans, c); // line 3 System.out.print(humans.get(0).age); Collections.sort(humans); // line 4 System.out.print(humans.get(0).age); } } class Human implements Comparable<Human> { Integer age; public Human(int age) { this.age = age; } public int compareTo(Human h) { return h.age.compareTo(this.age); } } class HumanComparator implements Comparator<Human> { public int compare(Human h1, Human h2) { return h1.age.compareTo(h2.age); } } Please choose only one answer: line 1 causes a compilation error (no duplicates are allowed) line 2 causes a compilation error, the correct instantiation for HumanComparator is as so : new HumanComparator<Human>() line 4 causes a compilation error, must supply a Comparator to methos sort.
(23) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
The program compiles and runs fine printing 1333 The program compiles and runs fine printing 3313
Check the answer of this question online on QuizOver.com: Question: how to sort a collection in java? Permalink: http://www.quizover.com/pdf/how-to-sort-a-collection-in-java
(24) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.17. The following code will throw a runtime exception, which line cause...
The following code will throw a runtime exception, which line causes the exception?
import java.util.HashSet; import java.util.Set; import java.util.TreeSet; public class Test{ public static void main(String[] args) { Set s = new HashSet(); s.add("A"); // Line 1 s.add(new Foo()); // Line 2 Set t = new TreeSet(); t.add("A"); // Line 3 t.add(new Foo()); // Line 4 } } class Foo {} Please choose only one answer: Line 1 Line 2 Line 3 Line 4
Check the answer of this question online on QuizOver.com: Question: inserting into a TreeSet in java Permalink: http://www.quizover.com/pdf/inserting-into-a-treeset-in-java
(25) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.18. What is the expected output after compiling and running the followi...
What is the expected output after compiling and running the following code?
import java.util.HashSet; import java.util.Set; public class Test{ public static void main(String[] args) { Set<Foo> myFooSet = new HashSet<Foo>(); myFooSet.add(new Foo(2)); myFooSet.add(new Foo(1)); myFooSet.add(new Foo(3)); myFooSet.add(new Foo(3)); myFooSet.add(new Foo(2)); System.out.print(myFooSet.size()); } } class Foo { Integer code; Foo(Integer c) { code = c; } public boolean equals(Foo f) { return false; } public boolean equals(Object f) { return true; } public int hashCode() { return 17; } } Please choose only one answer: 1 3 5 compilation error
Check the answer of this question online on QuizOver.com: Question: how to override equals() in java? Permalink: http://www.quizover.com/pdf/how-to-override-equals-in-java
(26) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
(27) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.19. What is the result of compiling and running the following code?
import java.util.Arrays; public class Tester { public static void main(String[] args) { String[] arr = { "java", "champ", "you" }; System.out.print(Arrays.binarySearch(arr, "java")); System.out.print(Arrays.binarySearch(arr, "You")); } } Please choose only one answer: 02 0-1 13 the result is unpredictable compilation error
Check the answer of this question online on QuizOver.com: Question: searching an array using binarySearch in java Permalink: http://www.quizover.com/pdf/searching-an-array-using-binarysearch-in-java
(28) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.20. What is the result of compiling and running the following code?
import java.util.Arrays; public class Tester { public static void main(String[] args) { Integer[] arr = { 1, 2, 3 }; System.out.print(Arrays.binarySearch(arr, 1)); // line 1 System.out.print(Arrays.binarySearch(arr, "javachamp")); // line 2 } } Please choose all the answers that apply: line 1 will print 0 line 1 will print 1 line 1 causes compilation error line 2 will print 0 line 2 will causes compilation error line 2 will causes RunTimeException
Check the answer of this question online on QuizOver.com: Question: binarySearch in java Permalink: http://www.quizover.com/pdf/binarysearch-in-java
(29) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved
4.1.21. What is the result of compiling and running the following code?
import java.util.Arrays; public class Tester{ public static void main(String[] args) { String[] arr = { "java", "champ", "champion" }; Arrays.sort(arr); System.out.print(Arrays.binarySearch(arr, "champion")); System.out.print(Arrays.binarySearch(arr, "You")); } } Please choose only one answer: 10 1-1 un expected result 20 2-1 compilation error
Check the answer of this question online on QuizOver.com: Question: using binarySearch with an array in java Permalink: http://www.quizover.com/pdf/using-binarysearch-with-an-array-in-java
(30) Powered by QuizOver.com - http://www.quizover.com QuizOver.com is the leading online quiz & exam creator Copyright (c) 2009-2014 all rights reserved