CS 122 -- Midterm of The E.R.
 
  1. Is your name on the test?
    1. Yes, it is.
    2. No, I'm not that kind of person

    3.  
  2. Assume this for loop is in a program.  What will it print?
  3. for(int i = 0; i < 10; i = i * 2) {
        System.out.println("The answer is " + i);
    }
  4. Write me a while loop (not a for loop) that prints the numbers from 1 ... 10.  Be sure to include the System.out.println command.  You don't need to include any "import" "class" or "public void main" lines.

  5.  

     
     
     
     
     
     
     

  6. What's the worst reason to go to the Emergency Room?
    1. Failure to wear a seatbelt
    2. Failure of the motorcycle's brakes
    3. Failure to stop drinking after the second bottle was finished
    4. Failure to listen to your parents

    5.  
  7. Write me two lines of code.  The should declare a variable names "patient"that is an array of 20 Strings.  The second should initalize the array.  It should include the word "new".

  8.  
  9. Write me an if statement that prints "Moxocilin Indicated" if the things below are true.  Otherwise, it prints "No Mox for you"
  10. (True/False) You cannot use Mergesort on strings.

  11.  
  12. (Queue/Stack)  I have a data structure.  I add a 3, add a 5, take a number, add a 7, and add a 12.  Then, when I take a number I get a 5.  Was my data structure a queue or a stack?

  13.  

     

  14. (2 points)  Write me a program that prints the following.  You must use a loop.  I don't care about the number format as long as the value is right.  Note there are 8 numbers there.  Show me the program working.

  15.  
    1
    0.1
    0.01
    0.001
    0.0001
    0.00001
    0.000001
    0.0000001
  16. (2 points) Find the file  http://euclid.nmu.edu/~randy/Classes/CS122/Array.java .  Where it says "code goes here" add code to print the largest value.  You must use a loop.  Show me the program working.  It's no fair to hardcode the correct answer (zero, as it happens).

  17.  
  18. (3 points) Use the same file.  Where it says "code goes here" add code to add the number "-5" in the 12th spot.  You must use a loop.  Show me the program working.  After your done, the array should look like:
  19. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    10 11 12 13 14 15 16 17 18 19 0 1 -5 23 34 4 5 6 7 8 9
  20. Suppose you have a string variable named "doctor".  What line of code would assign to the integer variable 'a' the length of the variable in the string 'doctor'?

  21. a = ___________________________________________________

  22. (3 points) Using the same file, write a program that prints the second smallest number.  You must use a loop.  Your program must work for any array data, not just the data I gave in this file.  (This is hard.)

  23.  
  24. Consider the following program.  What will it print?  (If you think you see a syntax error, come see me.
  25. class ER {
        public static void main(String args[]) {
             int data[] = new int[1000];
             data[1] = 3;  data[2] = 1; data[3] = 5;
             int i = 1;
             while (data[i] != 5) {

                  i = data[i];
                  System.out.println("Data was " + i);
             }
         }
    }