Question
a) Suppose that a TOY memory location holds the value 00AD. What is the corresponding value in decimal? Circle your answer. (b) How many values
a) Suppose that a TOY memory location holds the value 00AD. What is the corresponding value in decimal? Circle your answer. (b) How many values does the following for loop print? Recall that a Java int is a 32-bit two's complement integer. Circle the correct answer. for (int i = 1; i >= 0; i = i + i) { StdOut.println(i); } 0 1 30 31 32 230 1 231 1 232 1 infinite loop
2. Java basics. (10 points) (a) Give the type and value of each of the following Java expressions. If it leads to a compile-time or runtime-error, specify that for the type (and leave the value column blank).
(b) Which of the following are true of Java arrays. Circle all that apply. i. Array entries are auto-initialized to 0.0 when creating an array of type double[]. ii. Can change the size of the array after creation. iii. Given an array a[] that has been declared and initialized, accessing a[a.length] results in a runtime error. iv. Can use an array as a return type from a function. v. Can pass an array to a function and have that function change the values stored in the array entries.
3. Loops, conditionals, and arrays. (8 points) Consider the following Java code fragment.
int N = a.length; double min = Double.POSITIVE_INFINITY; for (int i = 0; i
(a) Suppose that the array a[] is initialized as follows double[] a = { 4.5, 3.5, 6.0, 20.0, 3.0 }; What is the value of min upon termination of the nested for loops? Circle your answer. (b) Given an array a[], describe in 15 words or less the value of min upon termination.
4. Input and output. (6 points) Consider the following Java program.
public class Mystery { public static void main(String[] args) { int curr = StdIn.readInt(); StdOut.print(curr + " "); int prev = curr; while (!StdIn.isEmpty()) { curr = StdIn.readInt(); StdOut.print((prev + curr) / 2 + " "); prev = curr; } StdOut.println(); } }
Assume the contents of the file input.txt are given below. % more input.txt 2 4 6 8 10 12 8 2 (a) What is the result of the following command? Circle your answer. % java Mystery
5. Functions. (8 points) The gcd() function, defined in a class Euclid, takes two nonnegative integer arguments and return the greatest common divisor of the two integers.
public class Euclid { public static int gcd(int p, int q) { if (q == 0) return p; return gcd(q, p % q); } }
(a) Write an overloaded function gcd() that takes three nonnegative integer arguments and returns the greatest common divisor of the three integers. Assume that the function is in the same class Euclid as the two-argument version above.
Hint: Use the identity gcd(p, q, r) = gcd(gcd( p, q), r). For example, gcd(504, 4116, 4410) = gcd(gcd(504, 4116), 4410) = gcd(84, 4410) = 42.
(b) Give the signature of a function that takes as an argument an array of nonnegative integers and returns the greatest common divisor of those integers. Do not implement the function.
Java expression type value 1 + 2.0 * 3 + 4.0 (-1 / -1) / 0 (-1.0 / -1.0) / 0.0 Math.sqrt(-2.0) 1 + "+" + 2.0 + "3" (double) (10 / 4) (1.0 0) pc - 20 if (R[A] -- 0) pe > R[t] TRANSFER between registers and memory 7: load address R[d] 0) peStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started