AP (Advanced Placement) · AP Computer Science A

Introduction to Algorithms, Programming, and Compilers:練習問題

その場で採点される選択問題 5 問と、解説つきの記述問題 1 問。すべて「Introduction to Algorithms, Programming, and Compilers」からの出題です。

6 問12 無料・登録不要
問 1
1

Which of the following statements correctly declares and initializes a String object named greeting with the value "Hello" in Java?

問 2
1

Consider the following code segment:

Integer num1 = new Integer(10);
Integer num2 = 20;
int sum = num1 + num2;

Which of the following concepts are demonstrated in this code?

問 3
1

Suppose a class Rectangle has a constructor public Rectangle(int w, int h) and a method public void scale(int factor). What happens in memory when the following is executed?
Rectangle r1 = new Rectangle(10, 20);
Rectangle r2 = r1;
r1 = new Rectangle(5, 5);
r2.scale(2);

問 4
1

Given the following code snippet:
String s = "Computer Science";
What is the result of the method call s.substring(3, 7)?

問 5
1

Consider the following method call to the Math class:

double result = Math.pow(Math.sqrt(16), 2);

What is the value of result?

問 6
7

You are using the String and Math classes to process geometric data provided as text.
Part a: Given a string coord = "(x1, y1)", write the Java code to extract the numerical values of \(x_1\) and \(y_1\) using indexOf and substring methods, assuming they are integers.
Part b: Use the Math.pow and Math.sqrt methods to calculate the distance \(d\) from the origin \((0,0)\) to the point \((x_1, y_1)\) using the formula \(d = \sqrt{x_1^2 + y_1^2}\).
Part c: If the coordinates are retrieved from a 2D grid where \(x_1 \in [0, 100]\) and \(y_1 \in [0, 100]\), use Math.random() to generate a random point and calculate its distance from the origin. Explain how you ensure the random value is an integer.

まず自分で答えを書いてから、解説と照らし合わせましょう。

※ thinkaのコンテンツはAIにより生成されているため、内容が正確でない場合があります。補助教材としてご使用いただき、公式の教材と合わせてご確認ください。

模範解答は見ました。次はあなたの答案を採点します。

このページは良い答案の形を示せますが、あなたの答案に何が足りないかは教えられません。thinka は実際の採点基準に沿って記述答案を約 15 秒で採点します。

同じような問題をもっと解きたい?このトピックの新しい問題を、解きながら採点。

練習を始める