CS 140 - Exam 2 Study Guide Answers

Most of this study guide is the same as the quiz 2 study guide. I've added some questions for material covered after quiz 2.
  1. Make sure that you can write methods to do all of the following operations on pictures: flipHorizontal, flipVertical, rotateLeft, rotateRight, shrink, and enlarge. Make sure that you can open a picture and call these from the main method.

    Same as quiz 2 study guide

  2. Write a Java code segment that prompts for and reads an integer from the user and prints yes if the number is divisible by 173.
    Scanner s = new Scanner(System.in);
    int x = s.nextInt();
    if (x % 173 == 0) {
       System.out.println("yes");
    }
    
  3. Write a Java code segment that prompts for and reads an integer from the user and prints yes if the number is between 70 and 100, and no if it is not.

    Same as quiz 2 study guide

  4. Write a Java code segment that prompts for and reads an integer x from the user and prints x asterisks on the console.

    Same as quiz 2 study guide

  5. Write a Java code segment that determines the largest value of three integer variables, x, y, and z.

    Same as quiz 2 study guide

  6. Write a complete Java program that uses a for loop to read ten integers from the user and computes the average of the integers.
    Scanner s = new Scanner(System.in);
    int sum = 0;
    for (int i = 0; i < 10; i++) {
       int x = s.nextInt();
       sum = sum + x;
    }
    double average = sum / 10.0;
    
  7. Write a complete Java program that allows the user to choose two colors from an RGB color pallette (hint use ColorChooser.pickAColor()) and prints out the distance between those two colors.
    See your last lab.
    
  8. Write a Java expression to generate a random integer between -180 and 180 inclusive and store the integer in the variable x. Inclusive means you should include -180 and 180 as values.
    (int) (Math.random() * 361) - 180
    
  9. Write a Turtle method named colorfulCircle() causes a turtle to turn in a circle and that, when every time it moves, changes its pen color to a random color.
      public void colorfulCircle() {
        for (int i = 0; i < 18; i++) {
           forward(10);
           int r = (int) (Math.random() * 256);
           int g = (int) (Math.random() * 256);
           int b = (int) (Math.random() * 256);
           setPenColor(new Color(r,g,b));       
           turn(20);
        }
      }
    
  10. Assume a, b, and c are integer variables. If there is something wrong with the statements below indicate what the problem is.
       a) if a > b {
             c = b;
          }
           
       b) if (a > b) {
             c = b
          } 
          
       c) if (a > b {
             c = b;
             System.out.println("a is bigger than b");
          }
          else 
          
       d) if (a > b) { c = b; } 
    

    See solution for quiz 2 study guide.

  11. The operator ! is logical negation. So !true is the same as false and !false is true. The precedence of &&, ||, and ! is that && has the highest precedence, then ||, and then !. As an example consider the expression below:
      int x = 5;
      int y = 7;
      System.out.println( x > y || !(3 > 4) && 5 <= 6 );
    

    The first condition checked is if x > y. This is false. Next we check if !(3 > 4). This is true, 3 is not greater than 4. Now we check if 5 is less than 6. This is true. Both sides of && are true so it evaluates to true.

    Assuming that x is 1 What is the output of the following:

        int x = 1;
        System.out.println((true) && (3 > 4));       
        System.out.println(!(x > 0) && (x > 0));
        System.out.println((x > 0) || (x < 0));
        System.out.println(x != 0 || x == 0);
        System.out.println(x >= 0 || x < 0);
        System.out.println((x != 1) == !(x == 1));
    	

    See solution for quiz 2 study guide.

  12. What is the output of the following code? Try and figure it out by thinking it through. Verify your answer by cutting and pasting it into a program.
      int x = 3;
      int y = 2;
      int z = 0;
      
      if (x > 2) {
        if (y > 2) {
           z = x + y;
           System.out.println("z is " + z);
        }
      }
      else {
        System.out.println("x is " + x);
      }
    

    See solution for quiz 2 study guide.

  13. In the above question what is the output if x is 3 and y is 4?

    See solution for quiz 2 study guide.

  14. In the above question what is the output if x is 2 and y is 2?

    See solution for quiz 2 study guide.

  15. Print the numbers from 1 to 100 that are divisible by 3 or 11.

    See solution for quiz 2 study guide.

  16. Study the code segment below and figure out what it does without running it.
        World w = new World();
        Turtle t = new Turtle(w);
        
        t.penUp();
        t.moveTo(w.getWidth(),w.getHeight());
        t.turnToFace(0,0);
        t.penDown();
        int v = 0;
        for (int i = 0; i < 500; i++) {
           t.forward(2);
           v = (v + 1) % 256;
           t.setPenColor(new Color(v,v,v));
        }
    

    This lays down a gray line going from the lower right to the upper left. The line starts black then gets progressively lighter until its white. Then the line gets black again and starts the process all over.