Java Reference
In-Depth Information
14. Write code to produce a cumulative product by multiplying together many numbers that are read from the console.
15. The following expression should equal 6.8 , but in Java it does not. Why not?
0.2 + 1.2 + 2.2 + 3.2
16. The following code was intended to print a message, but it actually produces no output. Describe how to fix the
code to print the expected message.
double gpa = 3.2;
if (gpa * 3 == 9.6) {
System.out.println("You earned enough credits.");
}
Section 4.3:Text Processing
17. What output is produced by the following program?
1 public class CharMystery {
2
public static void printRange( char startLetter, char endLetter) {
3
for ( char letter = startLetter; letter <= endLetter; letter++) {
4
System.out.print(letter);
5
}
6
System.out.println();
7
}
8
9
public static void main(String[] args) {
10
printRange('e', 'g');
11
printRange('n', 's');
12
printRange('z', 'a');
13
printRange('q', 'r');
14
}
15 }
18. Write an if statement that tests to see whether a String begins with a capital letter.
19. What is wrong with the following code, which attempts to count the number occurrences of the letter 'e' in a
String , case-insensitively?
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i).toLowerCase() == 'e') {
count++;
}
}
20. Consider a String stored in a variable called name that stores a person's first and last name (e. g., “Marla Singer”).
Write the expression that would produce the last name followed by the first initial (e. g., “Singer, M.”).
Search WWH ::




Custom Search