CSC 211 -- Java Programming I

Practice Midterm Exam C

Work only 5 out of 6 problems. If you choose problem 1, answer all three questions.

  1. Answer the following three questions.
    1. What is the output produced by the following code fragment?
      int x = 107, y = 0;
      while(x > 0)
      {
          y++;
          x /= 5;
      }
      System.out.println(y);

    2. Rewrite the code fragment in Part 1a using a for loop instead.

    3. What would happen in Part 1a if the line
      while(x > 0)
      were changed to
      while(x > 0);

  2. Construct the program trace table and predict the output.
    public static void main(String[] args)
    {
        int x = 2, y = 7;
        String a = "abc";
        while(x + y < 100)
        {
           x += (2 * x + y);
           y += (x + 2 * y);
           a = y + a;
           if(x + y > 50)
              a = a + x;
        }
        System.out.println(x + " " + y + " " + a);
    }

  3. Write a Java program that will input an amount in Francs and output the amount in Marks. Assume the exchange rate 6.5 Francs per Dollar and 2.1 Marks per Dollar.

  4. Write a Java program that will input (i) the number of hours worked and (ii) the hourly wage. It should output the total pay for that person, which is calculated as follows: hourly wage times number of hours up to 40 plus 1.5 times hourly wage times number of hours over 40.

  5. Write a Java program that will input a list of nonnegative integers. (Stop when a negative integer is entered.) Output the largest odd integer and the largest even integer.