nerdexam
Oracle

1Z0-808 · Question #96

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

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);

Java Class Design

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 */ }

How the community answered

(34 responses)
  • A
    3% (1)
  • B
    6% (2)
  • C
    76% (26)
  • D
    15% (5)

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);

Topics

#constructors#access modifiers#constructor syntax#class instantiation

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice