Midterm Exam CSC 211 -- Introduction to Java June 30, 2003 Staple this test paper onto the front of your answer sheets. Part A. True-False Questions. For each question, give the answer AND A REASON FOR EACH ANSWER, even when the question is true. (4 points per reason, 1 point for correct answer.) Do all six problems. 1. The main method must be static. 2. String is a primitive datatype. 3. The output from the following println statement is 15: System.out.println(" " + 1 + 2 + 3 + 4 + 5); 4. The value of f after the following lines is true: int x = 7; int y = 9; f = (!(x > y) || ((y != 7) && (x - y == 1)) 5. The following for loop will execute four times: for(i = 4; i <= 17; i+=3) System.out.print(i + " "); 6. In an applet, the init method executes every time the applet is restored after being minimized. Part B. Problems. Do three out of four problems. (15 points per problem.) 1. Draw the variable trace table and predict the output of the following main method: public static void main(String[] args) { String s = "abcdefghijklmnopqrstuvwxyz", t = ""; int x = 2, y = 32; while(y > 1) { t += s.charAt(x); x += 5; y /= 2; } System.out.println(t); } 2. Write a Java application that will input the number of miles (double) in an input dialog, and then output that number of miles converted to centimeters. Recall that there are 2.54cm per inch, 12 inches per foot, and 5280 feet per mile. 3. Write a Java application that will input a string and output true if the string represents a Java variable name (starts with a lower case letter and every character after the first is either a letter or a digit.) Output false otherwise. 4. Draw the picture produced by the following paint method. public void paint(Graphics g) { int y; setBackground(Color.white); g.setColor(Color.black); for(y = 50; y <= 200; y += 50) { g.fillOval(50, y, 50, 50); g.fillOval(100, y, 50, 50); g.fillOval(150, y, 50, 50); g.fillOval(200, y, 50, 50); } g.drawLine(25, 25, 275, 25); g.drawLine(275, 25, 275, 275); g.drawLine(275, 275, 25, 275); g.drawLine(25, 275, 25, 25); setSize(300, 300); }