AP Comp Science Chapter 1-2
In Java, a variable of type int is represented internally as a 32-bit signed integer. Suppose that one bit stores the sign, and the other 31 bits store the magnitude of the number in base 2. In this scheme, what is the largest value that can be stored as type int? (A) 232 (B) 232 - 1 (C) 231 (D) 231 - 1 (E) 230
D
0 0 0 //^ THAT IS THE ANSWER TO ONE OF THEM
.
9 27 6 3 //^ THAT IS THE ANSWER TO ONE OF THEM.
.
Just before exiting this program, what are the object values of x, y, and a, respectively? (A) 9,9,9 (B) 2,9,9 (C) 2, 8, 9 (D) 3,8,9 (E) 7,8,9
A.
Which of the following will cause an error message? I Date d1 = new Date(8, 2, 1947); Date d2 = d1; II Date d1 = null; Date d2 = d1; III Date d = null; int x = d.year(); (A) I only (B) II only (C) III only (D) II and III only (E) I, II, and III
C
Given that a, b, and c are integers, consider the boolean expression (a < b) || !((c == a * b) && (c < a)) Which of the following will guarantee that the expression is true? (A) c < a is false. (B) c < a is true. (C) a < b is false. (D) c == a * b is true. (E) c == a * b is true, and c < a is true.
A
Given that n and count are both of type int, which statement is true about the following code segments? I for (count = 1; count <= n; count++) System.out.println(count); II count = 1; while (count <= n) System.out.println(count); count++; (A) I and II are exactly equivalent for all input values n. (B) I and II are exactly equivalent for all input values n > 1, but differ when n<0. (C) I and II are exactly equivalent only when n=0. (D) I and II are exactly equivalent only when n is even. (E) I and II are not equivalent for any input values of n.
A
Suppose that base-2 (binary) numbers and base-16 (hexadecimal) numbers can be denoted with subscripts, as shown below: 2Ahex = 101010 bin Which is equal to 3Dhex? (A) 111101 bin (B) 101111 bin (C) 10011min (D) 110100 bin (E) 101101min
A
What values are stored in x and y after execution of the following program seg ment? int x = 30, y = 40; if (x >= 0) if (x <= 100) { y = x * 3; if (y < 50) x /= 10; } else y = x * 2; else y = -x; ILI (A) x = 30 y = 90 (B) x = 30 y = -30 (C) x = 30 y = 60 (D) x = 3 y = -3 (E) x = 30 y = 40
A
readInt (); //read user input Date birthDate = new Date (birthDate.month(), birthDate.day(), birthDate.year()); III System.out.println("Enter birthdate: mo, day, yr: "); int birthDate.month = IO.readInt (); //read user input int birthDate.day = 10.readInt (); //read user input int birthDate.year = 10.readInt(); //read user input Date birthDate = new Date(birthDate.month, birthDate.day, birthDate.year); (A) I only (B) II only (C) III only (D) I and II only (E) I and III only
A
1. Which of the following pairs of declarations will cause an error message? I double x = 14.7; int y = x; II double x = 14.7; int y = (int) x; III int x = 14; double y = x; (A) None (B) I only (C) II only (D) III only (E) I and III only
B
What output will be produced by this code segment? (Ignore spacing.) for (int i = 5; i >= 1; i--) for (int j = i; j >= 1; j--) System.out.print(2 * j - 1); System.out.println();
B
Which is true of the following boolean expression, given that x is a variable of type double? 3.0 == x * (3.0 / x) (A) It will always evaluate to false. (B) It may evaluate to false for some values of x. (C) It will evaluate to false only when x is zero. (D) It will evaluate to false only when x is very large or very close to zero. (E) It will always evaluate to true.
B
Which of the following correctly constructs a Date object in a client class? (A) Date d = new (2, 13, 1947); (B) Date d = new Date(2, 13, 1947); (C) Date d; (D) d = new (2, 13, 1947); Date d; d = Date (2, 13, 1947); (E) Date d = Date (2, 13, 1947);
B
Which of the following methods in the Frog class is the best candidate for being a static method? (A) swim //frog swims to new position in pond (B) getPondTemperature //returns temperature of pond (C) eat //frog eats and gains weight (D) getWeight //returns weight of frog (E) die //frog dies with some probability based //on frog's age and pond temperature
B
Which of the following represents correct implementation code for the constructor with parameters? (A) hrs = 0; mins = 0; secs = 0; (B) hrs = h; mins = m; secs = s; (C) resetTime (hrs, mins, secs); (D) h = hrs; m = mins; s = secs; (E) Time = new Time (h, m, s);
B
Which of the following values of num will result in valid having a value of true? (A) 6143 (B) 6144 (C) 6145 (D) 6146 (E) 6147
B
Which statement is true following execution of this segment? (A) t1, t2, t3, and t4 all represent the identical temperature, in degrees Celsius. (B) t1, t2, t3, and t4 all represent the identical temperature, in degrees Fahrenheit. (C) t4 represents a Fahrenheit temperature, while t1, t2, and t3 all represent degrees Celsius. (D) t1 and t2 refer to the same Temperature object; t3 refers to a Temperature object that is 20 degrees lower than t1 and t2, while t4 refers to an object that is t1 converted to Fahrenheit. (E) A NullPointerException was thrown.
B
A client class has a display method that writes the time represented by its pa rameter: /** Outputs time t in the form hrs:mins:secs. * @param t the time */ public void display (Time t) /* method body */ Which of the following are correct replacements for /* method body */? I Time T = new Time (h, m, s); System.out.println(T); II System.out.println(t.hrs + ":" + t.mins + ":" + t.secs); III System.out.println(t); (A) I only (B) II only (C) III only (D) II and III only (E) I, II, and III
C
A common use of hexadecimal numerals is to specify colors on web pages. Ev ery color has a red, green, and blue component. In decimal notation, these are denoted with an ordered triple (x, y, z), where x, y, and z are the three components, each an int from 0 to 255. For example, a certain shade of red, whose red, green, and blue components are 238, 9, and 63, is represented as (238,9,63). In hexadecimal, a color is represented in the format #RRGGBB, where RR, GG, and BB are hex values for the red, green, and blue. Using this notation, the color (238,9,63) would be coded as #EE093F. Which of the following hex codes represents the color (14, 20, 255)? (A) #1418FE (B) #0E20FE (C) #0E14FF (D)#OFE5FE (E) #0D14FF
C
In the following code segment, you may assume that a, b, and n are all type int. if (a != b &&n/ (a - b) > 90) /* statement 1 */ else /* statement 2 */ /* statement 3 */ What will happen if a == b is false? (A) /* statement 1 */ will be executed. (B) /* statement 2 */ will be executed. (C) Either /* statement 1 */ or /* statement 2 */ will be executed. (D) A compile-time error will occur. (E) An exception will be thrown.
C
Just before the end of execution of this program, what are the values of x, y, and temp, respectively? (A) 6,8,6 (B) 8,6,6 (C) 6,8, ?, where ? means undefined (D) 8, 6, ?, where ? means undefined (E) 8,6,8
C
Suppose that addition and subtraction had higher precedence than multiplication and division. Then the expression 2 + 3 * 12 / 7 - 4 + 8 would evaluate to which of the following? (A) 11 (B) 12 (C) 5 (D) 9 (E) -4
C
The Date class is modified by adding the following mutator method: public void addYears (int n) //add n years to date Here is part of a poorly coded client program that uses the Date class: public static void addCentury(Date recent, Date old) { old.addYears (100); recent = old; public static void main(String[] args) Date oldDate = new Date(1, 13, 1900); Date recentDate = null; addCentury (recentDate, oldDate); Which will be true after executing this code? (A) A NullPointerException is thrown. (B) The oldDate object remains unchanged. (C) recentDate is a null reference. (D) recentDate refers to the same object as oldDate. (E) recentDate refers to a separate object whose contents are the same as those of oldDate.
C
The method reduce() is not a public method because (A) methods whose return type is void cannot be public. (B) methods that change this cannot be public. (C) the reduce () method is not intended for use by clients of the Rational class. (D) the reduce() method is intended for use only by clients of the Rational class. (E) the reduce () method uses only the private data fields of the Rational class.
C
What is the purpose of the local variable nRemaining? (A) It is not possible to separate n into digits without the help of a temporary variable. (B) nRemaining prevents the parameter num from being altered. (C) nRemaining enhances the readability of the algorithm. (D) On exiting the method, the value of nRemaining may be reused. (E) nRemaining is needed as the left-hand side operand for integer division.
C
Which is a correct replacement for /* code to construct... */? I Temperature t = new Temperature (tempDegrees, tempScale); if (!t.isValidTemp (tempDegrees,tempScale)) /* error message and exit program */ II if (isValidTemp (tempDegrees, tempScale)) Temperature t = new Temperature (tempDegrees, tempScale); else /* error message and exit program */ III if (Temperature.isValidTemp (tempDegrees, tempScale)) Temperature t = new Temperature (tempDegrees, tempScale); else /* error message and exit program */ (A) I only (B) II only (C) III only (D) I and II only (E) I and III only
C
Which of the following will evaluate to true only if boolean expressions A, B, and C are all false? (A) !A && ! (B && !C) (B) !A || !B || !C (C) !(A || B || C) (D) ! (A && B && c) (E) !A || !(B || !C)
C
Which statement about parameters is false? (A) The scope of parameters is the method in which they are defined. (B) Static methods have no implicit parameter this. (C) Two overloaded methods in the same class must have parameters with different names. (D) All parameters in Java are passed by value. (E) Two different constructors in a given class can have the same number of parameters.
C
Assume that a and b are integers. The boolean expression ! (a <= b) && (a * b > 0) will always evaluate to true given that (A) a = b (B) a > b (C) a<b (D) a > b and b > 0 (E) a > b and b < 0
D
Assume these declarations: Rational a = new Rational(); Rational r = new Rational (numer, denom); int n = value; //numer, denom, and value are valid integer values Which of the following will cause a compile-time error? (A) r = a.plus (r); (B) a = r.plus (new Rational(n)); (C) r = r.plus (r); (D) a = n.plus(r); (E) r = r.plus (new Rational(n));
D
Consider the following code segment if (n != 0 && x / n > 100) statement1; else statement2; If n is of type int and has a value of o when the segment is executed, what will happen? (A) An ArithmeticException will be thrown. (B) A syntax error will occur. (C) statement1, but not statement2, will be executed. (D) statement2, but not statement1, will be executed. (E) Neither statement1 nor statement2 will be executed; control will pass to the first statement following the if statement.
D
Consider this code segment: int x = 10, y = 0; while (x > 5) y = 3; while (y < x) y *= 2; if (y % x == 1) y += x; X -= 3; System.out.println(x + " " + y); What will be output after execution of this code segment? (A) 1 6 (B) 7 12 (C) -3 12 (D) 4 12 (E) -3 6
D
Consider this program segment: int newNum = 0, temp; int num = k; while (num > 10) //k is some predefined integer value > 0 temp = num % 10; num /= 10; newNum = newNum * 10 + temp; System.out.print(newNum); Which is a true statement about the segment? I If 100 < num < 1000 initially, the final value of newNum must be in the range 10 < newNum < 100. II There is no initial value of num that will cause an infinite while loop. III If num < 10 initially, newNum will have a final value of o. (A) I only (B) II only (C) III only (D) II and III only (E) I, II, and III
D
Let x be a variable of type double that is positive. A program contains the boolean expression (Math.pow (x,0.5) == Math.sqrt(x)). Even though x1/2 is mathematically equivalent to x, the above expression returns the value false in a student's program. Which of the following is the most likely reason? (A) Math.pow returns an int, while Math.sart returns a double. (B) x was imprecisely calculated in a previous program statement. (C) The computer stores floating-point numbers with 32-bit words (D) There is round-off error in calculating the pow and sqrt functions. (E) There is overflow error in calculating the pow function.
D
The constructors in the Rational class allow initialization of Rational objects in several different ways. Which of the following will cause an error? (A) Rational r1 = new Rational(); (B) Rational r2 = r1; (C) Rational r3 = new Rational(2,-3); (D) Rational r4 = new Rational (3.5); (E) Rational r5 = new Rational(10);
D
The following fragment intends that a user will enter a list of positive integers at the keyboard and terminate the list with a sentinel: int value = 0; final int SENTINEL = -999; while (value != SENTINEL) // code to process value value = 10.readInt (); //read user input } The fragment is not correct. Which is a true statement? (A) The sentinel gets processed. (B) The last nonsentinel value entered in the list fails to get processed. (C) A poor choice of SENTINEL value causes the loop to terminate before all values have been processed. (D) The code will always process a value that is not on the list. (E) Entering the SENTINEL value as the first value causes a run-time error.
D
What will the output be for the following poorly formatted program segment, if the input value for num is 22? int num = call to a method that reads an integer; if (num > 0) if (num % 5 == 0) System.out.println(num); else System.out.println(num + " is negative"); (A) 22 (B) 4 (C) 2 is negative (D) 22 is negative (E) Nothing will be output.
D
Which of the following correctly replaces /* more code */ so that the postcondition is achieved? I result = new Temperature (degrees * 1.8 + 32, "F"); Il result = new Temperature (degrees * 1.8, "F"); result = result.raise (32); III degrees *= 1.8; this = this.raise (32); result = new Temperature (degrees, "F"); (A) I only (B) II only (C) ІІІ only (D) I and II only (E) I, II, and III
D
Which of the following is a false statement about the methods? (A) equals, lessThan, and toString are all accessor methods. (B) increment is a mutator method. (C) Time() is the default constructor. (D) The Time class has three constructors. (E) There are no static methods in this class
D
Which of the following replacements for /* code segment */ would cause the method to work as intended? I for (int i = 0; i <= n; i++) rem = n % 10; revNum = revNum * 10 + rem; n /= 10; II while (n != 0) rem = n % 10; revNum = revNum * 10 + rem; n /= 10; III for (int i = n; i != 0; i /= 10) rem = i % 10; revNum = revNum * 10 + rem; (A) I only (B) II only (C) I and II only (D) II and III only. (E) I and III only
D
2. What output will be produced by System.out.print("\\* This is not\n a comment *\\"); (A) * This is not a comment * (B) \* This is not a comment *\ (C) * This is not S a comment * (D) \\* This is not a comment *\\ (E) \* This is not a comment *\
E
A client program creates a Date object as follows: Date d = new Date(1, 13, 2002); Which of the following subsequent code segments will cause an error? (A) String s = d.toString(); (B) int x = d.day(); (C) Date e = d; (D) Date e = new Date(1, 13, 2002); (E) int y = d.year;
E
Refer to the following code fragment: double answer = 13 / 5; System.out.println("13 / 5 = " + answer); The output is 13 / 5 = 2.0 The programmer intends the output to be 13 / 5 = 2.6 Which of the following replacements for the first line of code will not fix the problem? (A) double answer = (double) 13 / 5; (B) double answer = 13 / (double) 5; (C) double answer = 13.0 / 5; (D) double answer = 13 / 5.0; (E) double answer = (double) (13 / 5);
E
What value is stored in result if int result = 13 - 3 * 6 / 4 % 3; (A) -5 (B) O (C) 13 (D) -1 (E) 12
E
Which of the following could be used as /* implementation code */? I System.out.println (month + "/" + day + "/" + year); II System.out.println(month() + "/" + day() + "/" + year()); III System.out.println(this); (A) I only (B) II only (C) III only (D) II and III only (E) I, II, and III
E
Which of the following is a correct replacement for /* more code */? (A) Rational rat (numer, denom); rat.reduce(); return rat; (B) return new Rational (numer, denom); (C) reduce(); Rational rat = new Rational (numer, denom); return rat; (D) Rational rat = new Rational (numer, denom); Rational.reduce(); return rat; (E) Rational rat = new Rational (numer, denom); rat.reduce(); return rat;
E
Which of the following program fragments will produce this output? (Ignore spacing.) - 10 - - - - 12 I for (int i = 1; i <= 6; i++) for (int k = 1; k <= 6; k++) if (k == i) System.out.print(2 * k); else - System.out.print("-"); System.out.println(); II for (int i = 1; i <= 6; i++) for (int k = 1; k <= i - 1; k++) System.out.print("-"); System.out.print(2 * i); for (int k = 1; k <= 6 - i; k++) System.out.print("-"); System.out.println(); } III for (int i = 1; i <= 6; i++) for (int k = 1; k <= i - 1; k++) System.out.print ("-"); System.out.print(2 * i); for (int k = i + 1; k<= 6; k++) System.out.print("-"); System.out.println(); } (A) I only (B) II only (C) III only (D) I and II only (E) I, II, and III
E