Vous êtes sur la page 1sur 21

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 05 Methods Multiple Choice

1. Methods are commonly used to a. aid in developing the logic to program a problem b. break a problem down into small manageable pieces c. emphasize certain parts of the logic d. document the program ANS: B 2. Which of the following is not a benefit derived from using methods in programming a. problems are more easily solved b. simplifies programs c. code reuse d. all of the above are benefits ANS: D 3. A ___ method performs a task and sends a value back to the code that called it. a. value-returning b. void c. complex d. local ANS: A 4. In the following code, System.out.println(num), is an example of ____. double num 5.4; System.out.println(num); num 0.0; a. a value-returning method b. a void method c. a complex method d. a local variable ANS: B 5. In the method header, the method modifier, public, means that the method belongs to the class, not a specific object. a. True b. False ANS: B 6. The ____ is a collection of statements that are performed when the method is executed. a. method header b. return type

c. d. ANS: C

method body method modifier

7. Which of the following is not part of a method call? a. method name b. return type c. parentheses d. all of the above are part of a method call ANS: B 8. If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens? a. control is returned to method A b. control is returned to method B c. control is returned to method C d. the program terminates ANS: C 9. When an argument is passed to a method, a. its value is copied into the methods parameter variable b. its value may be changed within the called method c. values may not be passed to methods d. the method must not assign another value to the parameter that receives the argument ANS: A 10. Constants, variables, and the values of expressions may be passed as arguments to a method. a. True b. False ANS: A 11. What is wrong with the following method call? displayValue (double x); a. There is nothing wrong with the statement b. displayValue will not accept a parameter c. Do not include the data type in the method call d. x should be a string ANS: C 12. Given the following method header, which of the method calls would be an error? public void displayValues(int x, int y) a. displayValue(a,b); where a is a short and b is a byte b. displayValue(a,b); where a is an integer and b is a byte c. displayValue(a,b); where a is a short and b is a long d. they would all give an error

ANS: C 13. A parameter variables scope is the method in which the parameter is declared. a. True b. False ANS: A 14. Which of the following would be a valid method call for the following method? public static void showProduct (int num1, double num2) { int product; product num1 * (int)num2; System.out.println(The product is product); } a. showProduct(5.5, 4.0); b. showProduct(10.0, 4); c. showProduct(10, 4.5); d. showProduct(33, 55); ANS: C 15. When an object, such as a String, is passed as an argument, it is a. Actually a reference to the object that is passed b. Passed by value like any other parameter value c. Encrypted d. Necessary to know exactly how long the string is when writing the program ANS: A 16. All @param tags in a methods documentation comment must a. End with a */ b. Appear after the general description of the method c. Appear before the method header d. Span several lines ANS: B 17. The lifetime of a methods local variable is a. The duration of the program b. The duration of the class to which the method belongs c. The duration of the method that called the local variables method d. Only while the method is executing ANS: D 18. Local variables a. Are hidden from other methods b. May have the same name as local variables in other methods c. Lose the values stored in them between calls to the method in which the variable is declared

d. ANS: D

All of the above

19. You must specify the ____ of the return value in the method header. a. Called methods local variable name b. Name of the variable in the calling program that will receive the returned value c. Data type d. All of the above ANS: C 20. You must have a return statement in a value-returning method. a. True b. False ANS: A 21. What will be returned from the following method? public static double MethodA() { double a 8.5 9.5; return a; } a. 18.0 b. 18 c. 8 d. This is an error ANS: A 22. In a @return tag statement the description a. Cannot be longer than one line b. Describes the return value c. Must be longer than one line d. Describes the parameter values ANS: B 23. When a method tests an argument and returns a true or false value, it should return a. A zero for true and a one for false b. A boolean value c. A zero for false and a non-zero for true d. A method should not be used for this type test ANS: B 24. Functional decomposition is a. The backbone of the scientific method b. The process of decomposing functions c. The process of breaking a problem down into smaller pieces

d. ANS: C

The process of dead plants decomposing and turning back into soil

25. Any method that calls a method with a throws clause in its header must either handle the potential exception or have the same throws clause. a. True b. False ANS: A 26. In a general sense, a method is a. a plan b. a statement inside a loop c. a comment d. a collection of statements that performs a specific task ANS: D 27. Breaking a program down into small manageable methods a. makes problems more easily solved b. allows for code reuse c. simplifies programs d. all of the above ANS: D 28. A ____ method performs a task and then terminates. a. value-returning b. void c. local d. simple ANS: B 29. In the following code, Integer.parseInt(st1), is an example of ____. int num; string st1 555; num Integer.parseInt(st1) 5; a. a value-returning method b. a void method c. a local variable d. a complex method ANS: A 30. In the method header the method modifier, static, means the method is available to code outside the class. a. True b. False

ANS: B 31. Which of the following is not a part of the method header? a. return type b. method name c. parentheses d. semicolon ANS: D 32. Which of the following is always included in a method call? a. return type b. method modifiers c. parentheses d. return variable ANS: C 33. You should always document a method by writing comments that appear a. just before the methods definition b. just after the methods definition c. inside the method d. only if the method is more than five lines long ANS: A 34. When an argument value is passed to a method, the receiving parameter variable is a. declared within the body of the method b. declared in the method header inside the parentheses c. declared in the calling method d. uses the declaration of the argument ANS: B 35. Only constants and variables may be passed as arguments to methods. a. True b. False ANS: B 36. What will be the result of the following code? int num; string st1 555; num Integer.parseInt(string st1) 5; a. num will be set to 560 b. st1 will have a value of 560 c. the last line of code will cause an error d. neither num or st1 will be changed ANS: C

37. No statement outside the method in which a parameter variable is declared can access the parameter by its name. a. True b. False ANS: A 38. Which of the following would be a valid method call for the following method? public static void showProduct(double num1, int num2) { double product; product num1 * num2; System.out.println(The product is product); } a. showProduct(5, 40); b. showProduct(10.0, 4.6); c. showProduct(10, 4.5); d. showProduct(3.3, 55); ANS: D 39. When a String object is passed as an argument, it is actually a reference to the object that is passed, which means a. The String object can be modified b. The String object can be set to point to a different string literal c. Any changes to the String reference will not change the values in the calling program d. This is another term for passing by value ANS: B 40. When writing the documentation comments for a method, you can provide a description of each parameter by using a a. @comment tag b. @doc tag c. @param tag d. @return tag ANS: C 41. Values stored in local variables a. Are lost between calls to the method in which they are declared b. Retain their values from the last call to the method in which they are declared c. May be referenced by the calling method d. May be referenced by any other method, if the method in which they are declared is a public method ANS: A 42. Local variables cannot be initialized with a. Constants b. Parameter values

c. d. ANS: D

The results of an arithmetic operation They may initialized with any of the above

43. A value-returning method must use ___ in its header. a. An integer b. A double c. A boolean d. Any valid data type ANS: D 44. The expression in a return statement can be any expression that has a value of the same data type as the return type. a. True b. False ANS: A 45. What will be returned from the following method? public static int methodA() { double a 8.5 9.5; return a; } a. 18.0 b. 18 c. 8 d. This is an error ANS: D 46. To document the return value of a method, use a. The @param tag b. The @comment tag c. The @return tag d. The // comment line ANS: C 47. A value-returning method can return a reference to a non-primitive type. a. True b. False ANS: A 48. The process of breaking a problem down into smaller pieces is called a. Functional decomposition b. Scientific method c. Top-down programming

d. ANS: A

Whole-into-part

49. Any method that calls a method with a throws clause in its header must a. Handle the potential exception b. Have the same throws clause c. (a) or (b) d. Do nothing, the called program will take care of the throws clause ANS: C

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 06 A First Look At Classes Multiple Choice
1. One or more objects may be created from a(n)_____. a. field b. class c. method d. instance ANS: B 2. Class objects normally have _____ that perform useful operations on their data, but primitive variables do not. a. fields b. instances c. methods d. relationships ANS: C 3. In the cookie cutter method: Think of the _____ as a cookie cutter and _____ as the cookies. a. object; classes b. class; objects c. class; fields d. field; methods ANS: B 4. A UML diagram does not contain _____.
a. b. c. d. class name methods fields object names

ANS: D

5. An access specifier indicates how the class may be accessed. a. True b. False ANS: A 6. Data hiding, which means that critical data stored inside the object is protected from code outside the object is accomplished in Java by _____. a. using the public access specifier on the class methods b. using the private access specifier on the class methods c. using the private access specifier on the class definition d. using the private access specifier on the class fields ANS: D 7. For the following code, which statement is not true? public class Sphere { private double radius; public double x; private double y; private double z; } a. x is available to code that is written outside the Sphere class. b. radius is not available to code written outside the Sphere class. c. radius, x, y, and z are called members of the Sphere class. d. z is available to code that is written outside the Sphere class. ANS: D 8. Which of the following is not part of the method header? a. Method name b. Return type c. Access specifier d. Parameter variable declaration e. All of the above are parts of the method header ANS: E 9. A method that stores a value in a classs field or in some other way changes the value of a field is known as a mutator method. a. True b. False ANS: A 10. You should not define a class field that is dependent upon the values of other class fields _____. a. in order to avoid having stale data

b. c. d. ANS: A

because it is redundant because it should be defined in another class in order to keep it current

11. The following UML diagram entry means _____ setHeight(h : double) : void a. this is a public field called Height and is a double data type b. this is a private method with no parameters and returns a double data type c. this is a private field called Height and is a double data type d. this is a public method with a parameter of data type double and does not return a value ANS: D 12. Instance methods should be declared static. a. True b. False ANS: B 13. Methods that operate on an objects fields are called a. instance variables b. instance methods c. public methods d. private methods ANS: B 14. The scope of a private instance field is a. the instance methods of the same class b. inside the class, but not inside any method c. inside the parentheses of a method header d. the method in which they are defined ANS: A 15. A constructor is a method that is automatically called when an object is created. a. True b. False ANS: A 16. A constructor a. always accepts two arguments b. has return type of void c. has the same name as the class d. always has an access specifier of private ANS: C

17. Shadowing is the term used to describe where the field name is hidden by the name of a local or parameter variable. a. True b. False ANS: A 18. Which of the following statements will create a reference, str, to the String, Hello, World? a. String str Hello, World; b. string str Hello, World; c. String str new Hello, World; d. str Hello, World; ANS: A 19. Two or more methods in a class may have the same name as long as a. they have different return types b. they have different parameter lists c. they have different return types, but the same parameter list d. you cannot have two methods with the same name ANS: B 20. Given the following code, what will be the value of finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum orderNumber; orderAmount orderAmt; orderDiscount orderDisc; } } public class CustomerOrder { public static void main(String[] args) { int ordNum 1234; double ordAmount 580.00; double discountPer 0.1; Order order; double finalAmount order.orderAmount order.orderAmount * order.orderDiscount; System.out.println(Final order amount $ finalAmount); } } a. 528.00 b. 580.00

c. d. ANS: D

There is no value because the constructor has an error. There is no value because the object order has not been created.

21. A class specifies the _____ and _____ that a particular type of object has. a. relationships; methods b. fields; object names c. fields; methods d. relationships; object names ANS: C 22. ____ refers to the combining of data and code into a single object. a. Data hiding b. Abstraction c. Object d. Encapsulation ANS: D 23. Another term for an object of a class is ____. a. access specifier b. instance c. member d. method ANS: B 24. In this book the general layout of a UML diagram is a box that is divided into three sections. The top section has the ____; the middle section holds ____; the bottom section holds ____. a. class name; fields; methods b. class name; object name; methods c. object name; fields; methods d. object name; methods; fields ANS: A 25. The public access specifier for an field indicates that the field may not be accessed by statements outside the class. a. True b. False ANS: B 26. For the following code, which statement is not true? public class Circle { private double radius; public double x; private double y;

} a. b. c. d. ANS: D

x is available to code that is written outside the Circle class. radius is not available to code written outside the Circle class. radius, x, and y are called members of the Circle class. y is available to code that is written outside the Circle class.

27. It is common practice in object-oriented programming to make all of a classs _____. a. methods private b. fields private c. fields public d. fields and methods public ANS: B 28. After the header, the body of the method appears inside a set of a. brackets, [] b. paretheses, () c. braces, {} d. double quotes, ANS: C 29. A method that gets a value from a classs field but does not change it is known as a mutator method. a. True b. False ANS: B 30. In UML diagrams, a ____ indicates the member is private and a _____ indicates the member is public. a. *; / b. #; @ c. ; d. (); : ANS: C 31. In a UML diagram to indicate the data type of a variable enter a. the variable name followed by the data type b. the variable name followed by a colon and the data type c. the class name followed by the variable name followed by the data type d. the data type followed by the variable name ANS: B 32. Instance methods do not have the key word static in their headers. a. True b. False ANS: A

33. When an object is created, the fields associated with the object are called a. instance fields b. instance methods c. fixed fields d. class instances ANS: A 34. A constructor is a method that a. returns an object of the class. b. never receives any arguments. c. with the name (class name).constructor. d. performs initialization or setup operations. ANS: D 35. The term default constructor is applied to any constructor that does not accept arguments. a. True b. False ANS: B 36. The scope of a public instance field is a. only the class in which it is defined b. inside the class, but not inside any method c. inside the parentheses of a method header d. the instance methods and methods outside the class ANS: D 37. When a local variable in an instance method has the same name as an instance field, the instance field hides the local variable. a. True b. False ANS: B 38. Which of the following statements will create a reference, str, to the string, Hello, world? A. String str new String(Hello, World); B. String str Hello, world; a. A b. B c. A and B d. Neither A or B ANS: C 39. Overloading means multiple methods in the same class a. have the same name, but different return types

b. c. d. ANS: C

have different names, but the same parameter list have the same name, but different parameter lists perform the same function

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 09 A Second Look at Classes Multiple Choice
1. An instance of a class does not have to exist in order for values to be stored in a classs static fields. a. True b. False ANS: A 2. A static field is created by placing the key word static a. After the field name b. After the access specifier and before the fields data type c. After the access specifier and fields data type d. It in a static field block ANS: B 3. Which of the following is not true about static methods? a. It is not necessary for an instance of the class to be created to execute the method. b. They are created by placing the key word static after the access specifier in the method header. c. They are called from an instance of the class. d. They are often used to create utility classes that perform operations on data, but have no need to collect and store data. ANS: C 4. The only limitation that static methods have is a. They can refer to only non-static members of the class b. They can only be called from static members of the class c. They must be declared as public methods d. They cannot refer to non-static members of the class ANS: D 5. If you have defined class SavingsAccount, public static data member numberOfAccounts, and SavingsAccount object account20, which of the following will assign numberOfAccounts to numAccounts? a. numAccounts account20.numberOfAccounts;

b. c. d. ANS: C

numAccounts numberOfAccounts; numAccounts SavingsAccount.numberOfAccounts; None of the above, you cannot reference a static data member.

6. When an object is passed as an argument, it is actually a reference to the object that ispassed. a. True b. False ANS: A 7. When a methods return type is a class, what is actually returned to the calling program? a. An object of that class b. A reference to an object of that class c. Only the values in the object that the method accessed d. Nothing, the return type is strictly for documentation in this situation ANS: B 8. If the following is from the method section of a UML diagram, which of the following statements is true? equals(object2:FeetInches) : boolean a. This is a private method that receives two objects from the FeetInches class and returns a boolean value b. This is a public method that accepts a FeetInches object as its argument and returns a boolean value c. This is a private method that returns a boolean value d. This is a public method that returns a string ANS: B 9. If you write a toString method to display the contents of an object, object1, for a class, Class1, then the following two statements are equivalent: System.out.println(object1); System.out.println(object1.toString()); a. True b. False ANS: A 10. To compare two objects in a class, a. Use the , e.g. object1 object2 b. Write a method to do a byte-by-byte compare of the two objects c. Write an equals method that will make a field by field compare of the two objects d. Since objects consist of several fields, you cannot compare them ANS: C 11. If object1 and object2 are objects of the same class, to make object2 a copy of object1

a. Assign object1 to object2, object2 object1; b. Write a copy method that will make a field by field copy of object1 data members into object2 data members c. Use the Java copy method that is a part of the Java language d. Use the default constructor to create object2 with object1 data members ANS: B 12. A deep copy of an object a. Is an assignment of that object to another object b. Is an operation that duplicates the object itself c. Is a bogus term, it has no meaning d. Is always a private method ANS: B 13. The term for the relationship created by object aggregation is a. Has a b. Is a c. Sub-class object d. Inner class ANS: A 14. The this key word is the name of a reference variable that is available to all static methods. a. True b. False ANS: A 15. If the this variable is used as a constructor call, a. A compiler error will result, if it is not the first statement of the constructor b. A compiler error will result, if it is the first statement of the constructor c. A compiler error will result d. The this variable cannot be used as a constructor call ANS: A 16. When you write and enumerated type declaration, you a. Should use the standard convention of writing the enum constants in uppercase b. Do not enclose the enum constants in quotation marks c. Are actually creating a special kind of class d. All of the above ANS: D 17. To get the name of a calling enum constant a. Write your own toString method b. Use the toString method that automatically comes with enum constants c. Simply use the enum constant in the statement d. Use the ordinal() method that automatically comes with the enum constants

ANS: B 18. If a class has a method named finalize, it is called automatically just before a data member that has been identified as final of the class is destroyed by the garbage collector. a. True b. False ANS: A 19. A classs static methods do not operate on the fields that belong to any instance of the class. a. True b. False ANS: A 20. When a field is declared static, there will be a. A copy of the field in each class object b. Only one copy of the field in memory c. A copy of the field for each static method in the class d. Only two copies of the field in memory ANS: B 21. Which of the following is not true about static methods? a. It is necessary for an instance of the class to be created to execute the method. b. They are created by placing the key word static after the access specifier in the method header. c. They are called directly from the class. d. They are often used to create utility classes that perform operations on data, but have no need to collect and store data. ANS: A 22. The only limitation that static methods have is a. They can refer to only non-static members of the class b. They can only be called from static members of the class c. They must be declared as public methods d. They cannot refer to non-static members of the class ANS: D 23. If you have defined class SavingsAccount, public static method getNumberOfAccounts(), and SavingsAccount object account20, which of the following will call getNumberOfAccounts()? a. getNumberOfAccounts(); b. SavingsAccount.getNumberOfAccounts(); c. account20.getNumberOfAccounts(); d. None of the above, you cannot call a static method. ANS: B 25. What will be returned from a method, if the following is the method header:

public Rectangle getRectangle() a. An object in the class Rectangle b. The address of an object in the class Rectangle c. The values stored in the data members of the rectangle object the method changed d. A graph of a rectangle ANS: B 26. If the following is from the method section of a UML diagram, which of the following statements is true? add(object2:FeetInches):FeetInches a. This is a private method called add that accepts and returns objects in the FeetInches class b. This is a private method called FeetInches that adds two objects c. This is a public method called add that accepts and returns references to objects in the FeetInches class d. This is a public method called FeetInches that adds two objects ANS: C 27. If you write a toString method for a class, Java will automatically call the method any time you concatenate an object of the class with a string. a. True b. False ANS: A 28. To compare two objects in a class, a. Use the , e.g. object1 object2 b. Write a method to do a byte-by-byte compare of the two objects c. Write an equals method that will make a field by field compare of the two objects d. Since objects consist of several fields, you cannot compare them ANS: C 29. To copy an object to another object in the same class a. Use the assignment statement, e.g. object1 object2 b. Write a copy method that will make a field by field assignment of the two objects c. Since objects consist of several fields, you cannot copy them d. Write a method to do a byte-by-byte assignment of the two objects ANS: B 30. To make a deep copy of object1 as object2, a. Use the assignment statement, object1 object2 b. Write a copy method that passes the arguments of object1 to the constructor for object2 c. Write a copy method that will assign each field in object1 to corresponding fields in object2 d. Write a copy method that will do a byte-by-byte copy of object1 to object2 ANS: B

31. The whole-part relationship created by object aggregation is more often called a. A has a relationship b. An inner class relationship c. An extra class relationship d. An inside class relationship ANS: A 32. The key word this is the name of a reference variable that an object can use to refer to itself. a. True b. False ANS: A 33. When the this variable is used as a constructor call, a. It must be the first statement in the constructor making the call b. It must be the last statement in the constructor making the call c. It can be anywhere in the constructor making the call d. You cannot use the this variable in a constructor call ANS: A 34. You can use the enum key word to a. Create your own data type b. Specify the values that belong to that type c. Both (a) and (b) d. Neither (a) or (b) ANS: C 35. You may use ____ to compare two enum data values a. Only , >, < b. The equals and compareTo methods that automatically come with the enum data type c. The moreThan, lessThan, equalsTo methods that automatically come with the enum data type d. The ordinal() method that automatically comes with the enum constants ANS: B 36. If a class has a method named finalize, it is called automatically just before an instance of the class is destroyed by the garbage collector. a. True b. False ANS: A

Vous aimerez peut-être aussi