-
Notifications
You must be signed in to change notification settings - Fork 1
Module 5 Mastery Check
wolvesled edited this page Sep 1, 2013
·
1 revision
- Show two way to declare a one-dimensional array of 12 doubles. Answer:
double d[] = new double[12]; double[] d = new double[12]; - Show how to initialize a one-dimensional array of integers to the values 1 through 5. Answer:
int[] i = { 1, 2, 3, 4, 5 }; - Write a program that uses an array to find the average of 10 double values. Use any 10 values you like.
- Change the sort in Project 5-1 so that it sorts an array of strings. Demonstrate that it works.
- What is the difference between the String methods indexOf() and lastIndexOf()? Answer: indexOf() returns the index of first occurring match of the given string against the testing string, lastIndexOf() returns the index of last occurring match of the given string against the testing string, both return 0 if doesn't find a match.
- Since all string are objects of type String, show how you can call the length() and charAt() methods on this string literal: "I like java". Answer:
"I like java".length(); "I like java".charAt(5); - Expanding on the Encode cipher class, modify it so that it uses an eight-character string as the key.
- Can the bitwise operators be applied to the double type? Answer: no.
- Show how this sequence can be rewritten using the ? operator.
if(x < 0) y = 10; else y = 20;Answer:y = x < 0 ? 10 : 20; - In the following fragment, is the & a bitwise or logical operator? Why?
boolean a, b; ... if(a & b) ...Answer: no, bitwise cannot apply to boolean type. - Is it an error to overrun the end of an array? Is it can error to index an array with a negative value? Answer: yes, both produce an error.
- What is the unsigned right-shift operator? Answer: >> is, it will bring an 0 at leftmost bit position each time on positive numbers, and bring a 1 at leftmost bit on negative numbers, because the sign in negative number takes 1 in the leftmost bit.
- Rewrite the MinMax class shown earlier in this chapter so that it uses a for-each style for loop.
- Can the for loops that perform sorting in the Bubble class shown in Project 5-1 be converted into a for-each style loops? If not, why not? Answer: it is not possible, the for-each loop is read-only for the iterated collection set, but in bubble sort, it needs to modify the value of the collection which is impossible for the for-each style loop.