CPSC 124, Fall 2001
Quiz #3, September 24This is the third quiz in CPSC 124: Introductory Programming.
Question 1: What is a block statement? How are block statements used in a Java program?
Answer:
A block statement is just a sequence of Java statements enclosed between braces, { and }. The body of a subroutine is a block statement. Block statements are often used in control structures. A block statement is generally used to group together several statements so that they can be used in a situation that only calls for a single statement. For example, the syntax of a while loop calls for a single statement: "while (condition) do statement". However, the statement can be a block statement, giving the structure: "while (condition) { statement; statement; ...}".(This question is taken directly from the quiz for Chapter 3 in the text. The answer is quoted from the answer page for that quiz.)
Question 2: Show the exact output produced by the following Java program segment:
int N; for (N = 1; N < 10; N++) { if ( N % 2 == 0) System.out.print('x'); else if ( N % 3 == 0) System.out.print('y'); else System.out.print('z'); } System.out.println('.');Answer: The exact output is:
zxyxzxzxy.(Note that all the letters are on one line, with a period at the end. The for loop processes the numbers N = 1, 2, 3, 4, 5, 6, 7, 8, and 9. The loop outputs one letter each time it is executed. For the even numbers, an "x" is output, because the test "N % 2 == 0" is true if N is evenly divisible by 2. For numbers that are not even but are divisible by 3, the output is a "y". This is the case for 3 and for 9. Note that for N=6, the output is an "x". 6 passes the first test in the if statement, so the rest of the if statement is skipped. For the remaining value of N, the output is a "z".)
Question 3: Write a for loop that will print out all the multiples of 3 from 3 to 36, that is: 3 6 9 12 15 18 21 24 27 30 33 36
Answer:
Here are two possible answers. Assume that N has been declared to be a variable of type int:for ( N = 3; N <= 36; N = N + 3 ) { System.out.println( N ); } or for ( N = 3; N <= 36; N++ ) { if ( N % 3 == 0 ) System.out.println( N ); }(This question is taken directly from the quiz for Chapter 3 in the text. The answer is quoted from the answer page for that quiz. Here is yet another possible answer:)
for ( N = 1; N <= 12; N++ ) { System.out.println( N * 3 ); }
Question 4: Recall that Math.random() gives a random number between 0 and 1. Write a program segment that will do the following 100000 times: Select two random numbers x and y and count the number of times for which x*x + y*y < 1. At the end, print out the number of times for which this occurred. (By the way, if you divided the answer by 100000.0, it would be an approximation for pi/4.)
Answer:
double x, y; // Randomly chosen numbers between 0 and 1. int i; // Loop control variable for the for loop. int count; // For counting the number of times x*x + y*y < 1. count = 0; // Counter must be initialized to zero. for ( i = 0; i < 100000; i++ ) { x = Math.random(); // generate a pair of random numbers y = Math.random(): if ( x*x + y*y < 1 ) { // count this case count++; } } // end for loop System.out.println(count);(The comments are not a required part of the answer. You might wonder why count/100000.0 would be an approximation for pi/4. The answer requires a bit of mathematics. If you think of (x,y) as a point in the plane, it lies somewhere in the square defined by 0<=x<1 and 0<=y<1. This square contains one quarter of the unit circle defined by x*x+y*y<1. When the program tests if (x*x+y*y<1), it is checking whether the point (x,y) lies within this quarter circle. Now, the area of the whole square is 1 while the area of one-quarter of a circle of radius 1 is pi/4. So the chance that (x,y) lies within the quarter circle is just pi/4 divided by 1. "pi" here refers to the mathematical constant 3.14159...)