Test Date: Tue and Wed, Mar 4-5, 2003
This document is also available as test1.doc.
Name: _________________________________
Programming and Computer Science: Test
#1
March 4-5, 2003
Part I:
Written: 75
minutes
Write your answers on this test sheet.
No notes, no books, no calculators, no talking, etc…
You may not use a computer for this section.
Once you begin Part II, you may not return to Part I, even to check your answers.
Good luck!
Question 1: Write "Hello World" in Java.
Question 2: What will the following code fragment print out? Show your work (for partial credit)! Note: To help you show your work, the lines are numbered, and a table is provided where, for each line on which x or y is changed, you can list the line number and the new values of x and y.
1 int x = 3, y = 15; Line# x value y value
2 while ((x % y) < (y / x)) 1 3 15
3 {
4 x = x + 1;
5 y = y - x;
6 }
7 System.out.println(x + "," + y);
Question 3: What will the following code fragment print out? Show your work (for partial credit)!
1 int x = -1, y = 6; Line# x value y value
2 while (true) 1 -1 6
3 {
4 x = x + y/2;
5 if (((x*y) % 10) == 0)
6 {
7 break;
8 }
9 while (x <= y)
10 {
11 x = x + 3;
12 }
13 y = y + 2;
14 }
15 System.out.println(x + "," + y);
Question 4: What will the following code fragment print out? Show your work (for partial credit)!
1 int x = 2, y = -2, z = 0; Line# x val y val z val
2 while (Math.abs(Math.max(y,z)) < x) 1 2 -2 0
3 {
4 if (x%2 == 0)
5 {
6 z = z - 3;
7 x = x + 1;
8 }
9 else
10 {
11 y = y - 3;
12 }
13 }
14 System.out.println(x + "," + y + "," + z);
Question 5: What will the following code fragment print out? Show your work (for partial credit)! Note: obviously, in this case, you will not list every change in the value of x. You have to analyze the pattern to figure out the final value of x.
1 int x = 1; Line# x value
2 while (((x%500) + (x/500)) < 500) 1 1
3 {
4 x = x + 1;
5 }
6 System.out.println(x);
Question 6: Write a Java program which repeatedly reads in an integer n, halting if n is negative, and otherwise prints out the sum of the integers from 0 to n, exactly as follows:
Enter an integer [<0 to exit]: 3
0 + 1 + 2 + 3 = 6
Enter an integer [<0 to exit]: 5
0 + 1 + 2 + 3 + 4 + 5 = 15
Enter an integer [<0 to exit]: -1
Goodbye!
Note that your program must work exactly as the example.
Question 7:
A "Pythagorean Triple"
is a point (a,b,c) where a^{2} + b^{2} = c^{2}.
(3,4,5) is such a triple, because 3^{2} + 4^{2} = 9 + 16
= 25 = 5^{2}. Write a Java
program which reads in an integer n and prints out all the Pythagorean Triples
(a,b,c) where a, b, and c are all less than n. Restrict your output to the cases where (a <= b) and (b
<= c), so you would output (3,4,5) but not (3,5,4) or (4,5,3) for example.
Hint:
use nested while loops to test every possible combination of a, b, and c
in the legal range.
End of Part I.
Do not proceed to Part II until instructed to do so.
Part II: Computer: 75 minutes
You
may use JCreator for this section.
No
notes, no books, no calculators, no talking, etc…
You
must create a new file, and you may download readInt() from the class web
site.
Once
you begin Part II, you may not return to Part I, even to check your answers.
Good
luck!
Question 8/9/10 (triple weighted): Goldbach's Conjecture states that every even number larger than 2 is the sum of two prime numbers. Interestingly, nobody has ever proven it to be true, but it sure seems to be true. Consider the following examples:
4 = 2 + 2
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
...
Write a Java program which empirically confirms Goldbach's Conjecture by repeatedly reading in an even integer n (halting when n < 4) and printing out that number as the sum of two primes (of course, your program must find the primes which sum to n). If n is odd, just print out that it is odd.
Here is exactly how your program should work:
Enter an even integer [<4 to exit]: 10
10 = 5 + 5
Enter an even integer [<4 to exit]: 17
17 is odd. Please try again.
Enter an even integer [<4 to exit]: 16
16 = 3 + 13
Enter an even integer [<4 to exit]: 0
Goodbye!
Hint:
for each number p which is less than n, check if p is prime, then check
if (n-p) is also prime. If both are
true, then p and (n-p) are the two primes which sum to n, because p + (n-p) ==
n.
Hint: Even if you cannot solve this problem outright, write as much of the solution as you can!
See Course Home Page.