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

[1Z0-808][OCAJP] Dump 문제 1~10

 

문제 1

Q. Which statement best describes encapsulation?
A Encapsulation ensures that classes can be designed so that only certain fields and methods of an object are accessible from other objects.
B Encapsulation ensures that classes can be designed so that their methods are inheritable.
C Encapsulation ensures that classes can be designed with some fields and methods declared as abstract.
D Encapsulation 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.

 

정답

A

 

해설/결과

A. 캡슐화는 클래스의 특정 필드와 메서드가 다른 개체에서 접근될 수 있도록 하는 것을 보장한다.

B. 캡슐화는 클래스의 메서드가 상속될 수 있음을 보장한다.

C. 캡슐화는 클래스의 특정 필드와 메서드가 추상적으로 선언될 수 있음을 보장한다.

D. 캡슐화는 메서드가 MyType X 인자를 갖고 있을 경우, MyType의 어떠한 서브 클래스가 해당 메서드에 전달될 수 있다는 것을 보장한다.

 

 

문제 2

Given :

// (1) SalesMan.java 
package sales;
public class SalesMan { }

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

// (3) Market.java 
package market;
// insert code here		// line 2
public class USMarket {
    SalesMan sm;
    Product p;
}

 

Q. Which code fragment, when inserted at line 2, enables the code to compile?
A import sales.*;
B import java.sales.products.*;
C import sales;
import sales.products;
D import sales.*;
import products.*;
E import sales.*;
import sales.products.*;

 

정답

E

 

해설/결과

import sales.*;

import sales.products.*;

또는

import sales.SalesMan;

import sales.products.Product;

 

 

문제 3

Given :

public class CheckingAccount {
    public int amount;

    public CheckingAccount(int amount) {
        this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }

    public void changeAmount(int x) {
        amount += x;
    }
}

 

And given the following main method, located in another class :

public static void main(String[] args) {
    CheckingAccount acct = new CheckingAccount((int) (Math.random() * 1000));
    // line n1
    System.out.println(acct.getAmount());
}

 

Q. Which three lines, when inserted independently at line n1, cause the program to print a 0 balance?
A this.amount = 0;
B amount = 0;
C acct (0);
D acct.amount = 0;
E acct.getAmount() = 0;
F acct.changeAmount(0);
G acct.changeAmount(-acct.amount);
H acct.changeAmount(-acct.getAmount());

 

정답

D, F, G, H

 

해설/결과

  • A 와 B는 컴파일이 되지 않음. (amount 변수가 main 메서드에 없기 때문)
  • C는 잘못됨. (생성자 acct를 직접 불러올 수 없기 때문)
  • E는 잘못됨. (acct 메서드를 0과 같게 설정할 수 없기 때문)

 

 

문제 4

Given the code fragment :

String shirts[][] = new String[2][2];
shirts[0][0] = "red";
shirts[0][1] = "blue";
shirts[1][0] = "small";
shirts[1][1] = "medium";

 

Q. Which code fragment prints red: blue: small: medium ?
A for (int index = 1; index < 2; index++) {
    for (int idx = 1; idx < 2; idx++) {
        System.out.print(shirts[index][idx] + ": ");
    }
}
B for (int index = 0; index < 2; ++index) {
    for (int idx = 0; idx < index; ++idx) {
        System.out.print(shirts[index][idx] + ": ");
    }
}
C for (String c : colors) {
    for (String s : sizes) {
        System.out.print(s + ": ");
    }
}
D for (int index = 0; index < 2;) {
    for (int idx = 0; idx < 2;) {
        System.out.print(shirts[index][idx] + ": ");
        idx++;
    }
    index++;
}

 

정답

D

 

해설/결과

medium:                         // A
small:                             // B
                                    // C
red: blue: small: medium:    // D

 

 

문제 5

Given the code fragment :

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

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

    public static void main(String[] args) {
        Test ex = new Test();
        int cardNo = 12344;
        ex.checkCard(cardNo);       // line n2
        ex.readCard(cardNo);        // line n3
    }
}

 

Q. What is the result?
A Reading Card
Checking Card
B Compilation fails only at line n1.
C Compilation fails only at line n2.
D Compilation fails only at line n3.
E Compilation fails at both line n2 and line n3.

 

정답

D

 

해설/결과

Unhandled exception: java.lang.Exception

 

 

문제 6

Given the code fragment :

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder(5);
    String s = "";

    if (sb.equals(s)) {
        System.out.println("Match 1");
    } else if (sb.toString().equals(s.toString())) {
        System.out.println("Match 2");
    } else {
        System.out.println("No Match");
    }
}

 

Q. What is the result?
A Match 1
B Match 2
C No Match
D A NullPointerException is thrown at runtime.

 

정답

B

 

해설/결과

// A
public class Circle implements Shape {		// (!) Class 'Circle' must either be declared abstract or implement abstract method 'draw()' in 'Shape'
    private int radius;
}

// B
public abstract class Circle extends Shape {
    private int radius;
}

// C
public class Circle extends Shape {
    private int radius;
    public void draw();		// (!) Missing method body, or declare abstract
}

// D
public abstract class Circle implements Shape {		// (!) Shape : Interface expected here
    private int radius;
    public void draw();		// (!) Missing method body, or declare abstract
}

// E
public class Circle extends Shape {
    private int radius;
    public void draw() { /* code here */ }
}

// F
public abstract class Circle implements Shape {		// (!) Shape : Interface expected here
    private int radius;
    public void draw() { /* code here */ }
}

 

 

문제 7

Given :

// (1) Acc.java
package p1;

public class Acc {
    int p;
    private int q;
    protected int r;
    public int s;
}

// (2) Test.java
package p2;
import p1.Acc;

public class Test extends Acc {
    public static void main(String[] args) {
        Acc obj = new Test();
    }
}

 

Q. Which statement is true?
A Both p and s are accessible by obj.
B Only s is accessible by obj.
C Both r and s are accessible by obj.
D p, r and s are accessible by obj.

 

정답

B

 

해설/결과

  • 변수 "s"는 Acc 클래스에서 public 멤버이므로, 다른 패키지에 있는 Test 클래스에서 접근이 가능하다.

 

문제 8

Given :

// (1) Base.java
public class Base {
    public void test() {
        System.out.println("Base ");
    }
}

// (2) DerivedA.java
public class DerivedA extends Base {
    public void test() {
        System.out.println("DerivedA ");
    }
}

// (3) DerivedB.java
public class DerivedB extends DerivedA {
    public void test() {
        System.out.println("DerivedB ");
    }

    public static void main(String[] args) {
        Base b1 = new DerivedB();
        Base b2 = new DerivedA();
        Base b3 = new DerivedB();
        b1 = (Base) b3;
        Base b4 = (DerivedA) b3;
        b1.test();
        b4.test();
    }
}

 

Q. What is result?
A Base
DerivedA
B Base
DerivedB
C DerivedB
DerivedB
D DerivedB
DerivedA
E A classcast Except ion is thrown at runtime.

 

정답

C

 

해설/결과

 

 

문제 9

Given the code fragment :

public static void main(String[] args) {
    ArrayList myList = new ArrayList();
    String[] myArray;

    try {
        while (true) {
            myList.add("My String");
        }
    }
    catch (RuntimeException re) {
        System.out.println("Caught a RuntimeException");
    }
    catch (Exception e) {
        System.out.println("Caught an Exception");
    }
    System.out.println("Ready to use");
}

 

Q. What is the result?
A Execution terminates in the first catch statement, and caught a RuntimeException is printed to the console.
B Execution terminates in the second catch statement, and caught an Exception is printed to the console.
C A runtime error is thrown in the thread "main".
D Execution completes normally, and Ready to use is printed to the console.
E The code fails to compile because a throws keyword is requred.

 

정답

C

 

해설/결과

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.base/java.util.Arrays.copyOf(Arrays.java:3512)
    at java.base/java.util.Arrays.copyOf(Arrays.java:3481)
    at java.base/java.util.ArrayList.grow(ArrayList.java:237)
    at java.base/java.util.ArrayList.grow(ArrayList.java:244)
    at java.base/java.util.ArrayList.add(ArrayList.java:454)
    at java.base/java.util.ArrayList.add(ArrayList.java:467)
    at Package.Question69.main(Question69.java:12)

Process finished with exit code 1

While 루프는 무한 루프이기 때문에 프로그램은 OutOfMemoryError 로 인해 종료된다.

이러한 에러는 Exception 또는 RuntimeException 으로 잡혀질(caught) 수 없다.

 

 

문제 10

Given :

System.out.println("5 + 2 = " + 3 + 4);
System.out.println("5 + 2 = " + (3 + 4));

 

Q. What is the result?
A 5 + 2 = 34
5 + 2 = 34
B 5 + 2 + 3+ 4
5 + 2 = 7
C 7 = 7
7 + 7
D 5 + 2 = 34
5 + 2 = 7

 

정답

D

 

해설/결과

+ 연산자가 피연산자와 String 객체 사이에 쓰일 경우, 피연산자는 String 객체로 변하게 된다.

피연산자가 숫자일 때 괄호와 함께 쓰일 경우, 괄호 안의 연산이 먼저 수행된 후, 그 결과값이 String 객체로 변하게 된다.

 

728x90
그리드형(광고전용)

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

[1Z0-808][OCAJP] Dump 문제 41~50  (0) 2022.01.07
[1Z0-808][OCAJP] Dump 문제 31~40  (0) 2022.01.04
[1Z0-808][OCAJP] Dump 문제 21~30  (0) 2022.01.02
[1Z0-808][OCAJP] Dump 문제 11~20  (0) 2022.01.02
[1Z0-851][OCAJP] Dump 문제 1~10  (0) 2021.12.30
[1Z0-851E] Question 21~30  (0) 2017.08.15
[1Z0-851E] Question 11~20  (0) 2017.08.15
[1Z0-851E] Question 01~10  (0) 2017.08.15
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖