Knowee
Questions
Features
Study Tools

Given the following code, find the compile error. Please select all that apply. public class Test {   public static void main(String[] args) {     m(new GraduateStudent());     m(new Student());     m(new Person());     m(new Object());   }   public static void m(Student x) {     System.out.println(x.toString());   } }  class GraduateStudent extends Student { } class Student extends Person {   @Override   public String toString() {     return "Student";   } } class Person extends Object {   @Override   public String toString() {     return "Person";   } } Group of answer choicesm(new GraduateStudent()) causes an errorm(new Student()) causes an errorm(new Person()) causes an errorm(new Object()) causes an error

Question

Given the following code, find the compile error. Please select all that apply. public class Test {   public static void main(String[] args) {     m(new GraduateStudent());     m(new Student());     m(new Person());     m(new Object());   }   public static void m(Student x) {     System.out.println(x.toString());   } }  class GraduateStudent extends Student { } class Student extends Person {   @Override   public String toString() {     return "Student";   } } class Person extends Object {   @Override   public String toString() {     return "Person";   } } Group of answer choicesm(new GraduateStudent()) causes an errorm(new Student()) causes an errorm(new Person()) causes an errorm(new Object()) causes an error

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The method m(Student x) is designed to accept objects of the Student class or any of its subclasses. In this case, GraduateStudent is a subclass of Student, so m(new GraduateStudent()) will not cause an error. Similarly, m(new Student()) will not cause an error because an object of the Student class is being passed.

However, m(new Person()) will cause a compile error because Person is a superclass of Student, not a subclass. The method m(Student x) cannot accept a Person object because a Person is not a Student.

Similarly, m(new Object()) will also cause a compile error. Even though all classes in Java are subclasses of Object, the m(Student x) method specifically requires a Student object or one of its subclasses. An Object is not a Student, so this will cause a compile error.

So, the correct answers are:

  • m(new Person()) causes an error
  • m(new Object()) causes an error

This problem has been solved

Similar Questions

The following code contains one compilation error, find it?public class Test { Test() { } // line 1 static void Test() { this(); } // line 2 public static void main(String[] args) { // line 3 Test(); // line 4 }}

Analyze the following code. Please select all that apply.  public class Test {   public static void main(String[] args) {     Object a1 = new A();     Object a2 = new Object();     System.out.println(a1);     System.out.println(a2);   } } class A {   int x;   @Override   public String toString() {     return "A's x is " + x;   } } Group of answer choicesThe program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString());When executing System.out.println(a1), the toString() method in the Object class is invoked.When executing System.out.println(a2), the toString() method in the Object class is invoked.When executing System.out.println(a1), the toString() method in the A class is invoked.

what is the result of the following piece of code:public class Person{ public void talk(){ System.out.print("I am a Person"); }}public class Student extends Person{ public void talk(){ System.out.print("I am a Student"); }}public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); }}I am a Person I am a Student I am a Person I am a StudentI am a Student I am a Person

DebuggingLook at the code for the School and University classes given below. There is an error in the constructor for the University class. Can you identify the error?class School { private String name; private int numStudents; public School(String name, int numStudents) { this.name = name; this.numStudents = numStudents; } public String getName() { return this.name; } public int getNumStudents() { return this.numStudents; }}class University extends School { private final String type = "University"; private boolean isPublic; public University(String name, int numStudents, boolean isPublic) { this.name = name; this.numStudents = numStudents; this.isPublic = isPublic; } public boolean getIsPublic() { return this.isPublic; }}

What is the output of the following code? _____public class Test {  public static void main(String[] args) {    new Person().printPerson();    new Student().printPerson();  }}class Student extends Person {  @Override  public String getInfo() {    return "Student";  }}class Person {  public String getInfo() {    return "Person";  }    public void printPerson() {    System.out.println(getInfo());  }}A. Person PersonB. Person StudentC. Stduent StudentD. Student Person

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.