Java Standard Edition 6 Programmer Certified Professional Exam v6.3 (1z0-851)

Page:    1 / 20   
Total 290 questions

Given:
1. public class TestString1 {
2. public static void main(String[] args) {
3. String str = "420";
4. str += 42;
5. System.out.print(str);
6. }
7. }
What is the output?

  • A. 42
  • B. 420
  • C. 462
  • D. 42042
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


Answer : D

Given:
12. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);
The variable df is an object of type DateFormat that has been initialized in line 11. What is the result if this code is run on December 14, 2000?

  • A. The value of s is 14-dic-2000.
  • B. The value of s is Dec 14, 2000.
  • C. An exception is thrown at runtime.
  • D. Compilation fails because of an error in line 13.


Answer : D

Given:
1. public class KungFu {
2. public static void main(String[] args) {
3. Integer x = 400;
4. Integer y = x;
5. x++;
6. StringBuilder sb1 = new StringBuilder("123");
7. StringBuilder sb2 = sb1;
8. sb1.append("5");
9. System.out.println((x==y) + " " + (sb1==sb2));
10. }
11. }
What is the result?

  • A. true true
  • B. false true
  • C. true false
  • D. false false
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


Answer : B

Given that the current directory is empty, and that the user has read and write privileges to the current directory, and the following:
1. import java.io.*;
2. public class Maker {
3. public static void main(String[] args) {
4. File dir = new File("dir");
5. File f = new File(dir, "f");
6. }
7. }
Which statement is true?

  • A. Compilation fails.
  • B. Nothing is added to the file system.
  • C. Only a new file is created on the file system.
  • D. Only a new directory is created on the file system.
  • E. Both a new file and a new directory are created on the file system.


Answer : B

Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();
What is the result?

  • A. Compilation fails.
  • B. After line 15, the value of age is 5.
  • C. After line 15, the value of age is 3.
  • D. An exception is thrown at runtime.


Answer : D

Given that t1 is a reference to a live thread, which is true?

  • A. The Thread.sleep() method can take t1 as an argument.
  • B. The Object.notify() method can take t1 as an argument.
  • C. The Thread.yield() method can take t1 as an argument.
  • D. The Thread.setPriority() method can take t1 as an argument.
  • E. The Object.notify() method arbitrarily chooses which thread to notify.


Answer : E

Given that Triangle implements Runnable, and:
31. void go() throws Exception {
32. Thread t = new Thread(new Triangle());
33. t.start();
34. for(int x = 1; x < 100000; x++) {
35. //insert code here
36. if(x%100 == 0) System.out.print("g");
37. } }
38. public void run() {
39. try {
40. for(int x = 1; x < 100000; x++) {
41. // insert the same code here
42. if(x%100 == 0) System.out.print("t");
43. }
44. } catch (Exception e) { }
45. }
Which two statements, inserted independently at both lines 35 and 41, tend to allow both threads to temporarily pause and allow the other thread to execute? (Choose two.)

  • A. Thread.wait();
  • B. Thread.join();
  • C. Thread.yield();
  • D. Thread.sleep(1);
  • E. Thread.notify();


Answer : C,D

Given:
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".


Answer : E

Given:
1. public class Threads5 {
2. public static void main (String[] args) {
3. new Thread(new Runnable() {
4. public void run() {
5. System.out.print("bar");
6. }}).start();
7. }
8. }
What is the result?

  • A. Compilation fails.
  • B. An exception is thrown at runtime.
  • C. The code executes normally and prints "bar".
  • D. The code executes normally, but nothing prints.


Answer : C

Given:
11. public class PingPong implements Runnable {
12. synchronized void hit(long n) {
13. for(int i = 1; i < 3; i++)
14. System.out.print(n + "-" + i + " ");
15. }
16. public static void main(String[] args) {
17. new Thread(new PingPong()).start();
18. new Thread(new PingPong()).start();
19. }
20. public void run() {
21. hit(Thread.currentThread().getId());
22. }
23. }
Which two statements are true? (Choose two.)

  • A. The output could be 8-1 7-2 8-2 7-1
  • B. The output could be 7-1 7-2 8-1 6-1
  • C. The output could be 8-1 7-1 7-2 8-2
  • D. The output could be 8-1 8-2 7-1 7-2


Answer : C,D

Given:
10. interface A { void x(); }
11. class B implements A { public void x() {} public void y() {} }
12. class C extends B { public void x() {} } And:
20. java.util.List<A> list = new java.util.ArrayList<A>();
21. list.add(new B());
22. list.add(new C());
23. for (A a : list) {
24. a.x();
25. a.y();
26. }
What is the result?

  • A. The code runs with no output.
  • B. An exception is thrown at runtime.
  • C. Compilation fails because of an error in line 20.
  • D. Compilation fails because of an error in line 21.
  • E. Compilation fails because of an error in line 23.
  • F. Compilation fails because of an error in line 25.


Answer : F

Given:
11. class Mammal { }
12.
13. class Raccoon extends Mammal {
14. Mammal m = new Mammal();
15. }
16.
17. class BabyRaccoon extends Mammal { }
Which four statements are true? (Choose four.)

  • A. Raccoon is-a Mammal.
  • B. Raccoon has-a Mammal.
  • C. BabyRaccoon is-a Mammal.
  • D. BabyRaccoon is-a Raccoon.
  • E. BabyRaccoon has-a Mammal.
  • F. BabyRaccoon is-a BabyRaccoon.


Answer : A,B,C,F

Given:
10: public class Hello {
11: String title;
12: int value;
13: public Hello() {
14: title += " World";
15: }
16: public Hello(int value) {
17: this.value = value;
18: title = "Hello";
19: Hello();
20: }
21: } and:
30: Hello c = new Hello(5);
31: System.out.println(c.title);
What is the result?

  • A. Hello
  • B. Hello World
  • C. Compilation fails.
  • D. Hello World 5
  • E. The code runs with no output.
  • F. An exception is thrown at runtime.


Answer : C

Given:
1. class ClassA {
2. public int numberOfInstances;
3. protected ClassA(int numberOfInstances) {
4. this.numberOfInstances = numberOfInstances;
5. }
6. }
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfInstances) {
9. super(numberOfInstances);
10. }
11. public static void main(String[] args) {
12. ExtendedA ext = new ExtendedA(420);
13. System.out.print(ext.numberOfInstances);
14. }
15. }
Which statement is true?

  • A. 420 is the output.
  • B. An exception is thrown at runtime.
  • C. All constructors must be declared public.
  • D. Constructors CANNOT use the private modifier.
  • E. Constructors CANNOT use the protected modifier.


Answer : A

Given:
1. public class Target {
2. private int i = 0;
3. public int addOne(){
4. return ++i;
5. }
6. } And:
1. public class Client {
2. public static void main(String[] args){
3. System.out.println(new Target().addOne());
4. }
5. }
Which change can you make to Target without affecting Client?

  • A. Line 4 of class Target can be changed to return i++;
  • B. Line 2 of class Target can be changed to private int i = 1;
  • C. Line 3 of class Target can be changed to private int addOne(){
  • D. Line 2 of class Target can be changed to private Integer i = 0;


Answer : D

Page:    1 / 20   
Total 290 questions