nerdexam
Oracle

1Z0-803 · Question #113

Given the code fragments: 9. class Student { 10. int rollnumber; 11. String name; 12. List courses = new ArrayList(); 13. // insert code fragment here 14. public String toString() { 15. return rollnum

The correct answer is C. Student(int i, String name, List cs) {. This question requires identifying the correct Java constructor definition that allows the Student class to be instantiated with specific arguments from another class.

Working with Methods and Encapsulation

Question

Given the code fragments: 9. class Student { 10. int rollnumber; 11. String name; 12. List courses = new ArrayList(); 13. // insert code fragment here 14. public String toString() { 15. return rollnumber + " : " + name + " : " + courses; 16. } 17. } And, public class Test { public static void main (String[] args) { List cs = new ArrayList(); cs.add("Java"); cs.add("C"); Student s = new Student(123,"Fred",cs); System.out.println(s); } } Which code fragment, when inserted at line 13, enables class Test to print 123 : Fred : [Java, C] ?

Options

  • Aprivate Student(int i, String name, List cs) {
  • Bpublic void Student(int i, String name, List cs) {
  • CStudent(int i, String name, List cs) {
  • DStudent(int i, String name, ArrayList cs) {

How the community answered

(38 responses)
  • A
    3% (1)
  • B
    13% (5)
  • C
    79% (30)
  • D
    5% (2)

Why each option

This question requires identifying the correct Java constructor definition that allows the `Student` class to be instantiated with specific arguments from another class.

Aprivate Student(int i, String name, List cs) {

The `private` access modifier makes the constructor inaccessible from the `Test` class (assuming they are not nested or in the same class), leading to a compilation error when `Test` attempts to instantiate `Student`.

Bpublic void Student(int i, String name, List cs) {

Including `void` as a return type transforms this into a regular method named `Student`, not a constructor, thus preventing `new Student()` from being recognized as an object creation call.

CStudent(int i, String name, List cs) {Correct

The fragment `Student(int i, String name, List cs)` defines a constructor with the correct class name, no return type, and parameter types (`int`, `String`, `List`) that precisely match the `new Student(123,"Fred",cs)` call in the `Test` class, utilizing default (package-private) accessibility.

DStudent(int i, String name, ArrayList cs) {

Declaring the parameter as `ArrayList cs` would require the argument in `new Student()` to be of type `ArrayList`, or explicitly cast to `ArrayList`, which is not done by the `List cs = new ArrayList();` declaration and usage in the `Test` class.

Concept tested: Java constructor syntax and access modifiers

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

Topics

#constructors#access modifiers#method signatures#data types

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice