Download Test #1 - UCF CS

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
no text concepts found
Transcript
AP Computer Science
Test #1
Date: 9/18/09
Directions: Please write all of your answers on your own paper. For all coding questions,
no comments are necessary. On all other questions, please show all of your work.
1) (5 pts) Write a single Java statement that will print the following to the screen:
newline\n
“lastline”
System.out.println(“newline\\n\n\”lastline\””);
Grading: 1pt S.o.p, 1pt regular stuff, 1pt \\, 1pt \n, 1pt \”
2) (10 pts) Determine the value of the following arithmetic expressions in Java:
a) 3*6 + 5
b) 3+6*5
c) 2*(6 – 4%7)
d) 18/(2 + 27%4)
e) 18.0/(2 + 27%4)
23
33
4
3
3.6
3) (9 pts) Determine the output of the following segment of code.
int a=3, b=7, c;
c = 3*a – b%a;
System.out.println(“a= “+a+”b = “+b+”c = “+c);
a = 3*a – 4;
System.out.println(“a= “+a);
System.out.println(“3b+7 = “+(3*b+7));
System.out.println(“b = “+b);
b = 3*b + 7;
c = c – b;
System.out.println(“a= “+a+”b = “+b+”c = “+c);
a= 3b = 7c = 8
a= 5
3b+7 = 28
b = 7
a= 5b = 28c = -20
//I’m sorry about the spacing – it’s based on what was dictated
//in the question. No credit will be taken away for that.
//Grading: 1 pt for each number. 1 pt off if formatting is
//
dramatically wrong.
4) (15 pts) A cup of ice cream costs $4 and a cone of ice cream costs $3. Write a program that
asks the user how many cups and cones of ice cream they are buying and prints out their total
bill. (Note: tax is already included in the $4 and $3 prices.)
import java.util.*;
public class IceCream {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println(“How many cups?”);
int cups = stdin.nextInt();
System.out.println(“How many cones?”);
int cones = stdin.nextInt();
int price = 4*cups+3*cones;
System.out.println(“Total = $”+price);
}
}
//
//
//
//
//
Necessary incidentals: 5 pts (import, main, etc.)
Scanner – 1 pt
each print and read – 1 pt
price calculation – 3 pts
final printout – 2 pts
5) (20 pts) Complete the program below (on your paper only write what should be added to
main) so that it calculates the distance between two points on the Cartesian plane.
import java.util.*;
public class Distance {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
double x1, y1, x2, y2;
System.out.println(“Enter the first point.”);
x1 = stdin.nextDouble();
y1 = stdin.nextDouble();
System.out.println(“Enter the second point.”);
x2 = stdin.nextDouble();
y2 = stdin.nextDouble();
double diffxsq = (x2-x1)*(x2-x1); // 5 pts
double diffysq = (y2-y1)*(y2-y1); // 5 pts
return Math.sqrt(diffxsq+diffysq); // return – 2 pts,
// sqrt – 4 pts
// sum – 4 pts
}
}
6) (18 pts) What is the output produced by the following segment of code?
int i, j=0, s=0;
for (i=1; i<=7; i++) {
System.out.println(“s = “+s+”i = “+i+”j = “+j);
s = s + 2*i + j;
j = j + i;
}
s
s
s
s
s
s
s
=
=
=
=
=
=
=
0i = 1j = 0
2i = 2j = 1
7i = 3j = 3
16i = 4j = 6
30i = 5j = 10
50i = 6j = 15
77i = 7j = 21
// Grading 1 point per blank. I screwed up – meant for the loop
// to go to 6, so I’ll be nice. If you get 18 or more of the
// numbers on this, you get full credit.
7) (20 pts) Write a program that prints out perfect squares. In particular, ask the user for a
positive integer, n, and then print out every perfect square in order: 1, 4, 9, …, n2. You may print
each perfect square on a different line. In the user enters n = 5, then your output should be as
follows:
1*1
2*2
3*3
4*4
5*5
=
=
=
=
=
1
4
9
16
25
import java.util.*;
public class square {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in); // 2 pts
System.out.println("Enter n."); // 2 pts
int n = stdin.nextInt(); // 2 pts
int i; // 1 pt
for (i=1; i<=n; i++) { // 5 pts
System.out.println(i+"*"+i+" = "+(i*i));
} // 2 pts println, 2 pts printing i’s, 1 pt print *
// 3 pts printing result, i*i
}
}
8) (3 pts) At which school’s stadium is tonight’s UHS Homecoming football game being played?
UHS 36 – 0 Woohoo!!! (Luis – good job outscoring Lake Nona!)
Related documents