exam 2

Ace your homework & exams now with Quizwiz!

Records in a sequential file are not usually updated in place. Instead ________. Answers: the updated data is placed in a "surrogate" file the entire file is usually rewritten the file is truncated The above statement is false—records in sequential files are usually updated in place.

Selected Answer: the entire file is usually rewritten

Constant variables also are called .

named constants

Files static method ________ receives a Path and returns a boolean indicating whether that Path represents a directory on disk. Answers: isDiskDirectory isDirectory isFileDirectory isPath

Selected Answer: isDirectory

Which of the following is not a package in the Java API? Answers: java.component java.awt javax.swing.event java.lang

Selected Answer: java.component

Which of the following is not a type of repetition statement in Java? Answers: while statement. do...while statement. for statement. loop statement.

Selected Answer: loop statement.

Overloaded methods always have the same _________. Answers: method name return type number of parameters order of the parameters

Selected Answer: method name

The identifiers in an enumeration ________. Answers: must be unique. may be duplicated. must be lowercase letters and cannot contain numbers. must be uppercase letters and cannot contain numbers.

Selected Answer: must be unique.

Which of the following methods is not in the Math class? Answers: ceil abs parseInt log

Selected Answer: parseInt

A well-designed method ________. Answers: performs multiple unrelated tasks repeats code found in other methods contains thousands of lines of code performs a single, well-defined task

Selected Answer: performs a single, well-defined task

A(n) ________ path starts from the directory in which the application began executing. Answers: absolute relative parallel comparative

Selected Answer: relative

Method calls cannot be distinguished by ________. Answers: method name return type parameter lists method signature

Selected Answer: return type

Which statement correctly passes the array itemsto method takeArray? Array itemscontains 10 elements. Answers: takeArray(items[]). takeArray(items). takeArray(items[9]). Arrays cannot be passed to methods—each item must be sent to the method separately.

Selected Answer: takeArray(items).

Information is passed to a method in ________. Answers: the method name that method's return the method body the arguments to the method

Selected Answer: the arguments to the method

To declare a method as static, place the keyword static before ________ in the method's declaration. Answers: the method modifier the return type the method name the argument list

Selected Answer: the return type

Variables should be declared as fields only if ________. Answers: they are local variables they are used only within a method they are required for use in more than one method or their values must be saved between calls to the class's methods they are arguments

Selected Answer: they are required for use in more than one method or their values must be saved between calls to the class's methods

Declaring main as static allows the JVM to invoke main ________. Answers: without knowing the name of the class in which main is declared. by creating an object of the class in which main is declared. without creating an instance of the class in which main is declared. None of the above.

Selected Answer: without creating an instance of the class in which main is declared.

Consider the following Java statements:int x = 9;double y = 5.3;result = calculateValue(x, y);Which of the following statements is false? Answers: A method is called with its name and parentheses. x and y are parameters. Copies of x and y are passed to the method calculateValue. x and y are arguments.

Selected Answer: x and y are parameters.

Relational expressions and logical expressions are both Boolean, which means they evaluate to true or false. Answers: True False

Selected Answer: True

Which of the following will count down from 10 to 1 correctly? Answers: for (int j = 10; j <= 1; j++) for (int j = 1; j <= 10; j++) for (int j = 10; j > 1; j--) for (int j = 10; j >= 1; j--)

Selected Answer: for (int j = 10; j >= 1; j--)

Path method ________ returns the String name of a file or directory without any location information. Answers: getStringName getFileOrDirectoryName getDirectoryName getFileName

Selected Answer: getFileName

Which expression is equivalent to if (!(grade == sentinelValue))? Answers: if (grade !== sentinelValue) if (grade != sentinelValue) ! if (grade == sentinelValue) ! if (grade !== sentinelValue)

Selected Answer: if (grade != sentinelValue)

The and operator the or operator are examples of relational operators. Answers: True False

Selected Answer: False

An exception object's ________ method returns the exception's error message. Answers: String Message Error toString

toString

Which of the following initializer lists would correctly set the elements of array n?

int[] n = {1, 2, 3, 4, 5};.

Which of the following statements about arrays are true? A. An array is a group of variables containing values that all have the same type. B. Elements are located by index. C. The length of an array c is determined by the expression c.length(); .D. The zeroth element of array c is specified by c[0]. Answers: A, C, D. A, B, D. C, D. A, B, C, D.

A, B, D.

Which of the following will not produce a compiler error? A)Changing the value of a constant after it is initialized. B)Changing the value at a given index of an array after it is created. C)Using a final variable before it is initialized. D)All of the above will produce compiler errors.

B)Changing the value at a given index of an array after it is created.

In a class containing methods with the same name, the methods are distinguished by ________. Answers: Number of arguments Types of arguments Return type (a) and (b) (b) and (c)

Selected Answer: (a) and (b)

Which expression adds 1 to the element of array arrayName at index i? Answers: ++arrayName[i]. arrayName++[i]. arrayName[i++]. None of the above.

Selected Answer: ++arrayName[i].

A(n) ________ indicates a problem that occurs while a program executes.

Exception

eThe not operator turns a result to false regardless of its previous value. Answers: True False

False

Which operator can be used in string concatenation? Answers: * += ++ =+

Selected Answer: +=

The control variable of a counter-controlled loop should be declared as ________to prevent errors Answers: int. float. double. Any of the above.

Selected Answer: int.

Invalid possibilities for array indices include . Answers: Positive integers. Negative integers. Zero. None of the above.

Negative integers.

Consider the class below:public class Test{public static void main(String[] args){int[] a = {99, 22, 11, 3, 11, 55, 44, 88, 2, -3};int result = 0;for (int i = 0; i < a.length; i++){ if (a[i] > 30) result += a[i];} System.out.printf("Result is: %d%n", result);}}The output of this Java program will be: x Answers: Result is: 280. Result is: 286. Result is: 154. Result is: 332.

Result is: 286.

boolean values can be displayed as the words true and false with the ________ format specifier. Answers: %bool. %b. %true. %boolean.

Selected Answer: %b.

Consider array items, which contains the values 0, 2, 4, 6 and 8. If method changeArray is called with the method call changeArray(items, items[2]), what values are stored in items after the method has finished executing?public static void changeArray(int[] passedArray, int value){passedArray[value] = 12;value = 5;} Answers: 0, 2, 5, 6, 12. 0, 2, 12, 6, 8. 0, 2, 4, 6, 5. 0, 2, 4, 6, 12.

Selected Answer: 0, 2, 4, 6, 12.

Which flag in a format specifier indicates that values with fewer digits than the field width should begin with a leading 0? Answers: p. l. w. 0.

Selected Answer: 0.

Consider the array:s[0] = 7s[1] = 0s[2] = -12s[3] = 9s[4] = 10s[5] = 3s[6] = 6The value of s[s[6] - s[5]] is: Answers: 0. 3. 9. 0.

Selected Answer: 9.

Which of the following statements about the continue statement is true? Answers: The continue statement is used to exit a repetition structure early and continue execution after the loop. The continue statement is used to continue after a switch statement. The continue statement does not alter the flow of control. A continue statement proceeds with the next iteration of the immediately enclosing while, for, do...while statement.

Selected Answer: A continue statement proceeds with the next iteration of the immediately enclosing while, for, do...while statement.

A Java class can have which of the following methods?A. void foo(int a)B. void foo(int a, int b)C. void foo(double a)D. void foo(double a, double b)E. void foo(int b) Answers: All of the above. A, B, D, E. A, B, C, D. A, C, D, E.

Selected Answer: A, B, C, D.

Which of the following is not a common name for one of the three phases that a program often can be split into using pseudocode? Answers: Termination phase Initialization phase Processing phase Action phase

Selected Answer: Action phase

Which of the following methods are overloaded with respect to one another?A. public int max (int a, int b) { ... }B. public double max (double a, double b) { ... }C. public int max (int a, int b, int c) { ... }D. public double max (double a, double b, double c) { ... } Answers: A and B are overloaded; C and D are overloaded. A and C are overloaded; B and D are overloaded. A, B and C are overloaded. All four methods are overloaded.

Selected Answer: All four methods are overloaded.

A static method can ________. Answers: call only other static methods of the same class directly manipulate only static fields in the same class directly be called using the class name and a dot (.) All of the above.

Selected Answer: All of the above.

Counter-controlled repetition requires Answers: A control variable and initial value. A control variable increment (or decrement). A condition that tests for the final value of the control variable. All of the above.

Selected Answer: All of the above.

Which of the following can be an argument to a method? Answers: Constants. Variables. Expressions. All of the above.

Selected Answer: All of the above.

Which of the following can be used in a switch statement in the expression after keyword case? (A) a constant integral expression.(B) a character constant.(C) a String(D) an enumeration constant. Answers: A and B. A and C. B and C. All.

Selected Answer: All.

1 out of 1 points Assume class Book has been declared. Which set of statements creates an array of Books? Answers: Book[] books;books = new Book[numberElements]; Book[] books[];books = new Book()[numberElements]; new Book() books[];books = new Book[numberElements]; All of the above.

Selected Answer: Book[] books;books = new Book[numberElements];

Consider the following two Java code segments: Segment 1 Segment 2int i = 0; for (int i = 0; i <= 20; i++)while (i < 20) {{ System.out.println(i); i++; } System.out.println(i);} Which of the following statements are true? Answers: The output from these segments is not the same. The scope of the control variable i is different for the two segments. Both (a) and (b) are true. Neither (a) nor (b) is true.

Selected Answer: Both (a) and (b) are true.

Which of the following primitive types is never promoted to another primitive type? Answers: double. byte. boolean. Both a and c.

Selected Answer: Both a and c.

Which of the following for-loop headers results in equivalent numbers of iterations:(A) for (int q = 1; q <= 100; q++)(B) for (int q = 100; q >= 0; q--)(C) for (int q = 99; q > 0; q -= 9)(D) for (int q = 990; q > 0; q -= 90) Answers: A and B. C and D. A and B have equivalent iterations and C and D have equivalent iterations. None of the loops have equivalent iterations.

Selected Answer: C and D.

Which of the following statements is true? Answers: Class Scannerprovides the ability to reposition to the beginning of a file with method seek. Class Scanner provides the ability to reposition to the beginning of a file with method reposition. Class Scanner does not provide the ability to reposition to the beginning of the file. If it is necessary to read a sequential file again, the program can simply keep reading—when the end of the file is reached, the Scanner is automatically set to point back to the beginning of the file.

Selected Answer: Class Scanner does not provide the ability to reposition to the beginning of the file.

What do the following statements do? double[] array; array = new double[14]; Answers: Create a double array containing 13 elements. Create a double array containing 14 elements. Create a double array containing 15 elements. Declare but do not create a double array.

Selected Answer: Create a double array containing 14 elements.

Which of the following statements is false? Answers: Storage of data in variables is temporary. Data is lost when a local variable "goes out of scope." Files are used for long-term retention of large amounts of data. Data maintained in files is often called transient data.

Selected Answer: Data maintained in files is often called transient data.

Class ________ provides static methods for common file and directory manipulations, including methods for copying files; creating and deleting files and directories; getting information about files and directories; reading the contents of files; getting objects that allow you to manipulate the contents of files and directories; and more. Answers: File FileAndDirectory Files FilesAndDirectories

Selected Answer: Files

Which of the following classes is not used for file input? Answers: FileInputStream FileReader ObjectInputStream Formatter

Selected Answer: Formatter

Which of the following statements is false? Answers: If a method does not return a value, the return-value-type in the method declaration can be omitted. Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error. Redeclaring a method parameter as a local variable in the method's body is a compilation error. Forgetting to return a value from a method that should return a value is a compilation error.

Selected Answer: If a method does not return a value, the return-value-type in the method declaration can be omitted.

1 out of 1 points Which of the following will not help prevent infinite loops? Answers: Include braces around the statements in a do...while statement. Ensure that the header of a for or while statement is not followed by a semicolon. If the loop is counter-controlled, the body of the loop should increment or decrement the counter as needed. If the loop is sentinel-controlled, ensure that the sentinel value is input eventually.

Selected Answer: Include braces around the statements in a do...while statement.

Which of the following tasks cannot be performed using an enhanced for loop? Answers: Calculating the product of all the values in an array. Displaying all even element values in an array. Comparing the elements in an array to a specific value. Incrementing the value stored in each element of the array.

Selected Answer: Incrementing the value stored in each element of the array.

Which method call converts the value in variable stringVariableto an integer? Answers: Convert.toInt(stringVariable) Convert.parseInt(stringVariable) Integer.parseInt(stringVariable) Integer.toInt(stringVariable)

Selected Answer: Integer.parseInt(stringVariable)

Which is a correct static method call of Math class method sqrt? Answers: sqrt(900); math.sqrt(900); Math.sqrt(900); Math math = new Math(); math.sqrt(900);

Selected Answer: Math.sqrt(900);

What does the following statement do?Scanner scanner = new Scanner(Paths.get("test.txt")); Answers: Opens a binary file for input. Opens a binary file for output. Opens a text file for input. Opens a text file for output.

Selected Answer: Opens a text file for input.

Consider the program below:public class Test{public static void main(String[] args){int[] a;a = new int[10];for (int i = 0; i < a.length; i++) a[i] = i + 2;int result = 0;for (int i = 0; i < a.length; i++) result += a[i]; System.out.printf("Result is: %d%n", result);}}The output of this program will be: Answers: Result is: 62. Result is: 64. Result is: 65. Result is: 67.

Selected Answer: Result is: 65.

Which of the following statements is true? Answers: Strings can be used in a switch statement's controlling expression and in its case labels. Strings can be used in a switch statement's controlling expression but not in its case labels. Strings cannot be used in a switch statement's controlling expression but can be used in its case labels. Strings cannot be used in a switch statement's controlling expression and cannot be used in its case labels.

Selected Answer: Strings can be used in a switch statement's controlling expression and in its case labels.

Which statement below is false? Answers: Structured programming produces programs that are easier to test. Structured programming requires four forms of control. Structured programming produces programs that are easier to modify Structured programming promotes simplicity.

Selected Answer: Structured programming requires four forms of control.

Which statement prints the floating-point value 123.456 right justified with a field width of 10? Answers: System.out.printf("%d10.3", 123.456); System.out.printf("%10.3d", 123.456); System.out.printf("%f10.3", 123.456); System.out.printf("%10.3f", 123.456);

Selected Answer: System.out.printf("%10.3f", 123.456);

Which case of the following would warrant using the boolean logical inclusive OR (|) rather than the conditional OR (||)? Answers: Testing if two conditions are both true. Testing if at least one of two conditions is true. Testing if at least one of two conditions is true when the right operand has a required side effect. Testing if at least one of two conditions is true when the left operand has a required side effect.

Selected Answer: Testing if at least one of two conditions is true when the right operand has a required side effect.

Which of the following statements is false? Answers: The Java API consists of packages. The Java API helps programmers avoid "reinventing the wheel." The Java API consists of import declarations. None of the above.

Selected Answer: The Java API consists of import declarations.

Which of the following statements about a do...while repetition statement is true? Answers: The body of a do...while loop is executed only if the terminating condition is true. The body of a do...while loop is executed only once. The body of a do...while loop is always executed at least once. None of the above.

Selected Answer: The body of a do...while loop is always executed at least once.

Which of the following statements about the break statement is false? Answers: The break statement is used to exit a repetition structure early and continue execution after the loop. A break statement can only break out of an immediately enclosing while, for, do...while or switch statement. The break statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch.

Selected Answer: The break statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

ppose variable gender contains MALE and age equals 60, how is the Expression (gender == FEMALE) && (age >= 65) evaluated? Answers: The condition (gender == FEMALE) is evaluated first and the evaluation stops immediately. The condition (age >= 65) is evaluated first and the evaluation stops immediately. Both conditions are evaluated, from left to right. Both conditions are evaluated, from right to left.

Selected Answer: The condition (gender == FEMALE) is evaluated first and the evaluation stops immediately.

Which of the following is equivalent to this code segment? int total = 0; for (int i = 0; i <= 20; i += 2) total += i; Answers: int total = 0;for (int i = 20; i < 0; i += 1) total += i; int total = 0;for (int i = 0; i <= 20; total += i, i += 2); int total = 0;for (int i = 0, i <= 20, total += i; i += 2); int total = 0;for (int i = 2; i < 20; total += i, i += 2);

Selected Answer: int total = 0;for (int i = 0; i <= 20; total += i, i += 2);

Which of the following statements about creating arrays and initializing their elements is false? Answers: The new keyword should be used to create an array. When an array is created with operator new, the number of elements must be placed in square brackets following the type of element being stored. The elements of an array of integers have a value of null before they are initialized. A for loop is commonly used to set the values of the elements of an array.

Selected Answer: The elements of an array of integers have a value of null before they are initialized.

For the two code segments below: Segment A int q = 5;switch(q){ case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); case 4: System.out.println(4); case 5: System.out.println(5); default: System.out.println("default");} Segment B q = 4;switch(q){ case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); case 4: System.out.println(4); case 5: System.out.println(5); default: System.out.println("default");} Which of the following statements is true? Answers: The output for Segment A is: default The output for Segment B is: 4 The output for Segment B is: 45default The output for Segment A is: 5 default

Selected Answer: The output for Segment A is: 5 default

Which of the following is false? Answers: The size of an ArrayList can be determined via its length instance variable. The size of an ArrayList can be determined via its size method. You can add a new item to the end of an ArrayList with its add method. You can get an item from a specified index in an ArrayList with its get method.

Selected Answer: The size of an ArrayList can be determined via its length instance variable.

Consider the code segment below. Which of the following statements is false?int[] g;g = new int[23]; Answers: The value of g[3] is -1. The first statement declares an array reference. The second statement creates the array. g is a reference to an array of integers.

Selected Answer: The value of g[3] is -1.

How do methods setIn, setOut and setErr affect the standard input, output and error streams? Answers: They output data to the standard input, output and error streams. They provide the only way to access the standard input, output and error streams. They redirect the standard input, output and error streams. They empty the standard input, output and error streams.

Selected Answer: They redirect the standard input, output and error streams.

Which of the following statements is true? Answers: The catch block contains the code that might throw an exception. The try block contains the code that handles the exception if one occurs. You can have many catch blocks to handle different types of exceptions. When a try block terminates, any variables declared in the try block are preserved.

Selected Answer: You can have many catch blocks to handle different types of exceptions.

Which of the following statements about the switch statement is false? . Answers: You can use Strings in a switch statement's controlling expression. You can use a String in a switch statement's case label. You can use a comma-separated list of Strings in a switch statement's case label. You cannot use a String in a switch statement's default case label.

Selected Answer: You can use a comma-separated list of Strings in a switch statement's case label

The parameter list in the method header and the arguments in the method call must agree in: Answers: number type order all of the above

Selected Answer: all of the above

When all the contents of a file are truncated, this means that ________. Answers: the data in the file is saved to a backup file the file is deleted a FileNotFoundException occurs all the data in the file is discarded

Selected Answer: all the data in the file is discarded

To exit out of a loop completely, and resume the flow of control at the next statement after the loop, use a _______. Answers: continue statement. break statement. return statement. Any of the above.

Selected Answer: break statement.

Identifiers in Java have ________ and ________ scopes? Answers: method, class. class, block. block, statement. statement, file.

Selected Answer: class, block.

Which formatting flag indicates that the floating-point values should be output with a thousands separator? Answers: plus (+). minus (-). comma (,). period (.).

Selected Answer: comma (,).

Which of the following promotions of primitive types is not allowed to occur? Answers: char to int. double to float. int to double. short to long.

Selected Answer: double to float.

A trailing _______ placed at the end of an if/else if/else statement provides default action when none of the ifs have true expressions. Answers: if break else exit

Selected Answer: else

A set of named constants that start with the value 0 for the first constant and increment by 1 for each subsequent constant can be declared as a(n) ________. Answers: class enum enumeration None of the above.

Selected Answer: enum

Any field declared with keyword ________ is constant. Answers: static const constant final

Selected Answer: final

Assume array items contains the integer values 0, 2, 4, 6 and 8. Which of the following uses the enhanced for loop to display each value in array items? Answers: for (int i = 0; i < items.length; i++)System.out.prinf("%d%n", items[i]); for (int i : items)System.out.prinf("%d%n", items[i]); for (int i : items)System.out.prinf("%d%n", i); for (int i = 0 : items.length)System.out.prinf("%d%n", items[i]);

Selected Answer: for (int i : items)System.out.prinf("%d%n", i);

An overloaded method is one that ________. x Answers: has a different name than another method, but the same parameters has the same name as another method, but different parameters (by number, types or order of the types) has the same name and parameters as a method defined in another class has the same name and parameters, but a different return type as another method

Selected Answer: has the same name as another method, but different parameters (by number, types or order of the types)

Consider the code segment below. if (gender == 1){ if (age >= 65) ++seniorFemales;} This segment is equivalent to which of the following? Answers: if (gender == 1 || age >= 65) ++seniorFemales; if (gender == 1 && age >= 65) ++seniorFemales; if (gender == 1 AND age >= 65) ++seniorFemales; if (gender == 1 OR age >= 65) ++seniorFemales;

Selected Answer: if (gender == 1 && age >= 65) ++seniorFemales;

Which of the following statements is false? Answers: A catch block declares a type and an exception parameter name. Inside the catch block, you can use the parameter's name to interact with a caught exception object. When a program is executed, array element indices are checked for validity—all indices must be greater than 0 and less than or equal to the length of the array. If an attempt is made to use an invalid index to access an element, an ArrayIndexOutOfBoundsException exception occurs.

When a program is executed, array element indices are checked for validity—all indices must be greater than 0 and less than or equal to the length of the array.

Which of the following statements is false? Answers: When an argument is passed by reference, the called method can access the argument's value in the caller directly but cannot modify it. All arguments in Java are passed by value. To pass an individual array element to a method, use the indexed name of the array. To pass an object reference to a method, simply specify in the method call the name of the variable that refers to the object.

When an argument is passed by reference, the called method can access the argument's value in the caller directly but cannot modify it.

Exception handling helps you create ________ programs. Answers: high-performance logic-error-free fault-tolerant compilation-error-free

fault-tolerant

Arrays are ________. Answers: variable-length entities fixed-length entities data structures that contain up to 10 related data items used to draw a sequence of lines, or "rays"

fixed-length entities

Class Arrays provides method __________ for comparing arrays to determine whether they have the same contents. Answers: compare compares equal equals

equals

Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4? Answers: values[3] = values[4];values[4] = values[3]; values[4] = values[3];values[3] = values[4]; int temp = values[3];values[3] = values[4];values[4] = temp; int temp = values[3];values[3] = values[4];values[4] = values[3];

int temp = values[3];values[3] = values[4];values[4] = temp;

In array items, which expression below accesses the value at row 3 and column 4?

items[3][4]

Which command below runs TestProgram, and passes in the values files.txtand 3?

java TestProgram files.txt 3

​When an argument is passed by reference, ________.

the called method can access the argument's value in the caller directly and modify that data


Related study sets

Chapter 9 Intermediate Financial 1

View Set

Starting Out With Visual C# Chapter 2 Checkpoint Questions 2.45-2.48

View Set

(C1W1): Foundations of User Experience (UX) Design - Weekly challenge 1: The basics of user experience design

View Set

anatomy 2 pearson chapter 18 cardiovascular heart

View Set

APES 5.11 - Ecological Footprint

View Set

NUR 2120 Exam #2 Practice Questions

View Set

سعة فضل الله تعالى

View Set

Point-Slope Form and Linear Equations Unit Test Review, Algebra, Unit Test Review, Unit Test Review

View Set