300 Câu hỏi trắc nghiệm lập trình Java có đáp án - Phần 1

Đoạn mã sau có lỗi biên dịch nào? Chọn một câu trả lời class Student { public String sayHello() { return "Student"; } } public class Man extends Student { public int sayHello() { return 0; }

32/50

Đoạn mã sau có lỗi biên dịch nào? Chọn một câu trả lời
class Student {
public String sayHello() {
return "Student";
}
}
public class Man extends Student {
public int sayHello() {
return 0;
}
}

Có lỗi biên dịch:sayHello() in Man cannot override sayHello() in Student return type int is not compatible with java.lang.String

Không có lỗi biên dịch.

Có lỗi biên dịch:sayHello() in Man cannot override sayHello() in Student because it is final

Có lỗi biên dịch:sayHello() in Man and in Student has the same access modifier

Giải thích

Chọn đáp án A.Vì:Lớp Man định viết đè sayHello() được viết ở Student, nhưng có kiểu dữ liệu trả về là int không thể chuyển thành String được.Và sayHello() không được đánh dấu là final, nên có thể được ghi đè. sayHello() ở cả hai lớp đều có cùng access modifier, điều này là hợp lệ.