0% found this document useful (0 votes)
2 views

csaTest01Fall2017

This document is a test for AP Computer Science A from Fall 2017, consisting of multiple-choice and coding questions related to Java programming. It covers topics such as variable naming, data types, string manipulation, loops, and functions. The test includes both theoretical questions and practical coding tasks to assess students' understanding of Java concepts.

Uploaded by

briansu368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

csaTest01Fall2017

This document is a test for AP Computer Science A from Fall 2017, consisting of multiple-choice and coding questions related to Java programming. It covers topics such as variable naming, data types, string manipulation, loops, and functions. The test includes both theoretical questions and practical coding tasks to assess students' understanding of Java concepts.

Uploaded by

briansu368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

AP Computer Science A Test 1 Name:

Fall 2017 Mr. Alwin Tareen


Part I. (55 points) Solve each of the following problems. For the multiple choice problems,
select the correct answer by placing an “X” in the box beside it.
(1pt ) 1. Which of the following choices is a legal and legitimate Java variable name?
2bad4you 1 pt
calvin&hobbes
year2000
#hammertime

(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 ) 3. What is the output of the following Java code?


System.out.println(19 % 5);
1 pt
3
0
4
1

(1pt ) 4. What is the output of the following Java code?


System.out.println(1 / 3);
1 pt
0.333333333333333333
0
0.3
It will give a compile-time error.

(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 ) 10. Which of the following choices is a Java reserved keyword?


console 1 pt
while
memory
result

(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 ) 13. What is the output of the following Java code?


for (int i = 3; i <= 12; i++)
1 pt
{
System.out.print(i + " ");
}
5 6 7 8 9
4 5 6 7 8 9 10 11 12
3 5 7 9 11
3 4 5 6 7 8 9 10 11 12

(1pt ) 14. What is the output of the following Java code?


String greetings = "Hello World!";
1 pt
System.out.println(greetings.substring(6));
Hello World!
The Java code will not compile.
World!
Hello

(1pt ) 15. What is the output of the following Java code?


String weather = "One Fine Day";
1 pt
String result = weather.substring(4, 8);
System.out.println(result);
Fine
One Fine
Fine Day
Day

(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";

(1pt ) 17. Consider the following Java code:


String drink = "sprite";
1 pt
String beverage = "pepsi";
How would you determine if these two Strings are the same, or different?
Divide one String by the other. If the result is one, then the Strings are equal.
Use the differential() method in the following manner:
double outcome = drink.differential(beverage);
Use the equals() method in the following manner:
boolean result = drink.equals(beverage);
Use the == operator in the following manner:
boolean answer = (drink == beverage);

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 –

(2pts ) 28. Convert the following binary(base-2) numbers to decimal(base-10).


(a) (1 pt) 1011 (b) (1 pt) 10001 2 pts

(2pts ) 29. Convert the following hexadecimal(base-16) numbers to decimal(base-10).


(a) (1 pt) A7 (b) (1 pt) 2E 2 pts

(2pts ) 30. Convert the following binary(base-2) numbers to hexadecimal(base-16).


(a) (1 pt) 10010011 (b) (1 pt) 110010100001 2 pts

(2pts ) 31. Convert the following hexadecimal(base-16) numbers to binary(base-2).


(a) (1 pt) B4 (b) (1 pt) 9C 2 pts

8 pts
APCSA/T1 – Page 6 of 10 –

(1pt ) 32. What is the output of the following while loop?


int num = 0;
1 pt
while (num < 3)
{
System.out.println(num);
num++;
}

(1pt ) 33. What is the output of the following while loop?


int num = 5;
1 pt
while (num < 10)
{
System.out.println(num);
num += 2;
}

(1pt ) 34. What is the output of the following for loop?


for (int i = 0; i <= 8; i += 2)
1 pt
{
System.out.println(i);
}

(1pt ) 35. What is the output of the following for loop?


for (int i = 5; i >= 1; i--)
1 pt
{
System.out.println(i);
}

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

You might also like