1. Given the following variable declarations: What is the result of the followin
ID: 3560022 • Letter: 1
Question
1. Given the following variable declarations:
What is the result of the following relational expression?
A. True B. False C. 0 D. -5
2. Consider the following method.
What output is produced for the following call?
A. 13 21 B. no output is produced C. 12 20 D. 12 21
3. The following code contains a logic error. Examine the code and figure out the case(s) in which the code would print something that is untrue about the number that was entered.
This would print something that is untrue for:
A. an input of 4 B. odd numbers C. an input of zero D. an input of 18 E. negative numbers
4. Write a static method called quadrant that takes as parameters a pair of real numbers representing an (x, y) point and that returns the quadrant number for that point. Recall that quadrants are numbered as integers from 1 to 4 with the upper-right quadrant numbered 1 and the subsequent quadrants numbered in a counter-clockwise fashion:
Notice that the quadrant is determined by whether the x and y coordinates are positive or negative numbers. If a point falls on the x-axis or the y-axis, then the method should return 0. Below are sample calls on the method.
5. How many times will the loop execute its body? Remember that "zero," "infinity," and "unknown" are legal answers.
A. 83 B. infinity C. zero D. 84 E. unknown
6. Write a method named randomWalk that performs a random one-dimensional walk, reporting each position reached and the maximum position reached during the walk. The random walk should begin at position 0. On each step, you should either increase or decrease the position by 1 (with equal probability). The walk stops when 3 or -3 is hit. The output should look like this:
Note that since this method uses a random variable, your output will vary from one run to another.
Call Value Returned quadrant(12.4, 17.8) 1 quadrant(-2.3, 3.5) 2 quadrant(-15.2, -3.1) 3 quadrant(4.5, -42.0) 4 quadrant(0.0, 3.14) 0Explanation / Answer
1. Given the following variable declarations:
int x = 4;
int y = -3;
int z = 4;
What is the result of the following relational expression?
x * (y + 3) > y - (y + z) * 2
A. True B. False C. 0 D. -5
2. Consider the following method.
public static void ifElseMystery1(int x, int y) {
int z = 4;
if (z <= x) {
z = x + 1;
} else {
z = z + 9;
}
if (z <= y) {
y++;
}
System.out.println(z + " " + y);
}
What output is produced for the following call?
ifElseMystery(3, 20);
A. 13 21 B. no output is produced C. 12 20 D. 12 21
3. The following code contains a logic error. Examine the code and figure out the case(s) in which the code would print something that is untrue about the number that was entered.
Scanner console = new Scanner(System.in);
System.out.print("Type a number: ");
int number = console.nextInt();
if (number % 2 == 0) {
if (number % 3 == 0) {
System.out.println("Divisible by 6.");
} else {
System.out.println("Odd.");
}
}
This would print something that is untrue for:
A. an input of 4 B. odd numbers C. an input of zero D. an input of 18 E. negative numbers
4. Write a static method called quadrant that takes as parameters a pair of real numbers representing an (x, y) point and that returns the quadrant number for that point. Recall that quadrants are numbered as integers from 1 to 4 with the upper-right quadrant numbered 1 and the subsequent quadrants numbered in a counter-clockwise fashion:
static int quadrant (double x, double y)
{
if (x > 0)
{
if (y > 0)
return 1;
else if (y < 0)
return 4;
else
return 0;
}
else if (x < 0)
{
if (y > 0)
return 2;
else if (y < 0)
return 3;
else
return 0;
}
else
return 0;
}
Notice that the quadrant is determined by whether the x and y coordinates are positive or negative numbers. If a point falls on the x-axis or the y-axis, then the method should return 0. Below are sample calls on the method.
Call
Value Returned
quadrant(12.4, 17.8)
1
quadrant(-2.3, 3.5)
2
quadrant(-15.2, -3.1)
3
quadrant(4.5, -42.0)
4
quadrant(0.0, 3.14)
0
5. How many times will the loop execute its body? Remember that "zero," "infinity," and "unknown" are legal answers.
int x = 250;
while (x % 3 != 0) {
System.out.println(x);
}
A. 83 B. infinity C. zero D. 84 E. unknown
6. Write a method named randomWalk that performs a random one-dimensional walk, reporting each position reached and the maximum position reached during the walk. The random walk should begin at position 0. On each step, you should either increase or decrease the position by 1 (with equal probability). The walk stops when 3 or -3 is hit. The output should look like this:
position = 0
position = 1
position = 0
position = -1
position = -2
position = -1
position = -2
position = -3
max position = 1
Note that since this method uses a random variable, your output will vary from one run to another.
static void randomWalk ()
{
Random generator = new Random();
int pos = 0;
int max = -4;
while (true)
{
pos = generator.nextInt(7) - 3 ;
System.out.println("position = " + pos);
if (pos > max)
max = pos;
if ((pos == 3) || (pos == -3))
break;
}
System.out.println("max position = " + max);
}
Call
Value Returned
quadrant(12.4, 17.8)
1
quadrant(-2.3, 3.5)
2
quadrant(-15.2, -3.1)
3
quadrant(4.5, -42.0)
4
quadrant(0.0, 3.14)
0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.