Java Standard Edition 6 Programmer Certified Professional Upgrade Exam v6.0 (1z0-852)

Page:    1 / 7   
Total 96 questions

Given:
11. public static void main(String[] args) {
12. Integer i = new Integer(1) + new Integer(2);
13. switch(i) {
14. case 3: System.out.println("three"); break;
15. default: System.out.println("other"); break;
16. }
17. }
What is the result?

  • A. three
  • B. other
  • C. An exception is thrown at runtime.
  • D. Compilation fails because of an error on line 12.
  • E. Compilation fails because of an error on line 13.
  • F. Compilation fails because of an error on line 15.


Answer : A

Given:
1. public class Mule {
2. public static void main(String[] args) {
3. boolean assert = true;
4. if(assert) {
5. System.out.println("assert is true");
6. }
7. }
8. }
Which command-line invocations will compile?

  • A. javac Mule.java
  • B. javac -source 1.3 Mule.java
  • C. javac -source 1.4 Mule.java
  • D. javac -source 1.5 Mule.java


Answer : B

Given:
1. public class Donkey2 {
2. public static void main(String[] args) {
3. boolean assertsOn = true;
4. assert (assertsOn) : assertsOn = true;
5. if(assertsOn) {
6. System.out.println("assert is on");
7. }
8. }
9. }
If class Donkey is invoked twice, the first time without assertions enabled, and the second timewith assertions enabled, what are the results?

  • A. no output
  • B. no output assert is on
  • C. assert is on
  • D. no output An AssertionError is thrown.
  • E. assert is on An AssertionError is thrown.


Answer : C

Given:
1. public class Venus {
2. public static void main(String[] args) {
3. int [] x = {1,2,3};
4. int y[] = {4,5,6};
5. new Venus().go(x,y);
6. }
7. void go(int[]... z) {
8. for(int[] a : z)
9. System.out.print(a[0]);
10. }
11. }
What is the result?

  • A. 1
  • B. 12
  • C. 14
  • D. 123
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


Answer : C

Given:
1. class TestException extends Exception { }
2. class A {
3. public String sayHello(String name) throws TestException {
4. if(name == null) throw new TestException();
5. return "Hello " + name;
6. }
7. }
8. public class TestA {
9. public static void main(String[] args) {
10. new A().sayHello("Aiko");
11. }
12. }
Which statement is true?

  • A. Compilation succeeds.
  • B. Class A does not compile.
  • C. The method declared on line 9 cannot be modified to throw TestException.
  • D. TestA compiles if line 10 is enclosed in a try/catch block that catches TestException.


Answer : D

Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

  • A. int []x = {1,2,3,4,5}; for(int y = 0; y < 6; y++) System.out.println(x[y]);
  • B. static int[] x = {7,6,5,4}; static { x[1] = 8; x[4] = 3; }
  • C. for(int y = 10; y < 10; y++) doStuff(y);
  • D. void doOne(int x) { doTwo(x); } void doTwo(int y) { doThree(y); } void doThree(int z) { doTwo(z); }
  • E. for(int x = 0; x < 1000000000; x++) doStuff(x);
  • F. void counter(int i) { counter(++i); }


Answer : D,F

Given:
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?

  • A. TestA
  • B. TestB
  • C. Compilation fails.
  • D. An exception is thrown at runtime.


Answer : B

Given:
11. public class Ball{
12. public enum Color { RED, GREEN, BLUE };
13. public void foo(){
14. // insert code here
15. { System.out.println(c); }
16. }
17. }
Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?

  • A. for( Color c : Color.values() )
  • B. for( Color c = RED; c <= BLUE; c++ )
  • C. for( Color c ; c.hasNext() ; c.next() )
  • D. for( Color c = Color[0]; c <= Color[2]; c++ )
  • E. for( Color c = Color.RED; c <= Color.BLUE; c++ )


Answer : A

Given:
5. class Atom {
6. Atom() { System.out.print("atom "); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super("granite ");
14. new Rock("granite ");
15. }
16. public static void main(String[] a) { new Mountain(); }
17. }
What is the result?

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


Answer : F

Given:
11. public class Rainbow {
12. public enum MyColor {
13. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);
14. private final int rgb;
15. MyColor(int rgb) { this.rgb = rgb; }
16. public int getRGB() { return rgb; }
17. };
18. public static void main(String[] args) {
19. // insert code here
20. }
21. }
Which code fragment, inserted at line 19, allows the Rainbow class to compile?

  • A. MyColor skyColor = BLUE;
  • B. MyColor treeColor = MyColor.GREEN;
  • C. if(RED.getRGB() < BLUE.getRGB()) { }
  • D. Compilation fails due to other error(s) in the code.
  • E. MyColor purple = new MyColor(0xff00ff);
  • F. MyColor purple = MyColor.BLUE + MyColor.RED;


Answer : B

Given:
10. class Line {
11. public class Point { public int x,y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // insert code here
17. }
18. }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?

  • A. Point p = Line.getPoint();
  • B. Line.Point p = Line.getPoint();
  • C. Point p = (new Line()).getPoint();
  • D. Line.Point p = (new Line()).getPoint();


Answer : D

Given:
11. public interface A111 {
12. String s = "yo";
13. public void method1();
14. }
17. interface B { }
20. interface C extends A111, B {
21. public void method1();
22. public void method1(int x);
23. }
What is the result?

  • A. Compilation succeeds.
  • B. Compilation fails due to multiple errors.
  • C. Compilation fails due to an error only on line 20.
  • D. Compilation fails due to an error only on line 21.
  • E. Compilation fails due to an error only on line 22.
  • F. Compilation fails due to an error only on line 12.


Answer : A

Given:
11. public class Barn {
12. public static void main(String[] args) {
13. new Barn().go("hi", 1);
14. new Barn().go("hi", "world", 2);
15. }
16. public void go(String... y, int x) {
17. System.out.print(y[y.length - 1] + " ");
18. }
19. }
What is the result?

  • A. hi hi
  • B. hi world
  • C. world world
  • D. Compilation fails.
  • E. An exception is thrown at runtime.


Answer : D

Given:
11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print("Bfoo "); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();
20. b.foo();
21. }
22. }
What is the result?

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


Answer : D

Given:
15. public class Pass2 {
16. public void main(String [] args) {
17. int x = 6;
18. Pass2 p = new Pass2();
19. p.doStuff(x);
20. System.out.print(" main x = " + x);
21. }
22.
23. void doStuff(int x) {
24. System.out.print(" doStuff x = " + x++);
25. }
26. }
And the command-line invocations:
javac Pass2.java
java Pass2 5
What is the result?

  • A. Compilation fails.
  • B. An exception is thrown at runtime.
  • C. doStuff x = 6 main x = 6
  • D. doStuff x = 6 main x = 7
  • E. doStuff x = 7 main x = 6
  • F. doStuff x = 7 main x = 7


Answer : B

Page:    1 / 7   
Total 96 questions