nerdexam
Oracle

1Z0-803 · Question #176

Given the code format: Which code fragment must be inserted at line 6 to enable the code to compile?

The correct answer is C. Return new DBConfiguration. The method 'createConfiguration()' is declared to return an object of type 'DBConfiguration', requiring an instance of that class to be returned at line 6 for successful compilation.

Working with Methods and Encapsulation

Question

Given the code format:

Which code fragment must be inserted at line 6 to enable the code to compile?

Exhibit

1Z0-803 question #176 exhibit

Options

  • ADBConfiguration f;
  • BReturn DBConfiguration;
  • CReturn new DBConfiguration;
  • DRetutn 0;

How the community answered

(31 responses)
  • B
    6% (2)
  • C
    90% (28)
  • D
    3% (1)

Why each option

The method 'createConfiguration()' is declared to return an object of type 'DBConfiguration', requiring an instance of that class to be returned at line 6 for successful compilation.

ADBConfiguration f;

'DBConfiguration f;' declares a reference variable but does not initialize it or return an object, hence it does not satisfy the method's return type and will not compile.

BReturn DBConfiguration;

'Return DBConfiguration;' attempts to return the class itself, not an instance of the class, which is syntactically incorrect for returning an object type.

CReturn new DBConfiguration;Correct

The method 'createConfiguration()' has a return type of 'DBConfiguration'. To satisfy this contract, an instance of 'DBConfiguration' must be created using the 'new' keyword and then returned, which 'Return new DBConfiguration;' achieves. This correctly provides an object of the specified type.

DRetutn 0;

'Return 0;' attempts to return an integer literal, which is incompatible with the declared return type of 'DBConfiguration' and will result in a compilation error.

Concept tested: Object instantiation and method return types

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html

Topics

#object creation#method return type#compilation

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice