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

[1Z0-808][OCAJP] Dump 문제 41~50

 

문제 41

Given  : 

class Vehicle {
    int x;
    Vehicle() {
        this(10);   // line n1
    }
    Vehicle(int x) {
        this.x = x;
    }
}

class Car extends Vehicle {
    int y;
    Car() {
        super();
        this(20);   // line n2
    }
    Car(int y) {
        this.y = y;
    }
    public String toString() {
        return super.x + ":" + this.y;
    }
}

 

And given the code fragment : 

Vehicle y = new Car();
System.out.println(y);

 

Q. What is the result?
A 10:20
B 0:20
C Compilation fails at line n2
D Compilation fails at line n1

 

정답

C

 

해설/결과

this(20);     // Call to 'this()' must be first statement in constructor body
  • this()첫 번째 라인이 아닌 아닌 두 번째 라인에서 사용되었기 때문에 발생한 문제.
  • 또한 컴파일러는 super()를 함축적으로(implicitly) 첫 번째 라인에 추가하게 되는데, 사용자가 임의적으로 super()를 첫 번째 라인에 추가할 경우 중복(duplicate)되어 컴파일 오류가 발생하게 된다.

 

 

문제 42

Given the code fragment : 

int a[] = {1, 2, 3, 4, 5};
for (XXX) {
    System.out.print(a[e]);
}

 

Q. Which option can replace XXX to enable the code to print 135?
A int e = 0; e < 5; e += 2
B int e = 1; e <= 5; e += 1
C int e = 0; e <= 4; e++ 
D int e = 1; e < 5; e += 2

 

정답

A

 

해설/결과

  • B : java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
  • C : 12345
  • D : 24

 

 

문제 43

Given the code fragment :

public class Person {
    String name;
    int age = 25;

    public Person(String name) {
        this();		// line n1
        setName(name);
    }

    public Person(String name, int age) {
        Person(name);	// line n2
        setAge(age);
    }

    // setter and getter methods go here

    public String show() {
        return name + " " + age + " " + number;
    }

    public static void main(String[] args) {
        Person p1 = new Person("Jesse");
        Person p2 = new Person("Walter", 52);
        System.out.println(p1.show());
        System.out.println(p2.show());
    }
}

 

Q. Which three lines fail to compile?
A Compilation fails only at line n1
B Compilation fails at both line n1 and line n2
C Compilation fails only at line n2
D Jesse 25
Walter 52

 

정답

B

 

해설/결과

  • Line n1 : 최소 1개의 생성자가 있을 경우, 자바는 기본(Default) 생성자를 추가하지 않기 때문에 컴파일 오류가 발생한다.
  • Line n2 : 지역 클래스에서 생성자를 호출하려면 this를 사용해야 한다. 따라서 컴파일 오류가 발생한다.

 

 

문제 44

Given :

interface Readable {
    public void readBook();
    public void setBookMark();
}

abstract class Book implements Readable {   // line n1
    public void readBook() { }
    // line n2
}

class EBook extends Book {      // line n3
    public void readBook() { }
    // line n4
}

 

Q. Which option enables the code to compile?
A Replace the code fragment at line n1 with :
class Book implements Readable {
B At line n2 insert :
public abstract void setBookMark();
C Replace the code fragment at line n3 with :
abstract class EBook extends Book {
D At line n4 insert :
public void setBookMark() { }

 

정답

D

 

해설/결과

 

 

문제 45

Given the code fragment :

public static void main(String[] args) {
    String[] arr = {"A", "B", "C", "D"};
    for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + " ");
        if (arr[i].equals("C")) {
            continue;
        }
        System.out.println("Work done");
        break;
    }
}

 

Q. What is the result?
A A B C D Work done
B A Work done
C Compilation fails
D A B C Work done

 

정답

B

 

해설/결과

 

 

문제 46

Given the code fragment : 

String[ ] strs = new String[2];
int idx = 0;
for (String s : strs) {
    strs[idx].concat(" element " + idx);
    idx++;
}
for (idx = 0; idx < strs.length; idx++) {
    System.out.println(strs[idx]);
}

 

Q. What is the result?
A Null element 0
Null element 1
B A NullPointerException is thrown at runtime.
C Null
Null
D Element 0
Element 1

 

정답

B

 

해설/결과

  • strs[idx].concat("element" + idx); 부분에서 NullPointerException 예외가 발생한다.
    • strs[0]가 null이기 때문.

 

 

문제 47

Given the code fragment :

if (aVar++ < 10) {
    System.out.println(aVar + " Hello World!");
} else {
    System.out.println(aVar + " Hello Universe!");
}

 

Q. What is the result if the integer aVar is 9?
A Hello Universe!
B Compilation fails.
C 10 Hello World!
D Hello World!

 

정답

C

 

해설/결과

 

 

문제 48

Given : 

public class Test {
    public static void main(String[] args) {
        if (args[0].equals("Hello") ? false : true) {
            System.out.print("Success");
        } else {
            System.out.println("Failure");
        }
    }
}

 

And given the commands :

javac Test.java
java Test Hello

 

 

Q. What is the result?
A Failure
B Success
C An exception is thrown at runtime
D Compilation fails.

 

정답

A

 

해설/결과

  • args[0].equals("Hello") 의 결과는 true이다. 하지만 삼항 연산자의 조건에 의하여 if의 매개변수는 false가 되게 된다. 따라서 "Failure"가 출력되게 된다.

 

 

문제 49

Given :

public class MainTest {
    public static void main(int[] args) {
        System.out.println("int main " + args[0]);
    }
    public static void main(Object[] args) {
        System.out.println("Object main " + args[0]);
    }
    public static void main(String[] args) {
        System.out.println("String main " + args[0]);
    }
}

 

And commands : 

javac MainTest.java
java MainTest 1 2 3

 

Q. What is the result?
A int main 1
B Compilation fails
C Object main 1
D String main 1
E An exception is thrown at runtime

 

정답

D

 

해설/결과

  • 메인 메서드가 과부하된(Overloaded) 상태이다.
    • JVM은 오직 정적(Static)이며 1개의 String 형 배열을 받는 메인 메서드만 찾는다.
    • 따라서 "String main" 이 출력되게 된다.

 

 

문제 50

Given the following class declarations : 

public abstract class Animal {}
public interface Hunter {}
public class Cat extends Animal implements Hunter {}
public class Tiger extends Cat {}

 

Q. Which answer fails to compile?
A ArrayList<Animal> myList = new ArrayList<>();
myList.add(new Tiger());
B ArrayList<Hunter> myList = new ArrayList<>();
myList.add(new Cat());
C ArrayList<Hunter> myList = new ArrayList<>();
myList.add(new Tiger());
D ArrayList<Tiger> myList = new ArrayList<>();
myList.add(new Cat());
E ArrayList<Animal> myList = new ArrayList<>();
myList.add(new Cat());

 

정답

D

 

해설/결과

  • 상속 순서 : Animal -> Hunter -> Cat -> Tiger.
    • Tiger 클래스Cat 클래스서브 클래스이기 때문에 Cat 클래스의 객체를 생성할 수 없다. 
728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖