Ch 7
What is the output of the following program segment? System.out.println(Math.round(25.01)); System.out.println(Math.round(25.49)); System.out.println(Math.round(25.51)); System.out.println(Math.round(25.99)); (A) 25 25 26 26 (B) 25 25 25 25 (C) 26 26 26 26 (D) Error Message
(A) 25 25 26 26
What is the output of the following program? public class Q35 { public static void main(String args [ ]) { int x = 25; int y = 10; System.out.println(Calc.add(x,y)); System.out.println(Calc.sub(x,y)); System.out.println(Calc.mul(x,y)); System.out.println(Calc.div(x,y)); } } class Calc { public static String add(int p, int q) { int sum = p + q; String result = (String) p + " + " + q + " = " + sum; return result; } public static String sub(int p, int q) { int diff = p - q; String result = (String) p + " - " + q + " = " + diff; return result; } public static String mul(int p, int q) { int prod = p * q; String result = (String) p + " * " + q + " = " + prod; return result; } public static String div(int p, int q) { int quot = p + q; String result = (String) p + " / " + q + " = " + quot; return result; } } ### (A) 25 + 10 = 35 (B) 25 + 10 = 15 (C) 25 - 10 = 15 (D) 1 25 - 10 = 15 25 - 10 = 35 25 + 10 = 35 2 25 * 10 = 250 25 * 10 = 2 25 / 10 = 2 3 25 / 10 = 2 25 / 10 = 250 25 * 10 = 250 4 (E) Error message
(A) 25 + 10 = 35 25 - 10 = 15 25 + 10 = 250 25 / 10 = 2
14. What is the output of the following program? public class Q14 { public static void main(String args [ ]) { method1(); method3(); method2(); } public static void method1() { System.out.println("Calling method 1"); } public static void method2() { System.out.println("Calling method 3"); } public static void method3() { System.out.println("Calling method 2"); } } (A) Calling method 1 Calling method 2 Calling method 3 (B) Calling method 1 Calling method 3 Calling method 2 (C) method 1 method 3 method 2 (D) Error message
(A) Calling method 1 Calling method 2 Calling method 3
What is the output of the following program? public class Q22 { public static void main(String args [ ]) { method1(1); method3(2); method2(3); } public static void method1(int n) { System.out.println("Calling method " + n); } public static void method2(int n) { System.out.println("Calling method " + n); } public static void method3(int n) { System.out.println("Calling method " + n); } } (A) Calling method 1 Calling method 2 Calling method 3 (B) Calling method 1 Calling method 3 Calling method 2 (C) method 1 method 3 method 2 (D) Error message
(A) Calling method 1 Calling method 2 Calling method 3
What distinguishes a call to a void method? (A) The method call is the only part of a complete program statement. (B) The method call provides a value, which is used in the program statement. (C) The method call includes the void keyword. (D) The method call includes the return keyword.
(A) The method call is the only part of a complete program statement.
The Java compiler checks for (A) correct program syntax. (B) correct program logic. (C) consistent program statement indentations. (D) the use of meaningful variable identifiers. (E) the proper implementation of methods.
(A) correct program syntax.
The gridworld.jar file (A) must be part of any functional GridWorld program. (B) provides online help for any person running a GridWorld program. (C) is the only file needed to run a GridWorld program. (D) stores all the GridWorld files that will be tested on the AP Computer Science exam. (E) only contains the web-based html files for GridWorld documentation.
(A) must be part of any functional GridWorld program.
Multiple methods that perform similar actions should be (A) placed in their own class. (B) placed inside the main method . (C) placed in the same class as the main method. (D) all be placed in a Utility class . (E) changed to a single method.
(A) placed in their own class.
Which of the following method headings uses proper parameter declarations? (A) public static void guess(double rate, double hours, int deductions); (B) public static void guess(double rate, hours, int deductions); (C) public static void guess(rate, hours, deductions); (D) public static void guess(7.85, 42.5, 3);
(A) public static void guess(double rate, double hours, int deductions);
A class method call (A) requires that the class identifier precedes the method identifier. (B) may be called with the method identifier only in certain circumstances. (C) is only possible after a new object is constructed. (D) uses the class identifier only for readability.
(A) requires that the class identifier precedes the method identifier.
The Math.sin, Math.cos and Math.tan methods return __________ values. (A) trigonometric (B) exponential (C) logarithmic (D) radian
(A) trigonometric
You need to write a new Bug method, called leftTurn, which turns 90-degrees counter-clockwise. This method must be created without altering any of the existing Bug methods. Which one of the following leftTurn method implementations satisfies the requirements. (A) public void leftTurn() { turn(); turn(); turn(); } (B) public void leftTurn() { for (int k = 1; k < 7; k++) turn(); } (C) public void leftTurn() { turn(); turn(); turn(); turn(); turn(); } (D) public void leftTurn() { for (int k = 1; k < 4; k++) turn(); }
(B) public void leftTurn() { for (int k = 1; k < 7; k++) turn(); }
What is the output of the following program segment? System.out.println(Math.floor(25.01)); System.out.println(Math.floor(25.49)); System.out.println(Math.floor(25.51)); System.out.println(Math.floor(25.99)); (A) 25 25 26 26 (B) 25 25 25 25 (C) 26 26 26 26 (D) Error message
(B) 25 25 25 25
What is the output of the following program? public class Q34 { public static void main(String args [ ]) { int x = 25; int y = 10; System.out.println(x + " + " + y + " = " + Calc.add(x,y)); System.out.println(x + " - " + y + " = " + Calc.sub(x,y)); System.out.println(x + " * " + y + " = " + Calc.mul(x,y)); System.out.println(x + " / " + y + " = " + Calc.div(x,y)); } } class Calc { public static int add(int p, int q) { int result = p + q; return result; } public static int sub(int p, int q) { int result = p + q; return result; } public static int mul(int p, int q) { return p / q; } public static int div(int p, int q) { return p * q; } } (A) 25 + 10 = 35 (B) 25 + 10 = 15 (C) 25 - 10 = 15 (D) 1 25 - 10 = 15 25 - 10 = 35 25 + 10 = 35 2 25 * 10 = 250 25 * 10 = 2 25 / 10 = 2 3 25 / 10 = 2 25 / 10 = 250 25 * 10 = 250 4 (E) Error message
(B) 25 + 10 = 15 25 - 10 = 35 25 * 10 = 2 25/10 = 250
What is the output of the following program? public class Q31 { public static void main(String args [ ]) { int x = 25; int y = 10; System.out.println(x + " + " + y + " = " + Calc.add(x,y)); System.out.println(x + " - " + y + " = " + Calc.sub(x,y)); System.out.println(x + " * " + y + " = " + Calc.mul(x,y)); System.out.println(x + " / " + y + " = " + Calc.div(x,y)); } } class Calc { public static int add(int p, int q) { int result = p + q; return result; } public static int sub(int p, int q) { int result = p - q; return result; } public static int mul(int p, int q) { return p * q; } public static int div(int p, int q) { return p / q; } } (A) 25 + 10 (B) 25 + 10 = 35 (C) 35 25 - 10 25 - 10 = 15 15 25 * 10 25 * 10 = 250 250 25 / 10 25 / 10 = 2 2 (D) Error message
(B) 25 + 10 = 35
29. What is the output of the following program? public class Q29 { public static void main(String args [ ]) { int x = 25; int y = 10; Calc.add(x,y); Calc.sub(x,y); Calc.mul(x,y); Calc.div(x,y); } } class Calc { public static void add(int p, int q) { int result = p + q; System.out.println(p + " + " + q + " = " + result); } public static void sub(int p, int q) { int result = p - q; System.out.println(p + " - " + q + " = " + result); } public static void mul(int p, int q) { int result = p * q; System.out.println(p + " * " + q + " = " + result); } public static void div(int p, int q) { int result = p / q; System.out.println(p + " / " + q + " = " + result); } } (A) 25 + 10 25 - 10 25 * 10 25 / 10 (B) 25 + 10 = 35 25 - 10 = 15 25 * 10 = 250 25 / 10 = 2 (C) 35 15 250 2 (D) Error message
(B) 25 + 10 = 35 25 - 10 = 15 25 * 10 = 250 25 / 10 = 2
What is the output of the following program? public class Q16 { public static void main(String args [ ]) { Tango.method3(); Tango.method2(); Tango.method1(); } } class Tango { public static void method1() { System.out.println("Calling method 1"); } public static void method2() { System.out.println("Calling method 2"); } public static void method3() { System.out.println("Calling method 3"); } } (A) Calling method 1 Calling method 2 Calling method 3 (B) Calling method 3 Calling method 2 Calling method 1 (C) method 1 method 2 method 3 (D) Error message
(B) Calling method 3 Calling method 2 Calling method 1
Consider the Java program below. public class Q12A { public static void main(String args[ ]) { boohiss(); // line 1 Q12A.boohiss.(); // line 2 Q12B.boohiss(); // line 3 } } class Q12B { public static void boohiss() { System.out.println("Calling boohiss"); } } Which of the statements in the main method is a proper method call? (A) Line 1 only (B) Line 3 only (C) Lines 1 and 2 (D) Lines 1 and 3
(B) Line 3 only
What distinguishes a call to a return method? (A) The method call is the only part of a complete program statement. (B) The method call provides a value, which is used in the program statement. (C) The method call includes the void keyword. (D) The method call includes the return keyword.
(B) The method call provides a value, which is used in the program statement.
The Math.exp(p) method returns the _______________ of argument p. (A) log (B) antilog (C) radian value (D) degrees
(B) antilog
What is the output of the following program? public class Q24 { public static void main(String args [ ]) { int n = 4; method1(n); method2(n + 2, 3); method3(n + n); } public static void method1(int x) { System.out.println("x = " + x); } public static void method2(int x, int y) { System.out.println("x + y = " + (x + y) ); } public static void method3(int n) { int x = 4; System.out.println("x = " + x); } } (A) 4 63 8 (B) x = 4 x + y = 9 x = 4 (C) x = 4 x + y = 63 x = 8 (D) Error message
(B) x = 4 x + y = 9 x = 4
33. What is the output of the following program? public class Q33 { public static void main(String args [ ]) { int x = 25; int y = 10; Calc.add(x,y); Calc.sub(x,y); Calc.mul(x,y); Calc.div(x,y); } } class Calc { public static void add(int p, int q) { int result = p - q; System.out.println(p + " - " + q + " = " + result); } public static void sub(int p, int q) { int result = p + q; System.out.println(p + " + " + q + " = " + result); } public static void mul(int p, int q) { int result = p / q; System.out.println(p + " / " + q + " = " + result); } public static void div(int p, int q) { int result = p * q; System.out.println(p + " * " + q + " = " + result); } } (A) 25 + 10 = 35 (B) 25 + 10 = 15 ###(C) 25 - 10 = 15 (D) 1 25 - 10 = 15 25 - 10 = 35 25 + 10 = 35 2 25 * 10 = 250 25 * 10 = 2 25 / 10 = 2 3 25 / 10 = 2 25 / 10 = 250 25 * 10 = 250 4 (E) Error message
(C) 25 - 10 = 15 25 + 10 = 35 25 / 10 = 2 25 * 10 = 250
What is the output of the following program segment? System.out.println(Math.ceil(25.01)); System.out.println(Math.ceil(25.49)); System.out.println(Math.ceil(25.51)); System.out.println(Math.ceil(25.99)); (A) 25 25 26 26 (B) 25 25 25 25 (C) 26 26 26 26 (D) Error Message
(C) 26 26 26 26
The program file, Lab05a.java, uses two classes, which are Lab05a and Util. Which of the following statements is true about the use of these classes? (A) Each class must be placed in a separate file. (B) Both classes must be placed in the same file. (C) It is possible to place both classes in separates files or in the same file. (D) It is only possible to place the two classes in separate files if you create a package.
(C) It is possible to place both classes in separates files or in the same file.
Which of the following statements is true about the use of parameters with Java methods? (A) Methods without parameters can compile, but will not execute correctly. (B) All method declarations require parameters. (C) Many methods use parameters. (D) The use of parameters is optional to increase program readability.
(C) Many methods use parameters.
What is true about a method declaration with multiple parameters? (A) All parameters must be the same data type. (B) All parameters must be different data types. (C) Parameter types may be the same or they may be different. (D) The parameter declarations depend on the method call.
(C) Parameter types may be the same or they may be different.
Consider the following altered act method of the Bug class. public void act() { if (canMove()) move(); else { turn(); turn(); } } How does a Bug object behave differently when this new act method is used? (A) The object turns counter-clockwise 90-degrees. (B) The object turns counter-clockwise 180-degrees. (C) The object turns clockwise 90-degrees. (D) The object turns clockwise 180-degrees. (E) The object turns clockwise 360-degrees and ends up facing the same direction.
(C) The object turns clockwise 90-degrees.
What distinguishes the declaration of a void method? (A) The public keyword in the method heading (B) The static keyword in the method heading (C) The void keyword in the method heading (D) The main keyword in the method heading
(C) The void keyword in the method heading
What type of methods are used by a Utility class? (A) Void methods only (B) Return methods only (C) Void methods and return methods with or without parameters (D) Void methods and returns methods with parameters only
(C) Void methods and return methods with or without parameters
Which of the following variable names is not a self-commenting identifier? (A) netPay (B) deductions (C) k (D) checkingBalance (E) grossPay
(C) k
Which of the following Math class methods requires more than one parameter? (A) PI (B) ceiling (C) pow (D) toRadians
(C) pow
64. You need to re-write the Bug act method so that it will act just like a Rock object. Which one of the following act method implementations satisfies the requirements. (A) public void act() { Rock.act(); } (B) public void act() { act(Rock); } (C) public void act() { // new act method } (D) public void act() { System.out.println("Act like Rock"); }
(C) public void act() { // new act method }
What is the output of the following program? public class Q36 { public static void main(String args [ ]) { int x = 25; int y = 10; System.out.println(Calc.add(x,y)); System.out.println(Calc.sub(x,y)); System.out.println(Calc.mul(x,y)); System.out.println(Calc.div(x,y)); } } class Calc { public static String add(int p, int q) { int sum = p + q; String result = (String) p + " + " + q + " = " + sum; return "1"; } public static String sub(int p, int q) { int diff = p - q; String result = (String) p + " - " + q + " = " + diff; return "2"; } public static String mul(int p, int q) { int prod = p * q; String result = (String) p + " * " + q + " = " + prod; return "3"; } public static String div(int p, int q) { int quot = p + q; String result = (String) p + " / " + q + " = " + quot; return "4"; } } (A) 25 + 10 = 35 (B) 25 + 10 = 15 (C) 25 - 10 = 15 ###(D) 1 25 - 10 = 15 25 - 10 = 35 25 + 10 = 35 2 25 * 10 = 250 25 * 10 = 2 25 / 10 = 2 3 25 / 10 = 2 25 / 10 = 250 25 * 10 = 250 4 (E) Error message
(D) 1 2 3 4
Which of the following Java keywords are used frequently in the declaration of a class method? (A) void (B) public (C) static (D) All of the above
(D) All of the above
Why would you create and use a Utility class? (A) You can re-use methods, which you created for previous programs. (B) It makes programs less cluttered when frequently needed tasks are kept in a separate file. (C) You can customize such a class for the style programs that you write. (D) All of the above
(D) All of the above
What distinguishes the declaration of a return method? (A) The return keyword in the method body (B) The static keyword in the method heading (C) The data type declaration in the method heading (do not confuse with parameter data types) (D) Both A and C
(D) Both A and C
What is the output of the following program segment? System.out.println(floor(25.01)); System.out.println(ceil(25.01)); (A) 25 26 (B) 25 25 (C) 26 26 (D) Error Message
(D) Error Message
What is the output of the following program? public class Q15 { public static void main(String args [ ]) { method1(); method2(); method3(); } } class Tango { public static void method1() { System.out.println("Calling method 1"); } public static void method2() { System.out.println("Calling method 2"); } public static void method3() { System.out.println("Calling method 3"); } } (A) Calling method 1 Calling method 2 Calling method 3 (B) Calling method 3 Calling method 2 Calling method 1 (C) method 1 method 2 method 3 (D) Error message
(D) Error message
What is the output of the following program? public class Q23 { public static void main(String args [ ]) { Q23.method1(int n = 1); Q23.method2(int n = 2); Q23.method3(int n = 3); } public static void method1(int n) { System.out.println("Calling method " + n); } public static void method2(int n) { System.out.println("Calling method " + n); } public static void method3(int n) { System.out.println("Calling method " + n); } } (A) Calling method1 Calling method2 Calling method3 (B) Calling method1 Calling method3 Calling method2 (C) method1 method2 method3 (D) Error message
(D) Error message
What is the output of the following program? public class Q30 { public static void main(String args [ ]) { int x = 25; int y = 10; System.out.println(Calc.add(x,y)); System.out.println(Calc.sub(x,y)); System.out.println(Calc.mul(x,y)); System.out.println(Calc.div(x,y)); } } class Calc { public static void add(int p, int q) { int result = p + q; System.out.println(p + " + " + q + " = " + result); } public static void sub(int p, int q) { int result = p - q; System.out.println(p + " - " + q + " = " + result); } public static void mul(int p, int q) { int result = p * q; System.out.println(p + " * " + q + " = " + result); } public static void div(int p, int q) { int result = p / q; System.out.println(p + " / " + q + " = " + result); } } (A) 25 + 10 (B) 25 + 10 = 35 (C) 35 25 - 10 25 - 10 = 15 15 25 * 10 25 * 10 = 250 250 25 / 10 25 / 10 = 2 2 (D) Error message
(D) Error message
Where must the gridworld.jar be located for a GridWorld program to function properly? I. In the same folder as the GridWorld project II. Anywhere, as long as a library file is attached with the path to the gridworld.jar file. III. At the AP Central website of the CollegeBoard (A) I only (B) II only (C) III only (D) I and II only (E) I, II and III
(D) I and II only
Consider the following altered act method of the Bug class. public void act() { if (canMove()) { move(); move(); } else turn(); } What are the consequences for the Bug's behavior with the new act method? I. The bug may move outside the boundary. II. The bug will make 45-degree clockwise turns. III. The bug may run into other objects. (A) I only (B) II only (C) I and II only (D) I and III only (E) I, II and III
(D) I and III only
Consider the Java program below. public class Q11 { public static void main(String args[ ]) { boohiss(); // line 1 boohiss.Q11(); // line 2 Q11.boohiss(); // line 3 } public static void boohiss() { System.out.println("Calling boohiss"); } } Which of the statements in the main method is a proper method call? (A) Line 1 only (B) Line 2 only (C) Line 3 only (D) Lines 1 and 3
(D) Lines 1 and 3
What types of methods should be placed in a user-declared Utility class? (A) Math class methods only (B) All types of practical methods found in the Math class, DecimalFormat and others like it. (C) Only those methods that are necessary to the program using the Utility class. (D) Methods that are often used in many programs, like Center, Skip, Justify, etc
(D) Methods that are often used in many programs, like Center, Skip, Justify, etc
What is the output of the following program? public class Q32 { public static void main(String args [ ]) { int x = 25; int y = 10; Calc.add(x,y); Calc.sub(x,y); Calc.mul(x,y); Calc.div(x,y); } } class Calc { public static int add(int p, int q) { int result = p + q; return result; } public static int sub(int p, int q) { int result = p - q; return result; } public static int mul(int p, int q) { return p * q; } public static int div(int p, int q) { return p / q; } } (A) 25 + 10 (B) 25 + 10 = 35 (C) 35 25 - 10 25 - 10 = 15 15 25 * 10 25 * 10 = 250 250 25 / 10 25 / 10 = 2 2 (D) No output
(D) No output
The Math.toDegrees(p) method returns the _______________ of the ______________ value of p. (A) log exponential (B) antilog radian (C) radian degrees (D) degrees radian
(D) degrees radian
Which of the following method calls might use parameters correctly? (A) guess(double rate, double hours, int deductions); (B) guess(double rate, hours, int deductions); (C) guess(int rate, hours, deductions); (D) guess(7.85, 42.5, 3);
(D) guess(7.85, 42.5, 3);
Which of the following are fundamental program design considerations? (A) Programs should use self-commenting identifiers. (B) Control structures need to use a consistent indentation style. (C) Specific tasks should be placed in modules, called methods. (D) The main method should be used for program sequence, not large numbers of program statements. (E) All of the above
(E) All of the above
The parameters in the method call and the method heading must be the same I. quantity. II. sequence. III. type. (A) I only (B) II only (C) I & II only (D) II & III only (E) I, II & III
(E) I, II & III
What is the output of the following program? public class Q13 { public static void main(String args [ ]) { method1(); method2(); method3(); } public static void method1() { System.out.println("Calling method 1"); } public static void method2() { System.out.println("Calling method 2"); } public static void method3() { System.out.println("Calling method 3"); } } (A) Calling method 1 Calling method 2 Calling method 3 (B) Calling method 3 Calling method 2 Calling method 1 (C) method 1 method 2 method 3 (D) Error message
A) Calling method 1 Calling method 2 Calling method 3