csaTest01Fall2017
csaTest01Fall2017
(1pt ) 2. You would like to set up a variable called ounces that has the value 16. What simple Java
statement will accomplish this?
1 pt
int ounces = 16;
int 16 = ounces;
public static int ounces(16)
ounces(16);
(1pt ) 5. What is the correct data type for decimal numbers such as 3.14159?
double 1 pt
int
boolean
String
(1pt ) 6. What is the correct data type for text data such as "hello world"?
double 1 pt
int
boolean
String
6 pts
APCSA/T1 – Page 2 of 10 –
(1pt ) 7. What is the value of amount after executing the following Java code?
String dinner = "Hamburger";
1 pt
int amount = dinner.length();
8
9
10
11
(1pt ) 8. What is the value of position after executing the following Java code?
String lunch = "Pizza";
1 pt
int position = lunch.indexOf("z");
0
1
2
3
(1pt ) 9. What is the value of first after executing the following Java code?
String breakfast = "Pancakes";
1 pt
String first = breakfast.substring(0, 1);
P
Pan
cakes
Pancakes
(1pt ) 11. Which of the following is a TRUE statement about the String data type?
String is a primitive data type. 1 pt
The standard Java library has a predefined class called String.
Strings can only contain numbers and digits, not punctuation.
Strings are mutable, once they are created they can be changed or altered.
(1pt ) 12. What is the data type of the following variable: num = 42;
boolean 1 pt
double
String
int
6 pts
APCSA/T1 – Page 3 of 10 –
(1pt ) 16. Which of the following choices demonstrates the correct way to concatenate two Strings
together?
1 pt
String answer = "Good" == "Burger";
String outcome = "Best" + "Pizza";
String display = "Fresh" <> "Salad";
String result = "Ripe" / "Fruit";
5 pts
APCSA/T1 – Page 4 of 10 –
(1pt ) 18. Which of Java’s primitive data types would be most suitable to store the square root of 2?
1 pt
(1pt ) 19. Which of Java’s primitive data types would be most suitable to store your age?
1 pt
(1pt ) 20. Write a single line of code that will create an integer variable called num and store the
number 407 in it.
1 pt
(1pt ) 21. Write a single line of code that will increment the previously declared integer variable num
by 1.
1 pt
(1pt ) 22. What are the two possible values of a boolean variable?
1 pt
(1pt ) 23. What is the Java operator for the boolean AND operation?
1 pt
(1pt ) 24. What is the Java operator for the boolean OR operation?
1 pt
(1pt ) 25. Write a single line of code that will create a String variable called name and store the text
"Bob" in it.
1 pt
(1pt ) 26. When comparing two Strings for equality, the assignment operator(==) should not be used.
What is the name of the method that should be used?
1 pt
(1pt ) 27. Write code using a for loop that will print out the numbers: 2 4 6 8 10
1 pt
10 pts
APCSA/T1 – Page 5 of 10 –
8 pts
APCSA/T1 – Page 6 of 10 –
4 pts
APCSA/T1 – Page 7 of 10 –
(2pts ) 36. Write a Java function that converts a Fahrenheit temperature to a Celsius temperature
using the following equation:
2 pts
5.0
Celsius = ∗ (Fahrenheit − 32.0)
9.0
Your function should be called public static double temperature(double fahrenheit),
which takes in a single parameter, fahrenheit. The function should return a double which
is the Celsius conversion of fahrenheit.
• The output of your program should be 30.0 if the following statements are executed:
double result = temperature(86.0);
System.out.println(result);
(2pts ) 37. Write a Java function that computes the length of the hypotenuse of a triangle by using
the Pythagorean theorem: p 2 pts
c = a2 + b2
Your function should be called public static double pythagoras(double a, double
b), which takes in two parameters, a and b. The function should return a double which is
the length of the hypotenuse of the triangle. Hint: You may need to use the Math functions
pow() and sqrt().
• The output of your program should be 5.4671747731346585 if the following state-
ments are executed:
double result = pythagoras(3.5, 4.2);
System.out.println(result);
4 pts
APCSA/T1 – Page 8 of 10 –
(4pts ) 38. In this question, you will write a Java function that generates the Hailstone sequence of
integers. This is a sequence of numbers that goes up and down repeatedly, but eventually
4 pts
the sequence comes down to end in one. The algorithm for the Hailstone sequence can be
expressed as follows:
• Pick some positive integer and call it num.
• If num is even, divide it by two.
• If num is odd, multiply it by three and add one.
• Print out this new value of num.
• Continue this process until num is equal to one.
Write a Java function that takes in a starting value num as a parameter, and generates the
Hailstone sequence from that value. Your function should be called:
public static void hailstone(int num)
Note that this function does not return a value. Instead, you must use: System.out.println()
to display your values as they are being generated.
• If the following statement is executed:
hailstone(5);
Then the output of your program should be:
16
8
4
2
1
4 pts
APCSA/T1 – Page 9 of 10 –
(4pts ) 39. In this question, you will write a Java function that performs the multiplication operation,
but with a technique that the Ancient Egyptians used. The algorithm for Ancient Egyptian
4 pts
Multiplication can be expressed as follows. Assume that grow and shrink are the numbers
to be multiplied together:
• Create an integer variable called product to hold the solution.
• Check to see if shrink is an odd number.
• If shrink is odd, then add the number grow to the variable product.
• Multiply the number grow by 2.
• Divide the number shrink by 2(Note: Use integer division).
• Continue until the number shrink becomes zero.
Write a Java function that takes in two integer values, grow and shrink, as parameters, and
calculates their multiplicative product using the Ancient Egyptian Multiplication algorithm.
Your function should be called:
public static int multiply(int grow, int shrink)
Note that this function returns an integer value.
• If the following statements are executed:
int result = multiply(23, 58);
System.out.println(result);
Then the output of your program should be: 1334
4 pts
APCSA/T1 – Page 10 of 10 –
(4pts ) 40. Pig Latin is a type of slang language that is easy to learn and understand. An English word
can be translated into Pig Latin by following these two simple rules:
4 pts
• If the English word begins with a vowel, then the corresponding Pig Latin word is gen-
erated by appending the letters "hay" to the end of the word. For example, "orange"
becomes "orangehay".
• If the English word begins with a consonant, then the corresponding Pig Latin word is
generated by moving the first letter to the end of the word, then appending the letters
"ay". For example, "peach" becomes "eachpay".
Write a Java function that takes in an English word as a parameter, and translates that word
to Pig Latin. Your function should be called public static String pigLatin(String
word), which takes in a single parameter, word. The function should return a String which
is the Pig Latin translation of word.
• The output of your program should be orangehay if the following statements are
executed:
String result = pigLatin("orange");
System.out.println(result);
• The output of your program should be eachpay if the following statements are exe-
cuted:
String result = pigLatin("peach");
System.out.println(result);
4 pts