50 Ch12 Quiz
-
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?");
-
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.
-
What is the output?
String c = "Lao-Tsu"; boolean d = c.equals("lao-tsu"); System.out.println(d);
-
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");
- Typing “LITHIUM” as the password will set access to true.
- Typing “lithium” as the password will set access to true.
- The code does not work as intended.
- The code does not compile.
-
The code below is intended to set hovering to true whenever height is 6.
int height; // code omitted boolean hovering = height.equals(6);
- Setting height to 6 will set hovering to true.
- Setting height to 7 will set hovering to true.
- The code does not work as intended, height should be a double.
- The code does not compile.
-
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.
-
Letter shuffle. Given two Strings
a
andb
of the same length, create a Stringans
which contains the first letter froma
followed by the first letter fromb
followed by the second letter froma
andb
, etc. Ignore the case whena
andb
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... */
-
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)