nerdexam
Oracle

1Z0-805 · Question #3

Given: public class SampleClass { public static void main(String[] args) { SampleClass sc = new SampleClass(); sc.processCD(); } Oracle 1Z0-805 Exam private void processCD() { try (CDStream cd = new…

The correct answer is A. Compilation CD stream. In this example the compilation of line " try (CDStream cd = new CDStream()) {" will fail, as try-with-resources not applicable to variable type CDStream. Note: The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that…

Exceptions and Assertions

Question

Given:

public class SampleClass { public static void main(String[] args) { SampleClass sc = new SampleClass(); sc.processCD(); } Oracle 1Z0-805 Exam private void processCD() { try (CDStream cd = new CDStream()) { cd.open(); cd.read(); cd.write("lullaby"); cd.close(); } catch (Exception e) { System.out.println("Exception thrown"); } } class CDStream { String cdContents = null; public void open() { cdContents = "CD Contents"; System.out.println("Opened CD stream"); } public String read() throws Exception { throw new Exception("read error"); } public void write(String str) { System.out.println("CD str is: " + str); } public void close() { cdContents = null; } What is the result?

Options

  • ACompilation CD stream
  • BOpened CD thrown
  • CException thrown
  • DOpened CD stream
  • ECD str is: lullaby

How the community answered

(47 responses)
  • A
    74% (35)
  • B
    6% (3)
  • C
    13% (6)
  • D
    4% (2)
  • E
    2% (1)

Explanation

In this example the compilation of line " try (CDStream cd = new CDStream()) {" will fail, as try-with-resources not applicable to variable type CDStream. Note: The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with- resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Topics

#try-with-resources#AutoCloseable#exception propagation#resource management

Community Discussion

No community discussion yet for this question.

Full 1Z0-805 Practice