Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ocjp 6

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

1.Which Man class properly represents the relationship "Man has a best friend wh o is a Dog"? A.

class Man extends Dog { } B. class Man implements Dog { } C. class Man { private BestFriend dog; } D. class Man { private Dog bestFriend; } E. class Man { private Dog<bestFriend>; } F. class Man { private BestFriend<dog>; } 2.Given: try { //some code here line 34 } catch (NullPointerException e1) { System.out.print("a"); } catch (Exception e2) { System.out.print("b"); } finally { System.out.print("c"); } If some sort of exception is thrown at line 34, which output is possible? A. a B. b C. c D. ac E. abc 3.abstract class A { abstract void a1(); void a2() { } } class B extends A { void a1() { } void a2() { } } class C extends B { void c1() { } } And: A x = new B(); C y = new C(); A z = new C(); What are four valid examples of polymorphic method calls? (Choose four.) A. x.a2(); B. z.a2(); C. z.c1(); D. z.a1(); E. y.c1(); F. x.a1(); 4.Given: 3. class Employee { 4. String name; double baseSalary; 5. Employee(String name, double baseSalary) { 6. this.name = name; 7. this.baseSalary = baseSalary; 8. }

9. } 10. public class SalesPerson extends Employee { 11. double commission; 12. public SalesPerson(String name, double baseSalary, double commission) { 13. // insert code here 14. } 15. } Which two code fragments, inserted independently at line 13, will compile? (Choo se two.) A. super(name, baseSalary); B. this.commission = commission; C. super(); this.commission = commission; D. this.commission = commission; super(); E. super(name, baseSalary); this.commission = commission; F. this.commission = commission; super(name, baseSalary); G. super(name, baseSalary, commission); 5.iven: 11. // insert code here 12. private N min, max; 13. public N getMin() { return min; } 14. public N getMax() { return max; } 15. public void add(N added) { 16. if (min == null || added.doubleValue() < min.doubleValue()) 17. min = added; 18. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added; 20. } 21. } Which two, inserted at line 11, will allow the code to compile? (Choose two.) A. public class MinMax<?> { B. public class MinMax<? extends Number> { C. public class MinMax<N extends Object> { D. public class MinMax<N extends Number> { E. public class MinMax<? extends Object> { F. public class MinMax<N extends Integer> { 6.A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of add(0, object), but does NOT need to support quick random access . What supports these requirements? A. java.util.Queue B. java.util.ArrayList C. java.util.LinearList D. java.util.LinkedList Given: 11. public class Person { 12. private name; 13. public Person(String name) { 14. this.name = name; 15. } 16. public int hashCode() { 17. return 420; 18. } 19. }

Which statement is true? A. The time to find the value from HashMap with a Person key depends on the size of the map. B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person. C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate. D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map. 8.Given: 1. class Alligator { 2. public static void main(String[] args) { 3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}}; 4. int [][]y = x; 5. System.out.println(y[2][1]); 6. } 7. } What is the result? A. 2 B. 3 C. 4 D. 6 E. 7 F. Compilation fails. 9.1. package packageA; 2. public class Message { 3. String getText() { return "text"; } 4. } and: 1. package packageB; 2. public class XMLMessage extends packageA.Message { 3. String getText() { return "<msg>text</msg>";} 4. public static void main(String[] args) { 5. System.out.println(new XMLMessage().getText()); 6. } 7. } What is the result of executing XMLMessage.main? A. text B. An exception is thrown at runtime. C. Compilation fails because of an error in line 2 of XMLMessage. D. Compilation fails because of an error in line 3 of XMLMessage. 10.1. class Programmer { 2. Programmer debug() { return this; } 3. } 4. class SCJP extends Programmer { 5. // insert code here 6. } Which, inserted at line 5, will compile? (Choose all that apply.) A. Programmer debug() { return this; } B. SCJP debug() { return this; } C. Object debug() { return this; } D. int debug() { return 1; } E. int debug(int x) { return 1; } F. Object debug(int x) { return this; }

11.package packageA; 2. public class Message { 3. String getText() { return "text"; } 4. } and: 1. package packageB; 2. public class XMLMessage extends packageA.Message { 3. String getText() { return "<msg>text</msg>";} 4. public static void main(String[] args) { 5. System.out.println(new XMLMessage().getText()); 6. } 7. } What is the result of executing XMLMessage.main? A. text B. An exception is thrown at runtime. C. Compilation fails because of an error in line 2 of XMLMessage. D. Compilation fails because of an error in line 3 of XMLMessage. 12.Given: class Foo { public int a = 3; public void addFive() { a += 5; System.out.print("f "); } } class Bar extends Foo { public int a = 8; public void addFive() { this.a += 5; System.out.print("b " ); } } Invoked with: Foo f = new Bar(); f.addFive(); System.out.println(f.a); What is the result? A. b 3 B. b 8 C. b 13 D. f 3 E. f 8 F. f 13 G. Compilation fails. H. An exception is thrown at runtime. 1.Given: 1. class PingPong2 { 2. synchronized void hit(long n) { 3. for(int i = 1; i < 3; i++) 4. System.out.print(n + "-" + i + " "); 5. } 6. } 1. public class Tester implements Runnable { 2. static PingPong2 pp2 = new PingPong2(); 3. public static void main(String[] args) { 4. new Thread(new Tester()).start(); 5. new Thread(new Tester()).start(); 6. } 7. public void run() { pp2.hit(Thread.currentThread().getId()); } 8. } Which statement is true? A. The output could be 5-1 6-1 6-2 5-2 B. The output could be 6-1 6-2 5-1 5-2

C. The output could be 6-1 5-2 6-2 5-1 D. The output could be 6-1 6-2 5-1 7-1 1.Given: 11. // insert code here 12. private N min, max; 13. public N getMin() { return min; } 14. public N getMax() { return max; } 15. public void add(N added) { 16. if (min == null || added.doubleValue() < min.doubleValue()) 17. min = added; 18. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added; 20. } 21. } Which two, inserted at line 11, will allow the code to compile? (Choose two.) A. public class MinMax<?> { B. public class MinMax<? extends Number> { C. public class MinMax<N extends Object> { D. public class MinMax<N extends Number> { E. public class MinMax<? extends Object> { F. public class MinMax<N extends Integer> { 1.Given: 22. StringBuilder sb1 = new StringBuilder("123"); 23. String s1 = "123"; 24. // insert code here 25. System.out.println(sb1 + " " + s1); Which code fragment, inserted at line 24, outputs "123abc 123abc"? A. sb1.append("abc"); s1.append("abc"); B. sb1.append("abc"); s1.concat("abc"); C. sb1.concat("abc"); s1.append("abc"); D. sb1.concat("abc"); s1.concat("abc"); E. sb1.append("abc"); s1 = s1.concat("abc"); F. sb1.concat("abc"); s1 = s1.concat("abc"); G. sb1.append("abc"); s1 = s1 + s1.concat("abc"); H. sb1.concat("abc"); s1 = s1 + s1.concat("abc"); 1.Think and comment ... import java.io.*; class Directories { static String [] dirs = {"dir1", "dir2"}; public static void main(String [] args) { for (String d : dirs) { // insert code 1 here File file = new File(path, args[0]); // insert code 2 here } } } and that the invocation java Directories file2.txt is issued from a directory that has two subdirectories, "dir1" and "dir1", and that "dir1" has a file "file1.txt" and "dir2" has a file "file2.txt",and the output is "false true", which set(s) of code fragments must be inserted? (Choose all that apply.) A. String path = d; System.out.print(file.exists() + " ");

B. String path = d; System.out.print(file.isFile() + " "); C. String path = File.separator + d; System.out.print(file.exists() + " "); D. String path = File.separator + d; System.out.print(file.isFile() + " "); 1.Que: 28 1. public class Plant { 2. private String name; 3. public Plant(String name) { this.name = name; } 4. public String getName() { return name; } 5. } 1. public class Tree extends Plant { 2. public void growFruit() { } 3. public void dropLeaves() { } 4. } Which statement is true? A. The code will compile without changes. B. The code will compile if public Tree() { Plant(); } is added to the Tree clas s. C. The code will compile if public Plant() { Tree(); } is added to the Plant cla ss. D. The code will compile if public Plant() { this("fern"); } is added to the Pla nt class. E. The code will compile if public Plant() { Plant("fern"); } is added to the Pl ant class. 1.Que: 27 Given: 3. import java.util.*; 4. public class Hancock { 5. // insert code here 6. list.add("foo"); 7. } 8. } Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two.) A. public void addStrings(List list) { B. public void addStrings(List<String> list) { C. public void addStrings(List<? super String> list) { D. public void addStrings(List<? extends String> list) { previous Que: 26 answer are "DE" 1.*OCJP 6 Question on 25 March 2013 Exam* Que: 26 Given: 11. public class Commander { 12. public static void main(String[] args) { 13. String myProp = /* insert code here */ 14. System.out.println(myProp); 15. } 16. } and the command line: java -Dprop.custom=gobstopper Commander Which two, placed

on line 13, will produce the output gobstopper? (Choose two.) A. System.load("prop.custom"); B. System.getenv("prop.custom"); C. System.property("prop.custom"); D. System.getProperty("prop.custom"); E. System.getProperties().getProperty("prop.custom"); previous Que: 25 answer are "BC" 1.Given: **Think and comment** its a previous OCJP6 exam question ... Que: 24 1. public class Threads3 implements Runnable { 2. public void run() { 3. System.out.print("running"); 4. } 5. public static void main(String[] args) { 6. Thread t = new Thread(new Threads3()); 7. t.run(); 8. t.run(); 9. t.start(); 10. } 11. } What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The code executes and prints "running". D. The code executes and prints "runningrunning". E. The code executes and prints "runningrunningrunning". previous Que: 23 answer are "ABD" 1.Que: 25 Given: 1. package com.company.application; 2. 3. public class MainClass { 4. public static void main(String[] args) {} 5. } And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH environment variable is set to "." (current directory). Which two java commands entered at the command line will run MainClass? (Choose two.) A. java MainClass if run from the /apps directory B. java com.company.application.MainClass if run from the /apps directory C. java -classpath /apps com.company.application.MainClass if run from any direc tory D. java -classpath . MainClass if run from the /apps/com/company/application dir ectory E. java -classpath /apps/com/company/application:. MainClass if run from the /ap ps directory F. java com.company.application.MainClass if run from the /apps/com/company/appl ication directory

previous Que: 24 answer is "E" 1.*OCJP 6 Question on 19-March-13* Que: 23 11. public interface Status { 12. /* insert code here */ int MY_VALUE = 10; 13. } Which three are valid on line 12? (Choose three.) A. final B. static C. native D. public E. private F. abstract G. protected previous Que: 22 answer is "A" 1. **Think and comment** its a previous OCJP6 exam question ... Que: 22 10. interface Data { public void load(); } 11. abstract class Info { public abstract void load(); } Which class correctly uses the Data interface and Info class? A. public class Employee extends Info implements Data { public void load() { /*do something*/ } } B. public class Employee implements Info extends Data { public void load() { /*do something*/ } } C. public class Employee extends Info implements Data { public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } } D. public class Employee implements Info extends Data { public void Data.load(){ /*do something*/ } public void load(){ /*do something*/ } } E. public class Employee implements Info extends Data { public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } } F. public class Employee extends Info implements Data{ public void Data.load() { /*do something*/ } public void Info.load() { /*do something*/ } } previous Que: 21 answer is "A"

You might also like