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

[1Z0-808][OCAJP] Dump 문제 71~80

 

문제 71

Given the code fragment  : 

public static void main(String[] args) {
    ArrayList myList = new ArrayList();
    String[] myArray;

    try {
        while (true) {
            myList.add("My String");
        }
    } catch (RuntimeException re) {
        System.out.println("Caught a RuntimeException");
    } catch (Exception e) {
        System.out.println("Caught an Exception");
    }

    System.out.println("Ready to use");
}

 

Q. What is the result?
A Exception terminates in the second catch statement, and caught an Exception is printed to the console.
B The code fails to compile because a trhows keyword is required.
C Execution completes normally, and Ready to use is printed to the console.
D Execution terminates in the first catch statement, and caught a RuntimeException is printed to the console.
E A runtime error is thrown in the thread "main".

 

정답

E

 

해설/결과

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

 

 

문제 72

Given the following code :

int[] intArr = {15, 30, 45, 60, 75};
intArr[2] = intArr[4];
intArr[4] = 90;

 

Q. What are the values of each element in intArr after this code has executed?
A 15, 4, 45, 60. 90
B 15, 60, 45, 90, 75
C 15, 30, 90, 60, 90
D 15, 30, 75, 60, 90
E 15, 90, 45, 90, 75

 

정답

D

 

해설/결과

 

 

문제 73

Q. Which statement is true about Java byte code?
A It can run on any playform.
B It can run on any platform only if it was compiled for that platform.
C It can run on any platform that has the Java Runtime Environment.
D It can run on any platform that has a Java compiler.
E It can run on any platform only if that platform has both the Java Runtime Environment and a Java compiler.

 

정답

C

 

해설/결과

  • 바이트 코드는 이미 컴파일 되어 만들어진 코드이기 때문에 실행 되기 위해서는 JRE(Java Runtime Environment)가 필요하다.
  • 따라서 어떤 플랫폼에서든지 JRE가 있다면, 자바의 바이트 코드를 실행할 수 있다.

 

 

문제 74

Given the code fragment :

public static void main(String[] args) {
    short s1 = 200;
    Integer s2 = 400;
    Long s3 = (long) s1 + s2;           // line n1
    String s4 = (String) (s3 * s2);     // line n2
    System.out.println("Sum is " + s4);
}

 

Q. What is the result?
A A ClassCastException is thrown at line n1.
B Compilation fails at line n1.
C Sum is 600
D Compilation fails at line n2.
E A ClassCastException is thrown at line n2.

 

정답

D

 

해설/결과

(String) (s3 * s2);      // Inconvertible types; cannot cast 'long' to 'java.lang.String'
  • 자바에서 Long 형을 String 형으로 캐스팅(castig) 하는 것은 불가능하다.

 

 

문제 75

Given :

public static void main(String[] args) {
    List<String> names = new ArrayList<>();
    names.add("Robb");
    names.add("Bran");
    names.add("Rick");
    names.add("Bran");
    
    if (names.remove("Bran")) {
        names.remove("Jon");
    }
    
    System.out.println(names);
}

 

Q. What is the result?
A [Robb, Rick]
B [Robb, Rick, Bran]
C [Robb, Bran, Rick, Bran]
D An exception is thrown at runtime.

 

정답

B

 

해설/결과

  • If문에서 names.remove("Bran")명령이 실행된 후 If문을 빠져나오게 된다.
    • 리스트에서 처음으로 등장하는 "Bran"이 삭제된다.

 

 

문제 76

Given the code fragment : 

public static void main(String[] args) {
    boolean opt = true;		// line 5
    switch (opt) {
        case true:		// line 7
            System.out.print("True");
            break;		// line 9
        default:
            System.out.print("***");
    }
    System.out.println("Done");
}

 

Q. Which modification enables the code fragment to print TrueDone? 
A At line 9, remove the break statement.
B Remove the default section.
C Replace line 5 with String opt = "true";
Replace line 7 with case 1:
D Replace line 5 with String opt = "true";
Replace line 7 with case "true":

 

정답

D

 

해설/결과

  • switch 문은 boolean형 변수를 매개변수로 받을 수 없다.

 

 

문제 77

Given the code fragment :

StringBuilder sb1 = new StringBuilder("Duke");
String str1 = sb1.toString();
// insert code here		// line 9
System.out.print(str1 == str2);

 

Q. Which code fragment, when inserted at line 9, enables the code to print true?
A String str2 = new String(str1);
B String str2 = sb1.toString();
C String str2 = str1;
D String str2 = "Duke";

 

정답

C

 

해설/결과

  • A : false
  • B : false
  • C : true (정답)
  • D : false

 

 

문제 78

Given the code fragment : 

LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.of(2014, 6, 20);
LocalDate date3 = LocalDate.parse("2014-06-20", DateTimeFormatter.ISO_DATE);

System.out.println("date1 = " + date1);
System.out.println("date2 = " + date2);
System.out.println("date3 = " + date3);

 

Q. Assume that the system date is June 20, 2014. What is the result?
A date1 = 2014-06-20
date2 = 2014-06-20
date3 = 2014-06-20
B date1 = 06/20/2014
date2 = 2014-06-20
date3 = June 20, 2014
C Compilation fails.
D A DateParseException is thrown at runtime.

 

정답

A

 

해설/결과

 

 

문제 79

Given : 

package clothing;
public class Shirt {
    public static String getColor() {
        return "Green";
    }
}

 

Given the code fragment : 

package clothing.pants;
// line n1
public class Jeans {
    public void matchShirt() {
        // line n2
        if (color.equals("Green")) {
            System.out.println("Fit");
        }
    }

    public static void main(String[] args) {
        Jeans trouser = new Jeans();
        trouser.matchShirt();
    }
}

 

Q. Which two sets of actions, independently, enable the code fragment to print Fit?
A At line n1 insert : impot clothing.*;
At line n2 insert : String color = Shirt.getColor();
B At line n1 insert : import static clothing.Shirt.getcolor;
At line n2 insert : String color = getColor();
C At line n1 insert : import clothing;
At line n2 insert : String color = shirt.getColor();
D At line n1 no changes required.
At line n2 insert : String color = Shirt.getColor();
E At line n1 insert : import clothing.Shirt;
At line n2 insert : String color = getColor();

 

정답

A, B

 

해설/결과

 

 

문제 80

Q. Which two statements are true? (Choose two.)
A Error is a RuntimeException.
B Error class is extendable.
C Error is a Throwable.
D Error is an Exception.
E Error class is unextendable.

 

정답

B, C

 

해설/결과

  • A : 에러는 RuntimeException 이 아니라 Throwable 의 서브 클래스이며 확장 가능하다. (오답)
  • B : 에러 클래스는 확장 가능하다. (정답)
  • C : 에러는 Throwable의 서브 클래스이기에 Throwable이라고 할 수 있다. (정답)
  • D : 에러는 예외이다. (오답)
  • E : 에러 클래스는 확장이 불가능하다. (오답)
728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖