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.
Question
Given the code format:
Which code fragment must be inserted at line 6 to enable the code to compile?
Exhibit
Options
- ADBConfiguration f;
- BReturn DBConfiguration;
- CReturn new DBConfiguration;
- DRetutn 0;
How the community answered
(31 responses)- B6% (2)
- C90% (28)
- D3% (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.
'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.
'Return DBConfiguration;' attempts to return the class itself, not an instance of the class, which is syntactically incorrect for returning an object type.
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.
'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
Community Discussion
No community discussion yet for this question.
