CS99

Fundamental Programming Concepts
Summer 2001

 

Prelim I: Solutions


  1. T
  2. T
  3. F
  4. T
  5. F
  6. F
  7. F
  8. T
  9. F
  10. T
  11. T
  12. F
  13. c)
  14. b)
  15. d)
  16. c)
  17. interpreters (or translators)
  18. semi-colon
  19. if-else
  20. comments
  21. direct sequencing, conditional branching, and conditional looping
  22. z is never updated in the body of the loop; it's an infinite loop if at the time of execution z actually is greater or equal to 0.
  23. a) int sum, x;
    b) String s = "Bob";
    c) sum = sum + x;
    d) System.out.println("The sum is: " + sum );
  24. CPU, Input, Output, Storage
  25. "=" assigns, "==" tests for equality
  26. An algorithm is a series of steps to solve a problem or task, while a program is an algorithm written in a way that a computer can understand.
  27. keyboard, mouse, scanner, touch screen, modem, fax machine
  28. Note:  there is more than one right answer.

    8 Add Broom.java to the project
    6 Delete Trivial Application
    3 In the New dialog, click on "Java Stationery" on the left side.  Enter Exam as the project name.
    7 Go to File->New, select "Text File", enter Broom.java as the file name.
    1 Start CodeWarrior
    5 Select the "Java Application" type and click OK.
    2 Go to the File menu and choose "New..."
    4 In the New Project dialog, select "JDK1.3".  Click on the '+' to the left of it, to see 4 more choices for the project type.

    The missing step was "Set Java Target Release Settings, main class to Broom"

  29. i. int x = 3;
    ii. if ( 7 - x == 4)  OR if ( x == 3 )
    iii. System.out.println("four is" + 4);  OR System.out.println("four is 4");
    iv. int x = 4; OR String x = "4";
    v. if (3 < 4 && 4 < 5)  OR if ( 3 < 4 || 4 < 5 )   //Note that both conditionals are equal to true always
    vi. System.out.println(" 4 is between 3 and 5 "); OR System.out.println( 4 + " is between " + 3 + " and " + 5 );
  30. Solution 1.
    while ( x < 0 ) {
        counter*=10;
        if (y < z)
            y++;
        else
            z=z*2;
        x = x + 2;
        w = w/7;
    }

    Solution 2.
    while ( x < 0 )
    {
        counter*=10;
        if (y < z)
            y++;
        else
            z=z*2;
        x = x + 2;
        w = w/7;
    }
  31. salesTax%, 52pickup and static are not legal (_class actually is)
  32. i. double
    ii. char
    iii. String
    iv. boolean
  33. i. boolean
    ii. illegal (variable c has not been declared; had it been declared, the expression would have been of type String)
    iii. illegal (cannot assign an int to a double)
    iv. int (dividing x by y, which are both ints, will result in an int expression, not a double)
    v. double (mixing doubles with ints nearly always results in the expression being promoted to a double)
    vi. illegal (cannot compare ints with booleans)
  34. 16.0   !72
    \9
    3
    "We're in the pipe, 5 by 5"
  35. a) product = 25, x = 6.
    b) sum = 2, y = 6.
    c) z = 27.
  36. Statement 1: x+=1;
    Statement 2: x = x + 1;
    Statement 3: x++;
    Statement 4: ++x;
  37. if ( Math.random() < 0.37 )
        System.out.println("Hi!");
    else
        System.out.println("Bye!");