nerdexam
Oracle

1Z0-808 · Question #96

1Z0-808 Question #96: Real Exam Question with Answer & Explanation

The correct answer is C. Student(int i, String name, List cs) { /* initialization code goes here */ }. Incorrect: Not A: Student has private access line; Student s = new Student(123,"Fred", cs); Not D: Cannot be applied to given types. Line: Student s = new Student(123,"Fred", cs);

Question

Given the code fragments:
9. class Student {
10. int rollnumber;
11. String name;
12. List<String> 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<String> 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) { /* initialization code goes here */ }
  • Bpublic void Student(int i, String name, List cs) { /* initialization code goes here */ }
  • CStudent(int i, String name, List cs) { /* initialization code goes here */ }
  • DStudent(int i, String name, ArrayList cs) { /* initialization code goes here */ }

Explanation

Incorrect: Not A: Student has private access line; Student s = new Student(123,"Fred", cs); Not D: Cannot be applied to given types. Line: Student s = new Student(123,"Fred", cs);

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice