Consider the following class header for a class named Book. Which of the following is a valid constructor for this class if the class has private instance variables title (String) and pages (int)?
AP (Advanced Placement) · AP Computer Science A
Abstraction, Program Design and Its Impact:練習問題
その場で採点される選択問題 5 問と、解説つきの記述問題 1 問。すべて「Abstraction, Program Design and Its Impact」からの出題です。
Which of the following best describes the purpose of a constructor in a Java class?
A class Student has an instance variable gpa. The constructor is defined as follows:
public Student(double gpa) {
gpa = gpa;
}
What happens when new Student(3.5) is called, assuming the instance variable is also named gpa?
A class Square is being designed. It needs to store the length of a side. Which of the following is the most appropriate way to declare the instance variable for side length to ensure encapsulation?
Consider the following class definition:
public class Runner {
private double distance;
public Runner(double d) {
distance = d;
}
public void addDistance(double d) {
distance += d;
}
}
If we execute the following code:
Runner r1 = new Runner(5.0);
r1.addDistance(2.5);
What is the value of distance in the object r1?
Part a: A software developer needs to create a Student class. Define a constructor that initializes a student's name (String) and their current GPA (double).
Part b: Write a method isEligible that returns true if the GPA is greater than or equal to 2.0, and false otherwise.
Part c: Explain why the instance variables should be declared as private and how the constructor helps in maintaining encapsulation.
まず自分で答えを書いてから、解説と照らし合わせましょう。
※ thinkaのコンテンツはAIにより生成されているため、内容が正確でない場合があります。補助教材としてご使用いただき、公式の教材と合わせてご確認ください。
模範解答は見ました。次はあなたの答案を採点します。
このページは良い答案の形を示せますが、あなたの答案に何が足りないかは教えられません。thinka は実際の採点基準に沿って記述答案を約 15 秒で採点します。
同じような問題をもっと解きたい?このトピックの新しい問題を、解きながら採点。
練習を始める