본문 바로가기

Certification/OCAJP

OCAJP 준비 (Q106 - Q125)

1.

A. Only unchecked exceptions can be rethrown.

>> Both checked and unchecked exceptions can be rethrown in Java.

 

B. All subclasses of the RuntimeException class are not recoverable.

>> RuntimeException and its subclasses are unchecked exceptions, but their recoverability depends on the specific situation and how the program is designed.

 

C. The parameter in a catch block is of Throwable type.

>> Throwable 클래스는 모든 예외와 오류의 최상위 클래스이므로 catch 블록에서 Throwable 타입을 사용하여 예외를 받을 수 있

 

D. All subclasses of the RuntimeException class must be caught or declared to be thrown.

>> Subclasses of the RuntimeException class are unchecked exceptions. It is not mandatory to handle or declare RuntimeException and its subclasses.

 

E. All subclasses of the RuntimeException class are unchecked exceptions.

>> RuntimeException의 모든 하위 클래스는 검사되지 않은 예외(unchecked exception)

n

F. All subclasses of the Error class are not recoverable.

>> Error 클래스의 모든 하위 클래스는 복구 불가능하다고 간주

 

2.

 

  • A. Error class is unextendable.
    • ❌ : Error 클래스는 확장이 불가능하지 않음
  • B. Error class is extendable.
    • ✅ : Error 클래스는 확장이 가능. Error를 상속하여 사용자 정의 에러 클래스를 만들 수도 있음.
              다만, Error와 그 하위 클래스는 일반적으로 JVM의 심각한 오류 상황을 나타내므로 확장을 자주 사용하지 X
  • C. Error is a RuntimeException.
    • ❌ : Error는 RuntimeException X. Error와 RuntimeException은 둘 다 Throwable의 하위 클래스이지만, 서로 별도의 계층. Error는 주로 JVM 오류를 나타내며, RuntimeException은 일반적인 프로그램 실행 중 발생할 수 있는 예외를 나타냄
  • D. Error is an Exception.
    • ❌ : Error와 Exception은 Throwable의 직접적인 하위 클래스이지만 서로 다른 계층을 형성
  • E. Error is a Throwable.
    • ✅ : Error는 Throwable의 하위 클래스입. Throwable은 Java에서 모든 에러와 예외의 최상위 클래스이며, Error와 Exception은 Throwable을 상속하는 두 주요 클래스

3.

Java에서는 continue 이후에 실행되지 않는 코드가 있을 경우 컴파일 오류가 발생

 

4.

public CheckingAccount() {
    amount = 100; // this.amount = 100;과 동일하게 처리됨
}

public CheckingAccount(int amount) {
    this.amount = amount; // 필드 amount와 매개변수 amount를 구분하기 위해 this 사용
}

 

this를 생략해도 컴파일러는 amount가 CheckingAccount 클래스의 필드임을 인식하고 this.amount로 처리

this 키워드는 클래스 내부에서 필드를 명확히 지정할 때 필요하지만, 필드와 동일한 이름의 매개변수가 없을 때는 생략해도 컴파일러가 현재 인스턴스의 필드로 간주

 

5.

interface Exportable {
    void export();  // 인터페이스의 메소드는 기본적으로 public abstract
}

class Tool implements Exportable {
    protected void export() {  // line n1 - 컴파일 에러!
        // protected는 public보다 더 제한적
        System.out.println("Tool::export");
    }
}

class ReportTool extends Tool implements Exportable {
    public void export() {     // line n2 - OK
        System.out.println("RTool::export");
    }
}

 

 

  • 인터페이스의 메소드는 기본적으로 public
  • 구현 클래스에서는:
    • 반드시 public으로 구현해야 함
    • 더 제한적인 접근 제어자(protected, default, private) 사용 불가

6.

// Shirt.java
package clothing;
public class Shirt {
   public static String getColor() {
       return "Green";
       >>>>>>>>>>>>>>>>
       getColor() 메서드는 "Green"을 반환하는데, 
       이 메서드는 정적(static) 메서드라서 클래스 이름으로 접근할 수 있음
       정적 메서드는 객체를 생성하지 않고도 바로 호출 가능
   }
}

// Jeans.java
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();
   }
}


답 : At line n1 insert: import static clothing.Shirt.getColor; 
     At line n2 insert: String color = getColor();
     
     At line n1 insert: import clothing.Shirt; 
     At line n2 insert: String color = Shirt.getColor();

                                        

7.

// 1. 처음 객체 생성
Student s1 = new Student();  // Object 1
Student s2 = new Student();  // Object 2
Student s3 = new Student();  // Object 3

// 2. 참조 변경
s1 = s3;  // s1이 Object 3을 가리킴 (Object 1은 참조 없음)
s3 = s2;  // s3이 Object 2를 가리킴 (Object 3은 s1이 가리킴)
s2 = null;  // s2의 참조 제거 (Object 2는 s3이 가리킴)

 

 

  • Object 1: 아무도 참조하지 않음 (가비지 대상 ✓)
  • Object 2: s3이 참조 중
  • Object 3: s1이 참조 중

8.

interface Downloadable {
    public void download();
}

interface Readable extends Downloadable {
    public void readBook();
}

abstract class Book implements Readable { // line n2
    public void readBook() {
        System.out.println("Read Book");
    }
}

class EBook extends Book { // line n3
    public void readBook() {
        System.out.println("Read E-Book");
    }
}

public class Test {
    public static void main(String[] args) {
        Book book1 = new EBook();
        book1.readBook();
    }
}

 

  1. Downloadable 인터페이스에는 download() 메소드
  2. Readable 인터페이스는 Downloadable을 상속하며, 추가로 readBook() 메소드를 선언
  3. Book 추상 클래스는 Readable 인터페이스를 구현하지만, download() 메소드를 구현하지 X
    대신 readBook() 메소드만 구현했습니다.
  4. EBook 클래스는 Book을 상속하고 readBook() 메소드를 오버라이드하여 "Read E-Book"을 출력하도록 구현
  5. EBook 클래스는 추상 클래스가 아닌 구체 클래스인데, download() 메소드를 구현하지 않아,  컴파일 오류가 발

 

'Certification > OCAJP' 카테고리의 다른 글

OCAJP 준비 (Q146 - Q165)  (0) 2024.11.21
OCAJP 준비 (Q126 - Q145)  (1) 2024.11.18
OCAJP 준비 (Q91 - Q105)  (1) 2024.11.07
OCAJP 준비 (Q66 - Q90)  (2) 2024.11.07
OCAJP 준비 (Q45 - Q65)  (0) 2024.11.06