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

[1Z0-808][OCAJP] Dump 문제 31~40

 

문제 31

Given the definitions of the MyString class and the Test class : 

// (1) MySting.java :
package p1;
public class MyString {
    String msg;
    MyString(String msg) {
        this.msg = msg;
    }
}

// (2) Test.java
package p1;
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello " + new StringBuilder("Java SE 8"));
        System.out.println("Hello " + new MyString("Java SE 8"));
    }
}

 

Q. What is the result?
A Hello Java SE 8
Hello Java SE 8 
B Hello java.lang.StringBuilder@<<hashcode1>>
Hello p1.MyString@<<hashcode2>>
C Hello Java SE 8
Hello Package.MyString@<<hashcode>>
D Compilation fails at the Test class.

 

정답

C

 

해설/결과

Hello Java SE 8
Hello Package.MyString@682a0b20

 

 

문제 32

Given the code fragment : 

int[] array = {1, 2, 3, 4, 5};

 

And given the requirements :

1. Process all the elements of the array in the order of entry.

2. Process all the elements of the array in the reverse order of entry.

3. Process alternating elements of the array in the order of entry.

 

Q. Which two statements are true? 
A Requirements 1, 2, and 3 can be implemented by using the enhaned for loop.
B Requirements 2 and 3 CANNOT be implemented by using the standard for loop.
C Requirements 1, 2, and 3 can be implemented by using the standard for loop.
D Requirement 3 CANNOT be implemented by using either the enhanced for loop or the standard for loop.
E Requirement 1 can be implemented by using the enhanced for loop.

 

정답

C, E

 

해설/결과

  • C
    • 예)
      • Requirement 1. for (int i = 0; i < array.length; i++)
      • Requirement 2. for (int i = 4; i >= 0; i--)
      • Requirement 3. for (int i = 0; i < array.length; i+=2)

 

 

문제 33

Given the code fragment :

int num[][] = new int[1][3];
for (int i = 0; i < num.length; i++) {
    for (int j = 0; j < num[i].length; j++) {
        num[i][j] = 10;
    }
}

 

Q. Which three lines fail to compile?
A num[0][0]=10
num[0][1]=10
num[0][2]=10
B num[0][0]=10
num[1][0]=10
num[2][0]=10
C num[0][0]=10
num[0][1]=0
num[0][2]=0
D num[0][0]=10
num[0][1]=10
num[0][2]=10
num[0][3]=10
num[1][0]=0
num[1][1]=0
num[1][2]=0
num[1][3]=0

 

정답

A

 

해설/결과

  • num.length : 1
  • num[i].length : 3

 

 

문제 34

Q. Which three statements describe the object-oriented features of the Java language?
A Objects cannot be reused.
B A subclass can inherit from a superclass.
C Objects can share behaviors with other objects.
D A pacakage must contain more than one class.
E Object is the root class of all other objects.
F A main method must be declared in every class.

 

정답

B, C, E

 

해설/결과

  • A : 객체는 다시 사용될 수 없다. (오답)
  • B : 하위 클래스는 상위 클래스로부터 상속될 수 있다.
  • C : 객체는 다른 객체들과 동작(Behaviors)를 공유할 수 있다.
  • D : 패키지는 반드시 2개 이상의 클래스를 포함해야 한다. (오답)
  • E : 객체는 다른 모든 객체들의 루트 클래스이다. 
  • F : 모든 클래스에는 반드시 메인 메서드가 선언되어야 한다. (오답)

 

문제 35

Given the code fragment :

int ii = 0;
int jj = 7;
for (ii = 0; ii < jj - 1; ii = ii + 2) {
    System.out.print(ii + " ");
}

 

Q. What is the result?
A 2 4
B 0 2 4 6
C 0 2 4 
D Compilation fails

 

정답

C

 

해설/결과

 

 

문제 36

Given : 

public class X {
    static int i;
    int j;
    public static void main(String[] args) {
        X x1 = new X();
        X x2 = new X();
        x1.i = 3;
        x1.j = 4;
        x2.i = 5;
        x2.j = 6;
        System.out.println(
                x1.i + " " +
                x1.j + " " +
                x2.i + " " +
                x2.j);
    }
}

 

Q. What is the result?
A 3 4 3 6
B 3 4 5 6
C 3 6 4 6
D 5 4 5 6

 

정답

D

 

해설/결과

 

 

문제 37

Given the code fragment :

abstract class Planet {
    protected void revolve() {      // line n1
    }

    abstract void rotate();         // line n2
}

class Earth extends Planet {
    void revolve() {                // line n3
    }

    protected void rotate() {        // line n4
    }
}

 

Q. Which two modifications, made independently, enable the code to compile?
A Make the method at line n2 public.
B Make the method at line n4 public.
C Make the method at line n1 public.
D Make the method at line n3 public.
E Make the method at line n3 protected.

 

정답

D, E

 

해설/결과

 

 

문제 38

Q. Which three are advantages of the Java exception mechanism?
A Improves the program structure because the error handling code is separated from the normal program function.
B Provides a set of standard exceptions that overs all the possible errors.
C Improves the program structure because the programmer can choose where to handle exceptions.
D Improves the program structure because exceptions must be handled in the method in which they occured.
E Allows the creation of new exceptions that are tailored to the particular program being created.

 

정답

A, C, E

 

해설/결과

  • A : 오류 처리 코드가 일반 프로그램의 함수와 분리되어 있어 프로그램 구조를 개선한다.
  • B : 가능한 모든 오류에 대한 일련의 표준 예외를 제공한다. (오답)
  • C : 프로그래머가 예외를 처리할 위치를 선택할 수 있기 때문에 프로그램 구조를 개선한다. 
  • D : 예외가 발생한 메서드에서 반드시 예외가 처리되어야 하기 때문에 프로그램 구조를 개선한다. (오답)
  • E : 생성 중인 특정 프로그램에 맞게 조정된 새 예외를 생성하는 것을 허용한다. 

 

 

문제 39

Given the following code :

public static void main(String[] args) {
    String[] planets = { "Mercury", "Venus", "Earth", "Mars" };

    System.out.println(planets.length);
    System.out.println(planets[1].length());
}

 

Q. What is the output?
A 4
5
B 3
5
C 4
7
D 4
4
E 4
21
F 5
4

 

정답

A

 

해설/결과

 

 

문제 40

Given the code fragment :

public class App {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = new String("java");
        // line n1
        {
            System.out.println("Equal");
        } else {
            System.out.println("Not Equal");
        }

    }
}

 

Q. What code fragment, when inserted at line n1, enables the App class to print Equal?
A String str3 = str2;
if (str1 == str3)
B if (str1.equalsIgnoreCase(str2))
C String str3 = str2;
if (str1.equals(str3))
D if (str1.toLowerCase() == str2.toLowerCase())

 

정답

B

 

해설/결과

  • B : equalsIgnoreCase() 메서드를 사용하여  대소문자 구별 없이 문자열을 비교할 수 있다.
  • D : "Not Equal"이 출력된다.
    • toLowerCase() 또는 toUpperCase()메서드로 변환한 값들이 서로 같은 값인지 비교할 수 없다.
728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖