Which of the following statements correctly declares and initializes a String object named greeting with the value "Hello" in Java?
AP (Advanced Placement) · AP Computer Science A
Introduction to Algorithms, Programming, and Compilers: แบบฝึกหัด
ข้อปรนัย 5 ข้อ ตรวจให้ทันทีที่ตอบ และข้อเขียน 1 ข้อ พร้อมวิธีทำละเอียด ทั้งหมดจากเรื่อง Introduction to Algorithms, Programming, and Compilers
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?
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);
Given the following code snippet:
String s = "Computer Science";
What is the result of the method call s.substring(3, 7)?
Consider the following method call to the Math class:
double result = Math.pow(Math.sqrt(16), 2);
What is the value of result?
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 วินาที
อยากฝึกโจทย์แบบนี้เพิ่มไหม เริ่มฝึกหัวข้อนี้ได้เลย ตรวจให้ทันทีทุกข้อ
เริ่มฝึกเลย