Comp Sci Mid Term CH 7,8
The String class is an example of which of the following types of classes? Select one: a. Static class b. Immutable class c. Abstract class d. Mutable class
Immutable class
When the order of the elements is unimportant, what is the most efficient way to remove an element from an array?
Replace the element to be deleted with the last element in the array
To construct an unambiguous package name, you should use a domain name in reverse. If you do not have a domain name, you should use your email address backwards. Select one: True False
The correct answer is 'True'.
What is the value of the count variable after the execution of the given code snippet? ArrayList<Integer> num = new ArrayList<Integer>(); num.add(1); num.add(2); num.add(1) int count = 0; for (int i = 0; i < num.size(); i++) { if (num.get(i) % 2) == 0) { count++): } }
The correct answer is: 1
What is the output of the given code snippet? int [] mynum = new int[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; System.out.print(mynum[i]); }
The correct answer is: 2345
Consider the following code snippet: int [][] numarray = { { 3, 2, 3 }, { 0, 1, 0 } }: System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code snippet? Select one: a. 00 b. 31 c. 30 d. 03
The correct answer is: 30
What is the output of the following code snippet? int [] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2]); System.out.println(myarray[3]);
The correct answer is: 3040
How many elements can be stored in an array of dimension 2 by 3?
The correct answer is: 6
What is the output of the code snippet below? int [][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 3, 2, 1, 3 } }; int val = arr[1][2] + arr[1][3]; System.out.println(val);
The correct answer is: 6
What will be printed by the statements below? int [] values = { 4, 5, 6, 7 }; values[0] = values[3]; values[3] = values[0]; for (int i = 0; i < values.length; i++) { System.out.print(values[i] + " "); }
The correct answer is: 7 5 6 7
Which annotation is used to mark test methods when using JUnit? Select one: a. @TestMethod b. @TestCode c. @Test d. @JUnitTest
The correct answer is: @Test
Your program needs to store an integer sequence of unknown length. Which of the following is most suitable to use? Select one: a. An array declared as int [] marks; b. A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>(); c. An array declared as int marks[10000]; d. An array declared as int marks[10];
The correct answer is: A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>();
Which of the following describes an immutable class? Select one: a. A class that has no accessor or mutator methods. b. A class that has no accessor methods, but does have mutator methods. c. A class that has accessor methods, but does not have mutator methods. d. A class that has both accessor and mutator methods.
The correct answer is: A class that has accessor methods, but does not have mutator methods.
A class named CurrencyTranslator would most probably fall into which of the following class categories? Select one: a. Actor classes b. Starter classes c. Abstract entities d. Utility classes
The correct answer is: Actor classes
Which of the following statements is true regarding method parameters in Java? Select one: a. All method parameters use the call-by-value mechanism. b. Only method parameters of primitive type use the call-by-value mechanism. c. All method parameters use the call-by-reference mechanism. d. Only method parameters of object type use the call-by-value mechanism.
The correct answer is: All method parameters use the call-by-value mechanism.
Under which of the following conditions would the public interface of a class be considered cohesive? Select one: a. All of its features are public and none of its features are static. b. The quality of the public interface is rated as moderate to high. c. All of its features are related to the concept that the class represents. d. It is obvious that the public interface refers to multiple concepts.
The correct answer is: All of its features are related to the concept that the class represents.
n Java, which of the following mechanisms describe the copying of an object reference into a parameter variable? Select one: a. Call-by-reference b. Call-by-value c. Call-by-precondition d. Call-by-object
The correct answer is: Call-by-value
Which of the following would be an appropriate name for a game-related class? Select one: a. CompletedLevelOne b. InitialLevel c. ResetCurrentLevel d. AscentToFinalLevel
The correct answer is: InitialLevel
Which of the following types of methods are invoked on objects? Select one: a. Static method b. Class method c. Instance method d. Either static or instance methods
The correct answer is: Instance method
Which of the following is one of the most popular testing frameworks? Select one: a. JUnit b. JUnitTest c. JTester d. JQATester
The correct answer is: JUnit
Which of the following represents a good strategy regarding cohesion and coupling? Select one: a. Maximize cohesion and remove unnecessary coupling. b. Minimize cohesion and remove unnecessary coupling c. Maximize cohesion and coupling d. Minimize cohesion and maximize coupling
The correct answer is: Maximize cohesion and remove unnecessary coupling.
Which type of method modifies the object on which it is invoked? Select one: a. Constructor method b. Static method c. Accessor method d. Mutator method
The correct answer is: Mutator method
Why can't Java methods change parameters of primitive type? Select one: a. Java methods can have no actual impact on parameters of any type. b. Parameters of primitive type are considered by Java methods to be local variables. c. Parameters of primitive type are immutable. d. Java methods cannot accept parameters of primitive type.
The correct answer is: Parameters of primitive type are considered by Java methods to be local variables.
What is the result of executing this code snippet? int [] marks = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { System.out.println(marks[i]); }
The correct answer is: The code snippet causes a bounds error.
Which of the following statements regarding static methods is true? Select one: a. The textbook recommends that you minimize the use of static methods. b. The textbook recommends that static methods should be liberally used within classes. c. Use of static methods usually leads to good object oriented design. d. Static methods can be easily evolved to add more functionality.
The correct answer is: The textbook recommends that you minimize the use of static methods.
Why is a static variable also referred to as a class variable? Select one: a. There is a single copy available to all objects of the class. b. It is encapsulated within the class. c. Each class has one and only one static variable. d. It is stored in the separate class area of each object.
The correct answer is: There is a single copy available to all objects of the class.
Which of the following statements is true relative to classes that will successfully compile? Select one: a. They must have both mutators and accessor methods. b. They must have accessors, and optionally may have mutators. c. They must have mutators, and optionally may have accessors. d. They may have either mutator or accessor methods, or both.
The correct answer is: They may have either mutator or accessor methods, or both.
Which class category has static methods and constants, but no objects? Select one: a. Real-life entity abstraction b. Actor classes c. Utility class d. Concept class
The correct answer is: Utility class
In which of the following cases could a static variable be declared as something other than private? Select one: a. When it will be accessed by multiple objects. b. When implementing static constants. c. When declared inside a private method. d. Static variables should never be declared as anything but private.
The correct answer is: When implementing static constants.
Consider the following code snippet: String [] data = { "abc", "def", "ghi", "jki" }; String [] data2; Which statement copies the data array to the data2 array?
The correct answer is: data2 = Arrays.copyOf(data, data.length);
Judging by the name of the method, which of the following methods would most likely be a mutator method? Select one: a. getListOfDeposits b. findAccountBalance c. isOverdrawn d. deposit
The correct answer is: deposit
Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. String [] arr = { "abc", "def", "ghi", "jkl" }; __________________________________ { System.out.println(str); }
The correct answer is: for (String str: arr)
Identify the correct statement for defining an integer array named numarray of ten elements. Select one: a. int [] numarray = new int[9]; b. int [] numarray = new int[10]; c. int [] numarray; d. int numarray[10];
The correct answer is: int [] numarray = new int[10];
Which code snippet calculates the sum of all the elements in even positions in an array? Select one: a. int sum = 0; for (int i = 1; i < values.length; i = i + 2) { sum++; } b. int sum = 0; for (int i = 0; i < values.length; i++) { sum++; } c. int sum = 0; for (int i = 0; i < values.length; i++) { sum = sum + values[i]; } d. int sum = 0; for (int i = 0; i < values.length; i = i + 2) { sum = sum + values[i]; }
The correct answer is: int sum = 0; for (int i = 0; i < values.length; i = i + 2) { sum = sum + values[i]; }
The enhanced for loop Select one: a. is convenient for traversing all elements in an array. b. is convenient for traversing elements in a partially filled array. Incorrect c. is only used for arrays of integers d. is used to initialize all array elements to a common value.
The correct answer is: is convenient for traversing all elements in an array.
When an array myArray is only partially filled, how can the programmer keep track of the current number of elements? Select one: a. access myArray.length() b. maintain a companion variable that stores the current number of elements c. access myArray.currentElements() d. access myArray.length() - 1;
The correct answer is: maintain a companion variable that stores the current number of elements
Assume the variable numbers has been declared to be an array that has at least one element. Which of the following represents the last element in numbers?
The correct answer is: numbers[numbers.length - 1]
General Java variable naming conventions would suggest that a variable named NICKEL_VALUE would most probably be declared using which of the following combinations of modifiers? Select one: a. public void final b. public static final double c. private static double d. private static
The correct answer is: public static final double
If you do not include a package statement at the top of the source file, its class will be placed in which package? Select one: a. java.lang b. java.util c. the default package, which has no name d. java.awt
The correct answer is: the default package, which has no name
It may be necessary to "grow" an array when reading inputs because Select one: a. the number of inputs may not be known in advance. b. arrays in Java must be resized after every 100 elements. c. arrays are based on powers of two. d. the only alternative is a bounds exception.
The correct answer is: the number of inputs may not be known in advance.
The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list? Select one: a. x = a.get(4); b. x = a.get(); c. x = a.get[4]; d. x = a[4];
The correct answer is: x = a.get(4);
What is the final size of names and friends? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); names.add("Harry");
The final size of names is 3; the final size of friends is 2