본문 바로가기

Certification/OCAJP

OCAJP 준비 (Q01 - Q25)

순서대로 정리아님 - 기억하고 싶은 내용만 정리중 

abstract class Parent {
protected void revolve() {} // n1
abstract void rotate(); // n2
}
class Earth extends Parent {
void revolve() {} // n3
protected void rotate() {} // n4
}

 

1.

Java의 캡슐화와 상속 규칙

위에서 보면 n3은 default로 되어있는데 추상화의 경우

부모클래스보다 자식클래스의 제한자의 범위는 무조건 같거나 더 넓어야 한다.
따라서 n3를 public인 protected로 수정해야

 

그리고 n4를 public으로 더 넓게 수정할 수 있음

 

2.

long타입은 String으로 형변환 불가 

 

3.

class Vehicle {
String type = "4w";
int maxSpeed = 100;

Vehicle(String type, int maxSpeed) {
this.type = type;
this.maxSpeed = maxSpeed;
}

Vehicle() {}
}

class Car extends Vehicle {
String trans;

Car (String trans) {
this.trans = trans;
}

Car(String type, int maxSpeed, String trans) {
super(type, maxSpeed); 
this.trans = trans;
}
}

public class q7 {
public static void main(String[] args) {
Car c1 = new Car ("Auto");
Car c2 = new Car ("4W", 150, "Manual");

System.out.println(c1.type + " "+ c1.maxSpeed + " " + c1.trans);
System.out.println(c2.type +" " +c2.maxSpeed + " "+c2.trans);
}
}

 

1) 묵시적 호출 

자식 클래스 생성자에서 super()를 명시하지 않으면 자동으로 부모의 기본 생성자 super()가 호출됨

항상 생성자의 첫 줄에 자동 삽입

 

2) 명시적 호출 

프로그래머가 직접 suepr() 작성

 

4W 100 Auto 4W 150 Manual

 

4.

String ta = "A ";             // ta = "A "
ta = ta.concat("B ");         // ta = "A B "
String tb = "C ";            // tb = "C "
ta = ta.concat(tb);          // ta = "A B C "
ta.replace('C', 'D');        // replace는 새 String을 반환하고 원본은 변경하지 X
                           // ta는 여전히 "A B C "
ta = ta.concat(tb);          // ta = "A B C C "
System.out.println(ta);      // "A B C C " 출력

 

ta가 replace하면서 자연스럽게 새롭게 생성되는건데, 그 점을 간과

 

5.

 

  • this()와 super()는 동시에 사용 불가
  • this()도 생성자의 첫 줄에만 올 수 있음

 

6.

Encapsulation ensures that classes can be designed so that only certain fields and methods of an object are accessible from other objects.  > DATA HIDING

 

Inheritance  ensures that classes can be designed so that their methods are inheritable.

 

Abstraction ensures that classes can be designed with some fields and methods declared as abstract.

 

Polymorphism ensures that classes can be designed so that if a method has an argument MyType x, any subclass of MyType can be passed to that method.

 

7.

// SalesMan.java
package sales;
public class SalesMan { }

// Product.java
package sales.products;
public class Product { }

// Market.java
package market;
// line 2: import 필요
public class USMarket {
    SalesMan sm;    // sales 패키지의 SalesMan
    Product p;      // sales.products 패키지의 Product
}

- `import sales.*;`로 SalesMan 클래스 import

- `import sales.products.*;`로 Product 클래스 import

 

8.

void readCard(int cardNo) throws Exception {
    System.out.println("Reading Card");
}

void checkCard(int cardNo) throws RuntimeException {
    System.out.println("Checking Card");
}

public static void main(String[] args) {
    Test ex = new Test();
    int cardNo = 12344;
    ex.readCard(cardNo);      //line n2 - Exception을 처리해야 함
    ex.checkCard(cardNo);     //line n3 - RuntimeException은 처리 안해도 됨
}

>>>>>

// 방법 1: try-catch 사용
public static void main(String[] args) {
    Test ex = new Test();
    int cardNo = 12344;
    try {
        ex.readCard(cardNo);      // OK
    } catch (Exception e) {
        e.printStackTrace();
    }
    ex.checkCard(cardNo);
}

// 방법 2: throws 사용
public static void main(String[] args) throws Exception {
    Test ex = new Test();
    int cardNo = 12344;
    ex.readCard(cardNo);      // OK
    ex.checkCard(cardNo);
}

1) throw Exception > checked exception

반드시 try-catch로 처리하거나 다시 throws 해야 함

Exception, IOException 등이 여기 해당

컴파일 시점에 체크

주로 외부 자원을 사용할 때 발생

 

2) throws RuntimeException > unchecked exception

try-catch 선택사항

RuntimeException, NullPointerException

컴파일 시점에 체크되지 않음 

프로그래머가 코드로 방지할 수 있음

 

> 모든 곳에서 프로그래머가 예외처리를 할 수 없으니 unchecked로 분류

 

🍄9.

int x = 100;
int a = x++;  // a = 100, x = 101 (후위 증가)
int b = ++x;  // b = 102, x = 102 (전위 증가)  
int c = x++;  // c = 102, x = 103 (후위 증가)
int d = (a < b) ? (a < c) ? a : (b < c) ? b : c : x;

1) (100 < 102) ? [(a < c) ? a : (b < c) ? b : c] : x

참 >  [(a < c) ? a : (b < c) ? b : c]  수행

 

2)  (100 < 102) ? a : (b < c) ? b : c

참> a

 

따라서 답은 100

 

10.

public class Employee {
   String name;
   boolean contract;
   double salary;
   
   Employee() {
       // line n1 >>>>>>>>>>>>>>
       this.name = new String("Joe");
       this.contract = new Boolean(true);
       this.salary = new Double(100);
   }
   
   public String toString() {
       return name + ":" + contract + ":" + salary;
   }
   
   public static void main(String[] args) {
       Employee e = new Employee();
       // line n2  >>>>>>>>>>>       
      e.name = "Joe";
      e.contract = true;
      e.salary = 100;
       System.out.print(e);
   }
}


this.name = new String("Joe");
this.contract = new Boolean(true);
this.salary = new Double(100);

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

OCAJP 준비 (Q106 - Q125)  (8) 2024.11.13
OCAJP 준비 (Q91 - Q105)  (1) 2024.11.07
OCAJP 준비 (Q66 - Q90)  (2) 2024.11.07
OCAJP 준비 (Q45 - Q65)  (0) 2024.11.06
OCAJP 준비 (Q26 - Q44)  (0) 2024.11.05