Deck 11: Java Collections Framework

Full screen (f)
exit full mode
Question
Expressions
For each expression at left, indicate its value in the right column. List a value of appropriate type and capitalization.
e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.
 Expression  Value 823+(51)      102+"6"+325+(333)+9      19÷8=3&&2=7/3      1.0/2+(4.51.5)7/2      \begin{array}{l}\underline{ \text { Expression } }& \underline{ \text { Value }} \\8-2 * 3+(5-1) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\10-2+" 6 "+3 * 25+(33-3)+9 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\19 \div 8=3 \& \& 2=7 / 3 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\1.0 / 2+(4.5-1.5)-7 / 2 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \end{array}
Use Space or
up arrow
down arrow
to flip the card.
Question
If/Else Simulation
For each call below to the following method, write the output that is produced, as it would appear on the console:
public static void ifElseMystery(int a, int b) {
if (a % 2 != 0) {
a = a * 2;
}
if (a > 10) {
b++;
} else if (a < 10) {
a--;
b--;
}
System.out.println(a + " " + b);
}
 Method Call  Output  ifelseMystery (12,12)      iftlsemystery (7,4)      ifelseMystery (5,8)      ifElseMystery (3,42)     \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Output }} \\\text { ifelseMystery }(12,12) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }}\\\text { iftlsemystery }(7,4) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }} \\\text { ifelseMystery }(5,8) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }} \\\text { ifElseMystery }(3,42) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }}\end{array}
Question
Parameter Mystery
At the bottom of the page, write the output produced by the following program, as it would appear on the console:
public class ParameterMystery {
public static void main(String[] args) {
String butters = "kenny";
String friends = "butters";
String kenny = "friends";
String kyle = "stan";
String ike = "cartman";
stotch(kenny, friends, butters);
stotch(ike, friends, kyle);
stotch(kyle, ike, "ike");
stotch(friends, "kyle", kenny);
}
public static void stotch(String kenny, String butters, String friends) {
System.out.println(butters + " is " + friends + " with " + kenny);
}
}
Question
Assertions
For each of the five points labeled by comments, identify each of the assertions in the table below as either being always true, never true, or sometimes true / sometimes false. (You may abbreviate them as A, N, or S.)
public static int funky(int a, int digit) {
int count = 0;
// Point A
while (a != 0) {
// Point B
if (a % 10 == digit) {
count++;
// Point C
} else if (count > 0) {
count--;
// Point D
}
a = a / 10;
}
// Point E
return count;
}
a=0 a =10== digit  count >0 Point A  Point B  Point C  Point D  Point E \begin{array}{l|l|l|l} & a=0 & \text { a }=10==\text { digit } & \text { count }>0 \\\hline \text { Point A } & & & \\\hline \text { Point B } & & & \\\hline \text { Point C } & & & \\\hline \text { Point D } & & & \\\hline \text { Point E } & & & \\\hline\end{array}
Question
Programming
Write a method named lucky that accepts an integer parameter min and rolls a 6-sided die until it gets four consecutive rolls in a row that have values of min or less. As the method rolls the die it should print each value rolled, and then it should print a message at the end to indicate how many rolls were made. For example, the call of lucky(3); should print output such as the following (though the output would be different on every call because of randomness). Notice that the method stops after rolling 3, 2, 1, and 2 consecutively because these are all values of 3 or less:
Programming Write a method named lucky that accepts an integer parameter min and rolls a 6-sided die until it gets four consecutive rolls in a row that have values of min or less. As the method rolls the die it should print each value rolled, and then it should print a message at the end to indicate how many rolls were made. For example, the call of lucky(3); should print output such as the following (though the output would be different on every call because of randomness). Notice that the method stops after rolling 3, 2, 1, and 2 consecutively because these are all values of 3 or less:   5 2 4 6 1 3 5 5 3 2 1 2 Finished after 12 rolls. A call of lucky(5); should print output such as the following. Notice that it continues rolling until it rolls four consecutive dice rolls produce a value of 5 or less. In this example, those values are 3, 5, 5, and 4. 1 3 6 3 5 5 4 Finished after 7 rolls. You may assume that the parameter value passed will be between 1 and 6 inclusive.<div style=padding-top: 35px> 5 2 4 6 1 3 5 5 3 2 1 2
Finished after 12 rolls.
A call of lucky(5); should print output such as the following. Notice that it continues rolling until it rolls four consecutive dice rolls produce a value of 5 or less. In this example, those values are 3, 5, 5, and 4.
1 3 6 3 5 5 4
Finished after 7 rolls.
You may assume that the parameter value passed will be between 1 and 6 inclusive.
Question
Programming
Write a method named invest that computes the interest earned on a financial investment. Once per year, the bank pays interest to the investor at a given fixed rate. As the investment earns interest, that interest is added to the investment and therefore the interest begins earning interest of its own ("compounding").
Your method should accept three parameters: the initial amount of the investment in dollars (as a real number such as 1500.0 for $1,500.00), the yearly interest rate (as a real number such as 3.5 for 3.5% interest) and the number of years for which to invest (as an integer such as 6 for 6 years).
Your method should print the value of the investment after each year, as well as the total amount of interest earned over all years. For example, an investment of $100.00 at 10% interest for 5 years would lead to this call:
invest(100.00, 10.0, 5); // $100.00 at 10% interest for 5 years
The call would produce the following console output. Dollar amounts print with 2 digits after the decimal.
After year 1: $110.00
After year 2: $121.00
After year 3: $133.10
After year 4: $146.41
After year 5: $161.05
Total interest earned: $61.05
Notice that we are not simply adding 10% of $100.00 (or $10.00) each year; that would lead to $50 of total interest earned. The first year adds 10% of $100.00, but the second year adds 10% of $110.00, or $11.00, leading to a total of $121.00. The third year adds 10% of $121.00, or $12.10, leading to a total of $133.10. The interest is cumulative.
You may assume that all parameter values passed are non-negative.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/6
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 11: Java Collections Framework
1
Expressions
For each expression at left, indicate its value in the right column. List a value of appropriate type and capitalization.
e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.
 Expression  Value 823+(51)      102+"6"+325+(333)+9      19÷8=3&&2=7/3      1.0/2+(4.51.5)7/2      \begin{array}{l}\underline{ \text { Expression } }& \underline{ \text { Value }} \\8-2 * 3+(5-1) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\10-2+" 6 "+3 * 25+(33-3)+9 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\19 \div 8=3 \& \& 2=7 / 3 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\1.0 / 2+(4.5-1.5)-7 / 2 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \end{array}
 Expression  Value 823+(51)6102+"6"+325+(333)+9"8675309"19÷8=3&&2=7/3true1.0/2+(4.51.5)7/20.5\begin{array}{l}\underline{ \text { Expression } }& \underline{ \text { Value }} \\8-2 * 3+(5-1) & 6 \\10-2+" 6 "+3 * 25+(33-3)+9 & "8675309"\\19 \div 8=3 \& \& 2=7 / 3 & true\\1.0 / 2+(4.5-1.5)-7 / 2 & 0.5\end{array}
2
If/Else Simulation
For each call below to the following method, write the output that is produced, as it would appear on the console:
public static void ifElseMystery(int a, int b) {
if (a % 2 != 0) {
a = a * 2;
}
if (a > 10) {
b++;
} else if (a < 10) {
a--;
b--;
}
System.out.println(a + " " + b);
}
 Method Call  Output  ifelseMystery (12,12)      iftlsemystery (7,4)      ifelseMystery (5,8)      ifElseMystery (3,42)     \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Output }} \\\text { ifelseMystery }(12,12) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }}\\\text { iftlsemystery }(7,4) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }} \\\text { ifelseMystery }(5,8) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }} \\\text { ifElseMystery }(3,42) \text {; } & \underline{\text { }} \underline{\text { }}\underline{\text { }}\underline{\text { }}\underline{\text { }}\end{array}
 Method Call  Output  ifelseMystery (12,12)12  13 iftlsemystery (7,4)14  5 ifelseMystery (5,8)10  8 ifElseMystery (3,42)5  43\begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Output }} \\\text { ifelseMystery }(12,12) \text {; } & 12 \text { } \text { } 13\\\text { iftlsemystery }(7,4) \text {; } & 14 \text { } \text { } 5\\\text { ifelseMystery }(5,8) \text {; } & 10 \text { } \text { } 8\\\text { ifElseMystery }(3,42) \text {; } & 5 \text { } \text { } 43\end{array}
3
Parameter Mystery
At the bottom of the page, write the output produced by the following program, as it would appear on the console:
public class ParameterMystery {
public static void main(String[] args) {
String butters = "kenny";
String friends = "butters";
String kenny = "friends";
String kyle = "stan";
String ike = "cartman";
stotch(kenny, friends, butters);
stotch(ike, friends, kyle);
stotch(kyle, ike, "ike");
stotch(friends, "kyle", kenny);
}
public static void stotch(String kenny, String butters, String friends) {
System.out.println(butters + " is " + friends + " with " + kenny);
}
}
butters is kenny with friends
butters is stan with cartman
cartman is ike with stan
kyle is friends with butters
4
Assertions
For each of the five points labeled by comments, identify each of the assertions in the table below as either being always true, never true, or sometimes true / sometimes false. (You may abbreviate them as A, N, or S.)
public static int funky(int a, int digit) {
int count = 0;
// Point A
while (a != 0) {
// Point B
if (a % 10 == digit) {
count++;
// Point C
} else if (count > 0) {
count--;
// Point D
}
a = a / 10;
}
// Point E
return count;
}
a=0 a =10== digit  count >0 Point A  Point B  Point C  Point D  Point E \begin{array}{l|l|l|l} & a=0 & \text { a }=10==\text { digit } & \text { count }>0 \\\hline \text { Point A } & & & \\\hline \text { Point B } & & & \\\hline \text { Point C } & & & \\\hline \text { Point D } & & & \\\hline \text { Point E } & & & \\\hline\end{array}
Unlock Deck
Unlock for access to all 6 flashcards in this deck.
Unlock Deck
k this deck
5
Programming
Write a method named lucky that accepts an integer parameter min and rolls a 6-sided die until it gets four consecutive rolls in a row that have values of min or less. As the method rolls the die it should print each value rolled, and then it should print a message at the end to indicate how many rolls were made. For example, the call of lucky(3); should print output such as the following (though the output would be different on every call because of randomness). Notice that the method stops after rolling 3, 2, 1, and 2 consecutively because these are all values of 3 or less:
Programming Write a method named lucky that accepts an integer parameter min and rolls a 6-sided die until it gets four consecutive rolls in a row that have values of min or less. As the method rolls the die it should print each value rolled, and then it should print a message at the end to indicate how many rolls were made. For example, the call of lucky(3); should print output such as the following (though the output would be different on every call because of randomness). Notice that the method stops after rolling 3, 2, 1, and 2 consecutively because these are all values of 3 or less:   5 2 4 6 1 3 5 5 3 2 1 2 Finished after 12 rolls. A call of lucky(5); should print output such as the following. Notice that it continues rolling until it rolls four consecutive dice rolls produce a value of 5 or less. In this example, those values are 3, 5, 5, and 4. 1 3 6 3 5 5 4 Finished after 7 rolls. You may assume that the parameter value passed will be between 1 and 6 inclusive. 5 2 4 6 1 3 5 5 3 2 1 2
Finished after 12 rolls.
A call of lucky(5); should print output such as the following. Notice that it continues rolling until it rolls four consecutive dice rolls produce a value of 5 or less. In this example, those values are 3, 5, 5, and 4.
1 3 6 3 5 5 4
Finished after 7 rolls.
You may assume that the parameter value passed will be between 1 and 6 inclusive.
Unlock Deck
Unlock for access to all 6 flashcards in this deck.
Unlock Deck
k this deck
6
Programming
Write a method named invest that computes the interest earned on a financial investment. Once per year, the bank pays interest to the investor at a given fixed rate. As the investment earns interest, that interest is added to the investment and therefore the interest begins earning interest of its own ("compounding").
Your method should accept three parameters: the initial amount of the investment in dollars (as a real number such as 1500.0 for $1,500.00), the yearly interest rate (as a real number such as 3.5 for 3.5% interest) and the number of years for which to invest (as an integer such as 6 for 6 years).
Your method should print the value of the investment after each year, as well as the total amount of interest earned over all years. For example, an investment of $100.00 at 10% interest for 5 years would lead to this call:
invest(100.00, 10.0, 5); // $100.00 at 10% interest for 5 years
The call would produce the following console output. Dollar amounts print with 2 digits after the decimal.
After year 1: $110.00
After year 2: $121.00
After year 3: $133.10
After year 4: $146.41
After year 5: $161.05
Total interest earned: $61.05
Notice that we are not simply adding 10% of $100.00 (or $10.00) each year; that would lead to $50 of total interest earned. The first year adds 10% of $100.00, but the second year adds 10% of $110.00, or $11.00, leading to a total of $121.00. The third year adds 10% of $121.00, or $12.10, leading to a total of $133.10. The interest is cumulative.
You may assume that all parameter values passed are non-negative.
Unlock Deck
Unlock for access to all 6 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 6 flashcards in this deck.