Deck 6: File Processing
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/7
Play
Full screen (f)
Deck 6: File Processing
1
Parameter Mystery
At the bottom of the page, write the output produced by the following program, as it would appear on the console.
(Though the program uses words related to arithmetic, the output does not necessarily follow the rules of addition.)
public class ParameterMystery {
public static void main(String[] args) {
String i = "j";
int j = -1;
int k = 2;
String x = "5";
int y = 7;
silly(k, i, j);
silly(y, x, k);
silly(k, "y", 4);
silly(j + 1, x + 1, j);
}
public static void silly(int k, String i, int j) {
System.out.println(j + " + " + k + " + " + i);
}
}
At the bottom of the page, write the output produced by the following program, as it would appear on the console.
(Though the program uses words related to arithmetic, the output does not necessarily follow the rules of addition.)
public class ParameterMystery {
public static void main(String[] args) {
String i = "j";
int j = -1;
int k = 2;
String x = "5";
int y = 7;
silly(k, i, j);
silly(y, x, k);
silly(k, "y", 4);
silly(j + 1, x + 1, j);
}
public static void silly(int k, String i, int j) {
System.out.println(j + " + " + k + " + " + i);
}
}
-1 + 2 + j
2 + 7 + 5
4 + 2 + y
-1 + 0 + 51
2 + 7 + 5
4 + 2 + y
-1 + 0 + 51
2
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.
public static int randomWalk(int steps) {
Random rand = new Random();
int x = 0;
int r = rand.nextInt(2);
// Point A
while (steps > 0 || x == 0) {
// Point B
if (r == 0) {
x++;
// Point C
} else {
x = 0;
// Point D
}
steps--;
r = rand.nextInt(2);
}
// Point E
return x;
}
Fill in each box below with one of ALWAYS, NEVER or SOMETIMES. (You may abbreviate them as A, N, or S.)
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.
public static int randomWalk(int steps) {
Random rand = new Random();
int x = 0;
int r = rand.nextInt(2);
// Point A
while (steps > 0 || x == 0) {
// Point B
if (r == 0) {
x++;
// Point C
} else {
x = 0;
// Point D
}
steps--;
r = rand.nextInt(2);
}
// Point E
return x;
}
Fill in each box below with one of ALWAYS, NEVER or SOMETIMES. (You may abbreviate them as A, N, or S.)
3
Programming
Write a static method hopscotch that shows a sequence of numbers similar to the appearance of a hopscotch board. (Hopscotch is a "hopping" game played by children.) For this problem, a hopscotch board is an increasing sequence of numbers in lines in an alternating pattern. Odd lines contain a single number indented by 3 spaces. Even lines contain a pair of numbers with the first number not indented, followed by 5 spaces, followed by the second number.
Your method should accept a parameter representing the number of "hops" worth of numbers to display. A board of 0 "hops" shows only the number 1, indented by 3 spaces. A "hop" is defined as a trio of additional numbers spread across 2 additional lines, following the pattern described previously. So for example, 1 hop means to show the numbers 2-3-4 to the board in the pattern shown below. 2 hops means to show the numbers 2-3-4, 5-6-7 on the board. 3 hops means to show 2-3-4, 5-6-7, 8-9-10, and so on. Assume that the number of hops passed will be at least 0.
Here are some example calls to the method and their resulting console output:

Write a static method hopscotch that shows a sequence of numbers similar to the appearance of a hopscotch board. (Hopscotch is a "hopping" game played by children.) For this problem, a hopscotch board is an increasing sequence of numbers in lines in an alternating pattern. Odd lines contain a single number indented by 3 spaces. Even lines contain a pair of numbers with the first number not indented, followed by 5 spaces, followed by the second number.
Your method should accept a parameter representing the number of "hops" worth of numbers to display. A board of 0 "hops" shows only the number 1, indented by 3 spaces. A "hop" is defined as a trio of additional numbers spread across 2 additional lines, following the pattern described previously. So for example, 1 hop means to show the numbers 2-3-4 to the board in the pattern shown below. 2 hops means to show the numbers 2-3-4, 5-6-7 on the board. 3 hops means to show 2-3-4, 5-6-7, 8-9-10, and so on. Assume that the number of hops passed will be at least 0.
Here are some example calls to the method and their resulting console output:

There are many ways to solve any programming problem. Here are some common correct solutions we saw:
public static void hopscotch(int hops) {
System.out.println(" 1");
for (int i = 1; i <= hops; i++) {
System.out.println((3 * i) + " " + (3 * i + 1));
System.out.println(" " + (3 * i + 2));
}
}
public static void hopscotch(int hops) {
for (int i = 0; i < hops; i++) {
System.out.println(" " + (3 * i + 1));
System.out.println((3 * i + 2) + " " + (3 * i + 3));
}
System.out.println(" " + (3 * hops + 1));
}
public static void hopscotch(int hops) {
for (int i = 1; i <= hops * 3 + 1; i++) {
if (i % 3 == 1) {
System.out.println(" " + i);
} else if (i % 3 == 2) {
System.out.print(i);
} else {
System.out.println(" " + i);
}
}
}
public static void hopscotch(int hops) {
System.out.println(" 1");
int num = 2;
for (int i = 1; i <= hops; i++) {
System.out.println(num + " " + (num + 1));
System.out.println(" " + (num + 2));
num = num + 3;
}
}
public static void hopscotch(int hops) {
System.out.println(" 1");
int num = 2;
while (num <= 3 * hops + 1) {
System.out.print(num);
num++;
System.out.println(" " + num);
num++;
System.out.println(" " + num);
num++;
}
}
public static void hopscotch(int hops) {
int count = 1;
for (int i = 1; i <= 2 * hops + 1; i++) {
if (i % 2 == 1) {
System.out.println(" " + count);
count++;
} else {
System.out.println(count + " " + (count + 1));
count = count + 2;
}
}
}
public static void hopscotch(int hops) {
int num = 1;
System.out.println(" " + num++);
for (int i = 1; i <= hops; i++)
System.out.println(num++ + " " + num++ + "\n " + num++);
}
public static void hopscotch(int hops) {
System.out.println(" 1");
for (int i = 1; i <= hops; i++) {
System.out.println((3 * i) + " " + (3 * i + 1));
System.out.println(" " + (3 * i + 2));
}
}
public static void hopscotch(int hops) {
for (int i = 0; i < hops; i++) {
System.out.println(" " + (3 * i + 1));
System.out.println((3 * i + 2) + " " + (3 * i + 3));
}
System.out.println(" " + (3 * hops + 1));
}
public static void hopscotch(int hops) {
for (int i = 1; i <= hops * 3 + 1; i++) {
if (i % 3 == 1) {
System.out.println(" " + i);
} else if (i % 3 == 2) {
System.out.print(i);
} else {
System.out.println(" " + i);
}
}
}
public static void hopscotch(int hops) {
System.out.println(" 1");
int num = 2;
for (int i = 1; i <= hops; i++) {
System.out.println(num + " " + (num + 1));
System.out.println(" " + (num + 2));
num = num + 3;
}
}
public static void hopscotch(int hops) {
System.out.println(" 1");
int num = 2;
while (num <= 3 * hops + 1) {
System.out.print(num);
num++;
System.out.println(" " + num);
num++;
System.out.println(" " + num);
num++;
}
}
public static void hopscotch(int hops) {
int count = 1;
for (int i = 1; i <= 2 * hops + 1; i++) {
if (i % 2 == 1) {
System.out.println(" " + count);
count++;
} else {
System.out.println(count + " " + (count + 1));
count = count + 2;
}
}
}
public static void hopscotch(int hops) {
int num = 1;
System.out.println(" " + num++);
for (int i = 1; i <= hops; i++)
System.out.println(num++ + " " + num++ + "\n " + num++);
}
4
While Loop 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 whileMystery(int x, int y, int z) {
while (x < z || y < z) {
x = x + y;
y = y * 2;
System.out.print(x + " " + y + " ");
}
System.out.println();
}
For each call below to the following method, write the output that is produced, as it would appear on the console:
public static void whileMystery(int x, int y, int z) {
while (x < z || y < z) {
x = x + y;
y = y * 2;
System.out.print(x + " " + y + " ");
}
System.out.println();
}
Unlock Deck
Unlock for access to all 7 flashcards in this deck.
Unlock Deck
k this deck
5
Programming
Write a static method named containsBothDigits that accepts three integer parameters a, b, and c. Assume that a is a positive integer (greater than 0) and that b and c are single-digit numbers from 0-9 inclusive. Your method should return true if a contains both b and c at among its digits, and false otherwise.
For example, the number 433407 contains 4, 3, 0, and 7 as its unique digit values, so a call to your method of containsBothDigits(433407, 7, 3) should return true. If b and c are the same number, your method should return true if that digit appears at least once among the digits; in other words, the same digit from a could match both b and c. So for example, a call of containsBothDigits(433407, 7, 7) should return true. Note that the digit b or c might appear more than once in a, as with 433407 containing two 3s.
The following table lists some additional calls to your method and their expected return values:
Note: You may not use a String to solve this problem.
Write a static method named containsBothDigits that accepts three integer parameters a, b, and c. Assume that a is a positive integer (greater than 0) and that b and c are single-digit numbers from 0-9 inclusive. Your method should return true if a contains both b and c at among its digits, and false otherwise.
For example, the number 433407 contains 4, 3, 0, and 7 as its unique digit values, so a call to your method of containsBothDigits(433407, 7, 3) should return true. If b and c are the same number, your method should return true if that digit appears at least once among the digits; in other words, the same digit from a could match both b and c. So for example, a call of containsBothDigits(433407, 7, 7) should return true. Note that the digit b or c might appear more than once in a, as with 433407 containing two 3s.
The following table lists some additional calls to your method and their expected return values:
Note: You may not use a String to solve this problem.
Unlock Deck
Unlock for access to all 7 flashcards in this deck.
Unlock Deck
k this deck
6
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 and capitalization.
e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.
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 and capitalization.
e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.
Unlock Deck
Unlock for access to all 7 flashcards in this deck.
Unlock Deck
k this deck
7
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 < b) {
a = a * 2;
}
if (a > b) {
a = a - 10;
} else {
b++;
}
System.out.println(a + " " + b);
}
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 < b) {
a = a * 2;
}
if (a > b) {
a = a - 10;
} else {
b++;
}
System.out.println(a + " " + b);
}
Unlock Deck
Unlock for access to all 7 flashcards in this deck.
Unlock Deck
k this deck