Download TOPICS ON APCS FINAL EXAM

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Computational fluid dynamics wikipedia , lookup

Pattern recognition wikipedia , lookup

Hardware random number generator wikipedia , lookup

Strähle construction wikipedia , lookup

Coding theory wikipedia , lookup

Lempel–Ziv–Welch wikipedia , lookup

Transcript
TOPICS ON APCS FINAL EXAM:
Java math including order of operations, mod, and casting.
Output with escape sequences.
System.out.println( 6 + 6 + “ “ + 3*3) // order of operations within an output line. Output is 12 9
System.out.println(4 + 4 + “ “ + 3 + 3) // order of operations within an output line. Output is 8 33
Why does java do the math for 3*3 in the first one, but not the 3+3 in the 2nd example.
Escape sequences (See review guide).
If statements (||means or && means and). == for primitive data. .equals for strings.
Strings and their methods. .length(); indexOf();, etc. What does indexOf() return if not found? What
happens if your program tries to access an index that is not within the string (NOTE: IT STILL COMPILES,
it just gives an out of bounds exception when running the program)?
In java what does every statement end in? What does every method end with? What does every class
end with?
For and while loops. For loops with substrings (see prinny example on for/while review)
Scanner and the methods involved.
The math class and its methods. See your notes or the java api. This includes Math.random() and how
to create numbers that are, for example, from 18-41 inclusive.
+=
-=
%=
*=
You are expected to know everything from day 1 of class. The review and the list of topics should only
be used as a guide. Best practices for studying for college level courses consist of reading through all
notes, old quizzes, labs, assignments, etc. It would be wise to re-do free response questions and
questions from your notes /quizzes without referencing the solution until you are finished.
Name : ___________________________ Date : _________________
A+ Computer Science - Output Worksheet 2
DIRECTIONS : Fill in each blank with the correct answer/output. While you do this
be sure to log in on your computer and moodle.
1) System.out.print( "da\tbears" );
__________________
2) System.out.print( "buil\tt" );
__________________
3) System.out.print( "hel\\lo" );
__________________
4) System.out.print( 1 + 2 + 3 );
__________________
5) System.out.print( 5 + " " + 3 );
__________________
6) System.out.print( "\\t\\a ");
__________________
7) System.out.print( "\\\\" );
__________________
8) System.out.print( 1 + " " + 2 + 3);
__________________
9) System.out.print( 3 + 7 + " " + 2 + 3);
__________________
Multi-statement code: Write the output as it would be displayed on your
screen.
10) System.out.print(“123”);
System.out.print(“45”);
11) System.out.println(“123”);
System.out.println(“45”);
12) System.out.print(“123”);
System.out.println(“45”);
13) System.out.print(“123”);
System.out.print(“\n45”);
KNOW HOW TO START A PROGRAM! Use good formatting (indent, braces aligned,
etc.)
public class Example
{
public static void main(String[] args)
{
}
}
Remember that \ is an escape sequence and that ; terminates a statement!
Java expects specific characters to be typed after an escape sequence.
For example this will result in an error 
BUT THIS ONE DOESN’T!!
The output is simply “
Why?
System.out.print(“\”);
System.out.print(“\””);
1) STRING BASICS: Given String s = “What?
Magikarp is evolving!”;
Fill in each blank with the correct answer / output. Assume that each question is independent
(one answer will not affect the other). If it results in an error write “ERROR”.
a) s.length() __________________
b) s.substring(0,5)____________________
c) s.substring(5) _______________
d) s.substring(s.length() – 5)____________
e) s.indexOf(“a”)_______________
f) s.indexOf(“magi”)___________________
g) s.indexOf(“hat”)___________
h) s.substring(18,20) + “ee”_____________
i) s.substring(s.indexOf(“ “) + 1)________________________
j) s.length(s.substring(20))____________________________
k) s.substring(20).length() ________________________
l) s.substring(1, s.length()/2 – 5).toUpperCase()________________________________________
m) s.substring(1, s.length()/2 – 5).toUpperCase().length()_______________________________
n) Write a statement that extracts the word “Magikarp”________________________________
o) s.substring(50)____________________
2) When would you get an IndexOutOfBounds error?
3) What is the lowest value that the indexOf() method would return?
4. Given the following code, what indexes must be passed to the substring method to set
expr1 to the string "ROB" and expr2 to the string "the mob" ?
String quote = "Robin, to the batmobile";
String expr1 = quote.substring(a, b).toUpperCase();
String expr2 = quote.substring(c, d) + quote.substring(e, f);
a = _____________
b = _____________
c = _____________
d = _____________
e = _____________
f = _____________
5)Write a program that creates a Scanner that prompts the user to enter his or her full name,
then prints the name with a title in between the first and last name. We will use “the Legend”
as our title. You should read the entire line of input at once using the Scanner and the break
it apart as needed. Here is a sample dialog with the user:
Please enter your full name: Steve Sinish
Oh! You’re Steve the Legend Sinish!
Sample output 2:
Please enter your full name: Vyse Inglebard
Oh! You’re Vyse the Legend Inglebard!
6) Given a string with a variable name of str, print true if it ends in "ly", otherise print false. The
string will have a length of at least 2.
7) Given a string with a variable name str, print a "rotated right 2" version where the last 2
chars are moved to the start. The string length will be at least 3.
Example: if str = “Mewtwo” then the output would be “woMewt”
8) Given a single word string of even length. Write code that prints the same word back to the
user except the first half of the word is all lowercase and the 2nd half of the word is all
uppercase. Here are some sample transformations:
Whoa yields whOA.
Baby yields baBY.
ME yields mE.
eWWW yields ewWW
9) Do the math (Show all work for credit):
24/7 * 2.3 * 2/4
1897 % 10 % 2/2.0
3 * (2.5 + 2.5)/ 30
31 % 2 * 10/5 + 5/2 * 2.5
10) Given the following declarations, what result is stored in each of the listed assignment
statements?
int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double dResult, val1 = 17.0, val2 = 12.78;
// Example iResult = num2 % num1;
// The result that gets stored is 15 because 40 % 25 equals 15.
a) iResult = num1 / num4;
j)
dResult = (double) (num1 / num2);
b) dResult = num1 / num4;
k) iResult = (int) (val1 / num4);
c) iResult = num3 / num4;
l)
dResult = (int) (val1 / num4);
d) dResult = num3 / num4;
m) dResult = (int) ((double) num1 / num2);
e) dResult = val1 / num4;
n) iResult = num3 % num4;
f)
dResult = val1 / val2;
o) iResult = num2 % num3;
g) iResult = num1 / num2;
p) iResult = num3 % num2;
h) dResult = (double) num1 / num2;
q) iResult = num2 % num4;
i)
dResult = num1 / (double) num2;
11) DIRECTIONS : Fill in each blank with the proper java code. Output must look
exactly like the output in the box, and not just a sys out with the answer only.
public class Difference
{
private int numOne, numTwo, answer;
}
//code in the main of another class
Difference test = new Difference();
test.setNums(32,21);
OUTPUT
32 - 21 = 11
test.substract();
test.print();
15 + 8 = 23
12) Trace the code to figure out what the input would be.
OUTPUT:
13) Write code that asks the user for a number and checks if it is divisible by 2, 5, and/or 10 and
then prints to the screen the results. Here are some examples:
Please enter a number: 18
18 is divisible by:
2
Please enter a number: 30
30 is divisible by:
2
5
10
14) Write code that asks the user how many hours of sleep they get (use double). Then,
respond to the user based on these requirements:
If they get 10 or more hours of sleep then respond with “Must be nice… sleeping beauty.”
If they get 7 or more hours but less than 10 respond with “That’s decent”.
If they get less than 7 respond with “That explains everything”.
15) Write code that generates a random integer between 1 and 10. Then ask the user to guess
a number between 1 and 10. Write code that checks if the user guessed the correct number.
Here is example output:
Please enter a number: 9
Wrong! The random number was 7
Please enter a number: 5
CORRECT! Good job.
Name : ___________________________ Date : _________________
A+ Computer Science – for and while loop tracing
Show the output of each block of code below.
1. What is the output?
2. What is the output?
3. What is the output?
for(int i=5; i<15; i=i+3)
{
System.out.println(i);
}
for(int x=53; x>40; x-=2)
{
out.println(x);
}
int i=35, j=0, m;
for(m=30; m>20; m=m-4)
{
i-=5;
j = j + i + m;
}
System.out.println(i + j + m);
4. What is the output?
int x=-1;
while(x<4)
{
x++;
System.out.print(x);
}
5. What is the output?
int a=3, total=0;
while(a<=6)
{
a++;
total=total+a;
}
System.out.print(total);
int b=1, sum=0;
6. Trace the code and show the output?
while(b<11)
{
if(b%3==0)
sum=sum+b;
b+=2;
}
System.out.print(sum);
7. What is the output?
int i=10;
int j =2;
n = 0;
while(i>4)
{
n = n + j – i;
j++;
i -= 2;
}
System.out.println(i);
System.out.println(j);
System.out.println(n);
String output = “”; // empty string
String word = “Prinny”;
8 What is the output?
int length = word.length();
while(length>=3)
{
output += word.substring(0,length-2);
length -=2;
}
System.out.println(output);
1)Compute the value of each expression below. Be sure to list a literal of appropriate type
examples 4.0 / 2 = 2.0
2/4 = 0
2/4.0 = 0.5
2. What is the output from the following code? [Do NOT enter it into JCreator]
Name : ___________________________ Date : _________________
A+ Computer Science - Arrays Worksheet
PART 1 : Fill in each blank with the correct answer/output. Assume each statement
happens in order and that one statement may affect the next statement.
double[] fun = {2.1,4.5,9.6,4.3,0.0,0.5,1.6};
System.out.print(fun[0]);
// LINE 1
1.
_______________
System.out.print(fun[2]);
// LINE 2
2.
_______________
System.out.print(fun[3]);
// LINE 3
3.
_______________
System.out.print(fun[6]);
// LINE 4
4.
_______________
System.out.print(fun [0]+ fun [2]);
// LINE 5
5.
_______________
System.out.print(fun [1]+ fun[2/5]);
// LINE 6
6.
_______________
System.out.print(fun[5*2/8]);
// LINE 7
7.
_______________
System.out.print(fun[9/3]);
// LINE 8
8.
_______________
System.out.print(fun.length);
// LINE 9
9.
_______________
System.out.print(fun.length/2);
// LINE 10
10.
_______________
PART 2 : Fill in the method below with the appropriate code.
//this method will return a count of how many 6s and7s are in the
//array example:if int[] array ={5,6,7,5,6,2} the method returns 3
public static int equalToSevenorSix(int[] array)
{
}
The following exercises will be due by Friday on a separate sheet of paper! Type your code into
the corresponding exercise in coding bat to see if you have correct answers.
(http://codingbat.com/java/Array-1).
Problem firstLast6: Given an array of ints, return true if 6 appears as either the
first or last element in the array. The array will be length 1 or more.
firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({13, 6, 1, 2, 3}) → false
public boolean firstLast6(int[] nums)
{
}
Problem midThree: Given an array of ints of odd length, return a new array length
3 containing the elements from the middle of the array. The array length will be at
least 3.
midThree({1, 2, 3, 4, 5}) → {2, 3, 4}
midThree({8, 6, 7, 5, 3, 0, 9}) → {7, 5, 3}
midThree({1, 2, 3}) → {1, 2, 3}
public int[] midThree(int[] nums)
{
}
Problem makeEnds: Given an array of ints, return a new array length 2 containing
the first and last elements from the original array. The original array will be length
1 or more.
makeEnds({1, 2, 3}) → {1, 3}
makeEnds({1, 2, 3, 4}) → {1, 4}
makeEnds({7, 4, 6, 2}) → {7, 2}
public int[] makeEnds(int[] nums)
{
}
Program plusTwo:Given 2 int arrays, each length 2, return a new array length 4
containing all their elements. Portion of the code is given. Complete the code.
plusTwo({1, 2}, {3, 4}) → {1, 2, 3, 4}
plusTwo({4, 4}, {2, 2}) → {4, 4, 2, 2}
plusTwo({9, 2}, {3, 4}) → {9, 2, 3, 4}
public int[] plusTwo(int[] a, int[] b) {
int[] c = new int[4];
for(int i = 0; i<a.length; i++)
{
}
for (int i = 0; i<b.length; i++)
{
}
return c;
}
Problem returnMax (NOT FROM CODING BAT): Given an array of ints, write a method that returns the
maximum value in the array. Portion of the code is given to you below.
public static int returnMax(int[] values) {
int max = values[0]; // starts by assigning the first value to max;
// use a for loop to find the max value
return max;
}