728x90
[1Z0-808][OCAJP] Dump 문제 91~100
문제 91
Given :
public class App {
public static void main(String[] args) {
Boolean[] bool = new Boolean[2];
bool[0] = new Boolean(Boolean.parseBoolean("true"));
bool[1] = new Boolean(null);
System.out.println(bool[0] + " " + bool[1]);
}
}
Q. What is the result? | |
A | true null |
B | Compilation fails |
C | true false |
D | A NullPointerException is thrown at runtime |
정답
C
해설/결과
- Boolean 형 자료형은 true(1) 값을 제외한 나머지 값들을 false로 처리한다.
- parseBoolean() 메서드는 String 형 변수를 Boolean 형 변수로 변환해준다.
- 따라서 bool[0]에는 "true" 값이 할당된다.
- null은 true가 아닌 값이므로 false로 처리된다.
- parseBoolean() 메서드는 String 형 변수를 Boolean 형 변수로 변환해준다.
문제 92
Given the code fragment :
public static void main(String[] args) {
ArrayList<Integer> points = new ArrayList<> ();
points.add(1);
points.add(2);
points.add(3);
points.add(4);
points.add(null);
points.remove(2);
points.remove(null);
System.out.println(points);
}
Q. What is the result? | |
A | A NullPointerException is thrown at runtime. |
B | [1, 2, 4] |
C | [1, 2, 4, null] |
D | [1, 3, 4, null] |
E | [1, 3, 4] |
F | Compilation fails. |
정답
B
해설/결과
- points.remove(2); 는 배열에서 인덱스가 2인 원소를 제거한다.
문제 93
Given the code fragment :
int wd = 0;
String days[] = { "sun", "mon", "wed", "sat" };
for (String s : days) {
switch (s) {
case "sat":
case "sun":
wd -= 1;
break;
case "mon":
wd++;
case "wed":
wd += 2;
}
}
System.out.println(wd);
Q. What is the result? | |
A | -1 |
B | 3 |
C | Compilation fails. |
D | 4 |
정답
B
해설/결과
문제 94
Given :
class C2 {
public void disaplyC2() {
System.out.print("C2");
}
}
interface I {
public void displayI();
}
class C1 extends C2 implements I {
public void displayI() {
System.out.print("C1");
}
}
And given the code fragment:
C2 obj1 = new C1();
I obj2 = new C1();
C2 s = obj2;
I t = obj1;
t.displayI();
s.displayC2();
Q. What is the result? | |
A | C2C2 |
B | C1C2 |
C | Compilation fails |
D | C1C1 |
정답
C
해설/결과
문제 95
Given the code fragment :
LocalDateTime dt = LocalDateTime.of(2014, 7, 31, 1, 1);
dt.plusDays(30);
dt.plusMonths(1);
System.out.print(dt.format(DateTimeFormatter.ISO_DATE));
Q. What is the result? | |
A | 2014-09-30 |
B | 2014-07-31 |
C | An exception is thrown at runtime. |
D | 07-31-2014 |
정답
B
해설/결과
- DateTime 클래스는 변경 불가능(immutable)하다. 따라서 초깃값인 2014-07-31이 정답이다.
문제 96
Given the code fragment :
public static void main(String[] args) {
LocalDate date = LocalDate.of(2012, 01, 32);
date.plusDays(10);
System.out.println(date);
}
Q. What is the result? | |
A | A DateTimeException is thrown at runtime. |
B | 2012-02-11 |
C | 2012-02-10 |
D | Compilation fails |
정답
A
해설/결과
Exception in thread "main" java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1 - 28/31): 32 |
- LocalDate.of(2012, 01, 32); 부분에서 일(Day) 표기가 잘못되어 있어 예외가 발생한다.
- 1~31 사이의 숫자만 입력 가능하다.
문제 97
Given the code fragment :
public static void main(String[] args) {
int array[] = {10, 20, 30, 40, 50};
int x = array.length;
/* line n1 */
}
Q. Which two code fragments can be independently inserted at line n1 to enable the code to print the element of the array in reverse order? (Choose two.) | |
A | while (x > 0) { x--; System.out.print(array[x]); } |
B | do { x--; System.out.print(array[x]); } while (x >= 0); |
C | while (x >= 0) { System.out.print(array[x]); x--; } |
D | do { System.out.print(array[x]); --x; } while (x >= 0); |
E | while (x > 0) { System.out.print(array[--x]); } |
정답
A, E
해설/결과
- A : 5040302010
- E : 5040302010
문제 98
Given :
class Caller {
private void init() {
System.out.println("Initialized");
}
private void start() {
init();
System.out.println("Started");
}
}
public class TestCall {
public static void main(String[] args) {
Caller c = new Caller();
c.start();
c.init();
}
}
Q. What is the result? | |
A | Compilation fails. |
B | Initialized Started Initialized |
C | Initialized Started |
D | An exception is thrown at runtime. |
정답
A
해설/결과
Caller c = new Caller(); // 'Package.Q98.this' cannot be referenced from a static context |
문제 99
The following grid shows the state of a 2D array :
0 | 0 | |
X | 0 | |
0 | X |
This grid is created with the following code :
char[][] grid = new char[3][3];
grid[1][1] = 'X';
grid[0][0] = '0';
grid[2][1] = 'X';
grid[0][1] = '0';
grid[2][2] = 'X';
grid[1][2] = '0';
// line n1
Q. Which line of code, when inserted in place of // line n1, adds an X into the grid so that the grid contains three consecutive X's? |
|
A | grid[1][3] = 'X'; |
B | grid[3][1] = 'X'; |
C | grid[0][2] = 'X'; |
D | grid[2][0] = 'X'; |
E | grid[1][2] = 'X' |
정답
D
해설/결과
- grid[2][0]에 'X'를 넣을 경우 3개의 X가 연속되게 만들 수 있다.
문제 100
Given the code fragments :
Interface Exportable {
void export();
}
class Tool implements Exportable {
protected void export() { // line n1
System.out.println("Tool::export");
}
}
class ReportTool extends Tool implements Exportable {
public void export() { // line n2
System.out.println("RTool::export");
}
public static void main(String[] args) {
Tool aTool = new ReportTool();
Tool bTool = new Tool();
callExport(aTool);
callExport(bTool);
}
public static void callExport(Exportable ex) {
ex.export();
}
}
Q. What is the result? | |
A | RTool::export Tool::export |
B | Tool::export Tool:export |
C | Compilation fails at both line n1 and line n2. |
D | Compilation fails only at line n1. |
E | Compilation fails only at line n2. |
정답
D
해설/결과
- 자식 클래스에서의 상속 메서드의 접근 지정자는 같거나 상위여야 한다.
- 따라서 lline n1에서 접근 지정자는 public이 되어야 한다.
- public void export() {} (O)
- 따라서 lline n1에서 접근 지정자는 public이 되어야 한다.
728x90
'Certificate > OCAJP' 카테고리의 다른 글
[1Z0-808][OCAJP] Dump 문제 81~90 (0) | 2022.01.13 |
---|---|
[1Z0-808][OCAJP] Dump 문제 71~80 (0) | 2022.01.11 |
[1Z0-808][OCAJP] Dump 문제 61~70 (0) | 2022.01.09 |
[1Z0-808][OCAJP] Dump 문제 51~60 (0) | 2022.01.07 |
[1Z0-808][OCAJP] Dump 문제 41~50 (0) | 2022.01.07 |
[1Z0-808][OCAJP] Dump 문제 31~40 (0) | 2022.01.04 |
[1Z0-808][OCAJP] Dump 문제 21~30 (0) | 2022.01.02 |
[1Z0-808][OCAJP] Dump 문제 11~20 (2) | 2022.01.02 |