APCSA – Java Basics Test (Units 1–3)
Name: __________________________ Date: __________________________
Part 1 – Correct or Incorrect (Syntax)
Write C if the code is correct or I if it contains an error.
1.
int number = 5;
[Link](number);
Answer: ______
2.
Int number = 5;
Answer: ______
3.
int x = 10
[Link](x);
Answer: ______
4.
String name = "Alice";
[Link](name);
Answer: ______
5.
string name = "Bob";
Answer: ______
6.
double price = 4.99;
Answer: ______
Part 2 – Class Structure
public class Dog
{
private String name;
private int age;
public Dog(String n, int a)
{
name = n;
age = a;
}
public String getName()
{
return name;
}
public void setAge(int newAge)
{
age = newAge;
}
}
7. Write the two attributes of the class.
8. What is the constructor name?
9. Write the accessor method name.
10. Write the mutator (setter) method name.
Part 3 – Method Calls
public class Car
{
private String model;
public Car(String m)
{
model = m;
}
public String getModel()
{
return model;
}
}
11. Write the line of code to create a Car object named myCar with model "Toyota".
12. Write the line of code to call the getModel method on myCar.
Part 4 – Write the Parts of a Class
Write a simple Java class called Student that has:
- one attribute: name
- a constructor
- an accessor method getName()