BPJ Quiz 11+17 B
-
Example: Counting from 17 to 100 by 5’s gives 17, 22, 27, 32, 37, 42, 47, etc. The first multiple of 7 in that list is 42.
Task: Ask the user for a number n. Counting from 1000 to 9000 by 13’s, print each number. Stop after printing the first multiple of n.
-
What does the following code segment print?
for(int k=0; k<30 ; k++) { if (k%10 == 0) System.out.println(k); if (k%13 != 0) continue; int n = (k-1)/2; System.out.println("Out: " + n); }
-
Give an example of values for
String a
andString b
so thata.compareTo(b)
returns a negative number? -
What is the sign (positive, negative, or zero) of
"aarhus".compareTo("aardvark")
? Bonus: what is the exact value? -
Read a word. If the word is “NUM” then the answer is the next number. Otherwise the answer is the length of the word you just read. If the first word is not “NUM” do not read the next word (there might not even be one).
Input: NUM 30 40 Output: 30 Input: APPLE Output: 5
The input to this function is the Scanner to use to get the words and numbers.
public static int wordNum (Scanner s) { // Your code replaces this body. return 0; }
-
Example. The sum of the indices that start the word “FUN” (see below) are 5 + 12 + 22 = 39.
0 1 2 0123456789012345678901234567 This FUN is FUNNY not FUNKY.
Task: Write code to find the sum of all of the indices that start the word “AP”. The input to this function is the String to use.
public static int apFind(String s) { // Your code goes replaces this body. return 0; }