CS 140 - Test 1 Study Guide

Make sure review the four homework assignments, all of the labs we've done, and the examples we've written in class.

  1. Write a complete Java program in a class named ExamOne that prints Hello World to the console.
  2. Write a complete Java program in a class named ExamOne that prints Hello World in the center of a 600-by-600 picture.
  3. Write a Java code segment that calculates and prints the number of minutes you have been alive up to yesterday at midnight. Don't do any calculations on the side, let Java do them all.
  4. Consider the following Java variable declarations. Which are okay as is and and which contain an error. Explain why.
         int count = 0;
         int age;
         DOuble pi = 3.14159;
         String myname = "Poindexter;
         int the answer = 9;
      
  5. What does the following Java statement do?
       StdOut.println("What do" + 1 + (2 + 3) + 4 + " I print?");
    
  6. Write two Java nested for-loops that prints the following pattern of stars.
     *****
     *****
     *****
     *****
     *****
    
  7. Write two Java nested for-loops that prints the following pattern of stars.
     *
     **
     ***
     ****
     *****
    
  8. What does the following code segment print to the console?
       int sum = 0; 
       for (int i = 7; i < 12; i=i+2) {
         sum = sum + i;
       }
       StdOut.println(sum);
    
  9. What is printed by the following Java code.
      int x;
      x = 1/3;
      StdOut.println(x);
      
  10. Find all of the syntax errors in the code below. (That is find all of the errors that keep the programing from compiling). Write a brief explanation next to a line if there is an error that explains what the error is. Some of the lines (statements) may have multiple errors.
                                                  ______________________________________________
    
      public class Exam1                          ______________________________________________
      
        public static void main (String args) {   ______________________________________________
         
          int XXXX = 7;                           ______________________________________________
    
          int pi = 3.14159;                       ______________________________________________
    
          StdOut.println(XXXX);                   ______________________________________________
    
          Picture p = new Picture();              ______________________________________________
    
          Graphics g = getGraphics();             ______________________________________________
    
          g.fillOval(XXXX, XXXX*100, 20);         ______________________________________________
       
          int sum = 0;                            ______________________________________________
     
          for (int i = 0; i < 10; i++ {           ______________________________________________
    
              sum = sum + I                       ______________________________________________
    
          }                                       ______________________________________________
    
          StdOut.println(sum);                    ______________________________________________
    
      }                                           ______________________________________________
                                                
                                                  ______________________________________________
    
  11. Assume we were to execute the following code segment. What would it print?
        int sum = 0;
        for (int i = 4; i < 9; i++) {
           sum = sum * i;
        }
        StdOut.println(sum);
    
  12. Declare an integer variable name myint and give it the value 127.
  13. Write a for-loop that prints out the numbers from 2000 to 150 in descending order.
  14. Examine the following code snippet. Why don't we see a black rectangle on the screen?
      Picture p = new Picture(200,200);
      Graphics g = p.getGraphics();
      g.setColor(Color.black);
      g.fillRect(0,200,50,50);
      p.repaint();
    

    The answer is not because we forgot the import statement or main program. Assume that all that stuff is there and works.

  15. Write some Java code that prompts for and reads three integers from the console and prints the average to one decimal place. For example, if I entered 33, 12, and 17 your code should print 20.6.
  16. Do the same thing as above but print the average rounded to the nearest decimal place. (Hint: Use the function rint in the Math class). For example, if I entered 33, 12, and 17 your code should print 20.7.
  17. Assume that the variables x, y, theta, and length are all declared to be integer variables. Write a Java code snippet that draws a red line that starts at point (x,y), is theta degrees from horizontal, and is length pixels long. (Hint: recall the clock example we did in class). Use the picture below as a guide.
  18. Which of the following are legal Java identifiers (circle your answers).
    hello		  count		 5times		void		pepper2
    
    iden_tifier	  APPLES		"x"         do-it
    
  19. What gets printed by the Java code fragment below? Show your work.
     int x = 7;
     int y = 9;
     int z = x + y / 4 * (x - 2);
     StdOut.println(z);