nerdexam
Oracle

1Z0-803 · Question #264

Given the code fragment: class Student { int rollnumber; String name; List cources = new ArrayList(); // insert code here public String toString() { return rollnumber + " : " + name + " : " + cources;

The correct answer is C. Student(int i, String name, List cs) {. 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);

Working with Methods and Encapsulation

Question

Given the code fragment:

class Student { int rollnumber; String name; List cources = new ArrayList(); // insert code here public String toString() { return rollnumber + " : " + name + " : " + cources; } } And, public class Test { public static void main(String[] args) { List cs = newArrayList(); cs.add("Java"); cs.add("C"); Student s = new Student(123,"Fred", cs); System.out.println(s); } } Which code fragment, when inserted at line // insert code here, enables class Test to print 123 :

Fred : [Java, C]?

Options

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

How the community answered

(36 responses)
  • A
    3% (1)
  • B
    14% (5)
  • C
    78% (28)
  • D
    6% (2)

Explanation

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#Method signatures#Access modifiers#ArrayList

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice