Lecture 14: Problem Solving, Arrays, and Topic Review


An Optimal Strategy For Nim

  - designing an optimal strategy for playing Nim
  - get a strategy and show why it's optimal
  - write down in comments the exact steps for performing that strategy
  - put that strategy into code
  - emphasize the use of logical structures: AND, OR, and NOT

  - go over topics for the midterm

Exam Format

You will have an entire normal class period to complete the exam. There will be 3 types of questions. Some will be of a form similar to true/false questions. Some will be of the form "what does this program output". For some you will have to write out code for small programs.

For the exam you will be able to bring in one normal-sized (8 1/2" x 11") page of notes. The notes can be on both sides of that page, but everything must be written by hand. No photo-copying. No typing.

Practice Questions

  1. What is the output of the following program? (If the program would crash and not run, state that instead with a brief explanation why.)
       public static void main(String[] args){
    	   
          String word1 = "hip-hop";
          String word2 = "hip";
          word2 = word2 + "-hop";
    		  
          if(word1 == word2)
             System.out.println("The two strings are equal.");
          else
             System.out.println("The two strings are not equal.");
       }
    	
  2. What is the output of the following program? (If the program would crash and not run, state that instead with a brief explanation why.)
       public static void main(String[] args){
    	   
          int num1 = 0;
          int num2 = 1;
          int num3 = 2;
    		
    		
          if(num1 > num2 > num 3)
             System.out.println("The numbers are in order.");
          else
             System.out.println("The numbers are out of order.");
       }
    	
  3. What is the output of the following program? (If the program would crash and not run, state that instead with a brief explanation why.)
       public static void main(String[] args){
    
          int num1 = 0;
    		
          for(int i=0; i < 128; i++){
             num1++;
          }
    	
          System.out.println("After incrementing " + i + " times, the value of num1 is: "+num1);
       }
    	
  4. Fill in the method below so that it returns the lowest value of the three input parameters.
       public static int findLowest(int num1, int num2, int num3){
    
       }