Q1.Given:
11. switch(x) {
12. default:
13. System.out.printIn(“Hello”);
14. }
Which two are acceptable types for x? (Choose two)
A. byte
B. long
C. char
D. float
E. Short
F. Long
Answer: A, C
Q2. Given:
1. public class X {
2. public static void main(String [] args) {
3. try {
4. badMethod();
5. System.out.print(“A”);
6. }
7. catch (RuntimeException ex) {
8. System.out.print(“B”);
9. }
10. catch (Exception ex1) {
11. System.out.print(“C”);
12. }
13. finally {
14. System.out.print(“D”);
15. }
16. System.out.print(“E”);
17. }
18. public static void badMethod() {
19. throw new RuntimeException();
20. }
21. }
What is the result?
A. BD
B. BCD
C. BDE
D. BCDE
E. ABCDE
F. Compilation fails.
Answer: C
Q3. Given:
1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0) ? “assertion failed” : “assertion passed”;
5. System.out.println(“Finished”);
6. }
7. }
What is the result?
A. finished
B. Compilation fails.
C. An AssertionError is thrown and finished is output.
D. An AssertionError is thrown with the message “assertion failed”.
E. An AssertionError is thrown with the message “assertion passed”.
Answer: B
Q4. Given:
1. public class ReturnIt {
2. return Type methodA(byte x, double y) {
3. return (long)x / y * 2;
4. }
5. }
What is the narrowest valid returnType for methodA in line2?
A. int
B. byte
C. long
D. short
E. float
F. double
Answer: F
Q5. Given:
1. public class OuterClass {
2. private double d1 = 1.0;
3. // insert code here
4. }
Which two are valid if inserted at line 3? (Choose two)
A. static class InnerOne {
public double methoda() { return d1; }
}
B. static class InnerOne {
static double methoda() { return d1; }
}
C. private class InnerOne {
public double methoda() { return d1; }
}
D. protected class InnerOne {
static double methoda() { return d1; }
}
E. public abstract class InnerOne {
public abstract double methoda();
}
Answer: C, E
Q6. Given:
1. public class Foo {
2. public void main( String[] args ) {
3. System.out.printIn( “Hello” + args[0] );
4. }
5. }
What is the result if this code is executed with the command line?
java Foo world
A. Hello
B. Hello Foo
C. Hello world
D. Compilation fails.
E. The code does not run.
Answer: E
Q7. Given:
11. public void foo( boolean a, boolean b ){
12. if( a ) {
13. System.out.println( “A” );
14. } else if ( a && b ) {
15. System.out.println( “A&&B” );
16. } else {
17. if ( !b ) {
18. System.out.println( “notB” );
19. } else {
20. System.out.println( “ELSE” );
21. }
22. }
23. }
What is correct?
A. If a is true and b is true then the output is “A&&B”.
B. If a is true and b is false then the output is “notB”.
C. If a is false and b is true then the output is “ELSE”.
D. If a is false and b is false then the output is “ELSE”.
Answer: C
Q8. Which two cause a compiler error? (Choose two)
A. int[] scores = {3, 5, 7};
B. int [][] scores = {2,7,6}, {9,3,45};
C. String cats[] = {“Fluffy”, “Spot”, “Zeus”};
D. boolean results[] = new boolean [3] {true, false, true};
E. Integer results[] = {new Integer(3), new Integer(5), new
Integer(8)};
F. String[] dogs = new String[]{new String(“Fido”),new
String(“Spike”), new String(“Aiko”)};
Answer: B, D
Q9. Given:
11. int i = 0, j = 5;12. tp; for (;;) {
12. i++;
13. for(;;) {
14. if (i> --j) {
15. break tp;
16. break tp;
17. }
18. }
19. System.out.printIn(“i=” +i “,j =”+j);
What is the result?
A. i = 1, j = 0
B. i = 1, j = 4
C. i = 3, j = 4
D. i = 3, j = 0
E. Compilation fails.
Answer: E
11. switch(x) {
12. default:
13. System.out.printIn(“Hello”);
14. }
Which two are acceptable types for x? (Choose two)
A. byte
B. long
C. char
D. float
E. Short
F. Long
Answer: A, C
Q2. Given:
1. public class X {
2. public static void main(String [] args) {
3. try {
4. badMethod();
5. System.out.print(“A”);
6. }
7. catch (RuntimeException ex) {
8. System.out.print(“B”);
9. }
10. catch (Exception ex1) {
11. System.out.print(“C”);
12. }
13. finally {
14. System.out.print(“D”);
15. }
16. System.out.print(“E”);
17. }
18. public static void badMethod() {
19. throw new RuntimeException();
20. }
21. }
What is the result?
A. BD
B. BCD
C. BDE
D. BCDE
E. ABCDE
F. Compilation fails.
Answer: C
Q3. Given:
1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0) ? “assertion failed” : “assertion passed”;
5. System.out.println(“Finished”);
6. }
7. }
What is the result?
A. finished
B. Compilation fails.
C. An AssertionError is thrown and finished is output.
D. An AssertionError is thrown with the message “assertion failed”.
E. An AssertionError is thrown with the message “assertion passed”.
Answer: B
Q4. Given:
1. public class ReturnIt {
2. return Type methodA(byte x, double y) {
3. return (long)x / y * 2;
4. }
5. }
What is the narrowest valid returnType for methodA in line2?
A. int
B. byte
C. long
D. short
E. float
F. double
Answer: F
Q5. Given:
1. public class OuterClass {
2. private double d1 = 1.0;
3. // insert code here
4. }
Which two are valid if inserted at line 3? (Choose two)
A. static class InnerOne {
public double methoda() { return d1; }
}
B. static class InnerOne {
static double methoda() { return d1; }
}
C. private class InnerOne {
public double methoda() { return d1; }
}
D. protected class InnerOne {
static double methoda() { return d1; }
}
E. public abstract class InnerOne {
public abstract double methoda();
}
Answer: C, E
Q6. Given:
1. public class Foo {
2. public void main( String[] args ) {
3. System.out.printIn( “Hello” + args[0] );
4. }
5. }
What is the result if this code is executed with the command line?
java Foo world
A. Hello
B. Hello Foo
C. Hello world
D. Compilation fails.
E. The code does not run.
Answer: E
Q7. Given:
11. public void foo( boolean a, boolean b ){
12. if( a ) {
13. System.out.println( “A” );
14. } else if ( a && b ) {
15. System.out.println( “A&&B” );
16. } else {
17. if ( !b ) {
18. System.out.println( “notB” );
19. } else {
20. System.out.println( “ELSE” );
21. }
22. }
23. }
What is correct?
A. If a is true and b is true then the output is “A&&B”.
B. If a is true and b is false then the output is “notB”.
C. If a is false and b is true then the output is “ELSE”.
D. If a is false and b is false then the output is “ELSE”.
Answer: C
Q8. Which two cause a compiler error? (Choose two)
A. int[] scores = {3, 5, 7};
B. int [][] scores = {2,7,6}, {9,3,45};
C. String cats[] = {“Fluffy”, “Spot”, “Zeus”};
D. boolean results[] = new boolean [3] {true, false, true};
E. Integer results[] = {new Integer(3), new Integer(5), new
Integer(8)};
F. String[] dogs = new String[]{new String(“Fido”),new
String(“Spike”), new String(“Aiko”)};
Answer: B, D
Q9. Given:
11. int i = 0, j = 5;12. tp; for (;;) {
12. i++;
13. for(;;) {
14. if (i> --j) {
15. break tp;
16. break tp;
17. }
18. }
19. System.out.printIn(“i=” +i “,j =”+j);
What is the result?
A. i = 1, j = 0
B. i = 1, j = 4
C. i = 3, j = 4
D. i = 3, j = 0
E. Compilation fails.
Answer: E
No comments :
Post a Comment