You can access complete document on following URL.
Contact me if site not loaded
https://solumanu.com/product/solution-manual-for-data-structures-and-algorithms-in-java/
sm
Chapter 1 Questions
Dan Myers
1. Which of the following is false?
A. The range of a Java int is approximately -2.1 billion to +2.1 billion
B. A boolean can only take on the values true and false
tb9
C. A char represents a single 16-bit Unicode character
*D. 1 and 1.0 are equivalent in Java
2. I want to test if a number is divisible by both 7 and 11. Which of the following tests
accomplishes this goal?
*A. n % 7 == 0 && n % 11 == 0
B. n % 7 && n % 11== 0
8@
C. n % 7 && 11 == 0
D. n % 7 == 0 || n % 11 == 0
3. What will be the output of the following code fragment?
int a = 0;
int b = 10;
while (a < b) {
gm
int c = b - a;
while (c > a) {
c--;
a++;
}
}
System.out.println(b - a);
ail
A. 1
B. 0
*C. The loops never terminate
D. 10
.co
4. Consider the following method. What is h("SPIDER")?
public static String h(String s) {
String output = "";
for (int i = s.length() - 1; i > 0; i--) {
char ch = s.charAt(i);
output += ch;
m
Test Bank to Dan S. Myers, Data Structures and Algorithms in Java by Dan S. Myers
WhatsApp: https://wa.me/message/2H3BV2L5TTSUF1 - Telegram: https://t.me/solutionmanual
Contact me in order to access the whole complete document - Email: [email protected]
return output;
}
A. "SPIDER"
B. "REDIPS"
C. "SPIDE"
*D. "REDIP"
5. Consider the following while loop. Which of the given for statements will iterate
through the same range of values?
int a = 5;
while (a < 200) {
a = 2 * a + 1;
}
*A. for (int a = 5; a < 200; a = 2 * a + 1)
B. for (int a = 5; a <= 200; a = 2 * a + 1)
C. for (int a = 5; a < 200; a += 2 * a + 1)
D. for (int a = 200; a > 5; a = a / 2 - 1)
6. Which of the following logical statements always evaluates to true regardless of the
boolean values of a and b? Choose all that apply.
*A. a || b || (!a && !b)
*B. a || b || !(a || b)
*C. !a || !b || (a && b)
D. !a || !b || !(a && b)
7. I want to write a code fragment that tests if a certain character ch is in a String
named s. Which of the following tests will do that?
A. String.indexOf(ch) == -1
B. s.indexOf(ch) == 0
C. s.charAt(ch) == -1
*D. s.indexOf(ch) == -1
8. Which of the following single-line statements is equivalent to the code fragment
below?
double r = Math.random();
if (r < 1.0 / 3.0) {
return 10;
} else if (r < 2.0 / 3.0){
return 20;
} else {
return 30;
}
*A. return ((int) (Math.random() * 3) + 1) * 10;
B. return ((int) (Math.random() * 3)) * 10;
C. return ((int) (Math.random() * 30);
D. return (int) (Math.random() * 3) + 1 * 10;
9. Which of the following is not a valid variable name in Java? A.
tempInDegreesCelsius
B. temp_in_degrees_celsius
C. tempindegreescelsius
*D. TempInDegreesCelsius
10. Consider the following method. What is m(m(10))?
public static int m(int n) {
n = n / 2 % 3;
if (n % 2 == 0) {
n = n + 1;
}
return n;
}
A. 3
B. 10
*C. 1
D. 2
11. Which of the following errors exists in the following method?
public static double circleArea(double radius) {
double area = Math.PI * radius * radius;
System.out.println(area);
}
A. Math must be imported.
*B. The method does not return a double.
C. radius should be an int
D. The result of the calculation should not be double.
12. I used the Caesar cipher to encode the message "HELLOWORLD" as "LIPPSASVPH".
What is the rotation value?
A. 4
B. 30
C. 56
*D. Any of the above
13. How many times will the following loop execute before terminating?
int n = 6;
while (n != 1) {
if (n % 2 == 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
}
*A. 8
B. 9
C. The loop never terminates
D. 1
14. Consider the following method call. Which of the given signatures could be a
declaration for the associated method?
double a = f("ABC", 3.14, 1);
A. public static void f(String s, double d, int i)
B. public double f(String s, double d, int i)
C. public static double fun(String s, double d, int i)
*D. public static double f(String s, double d, int i)
15. Choose the statement that generates a random roll of a ten-sided die.
A. (int)(Math.random()) * 10 + 1
B. Math.random() * 10 + 1
*C. (int)(Math.random()) * 10) + 1
D. (int)(Math.random()) * 10)
16. Which of the following comparisons result in true for the strings below?
String s1 = "Java";
String s2 = s1;
A. s2 == s1 but not s2.equals(s1)
*B. s2 == s1 and s2.equals(s1)
C. s2.equals(s1) but not s2 == s1
D. Neither comparison results in true
17. What data type could you use to represent an integer with a magnitude of 4 billion?
A. int
B. double
C. float
*D. long
18. What will be printed by the following lines?
double area = (2.0 / 3.0) * (1.0 / 2.0);
System.out.printf("%.4f", area);
A. 0
*B. .3333
C. .6666
D. .0000
19. How many errors exist in the following declaration? Assume that the Scanner class
has been correctly imported at the top of the class.
Scanner s = new scanner()
A. 0
B. 1
C. 2
*D. 3
20. Suppose you need to write a program to convert from degrees Kelvin to degrees
Celcius. What is an appropriate data type to use for your degree measurements?
Choose all that apply.
A. int
*B. double
*C. float
D. long
21. What is the final number printed by the following code fragment?
for (int i = 100; i % 5 != 0; i -= 3) {
System.out.println(i);
}
A. 100
B. 94
*C. 88
D. 85
22. Which of the following is not a primitive type in Java?
A. short
B. byte
C. float
*D. String
23. What is the proper method to use when comparing String objects by value?
A. Use the == operator
*B. equals
C. areEqual
D. equal
24. Suppose that i has the value 12 after executing the statement below. Which of the
following could not have been its value before executing the statement?
i = (i / 2 + 5) / 3;
*A. 61
B. 62
C. 63
D. 64
25. Look at the following static method. What is returned when calling f(50)?
public static int f(int n) {
if (n % 10 == 0) {
if (n % 3 == 2) {
return n / 3;
} else {
return n / 2;
}
} else if (n % 2 == 0){
return n - 1;
} else {
return n;
}
}
A. 25
B. 0
*C. 16
D. 49