200+ câu trắc nghiệm Java cơ bản có đáp án - Phần 5

Đoạn mã sau có lỗi biên dịch nào sau? class Student { protected String name; } public class Man extends Student{ public static void main(String[] args) { Man m = new Man(); m.name="John"; } }

23/30

Đoạn mã sau có lỗi biên dịch nào sau?

class Student {

protected String name;

}

public class Man extends Student{

public static void main(String[] args) {

Man m = new Man();

m.name="John";

}

}

name has protected access in Student.

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

class Man has no attribute name.

class Man has no attribute or method name.

Giải thích

Chọn đáp án B

Vì:

thuộc tính name ở Student là protected, nên không lớp con Man được thừa kế.