CS 140 - Test 1 Study Guide

Make sure review the homework assignments, quizzes, and all of the example programs we've developed in class.

  1. Write a complete Java program in a class named that prints Hello World to the console.
  2. Write a complete Java program that simulates tossing a coin until fifty heads occurs. Then print the total number of heads and tails.
  3. Write a complete Java program that simulates tossing a pair of dice until two ones are rolled. Have it print the total number of dice rolls that were attempted.
  4. Write a Java program that reads ten integers from the keyboard and prints their average to the console.
  5. 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;
      
  6. What does the following Java statement do?
       StdOut.println("What do" + 1 + (2 + 3) + 4 + " I print?");
    
  7. What is the output of the following Java code fragment?
       int i;
       int j;
        
       i = 0;    
       while (i < 5) {
         j = 1;
         while (j < i+1) {
           StdOut.print(j);
           j = j + 1;
         }
         StdOut.println();
         i = i + 1;
       }
    
  8. What does the following code segment print to the console?
       int sum;
       int i;
       sum = 0; 
       i = 0;
       while (i < 12) {
         sum = sum + i;
         i = i + 2;
       }
       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                          ______________________________________________
    
        void run() {                              ______________________________________________
         
          int XXXX;                               ______________________________________________
          
          XXXX = 7;                               ______________________________________________
    
          int pi;                                 ______________________________________________
          
          pi = 3.14159;                           ______________________________________________
    
          StdOut.println(XXXX);                   ______________________________________________
    
          Picture p;                              ______________________________________________
          
          p = new Picture();                      ______________________________________________
    
          graphics g;                             ______________________________________________
    
          g  = getGraphics();                     ______________________________________________
    
          g.fillOval(XXXX, XXXX*100, 20);         ______________________________________________
       
          int sum = 0;                            ______________________________________________
     
          while (sum < 10 {                       ______________________________________________
    
              sum = sum + I                       ______________________________________________
    
          }                                       ______________________________________________
    
          StdOut.println(sum);                    ______________________________________________
    
        }                                         ______________________________________________
    
        public static void main(String [] args) {
          Program p = new Program();
          p.run();
        }
      }
    
  11. Assume we were to execute the following code segment. What would it print?
        int sum;
        sum = 0;
        i = 4;
        while (i <= 8) {
           sum = sum * i;
        }
        StdOut.println(sum);
    
  12. Declare an integer variable name myint and give it the value 127.
  13. Write a while-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.

  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. Which of the following are legal Java identifiers (circle your answers).
    hello		  count		 5times		int		 pepper2        while
    
    iden_tifier	  APPLES		&*(         do-it        if        else    
    
  18. 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);
    
  19. What is an actual parameter? Give an example of one.
  20. What is a formal parameter? Give an example of one.
  21. What is the scope of a variable?
  22. What is a class method? Give an example of one you have used.
  23. What is an object method? Give an example of one you have used.
  24. What is a function method? Give an example of one you have used.