-->

별의 공부 블로그 🧑🏻‍💻
728x90

[1Z0-808][OCAJP] Dump 문제 61~70

 

문제 61

Given the code fragment  : 

public static void main(String[] args) {
String str = " ";
str.trim();
System.out.println(str.equals("") + " " + str.isEmpty());
}

 

Q. What is the result?
A false true
B true true
C true false
D false false

 

정답

D

 

해설/결과

 

 

문제 62

Given the following array :

int[] intArr = {8, 16, 32, 64, 128};

 

Q. Which two code fragments, indepdently, print each element in this array?
A for (int i : intArr) {
    System.out.print(intArr[i] + " ");
}
B for (int i : intArr) {
    System.out.print(i + " ");
}
C for (int i = 0 : intArr) {
    System.out.print(intArr[i] + " ");
    i++;
}
D for (int i = 0; i < intArr.length; i++) {
    System.out.print(i + " ");
}
E for (int i = 0; i < intArr.length; i++) {
    System.out.print(intArr[i] + " ");
}
F for (int i; i < intArr.length; i++) {
    System.out.print(intArr[i] + " ");
}

 

정답

B, 

 

해설/결과

  • A : 예외 발생 (Index 8 out of bounds for length 5.)
  • B : 8 16 32 64 128 (정답)
  • C :  컴파일 오류 (잘못 사용 : int i = 0 : intArr
  • D : 0 1 2 3 4 (인덱스 번호 출력)
  • E : 8 16 32 64 128 (정답)
  • F : 컴파일 오류 (잘못 사용 : for (int i; i < intArr.length; i++))

 

 

문제 63

Given : 

public class SumTest {
public static void doSum(Integer x, Integer y) {
System.out.println("Integer sum is " + (x + y));
}
public static void doSum(double x, double y) {
System.out.println("double sum is " + (x + y));
}
public static void doSum(float x, float y) {
System.out.println("float sum is " + (x + y));
}
public static void doSum(int x, int y) {
System.out.println("int sum is " + (x + y));
}
public static void main(String[] args) {
doSum(10, 20);
doSum(10.0, 20.0);
}
}

 

Q. What is the result?
A int sum is 30
float sum is 30.0
B int sum is 30
double sum is 30.0
C Integer sum is 30
double sum is 30.0
D Integer sum is 30
float sum is 30.0

 

정답

B

 

해설/결과

  • 첫 번째 doSum() 함수
    • 매개변수 10과 20은 Integer 자료형 중의 int 자료형에 더 가깝기 때문에 int 형식으로 출력된다.
  • 두 번째 doSum() 함수
    • 어미에 "f" 리터럴을 붙이지 않으면 double 형식으로 출력된다.
    • float 형식으로 출력하게 만들고 싶다면 doSum(10.0f, 20.0f); 와 같이 어미(post-fix)에 "f" 리터럴을 붙여주면 된다.

 

문제 64

Given :

public class MarkList {
int num;
public static void graceMarks(MarkList obj4) {
obj4.num += 10;
}
public static void main(String[] args) {
MarkList obj1 = new MarkList();
MarkList obj2 = obj1;
MarkList obj3 = null;
obj2.num = 60;
graceMarks(obj2);
}
}

 

Q. How many MarkList instances are created in memory at runtime?
A 1
B 3
C 2
D 4

 

정답

A

 

해설/결과

  • obj1은 새로운 MarkList 인스턴스를 가리키게 되고, obj2는 obj1, obj3는 null을 가리키게 된다.
    • 따라서 총 1개의 인스턴스가 실행 중에 메모리에 생성된다.

 

 

문제 65

Given :

public static void main(String[] args) {
int num = 5;
do {
System.out.print(num-- + " ");
} while (num == 0);
}

 

Q. What is the result?
A 5 4 3 2 1 0
B 5 4 3 2 1
C 4 2 1
D Nothing is printed
E 5

 

정답

E

 

해설/결과

  • do while문의 조건이 잘못되었다.
    • "5 4 3 2 1" 을 출력시키고 싶을 경우, while의 조건을 (num != 0) 으로 해준다.

 

 

문제 66

Given : 

public class Test {
static int count = 0;
int i = 0;
public void changeCount() {
while (i < 5) {
i++;
count++;
}
}
public static void main(String[] args) {
Test check1 = new Test();
Test check2 = new Test();
check1.changeCount();
check2.changeCount();
System.out.print(check1.count + " : " + check2.count);
}
}

 

Q. What is the result?
A 5 : 5
B 5 : 10
C Compilation fails
D 10 : 10

 

정답

D

 

해설/결과

 

 

문제 67

Given :

public class TestSCope {
public static void main(String[] args) {
int var1 = 200;
System.out.print(doCalc(var1));
System.out.print(" " + var1);
}
static int doCalc(int var1) {
var1 = var1 * 2;
return var1;
}
}

 

Q. What is the result?
A Compilation fails.
B 200 200
C 400 200
D 400 400

 

정답

C

 

해설/결과

 

 

문제 68

Given the code from the Greeing.java file : 

public class Greeting {
public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}

 

Q. Which set of commands prints Hello Duke in the console?
A javac Greeting
java Greeting Duke
B javac Greeting.java Duke
java Greeting
C javac Greeting.java
java Greeting Duke
D javac Greeting.java
java Greeting.class Duke

 

정답

C

 

해설/결과

 

 

문제 69

Given the code fragment :

public static void main(String[] args) {
String date = LocalDate
.parse("2014-05-04")
.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(date);
}

 

Q. What is the result?
A 2014-05-04T00:00: 00. 000
B An exception is thrown at runtime.
C May 04, 2015T00:00:00.000
D 5/4/14T00:00:00.000

 

정답

B

 

해설/결과

.format(DateTimeFormatter.ISO_DATE_TIME);     // Cannot resolve symbol 'DateTimeFormatter'
  • UnsupportedTemporalTypeException 예외가 실행 중에 발생하게 된다.
    • DateTimeFormatter인 ISO_DATE_TIME이 LocalDate 객체에 사용될 수 없기 때문
      • LocalDate 객체는 시간(Time) 정보를 갖고 있지 않다.
  • 만약 DateTimeFormatter로 ISO_DATE 를 사용할 경우, 다음의 결과를 얻을 수 있다.
    • 2014-05-04

 

 

문제 70

Given :

class CD {
int r;
CD(int r) {
this.r = r;
}
}
class DVD extends CD {
int c;
DVD(int r, int c) {
// line n1
}
}

 

And given the code fragment :

DVD dvd = new DVD(10, 20);

 

Q. Which code fragment should you use at line n1 to instantiate the dvd object successfully?
A super.r = r;
this.c = c;
B super(r);
this(c);
C super(c);
this.c = c;
D this.c = r;
super(c);

 

정답

C

 

해설/결과

  • A : CD 클래스에서 사용 가능한 기본 생성자가 없기 때문에 오답.
  • B : 생성자 호출을 2번 할 수 없기 때문에 오답.
  • D : super() 는 반드시 생성자의 첫 번째 라인에서 호출되어야 하므로 오답.
728x90


📖 Contents 📖
[1Z0-808][OCAJP] Dump 문제 61~70문제 61정답해설/결과문제 62정답해설/결과문제 63정답해설/결과문제 64정답해설/결과문제 65정답해설/결과문제 66정답해설/결과문제 67정답해설/결과문제 68정답해설/결과문제 69정답해설/결과문제 70정답해설/결과