Exam 5: Program Logic and Indefinite Loops
Exam 1: Introduction to Java Programming8 Questions
Exam 2: Primitive Data and Definite Loops7 Questions
Exam 3: Introduction to Parameters and Objects8 Questions
Exam 4: Conditional Execution8 Questions
Exam 5: Program Logic and Indefinite Loops8 Questions
Exam 6: File Processing7 Questions
Exam 10: Arraylists7 Questions
Exam 11: Java Collections Framework6 Questions
Exam 13: Searching and Sorting8 Questions
Select questions type
Programming
Write a static method named printTwoDigit that accepts a Random object and an integer n as parameters and that prints a series of n randomly generated two-digit numbers. The method should use the Random object to select numbers in the range of 10 to 99 inclusive where each number is equally likely to be chosen. After displaying each number that was produced, the method should indicate whether the number 42 was ever selected ("we saw a 42!") or not ("no 42 was seen."). You may assume that the value of n passed is at least 0.
The following table shows two sample calls and their output:
Call Random r= new Random (); print TwoDigit (r,4); Random r= new Random ( ): Orint TwoDigit (r,7); Output next =52 next =83 next =10 next =29 next =96 next =42 next =86 next =22 no 42 was seen. next =91 next =36 next =73 we saw a 42!
Free
(Essay)
4.9/5
(38)
Correct Answer:
(one solution shown)
public static void printTwoDigit(Random r, int n) {
boolean seen42 = false;
for (int i = 1; i <= n; i++) {
int number = r.nextInt(90) + 10;
System.out.println("next = " + number);
if (number == 42) {
seen42 = true;
}
}
if (seen42) {
System.out.println("we saw a 42!");
} else {
System.out.println("no 42 was seen.");
}
}
While Loop Simulation
For each call below to the following method, write the value that is returned:
public static int mystery(int x) {
int a = 1;
int c = 0;
while (x > 0) {
a = x % 2;
if (a == 1) {
c++;
}
x = x / 2;
}
return c;
}
mystery (2); mystery (-1); mystery (7); mystery (18); mystery (43);
Free
(Essay)
4.9/5
(48)
Correct Answer:
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 p = "cause";
String q = "support";
String r = "troops";
String support = "hillary";
String cause = "rudy";
troops(p, q, r);
troops(q, r, p);
troops(support, p, cause);
troops(r, "p", support);
troops(q, "cause", q);
}
public static void troops(String r, String p, String q) {
System.out.println(q + " gave " + r + " to the " + p);
}
}
Free
(Essay)
4.8/5
(31)
Correct Answer:
troops gave cause to the support
cause gave support to the troops
rudy gave hillary to the cause
hillary gave troops to the p
support gave support to the cause
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 mystery(int n) {
System.out.print(n + " ");
if (n > 10) {
n = n / 2;
} else {
n = n + 7;
}
if (n * 2 < 25) {
n = n + 10;
}
System.out.println(n);
}
mystery(40); mystery(8); mystery(0); mystery(12); mystery(20);
(Essay)
4.9/5
(36)
Expressions
For each expression in the left-hand column, indicate its value in the right-hand column.
Be sure to list a constant of appropriate type.
(e.g., 7.0 rather than 7 for a double, Strings in quotes, true or false for a boolean).
3+4*5/2 13\%5+43\%(11\%3) 1.5*3.0+25.0/10.0 7/2!=123/12\%7 5/2+123/10/10.0 5+2+"(1+1)"+4+2*3
(Essay)
4.8/5
(38)
Programming
Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4.
For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on the method. You are not allowed to use a String to solve this problem. You may assume that the value passed to the method is greater than or equal to 0.
Call Returns consecutiveDigits (0) 1 consecutiveDigits (18) 1 consecutiveDigits (394) 1 consecutiveDigits (99) 2 consecutiveDigits (8229) 2 Call Returns consecutiveDigits (8823) 2 consecutiveDigits (777) 3 consecutiveDigits (82888) 3 consecutiveDigits (7111171) 4 consecutiveDigits (233333888) 5
(Essay)
4.9/5
(29)
Programming (15 points)
In this question, we'll address the following problem: Can a cash register containing a given amount of pennies (1-cent coins) and a given amount of nickels (5-cent coins) give a customer a given exact amount of cents of change? For example, if there are 3 pennies and 5 nickels in the cash register, is it possible to give exactly 19 cents of change? (No.) If there are 2 pennies and 7 nickels in the register, is it possible to give exactly 26 cents of change? (Yes.)
Write a static method named canMakeChange that accepts three integer parameters representing the number of pennies in the cash register, the number of nickels in the cash register, and the desired amount of change to make. The method should return true if the coins in the register can produce this exact amount of change, and false if not. The coins in the register must be able to exactly produce the desired amount of change in order to return true; for example, if the register contains 0 pennies and 100 nickels, it is not able to exactly produce 8 cents of change.
The following are several sample calls to your method and the values they should return. You may assume that no negative parameter values are passed, but otherwise your method should work with any values passed.


(Essay)
4.8/5
(45)
Assertions
For the following method, identify each of the three assertions in the table below as being either ALWAYS true, NEVER true or SOMETIMES true / sometimes false at each labeled point in the code. (can abbreviate as A/N/S)
public static int threeHeads() { // Counts coin tosses till we get heads 3x in a row.
Random rand = new Random();
int flip = 1;
int heads = 0;
int count = 0;
// Point A
while (heads < 3) {
// Point B
flip = rand.nextInt(2); // flip coin
if (flip == 0) { // heads
heads++;
// Point C
} else { // tails
// Point D
heads = 0;
}
count++;
}
// Point E
return count;
}
flip==0 heads ==0 flip > heads Point A Point B Point C Point D Point E
(Essay)
4.7/5
(37)
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)