AP (Advanced Placement) · AP Computer Science A

Introduction to Algorithms, Programming, and Compilers: Practice Questions

5 multiple-choice questions marked as you go, and 1 written questions with worked solutions. All on Introduction to Algorithms, Programming, and Compilers.

6 questions12 marksFree, no account
Question 1
1 mark

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

Question 2
1 mark

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?

Question 3
1 mark

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);

Question 4
1 mark

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

Question 5
1 mark

Consider the following method call to the Math class:

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

What is the value of result?

Question 6
7 marks

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.

Write your answer out first, then check it against the worked solution.

* The content provided by thinka is generated by AI and may not always be accurate or up-to-date. Please use it as a supplementary resource and verify with official materials.

You've seen the model answer. Now get yours marked.

This page can show you how a good answer looks. It cannot tell you what your answer was missing. thinka marks your written work against the real mark scheme in about 15 seconds.

Want more questions like these? Get a fresh set on this topic, graded as you go.

Practice More