Java 195 final

¡Supera tus tareas y exámenes ahora con Quizwiz!

1) The increment operator is: A) ++ B) -- C) *= D) -=

Answer: A

11) When an argument is passed to a method: A) its value is copied into the method's 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

Answer: A

12) What would be the results of the following code? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] > x[5]) a = 5; else a = 8; A) a = 5 B) a = 8 C) a = 10 D) This is a compilation error, you cannot compare array elements.

Answer: A

12) 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) because it is redundant C) because it should be defined in another class D) in order to keep it current

Answer: A

15) 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

Answer: A

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

Answer: A

43) Look at the following statement. import java.util.*; This is an example of: A) a wildcard import B) an explicit import C) unconditional import D) conditional import

Answer: A

44) Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file? A) int number = inputFile.nextInt(); B) int number = inputFile.next(); C) int number = inputFile.readInt(); D) int number = inputFile.integer();

Answer: A

46) The process of breaking a problem down into smaller pieces is sometimes called: A) divide and conquer B) scientific method C) top-down programming D) whole-into-part

Answer: A

48) This ArrayList class method deletes an item from an ArrayList. A) remove B) delete C) erase D) purge

Answer: A

5) Subscript numbering always starts at what value? A) 0 B) 1 C) -1 D) None of the above

Answer: A

18) 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

Answer: B

21) Before entering a loop to compute a running total, the program should first do this. A) Read all the values into main memory B) Set the accumulator where the total will be kept to an initial value, usually zero C) Know exactly how many values there are to total D) Set all variables to zero

Answer: B

22) Another term for an object of a class is: A) access specifier B) instance C) member D) method

Answer: B

40) The binary search algorithm: A) is less efficient than the sequential search algorithm B) will cut the portion of the array being searched in half each time the loop fails to locate the search value C) will have a maximum number of comparisons equal to the number of elements in the array D) will have an average of N/2 comparisons, where N is the number of elements in the array

Answer: B

40) This is a group of related classes. A) archive B) package C) collection D) attachment

Answer: B

41) This type of loop is ideal in situations where you always want the loop to iterate at least once. A) while loop B) do-while loop C) for loop D) if statement

Answer: B

15) What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x > 5); A) 10 B) 200 C) This is an infinite loop. D) The loop will not be executed, the initial value of x > 5.

Answer: C

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

Answer: C

17) A loop that repeats a specific number of times is known as a(n): A) sentinel loop B) conditional loop C) counter-controlled loop D) infinite loop

Answer: C

17) What do you normally use with a partially-filled array? A) A class that does nothing but manage the array B) An accompanying parallel array C) An accompanying integer value that holds the number of items stored in the array D) An accumulator

Answer: C

18) How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!"); A) 1 B) 10 C) 12 D) 0

Answer: C

11) 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 Circle class. B) radius is not available to code written outside the Circle class. C) radius, x, y, and z are called members of the Circle class. D) z is available to code that is written outside the Circle class.

Answer: D

11) What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; } A) 90 B) 110 C) 210 D) This is an infinite loop.

Answer: D

13) What does the following UML diagram entry mean? + setHeight(h : double) : void A) this is a public attribute named 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 attribute named 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

Answer: D

15) When an array is passed to a method: A) a reference to the array is passed B) it is passed just as an object C) the method has direct access to the original array D) All of the above

Answer: D

20) This is a value that signals when the end of a list of values has been reached. A) Terminal value B) Final value C) End value D) Sentinel

Answer: D

27) What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--; A) x = 12, y = 5 B) x = 16, y = 4 C) x = 17, y = 5 D) x = 17, y = 4

Answer: D

T/F: 3) The do-while loop is a pre-test loop.

Answer: FALSE

T/F: 6) A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.

Answer: TRUE

T/F: 6) You must have a return statement in a value-returning method.

Answer: TRUE

T/F: 7) Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more arguments that are to be passed into the main method.

Answer: TRUE

T/F: 7) 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.

Answer: TRUE

T/F: 7) Shadowing is the term used to describe where the field name is hidden by the name of a local or parameter variable.

Answer: TRUE

T/F: 7) The do-while loop must be terminated with a semicolon.

Answer: TRUE

T/F: 9) When the continue statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

Answer: TRUE

T/F; 6) You can use the PrintWriter class to open a file for writing and write data to it.

Answer: TRUE

T/F: 14) An ArrayList object automatically expands in size to accommodate the items stored in it.

`Answer: TRUE

17) 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";

Answer: A

18) When you pass an argument to a method, be sure that the argument's data type is compatible with: A) the parameter variable's data type B) the method's return type C) the version of Java currently being used D) IEEE standards

Answer: A

19) A parameter variable's scope is: A) the method in which the parameter is declared B) the class to which the method belongs C) the main method D) All of the above

Answer: A

19) In memory, an array of String objects: A) consists of elements, each of which is a reference to a String object B) is always implemented as a ragged array C) consists of elements, each of which is a String object D) must be initialized when the array is declared

Answer: A

19) What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y; A) 40 B) 25 C) 30 D) Invalid for statement

Answer: A

23) In your textbook 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; attributes or fields; methods B) class name; object name; methods C) object name; attributes or fields; methods D) object name; methods; attributes or fields

Answer: A

23) What is the value of scores[2][3] in the following array? int [] [] scores = { {88, 80, 79, 92}, {75, 84, 93, 80}, {98, 95, 92, 94}, {91, 84, 88, 96} }; A) 94 B) 84 C) 93 D) 95

Answer: A

24) 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 (as an integer) C) 8 D) This is an error.

Answer: A

25) Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents? A) FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter); B) FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter); C) PrintWriter outfile = new PrintWriter("MyFile.txt", true); D) PrintWriter outfile = new PrintWriter(true, "MyFile.txt");

Answer: A

28) What will be the value of x after the following code is executed? int x, y = 15, z = 3; x = (y--) / (++z); A) 3 B) 4 C) 5 D) 6

Answer: A

29) What will be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5}; A) An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created. B) A compilation error will occur. C) The program will crash when it is executed. D) The value of x[1] will be 0, x[2] will be 0, x[3] will be 0, x[4] will be 0, x[5] will be 0, and x[6] will be 0.

Answer: A

3) This type of method performs a task and sends a value back to the code that called it. A) value-returning B) void C) complex D) local

Answer: A

30) When an object is created, the attributes associated with the object are called: A) instance fields B) instance methods C) fixed attributes D) class instances

Answer: A

31) In the following code, Integer.parseInt(str), is an example of: int num; string str = "555"; num = Integer.parseInt(str) + 5; A) a value-returning method B) a void method C) a local variable D) a complex method

Answer: A

34) You should always document a method by writing comments that appear: A) just before the method's definition B) just after the method's definition C) at the end of the file D) only if the method is more than five lines long

Answer: A

36) If you attempt to use a local variable before it has been given a value: A) a compiler error will occur B) the local variable will always contain the value 0 C) the results will be unpredictable D) the local variable will be ignored

Answer: A

39) A search algorithm: A) is a way to locate a specific item in a larger collection of data B) is rarely used with arrays C) arranges elements in ascending order D) arranges elements in descending order

Answer: A

39) This is a sum of numbers that accumulates with each iteration of a loop. A) Running total B) Final total C) Grand finale D) Galloping total

Answer: A

4) What do you call the number that is used as an index to pinpoint a specific element within an array? A) subscript B) global unique identifier C) element D) argument

Answer: A

41) Quite often you have to use this statement to make a group of classes available to a program. A) import B) use C) link D) assume

Answer: A

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

Answer: A

49) You use this method to determine the number of items stored in an ArrayList object. A) numberItems B) capacity C) size D) items

Answer: A

5) When you are working with a ________, you are using a storage location that holds a piece of data. A) primitive variable B) reference variable C) numeric literal D) binary number

Answer: A

50) The following statement creates an ArrayList object. What is the purpose of the <String> notation? ArrayList<String> arr = new ArrayList<String>(); A) It specifies that only String objects may be stored in the ArrayList object. B) It specifies that the get method will return only String objects. C) It specifies that String objects may not be stored in the ArrayList object. D) It specifies that everything stored in the ArrayList object will be converted to a String.

Answer: A

6) By default, Java initializes array elements with what value? A) 0 B) 100 C) 1 D) -1

Answer: A

6) Each repetition of a loop is known as what? A) An iteration B) A cycle C) An execution D) A Lap

Answer: A

6) In the header, the method name is always followed by this: A) parentheses B) return type C) data type D) braces

Answer: A

9) If you are using a block of statements, don't forget to enclose all of the statements in a set of: A) Braces B) Double quotes C) Semicolons D) Parentheses

Answer: A

1) Methods are commonly used to: A) speed up the compilation of a program B) break a problem down into small manageable pieces C) emphasize certain parts of the logic D) document the program

Answer: B

1) One or more objects may be created from a(n): A) field B) class C) method D) instance

Answer: B

1) This indicates the number of elements, or values, the array can hold. A) the new operator B) the array's size declarator C) the array's data type D) the version of Java

Answer: B

10) Values that are sent into a method are called: A) variables B) arguments C) literals D) types

Answer: B

10) What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; } A) 90 B) 100 C) 110 D) This is an infinite loop.

Answer: B

12) ________ is the process of inspecting data given to the program by the user and determining if it is valid. A) Data parsing B) Input validation C) User authentication D) Defensive coding

Answer: B

13) What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; for(int a = 0; a < x.length; a++) { x[a] = y[a]; y[a] = x[a]; } A) x[] = {36, 78, 12, 24} and y[] = {23, 55, 83, 19} B) x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24} C) x[] = {23, 55, 83, 19} and y[] = {23, 55, 83, 19} D) This is a compilation error.

Answer: B

14) Methods that operate on an object's fields are called: A) instance variables B) instance methods C) public methods D) private methods

Answer: B

16) All @param tags in a method's 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

Answer: B

16) How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100); A) 0 B) 1 C) 4 D) 5

Answer: B

16) What would be the results of the following code? int[] array1 = new int[25]; ... // Code that will put values in array1 int value = array1[0]; for (int a = 1; a < array1.length; a++) { if (array1[a] < value) value = array1[a]; } A) Value contains the highest value in array1. B) Value contains the lowest value in array1. C) Value contains the sum of all the values in array1. D) Value contains the average of the values in array1.

Answer: B

17) A special variable that holds a value being passed into a method is called what? A) Modifier B) Parameter C) Alias D) Argument

Answer: B

18) To return an array of long values from a method, use this as the return type for the method. A) long B) long[] C) long[ARRAY_SIZE] D) []long

Answer: B

22) In order to do a binary search on an array: A) the values of the array must be numeric B) the array must first be sorted in ascending order C) you must first do a sequential search of the array to assure the element you are looking for is there D) there are no requirements

Answer: B

24) When using the PrintWriter class, which of the following import statements would you write near the top of your program? A) import javax.swing.*; B) import java.io.*; C) import PrintWriter; D) import java.file.*;

Answer: B

25) 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

Answer: B

25) It is common practice in object-oriented programming to make all of a class's: A) methods private B) fields private C) fields public D) fields and methods public

Answer: B

25) Which of the following is a correct method header for receiving a two-dimensional array as an argument? A) public static void passArray(int[1,2]) B) public static void passArray(int [][]) C) public static void passArray(int[1],[2]) D) public static void passArray(int[], int[])

Answer: B

26) 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

Answer: B

29) 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

Answer: B

3) In the cookie cutter metaphor, think of the ________ as a cookie cutter and ________ as the cookies. A) object; classes B) class; objects C) class; fields D) attribute; methods

Answer: B

3) What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); A) 24 B) 28 C) 30 D) 35

Answer: B

30) This type of method performs a task and then terminates. A) value-returning B) void C) local D) simple

Answer: B

31) What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; x = y; y = x; A) x[] = {36, 78, 12, 24} and y[] = {23, 55, 83, 19} B) x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24} C) x[] = {23, 55, 83, 19} and y[] = {23, 55, 83, 19} D) This is a compilation error.

Answer: B

31) When an object is passed as an argument to a method, what is passed into the method's parameter variable? A) the class name B) the object's memory address C) the values for each field D) the method names

Answer: B

32) What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x[1]); ... public static void arrayProcess(int a) { a = a + 5; } A) 27 B) 33 C) 38 D) 49

Answer: B

34) What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x < 5); A) 10 B) 200 C) This is an infinite loop. D) The loop will not be executed, the initial value of x > 5.

Answer: B

35) 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

Answer: B

36) A loop that executes as long as a particular condition exists is called a(n): A) sentinel loop B) conditional loop C) count-controlled loop D) infinite loop

Answer: B

36) What will be returned from the following method? public static float[] getValue(int x) A) A float value B) An array of float values C) An integer D) An array of integers

Answer: B

38) Instance methods do not have this key word in their headers: A) public B) static C) private D) protected

Answer: B

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

Answer: B

4) This is a control structure that causes a statement or group of statements to repeat. A) Block B) Loop C) Prefix mode D) Body

Answer: B

40) A sentinel value ________ and signals that there are no more values to be entered. A) is a different data type than the values being processed B) is a special value that cannot be mistaken as a member of the list C) indicates the start of a list D) guards the list

Answer: B

41) Given the following two-dimensional array declaration, which statement is TRUE? int [][] numbers = new int [6] [9]; A) The array numbers has 6 columns and 9 rows. B) The array numbers has 6 rows and 9 columns. C) The array numbers has 15 rows. D) The array numbers has 54 rows.

Answer: B

42) Look at the following statement. import java.util.Scanner; This is an example of A) a wildcard import B) an explicit import C) unconditional import D) conditional import

Answer: B

43) Which of the following is a correct method header for receiving a two-dimensional array as an argument? A) public static void passArray(int[2]) B) public static void passArray(int [][]) C) public static void passArray(int[1][2]) D) public static void passArray(int[], int[])

Answer: B

45) This ArrayList class method is used to insert an item into an ArrayList. A) insert B) add C) store D) putItem

Answer: B

45) You can use this method to determine whether a file exists. A) The Scanner class's exists method B) The File class's exists method C) The File class's canOpen method D) The PrintWriter class's fileExists method

Answer: B

46) You can use this ArrayList class method to insert an item at a specific location in an ArrayList. A) insert B) add C) store D) putItem

Answer: B

48) Assume that the following method header is for a method in class A. public void displayValue(int value) Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method? A) int x = 7; void displayValue(x); B) int x = 7; displayValue(x); C) int x = 7; displayValue(int x); D) int x = 7; displayValue(x)

Answer: B

6) What is stored by a reference variable? A) A binary encoded decimal B) A memory address C) An object D) A string

Answer: B

7) This variable controls the number of times that the loop iterates. A) Counter variable B) Loop control variable C) Running total D) Decrement variable

Answer: B

7) What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int[SUB]; int y = 100; for(int i = 0; i < SUB; i++) { x[i] = y; y += 10; } A) 170 B) 180 C) 190 D) 200

Answer: B

8) Java allows you to create objects of this class in the same way you would create primitive variables. A) Random B) String C) PrintWriter D) Scanner

Answer: B

8) Java performs ________, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for the array. A) active array sequencing B) array bounds checking C) scope resolution binding D) buffer overrun protection

Answer: B

8) This type of loop will always be executed at least once. A) pre-test loop B) post-test loop C) sentinel loop D) for loop

Answer: B

8) 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

Answer: B

11) Each array in Java has a public field named ________ that contains the number of elements in the array. A) size B) capacity C) length D) limit

Answer: C

12) 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.

Answer: C

13) 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 int 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.

Answer: C

13) This type of loop allows the user to decide the number of iterations. A) Counter-controlled loop B) Dynamically executed loop C) User controlled loop D) Infinite loop

Answer: C

14) What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x); ... public static void arrayProcess(int[] a) { for(int k = 0; k < 3; k++) { a[k] = a[k] + 5; } } A) 27 B) 33 C) 38 D) 49

Answer: C

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.0, 55.0);

Answer: C

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

Answer: C

2) What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++; A) x = 25, y = 8 B) x = 33, y = 8 C) x = 33, y = 9 D) x = 34, y = 9

Answer: C

20) 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

Answer: C

21) The sequential search algorithm: A) requires the array to be ordered B) must always be implemented as a method C) uses a loop to sequentially step through an array, starting with the first element D) will not execute, if the element is not in the array

Answer: C

22) This type of loop is ideal in situations where the exact number of iterations is known. A) while loop B) do-while loop C) for loop D) if statement

Answer: C

23) The header of a value-returning method must specify this. A) The method's local variable names B) The name of the variable in the calling program that will receive the returned value C) The data type of the return value D) All of the above

Answer: C

26) A ragged array is: A) a two-dimensional array for which the number of rows is unknown B) a one-dimensional array for which the number of elements is unknown C) a two-dimensional array where the rows are of different lengths D) There is no such thing as a ragged array

Answer: C

26) After the header, the body of the method appears inside a set of: A) brackets, [] B) parentheses, () C) braces, {} D) double quotes, ""

Answer: C

44) The following package is automatically imported into all Java programs. A) java.java B) java.default C) java.util D) java.lang

Answer: D

26) Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached? A) while (inputFile != null) { ... } B) while (!inputFile.EOF) { ... } C) while (inputFile.hasNext()) { ... } D) while (inputFile.nextLine == " ") { ... }

Answer: C

27) In UML diagrams, this symbol indicates that a member is private: A) * B) # C) - D) +

Answer: C

27) The phrase divide and conquer is sometimes used to describe: A) the backbone of the scientific method B) the process of dividing functions C) the process of breaking a problem down into smaller pieces D) the process of using division to solve a mathematical problem

Answer: C

28) What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int[SUB]; int y = 20; for(int i = 0; i < SUB; i++) { x[i] = y; y += 5; } A) 50 B) 55 C) 60 D) 65

Answer: C

29) In all but rare cases, loops must contain within themselves: A) arithmetic statements B) if statements C) a way to terminate D) nested loops

Answer: C

3) It is common practice to use a ________ variable as a size declarator. A) static B) reference C) final D) boolean

Answer: C

30) If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]? A) 1 through 15 B) 1 through 14 C) 0 through 14 D) 0 through 15

Answer: C

30) Which of the following are pre-test loops? A) while, for, do-while B) while, do-while C) while, for D) for, do-while

Answer: C

33) When an individual element of an array is passed to a method: A) a reference to the array is passed B) it is passed like any other variable C) the method does not have direct access to the original array D) All of the above

Answer: C

33) Which of the following is included in a method call? A) return type B) method modifiers C) parentheses D) return variable

Answer: C

34) Which of the following statements will create a reference, str, to the string, "Hello, world"? (1) String str = new String("Hello, world"); (2) String str = "Hello, world"; A) 1 B) 2 C) 1 and 2 D) neither 1 or 2

Answer: C

35) Overloading means multiple methods in the same class: A) have the same name, but different return types B) have different names, but the same parameter list C) have the same name, but different parameter lists D) perform the same function

Answer: C

35) What would be the results of the following code? final int SIZE = 25; int[] array1 = new int[SIZE]; ... // Code that will put values in array1 int value = 0; for (int a = 0; a < array1.length; a++) { value += array1[a]; } A) Value contains the highest value in array1. B) Value contains the lowest value in array1. C) Value contains the sum of all the values in array1. D) This would cause the program to crash.

Answer: C

36) 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 double finalOrderTotal() { return orderAmount - orderAmount * orderDiscount; } } public class CustomerOrder { public static void main(String[] args) { Order order; int orderNumber = 1234; double orderAmt = 580.00; double orderDisc = .1; order = new Order(orderNumber, orderAmt, orderDisc); double finalAmount = order.finalOrderTotal(); System.out.printf("Final order amount = $%,.2f\n", finalAmount); } } A) 528.00 B) 580.00 C) 522.00 D) There is no value because the object order has not been created.

Answer: C

37) A class's responsibilities include: A) the things a class is responsible for doing B) the things a class is responsible for knowing C) both A and B D) neither A nor B

Answer: C

37) For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"}; A) "ghi" B) "def" C) A reference to the String "ghi" D) A reference to the String "def"

Answer: C

37) What will be the result of the following code? int num; string str = "555"; num = Integer.parseInt(string str) + 5; A) num will be set to 560. B) str will have a value of "560". C) The last line of code will cause an error. D) Neither num or str will be changed.

Answer: C

38) Given the following method header, which of the method calls would be an error? public void displayValues(double x, int y) A) displayValue(a,b); // where a is a long and b is a byte B) displayValue(a,b); // where a is an int 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.

Answer: C

38) What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", "); A) 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, B) 5, 8, 11, 14, 17, C) 5, 8, 11, 14, D) This is an invalid for statement.

Answer: C

38) Which of the following for loops is valid, given the following declaration? String[] names = {"abc", "def", "ghi", "jkl"}; A) for (int i = 0; i < names.length; i++) System.out.println(names[i].length); B) for (int i = 0; i < names.length(); i++) System.out.println(names[i].length); C) for (int i = 0; i < names.length; i++) System.out.println(names[i].length()); D) for (int i = 0; i < names.length(); i++) System.out.println(names[i].length());

Answer: C

39) Which of the following is NOT involved in finding the classes when developing an object-oriented application? A) Describe the problem domain. B) Identify all the nouns. C) Write the code. D) Refine the list of nouns to include only those that are relevant to the problem.

Answer: C

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

Answer: C

43) Which of the following will open a file named MyFile.txt and allow you to read data from it? A) File file = new File("MyFile.txt"); B) Scanner inputFile = new Scanner("MyFile.txt"); C) File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); D) PrintWriter inputFile = new PrintWriter("MyFile.txt");

Answer: C

44) Which of the following is a valid declaration for a ragged array? A) int[] ragged = new int[5]; B) int[][] ragged = new int[5][6]; C) int[][] ragged = new int[5][]; D) int[][] ragged = new int[][5];

Answer: C

45) To document the return value of a method, use this in a documentation comment. A) The @param tag B) The @comment tag C) The @return tag D) The @returnValue tag

Answer: C

47) 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) both of the above D) do nothing, the called program will take care of the throws clause

Answer: C

7) Most programming languages that are in use today are: A) procedural B) logic C) object-oriented D) functional

Answer: C

7) This part of a method is a collection of statements that are performed when the method is executed. A) method header B) return type C) method body D) method modifier

Answer: C

9) 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.

Answer: C

9) In Java, you do not use the new operator when you use a(n): A) array size declarator B) initialization list C) two-dimensional array D) all of the above Answer: B 10) What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } A) All the values in the array are initialized to 10.0. B) All the values, except the first, are set to 10.0. C) An error will occur when the program runs. D) There will be a compilation error.

Answer: C

10) 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

Answer: D

14) In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number "); int number = keyboard.nextInt(); while (number < 100 && number > 500) { System.out.print("Enter another number "); number = keyboard.nextInt(); } A) Numbers less than 100 or greater than 500 B) Numbers in the range 100 - 499 C) Numbers in the range 100 - 500 D) The boolean condition can never be true.

Answer: D

19) 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 int getOrderAmount() { return orderAmount; } public int getOrderDisc() { return orderDisc; } } public class CustomerOrder { public static void main(String[] args) { int ordNum = 1234; double ordAmount = 580.00; double discountPer = .1; Order order; double finalAmount = order.getOrderAmount() — order.getOrderAmount() * order.getOrderDisc(); System.out.printf("Final order amount = $%,.2f\n", finalAmount); } } A) 528.00 B) 580.00 C) There is no value because the constructor has an error. D) There is no value because the object order has not been created.

Answer: D

2) What does the following statement do? double[] array1 = new double[10]; A) Declares array1 to be a reference to an array of double values B) Creates an instance of an array of 10 double values C) Will allow valid subscripts in the range of 0 - 9 D) All of the above

Answer: D

2) Which of the following is NOT a benefit derived from using methods in programming? A) Pproblems are more easily solved. B) simplifies programs C) code reuse D) All of the above are benefits.

Answer: D

20) Given that String[] str has been initialized, to get a copy of str[0] with all characters converted to upper case, use the following statement: A) str.uppercase(); B) str[0].upperCase(); C) str.toUpperCase(); D) str[0].toUpperCase();

Answer: D

20) The lifetime of a method's 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 variable's method D) only while the method is executing

Answer: D

21) 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) All of the above

Answer: D

21) This refers to the combining of data and code into a single object. A) Data hiding B) Abstraction C) Object D) Encapsulation

Answer: D

23) Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt"); A) System.out.println(diskOut, "Calvin"); B) DiskFile.println("Calvin"); C) PrintWriter.println("Calvin"); D) diskOut.println("Calvin");

Answer: D

24) For the following code, which statement is NOT true? public class Circle { private double radius; public double x; private double y; } A) x is available to code that is written outside the Circle class. B) radius is not available to code written outside the Circle class. C) radius, x, and y are called members of the Circle class. D) y is available to code that is written outside the Circle class.

Answer: D

24) If numbers is a two-dimensional array, which of the following would give the length of row r? A) numbers.length B) numbers.length[r] C) numbers[r].length[r] D) numbers[r].length

Answer: D

27) Which of the statements are TRUE about the following code? final int ARRAY_SIZE = 10; long[] array1 = new long[ARRAY_SIZE]; A) Declares array1 to be a reference to an array of long values B) Creates an instance of an array of 10 long values C) Will allow valid subscripts in the range of 0 - 9 D) All of the above

Answer: D

28) In UML diagrams, this symbol indicates that a member is public. A) / B) @ C) - D) +

Answer: D

28) 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

Answer: D

29) 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

Answer: D

31) What will be the value of x after the following code is executed? int x = 10; while (x < 100); { x += 10; } A) 90 B) 100 C) 110 D) This is an infinite loop.

Answer: D

32) A constructor is a method that: A) returns an object of the class. B) never receives any arguments. C) with the name ClassName.constructor. D) performs initialization or setup operations.

Answer: D

32) What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; y += 20; } A) 90 B) 110 C) 130 D) 210

Answer: D

32) Which of the following is NOT a part of the method header? A) return type B) method name C) parentheses D) semicolon

Answer: D

33) In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number "); int number = keyboard.nextInt(); while (number < 100 || number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); } A) Numbers less than 100 B) Numbers greater than 500 C) Numbers in the range 100 - 499 D) Numbers in the range 100 - 500

Answer: D

33) 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

Answer: D

34) What would be the results of the following code? final int SIZE = 25; int[] array1 = new int[SIZE]; ... // Code that will put values in array1 int value = 0; for (int a = 0; a <= array1.length; a++) { value += array1[a]; } A) Value contains the highest value in array1. B) Value contains the lowest value in array1. C) Value contains the sum of all the values in array1. D) This would cause the program to crash.

Answer: D

35) How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100); A) 1 B) 3 C) 4 D) 5

Answer: D

37) A for loop normally performs which of these steps? A) initializes a control variable to a starting value B) tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value C) updates the control variable during each iteration D) all of the above E) none of the above

Answer: D

39) 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);

Answer: D

4) Which of the following are classes from the Java API? A) Scanner B) Random C) PrintWriter D) All of the above

Answer: D

42) If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array? A) for (int row = 1; row < numbers.length; row++) { for (int col = 1; col < numbers.length; col++) total += numbers[row][col]; } B) for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers.length; col++) total += numbers[row][col]; } C) for (int row = 0; row < numbers[row].length; row++) { for (int col = 0; col < numbers.length; col++) total += numbers[row][col]; } D) for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; }

Answer: D

42) Local variables can be initialized with: A) constants B) parameter values C) the results of an arithmetic operation D) any of the above

Answer: D

42) This is an item that separates other items. A) Controller B) Partition C) Doorway D) Delimiter

Answer: D

43) A value-returning method must specify this as its return type in the method header. A) an int B) a double C) a boolean D) any valid data type

Answer: D

44) 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 (as an integer) C) 8.0 D) This is an error.

Answer: D

47) You can use this ArrayList class method to replace an item at a specific location in an ArrayList. A) replace B) add C) store D) set

Answer: D

5) If a loop does not contain within itself a way to terminate, it is called a(n): A) while loop B) do-while loop C) for loop D) infinite loop

Answer: D

5) To create a method you must write its: A) header B) return type C) body D) definition

Answer: D

51) The ArrayList class is in this package. A) java.arraylist B) java.lang C) java.array D) java.util

Answer: D

9) A UML diagram does not contain: A) the class name B) the method names C) the field names D) object names

Answer: D

22) Which of the following values can be passed to a method that has an int parameter variable? A) float B) double C) long D) All of the above E) None of the above

Answer: E

T/F: 1) An array can hold multiple values of several different data types simultaneously.

Answer: FALSE

T/F: 11) A sorting algorithm is used to locate a specific item in a larger collection of data.

Answer: FALSE

T/F: 11) The term "default constructor" is applied to the first constructor written by the author of a class.

Answer: FALSE

T/F: 12) When a local variable in an instance method has the same name as an instance field, the instance field hides the local variable.

Answer: FALSE

T/F: 3) In the method header, the method modifier public means that the method belongs to the class, not a specific object.

Answer: FALSE

T/F: 4) In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value.

Answer: FALSE

T/F: 5) Instance methods should be declared static.

Answer: FALSE

T/F: 5) When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

Answer: FALSE

T/F: 8) In a for statement, the control variable can only be incremented.

Answer: FALSE

T/F: 8) In the method header the static method modifier means the method is available to code outside the class.

Answer: FALSE

T/F: 8) Java limits the number of dimensions that an array may have to 15.

Answer: FALSE

T/F: 8) The public access specifier for a field indicates that the attribute may not be accessed by statements outside the class.

Answer: FALSE

T/F: 9) A method that gets a value from a class's field but does not change it is known as a mutator method.

Answer: FALSE

T/F: 9) If a[] and b[] are two integer arrays, the expression a == b compares the array contents.

Answer: FALSE

T/F: 9) Only constants and variables may be passed as arguments to methods.

Answer: FALSE

T/F: 1) An object can store data.

Answer: TRUE

T/F: 1) Java provides a set of simple unary operators designed just for incrementing and decrementing variables.

Answer: TRUE

T/F: 1) Methods are commonly used to break a problem into small manageable pieces.

Answer: TRUE

T/F: 10) A file must always be opened before using it and closed when the program is finished using it.

Answer: TRUE

T/F: 10) Instance methods do not have the key word static in their headers.

Answer: TRUE

T/F: 10) No statement outside the method in which a parameter variable is declared can access the parameter by its name.

Answer: TRUE

T/F: 10) Objects in an array are accessed with subscripts, just like any other data type in an array.

Answer: TRUE

T/F: 11) The expression in a return statement can be any expression that has a value.

Answer: TRUE

T/F: 11) When you open a file with the PrintWriter class, the class can potentially throw an IOException.

Answer: TRUE

T/F: 12) A value-returning method can return a reference to a non-primitive type.

Answer: TRUE

T/F: 12) The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.

Answer: TRUE

T/F: 12) When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.

Answer: TRUE

T/F: 13) Java does not limit the number of dimensions that an array may have.

Answer: TRUE

T/F: 13) The term "no-arg constructor" is applied to any constructor that does not accept arguments.

Answer: TRUE

T/F: 14) The java.lang package is automatically imported into all Java programs.

Answer: TRUE

T/F: 2) A class in not an object, but a description of an object

Answer: TRUE

T/F: 2) Declaring an array reference variable does not create an array.

Answer: TRUE

T/F: 2) The while loop has two important parts: (1) a boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.

Answer: TRUE

T/F: 2) Two general categories of methods are void methods and value returning methods.

Answer: TRUE

T/F: 3) An access specifier indicates how the class may be accessed.

Answer: TRUE

T/F: 3) To compare the contents of two arrays, you must compare the elements of the two arrays.

Answer: TRUE

T/F: 4) A method that stores a value in a class's field or in some other way changes the value of a field is known as a mutator method.

Answer: TRUE

T/F: 4) Constants, variables, and the values of expressions may be passed as arguments to a method.

Answer: TRUE

T/F: 4) Once an array is created, its size cannot be changed.

Answer: TRUE

T/F: 5) A parameter variable's scope is the method in which the parameter is declared.

Answer: TRUE

T/F: 5) When an array of objects is declared, but not initialized, the array values are set to null.

Answer: TRUE

T/F: 6) A constructor is a method that is automatically called when an object is created.

Answer: TRUE


Conjuntos de estudio relacionados

Geography Final Chapter 4: Geography of Economic Development

View Set

Cognitive Psychology 2. Visual Perception

View Set

Processing Lernhilfe Zusammenfassung WOLF

View Set

Loss of Biodiversity & Relevant Laws/Acts

View Set

Assignment - 16 The Globally Harmonized System & Hazard Communication

View Set