50 Ch12 Quiz

  1. The human types the area of a square. The computer responds with the side length.

     What is the area of your square?
     123.45
     Side length:
     11.110805551354051
    

    Starter code:

     Scanner sc; /* ... setup code is omitted ... */
     System.out.println("What is the area of your square?");
    
  2. Get a single word from the user and print out “The word xx contains yy letters”.

     Enter a word:
     pinochle
     The word pinochle contains 8 letters.
    
  3. What is the output?

     String c = "Lao-Tsu";
     boolean d = c.equals("lao-tsu");
     System.out.println(d);
    
  4. The code below runs after password is read from a Scanner connected to the keyboard. It is intended to set access to true when an appropriate password is given.

     boolean access = (password == "lithium");
    
    1. Typing “LITHIUM” as the password will set access to true.
    2. Typing “lithium” as the password will set access to true.
    3. The code does not work as intended.
    4. The code does not compile.
  5. The code below is intended to set hovering to true whenever height is 6.

     int height; 
     // code omitted
     boolean hovering = height.equals(6);
    
    1. Setting height to 6 will set hovering to true.
    2. Setting height to 7 will set hovering to true.
    3. The code does not work as intended, height should be a double.
    4. The code does not compile.
  6. Write a program that reads in an integer that the human types and prints out “XX is a multiple of 5” when they type a multiple of 5.

  7. Letter shuffle. Given two Strings a and b of the same length, create a String ans which contains the first letter from a followed by the first letter from b followed by the second letter from a and b, etc. Ignore the case when a and b are not the same length.

    Example: given that a = “dog” and x = “123”, the answer is “d1o2g3”.

     String a, b, ans; 
     /* ... omitted code to set a, b... */
    
  8. Given a number n, return true if n is in the range 10..20, inclusive. Unless outsideMode is true, in which case return true if the number is less or equal to 10, or greater or equal to 20.

     in10To20(15, false) → true
     in10To20(21, false) → false
     in10To20(21, true) → true
    
     public static boolean in10To20(int n, boolean outsideMode)