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 gì? public class Person { protected String name; protected int age; public static void main(String[] args) { Person p = new Person(); p.name = "Tom"; System.out.p

42/50

Đoạn mã sau có lỗi biên dịch gì?
public class Person {
protected String name;
protected int age;
public static void main(String[] args) {
Person p = new Person();
p.name = "Tom";
System.out.println(p.name);
}
}

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

name has protected access.

age has protected access.

name has public access

Giải thích

Chọn đáp án A.Vì: hàm main ở trong cùng lớp Person, nên có thể truy xuất tới tất cả các trường của Person.