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

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

 

문제 1

Given :

public class Threads2 implements Runnable {
    public void run() {
        System.out.println("run.");
        throw new RuntimeException("Problem");
    }

    public static void main(String[] args) {
        Thread t = new Thread(new Threads2());
        t.start();
        System.out.println("End of method.");
    }
}

 

Q. Which two can be results? (Choose two.)
A java.lang.RuntimeException: Problem
B run.
java.lang.RuntimeException: Problem
C End of method.
java.lang.RuntimeException: Problem
D End of method.
run.
java.lang.RuntimeException: Problem
E run.
java.lang.RuntimeException: Problem
End of method.

 

정답

D, E

 

해설/결과

End of method.
run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
at com.starrykss.Threads2.run(Threads2.java:6)
at java.base/java.lang.Thread.run(Thread.java:833)

 

 

문제 2

Q. Which two statements are true? (Choose two.)
A It is possible for more than two threads to deadlock at once.
B The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.
C Deadlocked threads release once their sleep() method's sleep duration has expired.
D Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are used incorrently.
E It is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.
F If a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by inserting invocations of Thread.yield().

 

정답

A, F

 

해설/결과

A. 한번에 2개 이상의 스레드가 교착 상태에 빠질 수 있다.

B. JVM 구현은 여러 스레드가 교착 상태에 빠질 수 없도록 보증한다.

C. 교착 상태 스레드는 sleep() 메서드의 지속 기간이 만료되면 해제된다.

D. 교착 상태는 오직 wait(), notify() 및 notifyAll() 메서드가 잘못 사용될 때만 발생할 수 있다.

E. 동기화된 블록이 잘못 사용되면 단일 스레드 응용 프로그램이 교착 상태에 빠질 수 있다.

F. 코드의 일부분이 교착 상태에 빠질 수 있는 경우, Thread.yield() 호출을 삽입하여 교착 상태의 가능성을 제거할 수 없다.

 

 

문제 3

Given :

public class waitForSignal {
    Object obj = new Object();
    synchronized (Thread.currentThread()) {
        obj.wait();
        obj.notify();
    }
}

 

Q. Which statement is true?
A This code can throw an InterruptedException.
B This code can throw an IllegalMonitorStateException.
C This code can throw a TimeoutException after ten minutes.
D Reversing the order of obj.wait() and obj.notify() might cause this method to complete normally.
E A call to notify() or notifyAll() from another thread might cause this method to complete normally.
F This code does NOT compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".

 

정답

B

 

해설/결과

  • IllegalMonitorStateException 예외
    • 스레드가 락을 부정적으로 점유하였을 때 발생하는 예외
  • 문제 해설 : click

 

 

문제 4

Given :

public class PingPong2 {
    synchronized void hit(long n) {
        for (int i = 1; i < 3; i++) {
            System.out.print(n + "-" + i + " ");
        }
    }
}

public class Tester implements Runnable {
    static PingPong2 pp2 = new PingPong2();

    public static void main(String[] args) {
        new Thread(new Tester()).start();
        new Thread(new Tester()).start();
    }

    public void run() { pp2.hit(Thread.currentThread().getId()); }
}

 

Q. Which statement is true?
A The output could be 5-1 6-1 6-2 5-2.
B The output could be 6-1 6-2 5-1 5-2
C The output could be 6-1 5-2 6-2 5-1
D The output could be 6-1 6-2 5-1 7-1

 

정답

B

 

해설/결과

15-1 15-2 16-1 16-2 

실행 결과 a-1 a-2 b-1 b-2 형식으로 출력되는 것을 확인할 수 있었다.

 

 

문제 5

Given :

package com.starrykss;

public class Threads4 {
    public static void main(String[] args) {
        new Threads4().go();
    }

    public void go() {
        Runnable r = new Runnable() {
            public void run() {
                System.out.println("foo");
            }
        };
        Thread t = new Thread(r);
        t.start();
        t.start();
    }
}

 

Q. What is the result?
A Compilation fails.
B An exception is thrown at runtime.
C The code executes normally and prints "foo".
D The code executes normally, but nothing is printed.

 

정답

B

 

해설/결과

Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.start(Thread.java:793)
at com.starrykss.Threads4.go(Threads4.java:17)
at com.starrykss.Threads4.main(Threads4.java:6)
foo

Process finished with exit code 1

실행 결과 IllegalThreadStateException 예외가 발생하는 것을 확인할 수 있었다.

 

 

문제 6

Given :

public abstract class Shape {
    private int x;
    private int y;

    public abstract void draw();

    public void setAnchor(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

 

Q. Which two classes use the Shape class correctly? (Choose two.)
A public class Circle implements 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();
}
D public abstract class Circle implements Shape {
    private int radius;
    public void draw();
}
E public class Circle extends Shape {
    private int radius;
    public void draw() { /* code here */ }
}
F public abstract class Circle implements Shape {
    private int radius;
    public void draw() { /* code here */ }
}

 

정답

B, E

 

해설/결과

// 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 :

public class Barn {
    public static void main(String[] args) {
        new Barn().go("hi", 1);
        new Barn().go("hi", "world", 2);
    }

    public void go(String... y, int x) {
        System.out.print(y[y.length - 1] + " ");
    }
}

 

Q. What is the result?
A hi hi
B hi world
C world world
D Compilation fails,
E An exception is thrown at runtime.

 

정답

D

 

해설/결과

java: varargs parameter must be the last parameter
  • 실행 결과 컴파일 오류가 발생하는 것을 확인할 수 있었다.
  • ※ String 타입의 가변 인수(Variable Argument)는 항상 매개변수의 마지막 자리에 위치해야 한다.

 

 

문제 8

Given :

public class Nav {
    public enum Direction { NORTH, SOUTH, EAST, WEST }
}

public class Sprite {
    // insert code here
}

 

Q. Which code, inserted at line 14, allows the Sprite class to compile?
A Direction d = NORTH;
B Nav.Direction d = NORTH;
C Direction d = Direction.NORTH;
D Nav.Direction d = Nav.Direction.NORTH;

 

정답

D

 

해설/결과

public class Sprite {
    // insert code here
    Direction d = NORTH;	// (!) Cannot resolve symbol 'Direction', Cannot resolve symbol 'NORTH'
    Nav.Direction d = NORTH;		// (!) Cannot resolve symbol 'NORTH'
    Direction d = Direction.NORTH;		// (!) Cannot resolve symbol 'Direction', Cannot resolve symbol 'NORTH'
    Nav.Direction d = Nav.Direction.NORTH;
}

 

 

문제 9

Given :

public interface A {
    public void doSomething(String thing);
}

public class AImpl implements A {
    public void doSomething(String msg) {}		// line 2
}

public class B {
    public A doit() {
        // more code here
    }
    public String execute() {
        // more code here
    }
}

public class C extends B {
    public AImpl doit() {		// line 2
        // more code here
    }
    
    public Object execute() {		// line 6
        // more code here
    }
}

 

Q. Which statement is true about the classes and interfaces in the exhibit?
A Compilation will succeed for all classes and interfaces.
B Compilation of class C will fail because of an error in line 2.
C Compilation of class C will fail because of an error in line 6.
D Compilation of class AImpl will fail because of an error in line 2.

 

정답

C

 

해설/결과

'execute()' in 'com.starrykss.C' clashes with 'execute()' in 'com.starrykss.B'; attempting to use incompatible return type

 

 

문제 10

Given :

public class Person {
    String name = "No name";
    public Person(String nm) { name = nm; }
}

public class Employee extends Person {
    String empID = "0000";
    public Employee(String id) { empID = id; }		// line 18
}

public class EmployeeTest {
    public static void main(String[] args) {
        Employee e = new Employee("4321");
        System.out.println(e.empID);
    }
}

 

Q. What is the result?
A 4321
B 0000
C An exception is thrown at runtime.
D Compilation fails because of an error in line 18.

 

정답

D

 

해설/결과

There is no default constructor available in 'com.starrykss.Person'

 

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

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

[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-808][OCAJP] Dump 문제 1~10  (2) 2021.12.31
[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
[1Z0-851D] Question 41~50  (0) 2017.08.13
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️


📖 Contents 📖