Exam 1: Introduction to Java Programming

arrow
  • Select Tags
search iconSearch Question
  • Select Tags

While Loop Simulation For each call of the method below, write the output that is printed: public static void mystery(int i, int j) { while (i != 0 && j != 0) { i = i / j; j = (j - 1) / 2; System.out.print(i + " " + j + " "); } System.out.println(i); } mystery (5,0) mystery (3,2) mystery (16,5) mystery (80,9) mystery (1600,40)

Free
(Essay)
4.8/5
(38)
Correct Answer:
Verified

 Method Call  Output mystery (5,0)5 mystery (3,2)101 mystery (16,5)32101 mystery (80,9)8421202 mystery (1600,40)401929040\begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Output}} \\\text { mystery }(5,0) & 5 \\\text { mystery }(3,2) & 1 \quad 0 \quad 1\\\text { mystery }(16,5) &3\quad2\quad1\quad0\quad1\\\text { mystery }(80,9) & 8\quad4\quad2\quad1\quad2\quad0\quad2 \\\text { mystery }(1600,40) & 40\quad19\quad2\quad9\quad0\quad4\quad0\end{array}

Programming Write a static method named sequenceSum that prints terms of the following mathematical sequence: 1+12+13+14+15+16+1+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+\frac{1}{5}+\frac{1}{6}+\ldots  (also written as i=11j ) \text { (also written as } \sum_{i=1}^{\infty} \frac{1}{j} \text { ) } Your method should accept a real number as a parameter representing a limit, and should add and print terms of the sequence until the sum of terms meets or exceeds that limit. For example, if your method is passed 2.0, print terms until the sum of those terms is at ≥ 2.0. You should round your answer to 3 digits past the decimal point. The following is the output from the call sequenceSum(2.0); 1 + 1/2 + 1/3 + 1/4 = 2.083 (Despite the fact that the terms keep getting smaller, the sequence can actually produce an arbitrarily large sum if enough terms are added.) If your method is passed a value less than 1.0, no output should be produced. You must match the output format shown exactly; note the spaces and pluses separating neighboring terms. Other sample calls: Calls sequenceSum (0.0); sequenceSum (1.0); sequenceSum (1.5); Output 1=1.000 1+1/2=1.500 Call sequenceSum (2.7); Output 1+1/2+1/3+1/4+1/5+1/6+1/7+1/8=2.718

Free
(Essay)
4.7/5
(41)
Correct Answer:
Verified

(one solution shown)
public static void sequenceSum(double limit) {
if (limit >= 1) {
System.out.print("1");
int denomenator = 1;
double sum = 1.0;
while (sum < limit) {
denomenator++;
sum += 1.0 / denomenator;
System.out.print(" + 1/" + denomenator);
}
System.out.printf(" = %.3f\n", sum);
}
}

Programming Write a static method named hasMidpoint that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your method should return false if no such midpoint relationship exists. The integers could be passed in any order; the midpoint could be the 1st, 2nd, or 3rd. You must check all cases. Calls such as the following should return true: hasmidpoint (4,6,8) hasmidpoint (2,10,6) hasmidpoint (8,8,8) hasmidpoint (25,10,-5) Calls such as the following should return false : hasmidpoint (3,1,3) hasmidpoint (1,3,1) hasmidpoint (21,9,58) hasmidpoint (2,8,16)

Free
(Essay)
4.8/5
(35)
Correct Answer:
Verified

(five solutions shown)
public static boolean hasMidpoint(int a, int b, int c) {
double mid = (a + b + c) / 3.0;
if (a == mid || b == mid || c == mid) {
return true;
} else {
return false;
}
}
public static boolean hasMidpoint(int a, int b, int c) {
double mid = (a + b + c) / 3.0;
return (a == mid || b == mid || c == mid);
}
public static boolean hasMidpoint(int a, int b, int c) {
return (a == (b + c) / 2.0 || b == (a + c) / 2.0 || c == (a + b) / 2.0);
}
public static boolean hasMidpoint(int a, int b, int c) {
int max = Math.max(a, Math.max(b, c));
int min = Math.min(a, Math.min(b, c));
double mid = (max + min) / 2.0;
return (a == mid || b == mid || c == mid);
}
public static boolean hasMidpoint(int a, int b, int c) {
return (a - b == b - c || b - a == a - c || a - c == c - b);
}

If/Else Simulation For each call of the method below, write the value that is returned: public static int mystery(int a, int b) { int c; if (a > b) { c = a; } else if (b % a == 0) { c = b; } else { c = b + (a - (b % a)); } return c; } mystery (4,2) mystery (5,4) mystery (5,13) mystery (5,17) mystery (4,8)

(Essay)
4.9/5
(37)

Parameter Mystery At the bottom of the page, write the output produced by the following program. public class ParameterMystery { public static void main(String[] args) { String x = "java"; String y = "tyler"; String z = "tv"; String rugby = "hamburger"; String java = "donnie"; hamburger(x, y, z); hamburger(z, x, y); hamburger("rugby", z, java); hamburger(y, rugby, "x"); hamburger(y, y, "java"); } public static void hamburger(String y, String z, String x) { System.out.println(z + " and " + x + " like " + y); } }

(Essay)
4.9/5
(42)

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/false for a boolean). 3*4+5*6+7*-2 1.5*2.0+(5.5/2)+5/4 23\%5+31/4\%3-17\%(16\%10) "1"+2+3+"4"+5*6+"77"+(8+9) 345/10/3*55/5/6+10/(5/2.0) 1/2>0||4==9\%5||1+1<1-1

(Essay)
4.9/5
(37)

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. (You may abbreviate these choices as A/N/S respectively.) public static int mystery(int x) { int y = 1; int z = 0; // Point A while (y <= x) { // Point B y = y * 10; z++; // Point C } // Point D z--; // Point E return z; } > z<0 z>0 Point A Point B Point C Point D Point E

(Essay)
4.8/5
(32)

Programming Write a static method named favoriteLetter that accepts two parameters: a Scanner for the console, and a favorite letter represented as a one-letter String. The method repeatedly prompts the user until two consecutive words are entered that start with that letter. The method then prints a message showing the last word typed. You may assume that the user will type a single-word response to each prompt. Your code should be case-sensitive; for example, if the favorite letter is a, you should not stop prompting if the user types words that start with an A. For example, the following logs represent the output from two calls to your method: (User input is underlined.) Programming Write a static method named favoriteLetter that accepts two parameters: a Scanner for the console, and a favorite letter represented as a one-letter String. The method repeatedly prompts the user until two consecutive words are entered that start with that letter. The method then prints a message showing the last word typed. You may assume that the user will type a single-word response to each prompt. Your code should be case-sensitive; for example, if the favorite letter is a, you should not stop prompting if the user types words that start with an A. For example, the following logs represent the output from two calls to your method: (User input is underlined.)

(Essay)
4.8/5
(36)
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)