본문 바로가기

Certification/OCAJP

(20)
OCAJP 준비 (Q219 - Q236) 1.  인터페이스는 기본적으로 모든 메서드가 추상 메서드로 선언>> public abstract 인터페이스의 메서드는 기본적으로 모두 추상 메서드이지만, default 메서드나 static 메서드는 구현이 가능interface Speakable { void speak(String s); // 추상 메서드 default void greet() { // 기본 구현 제공 System.out.println("Hello!"); }} 2.public class App { // 클래스 멤버 변수 String myStr = "9009"; public void doStuff(String str) { int myNum = 0; // 로컬 변수 try {..
OCAJP 준비 (Q201 - Q218) 1.class Vehicle { Vehicle() { System.out.println("Vehicle"); }}class Bus extends Vehicle { Bus() { System.out.println("Bus"); }}public class Transport { public static void main(String[] args) { Vehicle v = new Bus(); }}생성자 호출객체를 생성할 때 자동으로 호출됨생성자 호출을 막으려면 객체를 생성하지 않아야 함.생성자는 호출을 안 하고 객체를 생성하는 방법은 XX (Java는 기본적으로 생성자를 반드시 호출해야만 객체를 생성할 수 있게 설계되어 있음.)일반 메서드 호..
OCAJP 준비 (Q177 - Q200) 1.System.out.println("Hello " + new StringBuilder("Java SE 8")); Java에서 문자열과 객체를 + 연산으로 연결할 경우, 객체의 toString() 메서드가 호출따라서 출력은 **Hello Java SE 8** System.out.println("Hello " + new MyString("Java SE 8").msg); MyString 객체에서 .msg 필드를 명시적으로 접근했기 때문에 toString() 호출 없이 필드 값 자체가 출력 2.class Bird { public void fly() { System.out.print("Fly."); }}class Peacock extends Bird { public void dance()..
OCAJP 준비 (Q166 - Q176) 1. 각 행의 배열은 독립적으로 초기화 가능chs[0] = new String[3]; // 첫 번째 행의 크기를 3으로 변경chs[1] = new String[5]; // 두 번째 행의 크기를 5로 변경chs[2] = new String[1]; // 세 번째 행의 크기를 1로 변경chs[3] = null; // 네 번째 행은 null 상태 유지chs[4] = new String[10]; // 다섯 번째 행의 크기를 10으로 변경각 행의 배열은 초기 선언된 크기와 달리 재정의할 수 있음이는 Java에서 2차원 배열이 고정된 "행렬"이 아니라, 배열의 배열로 구현 2.public class Test { public static void main(String[] args) { ..
OCAJP 준비 (Q146 - Q165) 1.interface Exportable { void export(); // 인터페이스 메소드는 기본적으로 public}class Tool implements Exportable { public void export() { // line n1 - OK (public) System.out.println("Tool::export"); }}class ReportTool extends Tool { void export() { // line n2 - 컴파일 에러! // 접근 제어자를 default로 변경하여 더 제한적으로 만듦 System.out.println("RTool::export"); }} public > protected > de..
OCAJP 준비 (Q126 - Q145) 1.abstract 클래스의 특징:1. 일반 메소드와 추상 메소드 모두 가질 수 있음2. 인스턴스화(new) 불가능3. public, protected, private 접근 제어자 모두 사용 가능 2.// 1. Test 생성자 호출public Test(int x, int y) { initialize(x, y);}// 2. initialize 메소드 실행public void initialize(int x, int y) { this.x = x * x; // 클래스 변수 x = 3 * 3 = 9 this.y = y * y; // 클래스 변수 y = 5 * 5 = 25}// 3. main의 출력문// 여기서는 main의 지역변수 x, y를 출력System.out.println(x +..
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.>> Th..
OCAJP 준비 (Q91 - Q105) 1.class Animal { String type = "Canine"; int maxSpeed = 60; Animal() { } Animal(String type, int maxSpeed) { this.type = type; this.maxSpeed = maxSpeed; }}class WildAnimal extends Animal { String bounds; WildAnimal(String bounds) { //line n1 } WildAnimal(String type, int maxSpeed, String bounds) { //line n2 }}public class Test { public static void ma..