Oracle 1z0-851 Exam Practice Questions (P. 1)
- Full Access (260 questions)
- Six months of Premium Access
- Access to one million comments
- Seamless ChatGPT Integration
- Ability to download PDF files
- Anki Flashcard files for revision
- No Captcha & No AdSense
- Advanced Exam Configuration
Question #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.");
}
}
Which two can be results? (Choose two.)
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.");
}
}
Which two can be results? (Choose two.)
- Ajava.lang.RuntimeException: Problem
- Brun. java.lang.RuntimeException: Problem
- CEnd of method. java.lang.RuntimeException: Problem
- DEnd of method. run. java.lang.RuntimeException: Problem
- Erun. java.lang.RuntimeException: Problem
Correct Answer:
DE
End of method.
run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem at Threads2.run(Threads2.java:5) at java.lang.Thread.run(Unknown Source)
DE
End of method.
run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem at Threads2.run(Threads2.java:5) at java.lang.Thread.run(Unknown Source)
send
light_mode
delete
Question #2
Which two statements are true? (Choose two.)
- AIt is possible for more than two threads to deadlock at once.
- BThe JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.
- CDeadlocked threads release once their sleep() method's sleep duration has expired.
- DDeadlocking can occur only when the wait(), notify(), and notifyAll() methods are used incorrectly.
- EIt is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.
- FIf a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by inserting invocations of Thread.yield().
Correct Answer:
AF
AF
send
light_mode
delete
Question #3
Given:
void waitForSignal() {
Object obj = new Object();
synchronized (Thread.currentThread()) {
obj.wait();
obj.notify();
}
}
Which statement is true?
void waitForSignal() {
Object obj = new Object();
synchronized (Thread.currentThread()) {
obj.wait();
obj.notify();
}
}
Which statement is true?
- AThis code can throw an InterruptedException.
- BThis code can throw an IllegalMonitorStateException.
- CThis code can throw a TimeoutException after ten minutes.
- DReversing the order of obj.wait() and obj.notify() might cause this method to complete normally.
- EA call to notify() or notifyAll() from another thread might cause this method to complete normally.
- FThis code does NOT compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
Correct Answer:
B
Not quite sure about the answer, because first of all this code will not compile:
Threads2.java:15: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown obj.wait();
^
1 error
B
Not quite sure about the answer, because first of all this code will not compile:
Threads2.java:15: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown obj.wait();
^
1 error
send
light_mode
delete
Question #4
Given:
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()); }
}
Which statement is true?
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()); }
}
Which statement is true?
- AThe output could be 5-1 6-1 6-2 5-2
- BThe output could be 6-1 6-2 5-1 5-2
- CThe output could be 6-1 5-2 6-2 5-1
- DThe output could be 6-1 6-2 5-1 7-1
Correct Answer:
B
B
send
light_mode
delete
Question #5
Given:
public class Threads4 {
public static void main (String[] args) {
new Threads4().go();
}
public void go() {
Runnable r = new Runnable() {
public void run() {
System.out.print("foo");
}
};
Thread t = new Thread(r);
t.start();
t.start();
}
}
What is the result?
public class Threads4 {
public static void main (String[] args) {
new Threads4().go();
}
public void go() {
Runnable r = new Runnable() {
public void run() {
System.out.print("foo");
}
};
Thread t = new Thread(r);
t.start();
t.start();
}
}
What is the result?
- ACompilation fails.
- BAn exception is thrown at runtime.
- CThe code executes normally and prints "foo".
- DThe code executes normally, but nothing is printed.
Correct Answer:
B
Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at Threads4.go(Threads4.java:14) at Threads4.main(Threads4.java:3) foo
B
Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at Threads4.go(Threads4.java:14) at Threads4.main(Threads4.java:3) foo
send
light_mode
delete
All Pages