카테고리 없음
OCJP Study 24
Soul-Learner
2013. 12. 31. 21:31
OCJP(Java 6) 기출문제 연습 24(231~240)
PC나 스마트폰에서도 동일하게 실행되므로 장소를 가리지 않고 OCJP(JAVA 6) 국제 자격증을 목표로 학습할 수 있다.
QUESTION 231
Given:
public static void main(String[] args) {
String str = "null";
if (str == null) {
System.out.println("null");
} else (str.length() == 0) {
System.out.println("zero");
} else {
System.out.println("some");
}
}
What is the result?
Given:
public static void main(String[] args) {
String str = "null";
if (str == null) {
System.out.println("null");
} else (str.length() == 0) {
System.out.println("zero");
} else {
System.out.println("some");
}
}
What is the result?
D
The first "else" must be "else if"
ZZZ
ZZZ
QUESTION 232
Given:
1. import java.io.IOException;
2.
3. class A {
4.
5. public void process() {
6. System.out.print("A,");
7. }
8.
9. }
10.
11.
12.class B extends A {
13.
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19.
20. public static void main(String[] args) {
21. try {
22. new B().process();
23. } catch (IOException e) {
24. System.out.println("Exception");
25. }
26. }
27.}
What is the result?
1. import java.io.IOException;
2.
3. class A {
4.
5. public void process() {
6. System.out.print("A,");
7. }
8.
9. }
10.
11.
12.class B extends A {
13.
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19.
20. public static void main(String[] args) {
21. try {
22. new B().process();
23. } catch (IOException e) {
24. System.out.println("Exception");
25. }
26. }
27.}
What is the result?
D
Explanation/Reference:
Exception IOException is not compatible with throws clause in A.process()
-------------------------
To work, line 5 should have throws IOException
(ZZZ)
Exception IOException is not compatible with throws clause in A.process()
-------------------------
To work, line 5 should have throws IOException
(ZZZ)
QUESTION 233
Given:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i = 0; i < 10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i = 0; i < 10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?
D
설명없음
QUESTION 234
Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11.}
When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection?
Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11.}
When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection?
D
설명없음
QUESTION 235
Given:
public class Spock {
public static void main(String[] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
if ((tail > distance) ^ ((story * 2) == tail))
System.out.print("1");
if ((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print("2");
}
}
What is the result?
Given:
public class Spock {
public static void main(String[] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
if ((tail > distance) ^ ((story * 2) == tail))
System.out.print("1");
if ((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print("2");
}
}
What is the result?
E
설명없음
QUESTION 236
Given:
1. public class Pass2 {
2. public void main(String[] args) {
3. int x = 6;
4. Pass2 p = new Pass2();
5. p.doStuff(x);
6. System.out.print(" main x = " + x);
7. }
8.
9. void doStuff(int x) {
10. System.out.print(" doStuff x = " + x++);
11. }
12.}
And the command-line invocations:
javac Pass2.java
java Pass2 5
What is the result?
1. public class Pass2 {
2. public void main(String[] args) {
3. int x = 6;
4. Pass2 p = new Pass2();
5. p.doStuff(x);
6. System.out.print(" main x = " + x);
7. }
8.
9. void doStuff(int x) {
10. System.out.print(" doStuff x = " + x++);
11. }
12.}
And the command-line invocations:
javac Pass2.java
java Pass2 5
What is the result?
B
Exception in thread "main" java.lang.NoSuchMethodError: main
On line 2, the main method should be static (ZZZ)
QUESTION 237
Given:
1. interface DeclareStuff {
2. public static final int EASY = 3;
3.
4. void doStuff(int t);
5. }
6.
7. public class TestDeclare implements DeclareStuff {
8. public static void main(String[] args) {
9. int x = 5;
10. new TestDeclare().doStuff(++x);
11. }
12.
13. void doStuff(int s) {
14. s += EASY + ++s;
15. System.out.println("s " + s);
16. }
17.}
What is the result?
Given:
1. interface DeclareStuff {
2. public static final int EASY = 3;
3.
4. void doStuff(int t);
5. }
6.
7. public class TestDeclare implements DeclareStuff {
8. public static void main(String[] args) {
9. int x = 5;
10. new TestDeclare().doStuff(++x);
11. }
12.
13. void doStuff(int s) {
14. s += EASY + ++s;
15. System.out.println("s " + s);
16. }
17.}
What is the result?
D
Explanation/Reference:
Cannot reduce the visibility of the inherited method from DeclareStuff
Interface methods are public so doStuff is public in DeclareStuff interface. doStuff is package-private in TestDeclare class and this visibility change is not allowed.
(ZZZ)
Interface methods are public so doStuff is public in DeclareStuff interface. doStuff is package-private in TestDeclare class and this visibility change is not allowed.
(ZZZ)
QUESTION 238
A class games.cards.Poker is correctly defined in the jar file Poker.jar.
A user wants to execute the main method of Poker on a UNIX system using the command:
java games.cards.Poker
What allows the user to do this?
A class games.cards.Poker is correctly defined in the jar file Poker.jar.
A user wants to execute the main method of Poker on a UNIX system using the command:
java games.cards.Poker
What allows the user to do this?
C
설명없음
QUESTION 239
Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2.
3. public class Commander {
4. public static void main(String[] args) {
5. // more code here
6. }
7. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that the classpath contains "." (current directory). Which command line correctly runs Commander?
Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2.
3. public class Commander {
4. public static void main(String[] args) {
5. // more code here
6. }
7. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that the classpath contains "." (current directory). Which command line correctly runs Commander?
B
설명없음
QUESTION 240
Given:
interface DoStuff2 {
float getRange(int low, int high);
}
interface DoMore {
float getAvg(int a, int b, int c);
}
abstract class DoAbstract implements DoStuff2, DoMore {
}
class DoStuff implements DoStuff2 {
public float getRange(int x, int y) {
return 3.14f;
}
}
interface DoAll extends DoMore {
float getAvg(int a, int b, int c, int d);
}
What is the result?
Given:
interface DoStuff2 {
float getRange(int low, int high);
}
interface DoMore {
float getAvg(int a, int b, int c);
}
abstract class DoAbstract implements DoStuff2, DoMore {
}
class DoStuff implements DoStuff2 {
public float getRange(int x, int y) {
return 3.14f;
}
}
interface DoAll extends DoMore {
float getAvg(int a, int b, int c, int d);
}
What is the result?
A
설명없음