Exam 3 Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Using the classes above, what is the output of the following lines of code? Child kid = new Child (); Parent adult = new Parent(); kid.display(); adult.display();

-7 24

Consider the Checker class below. public class Checker { public static int count = 0; public int number = 0; public Checker() { count++; number = count; } public int getCount() { return count; } public int getNumber() { return number; } } What is output from the code fragment below? Checker one = new Checker(); Checker two = new Checker(); System.out.println(one.getCount() + " " + two.getCount()); 2 2 1 1 1 2 2 1

1 2

What is the output of the following lines of code? Child kid = new Child(-14); Parent kid2 = new Child(21); System.out.println(kid.getValue() + " " + kid2.getValue());

100 100

What is the output of the following code? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; System.out.println(counts[3].length);

2

What is the output of the following code snippet? int[] mynum = new int[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; System.out.print(mynum[i]); }

2345

What is the output of the following code snippet? int[] values = { 10, 20, 30, 7 }; int[] numbers = values; numbers[2] = 24; values[3] = numbers[0] + 6; System.out.println(numbers[2] + " " + numbers[3] + " " + values[2] + " " + values[3]);

24 16 24 16

Assume the following variable has been declared and given a value as shown: int [] [] data = { { 9, 17, -4, 21 }, { 15, 24, 0, 9 }, { 6, 2, -56, 8 }, }; Which is the value of data.length?

3

Consider the following code snippet: int[][] numarray = { { 3, 2, 3 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the following code snippet?

30

Consider the following code snippet: int count = 0; int[][] numarray = new int[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { numarray[j][i] = count; count++; } } What is the value of numarray[1][2] after the code snippet is executed?

5

What is the output of the following statements? cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty());

6 true 1 false

Consider the following code snippet: public class Vehicle { private String manufacturer; . . . public void setVehicleClass(double numberAxles) { . . . } } If a Motorcycle class is created as a subclass of the Vehicle class, which of the following statements is correct?

A Motorcycle object inherits and can directly use the method setVehicleClass, but cannot directly use the instance variable manufacturer

Which of the following is true regarding subclasses?

A subclass has no access to private instance variables of its superclass

Which of the following is true regarding subclasses? (pt 2.)

A subclass inherits methods and instance variables from its superclass

Which of the following is true regarding inheritance?

A superclass can force a programmer to override a method in any subclass created from it.

Which of the following statements is true regarding method parameters in Java?

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?

All of its features are related to the concept that the class represents.

Which of the following statements is correct?

An object of type Programmer can call the setDepartment method of the Employee class on itself.

Which of the following statements is NOT correct?

An object of type Vehicle can call the setModelName method on itself

What is the output of the following statements? ArrayList<String> names = new ArrayList<String(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); }

Ann, Tony,

When are statements in a class-level initialization block executed?

Before the code in the class constructor is executed

When are statements in a class-level static initialization block executed?

Before the code in the class constructor is executed

A class (ClassOne) is considered to have a dependency on another class (ClassTwo) under which of the following conditions?

ClassOne uses objects of ClassTwo

Consider the following code snippet: public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); names.add("Janet"); ArrayList<String> names2 = reverse(names); } public static ArrayList<String> reverse(ArrayList<String> names) { ArrayList<String> result = new ArrayList<String>(); for (int i = names.size() - 1; i >= 0; i--) { result.add(names.get(i)); } return <String>result; } Which statement is true after the main method is executed?

Compilation error due to the return statement in reverse method.

A new class is proposed to collect information about a group of DVDs. Which of the following is the best name for this class?

DVDCollection

A new class is proposed to collect information about a group of DVDs. A separate class containing information about a single DVD, named DVD, has already been created.Which of the following is the best design to store the data and size of this collection?

Have an ArrayList of DVD and use its size as the size of the collection.

A new class is proposed to collect information about a group ofDVDs. Which of the following is the best structure for this class?

Have separate classes for a single DVD and the entire DVD collection.

Why is it generally considered good practice to minimize dependencies between classes?

High dependency increases program maintenance and hinders code reuse.

Which of the following is a true statement regarding consistency in coding?

Inconsistencies are an annoyance, because they can be so easily avoided.

Which of the following would be an appropriate name for a game-related class?

InitialLevel

Consider the following code snippet: public class Motorcycle extends Vehicle{ . . . public Motorcycle(int numberAxles) { super.numberAxles = numberAxles; }} What does this code do?

It attempts to access directly an instance variable of the Vehicle superclass rather than using a constructor or an accessor

What is displayed after executing the given code snippet? int[] mymarks = new int[10]; int total = 0; Scanner in = new Scanner(System.in); for (int cnt = 1; cnt <= 10; cnt++) { System.out.print("Enter the marks: "); mymarks[cnt] = in.nextInt(); total = total + mymarks[cnt]; } System.out.println(total);

It causes a bounds error.

Consider the following code snippet: public class Vehicle { protected int numberAxles; . . . } Which statement is true about the accessibility of data in the numberAxles variable?

It is only accessible by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.

Which of the following is true regarding objects created from immutable classes?

It is safe to give out references to immutable class objects freely.

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values) { for (int i = 1; i < values.length; i++) { values[i - 1] = values[i]; } } What does the method do?

It moves each element of the array values to a lower index position and overwrites the 1st element.

What must a subclass do to modify a private superclass instance variable?

It must use a public method of the superclass (if it exists) to update the superclass's private instance variable.

Why does the Scanner class belong to the category of classes known as actors?

It performs a task, such as scanning a stream for numbers and characters.

Consider the method in the following code snippet: public void getRewardPoints() { System.out.println("Your Reward Points balance is now " + pointBalance); } Which of the following statements would NOT be a valid criticism of the design of this method?

It should not be a separate method since it is only 1 line long.

Assume the method doSomething has been defined as follows: public static void doSomething(int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = values[p1]; } What does the method do?

It swaps the integer values stored in positions p1 and p2 of the array values.

Assume the method doSomething has been defined as follows: public static void doSomething(int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = temp; } What does the method do?

It swaps the integer values stored in positions p1 and p2 of the array values.

Which of the following code fragments is NOT valid in Java?

LandVehicle myAuto = new Vehicle ("sedan");

All rodents are mammals and all canines are mammals. No canines are rodents and no rodents are canines. What hierarchy best captures this information?

Mammal is a superclass of Rodent and Mammal

If the variable kid is defined below, which version of the doSomething method can be called on the variable kid? Parent kid = new Child ();

Method 1

If the variable kid is defined below, which version of the doSomething method can be called on the variable kid? Child kid = new Child ();

Methods 2 and 3

Which of the following classifications of method behavior is accurate?

Methods that change parameter variables always have side effects.

Which of the following is true regarding side effects of methods?

Minimize side effects that go beyond modification of private instance variables.

Mutator methods exhibit which of the following types of side effect?

Modification of private instance variables

What must a subclass method do in order to override a superclass method?

Must use the same method name and the same parameter types

Which type of method modifies the object on which it is invoked?

Mutator method

Which of the following classifications of method behavior produces acceptable side effects?

Mutator methods that do not change parameter variables

Can the method lastDayOfMonth be changed to be a static method by just changing the header to the following? private static int lastDayOfMonth()

No. It could no longer access the month instance variable.

Consider the following code snippet: ArrayList<Integer> arrList = new ArrayList<Integer>(); for (int i = 0; i < arrList.size(); i++) { arrList.add(i + 3); } What value is stored in the element of the array list at index 0?

None of the above

A static method can have which of the following types of parameters?

Only parameter variables

Why can't Java methods change parameters of primitive type?

Parameters of primitive type are considered by Java methods to be local variables.

Which of the following does NOT describe a particular side effect related to standard output?

Programmers may be surprised by unexpected method actions.

Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array. Which array algorithms might be adapted for this?

Remove an element

What does the getClass method do?

Returns an object that describes a class and its properties

Which of the following statements regarding static methods is true?

Static methods are not very common

Consider the following code snippet. Which statement should be used to fill in the empty line so that the output will be [32, 54, 67.5, 29, 35]? public static void main(String[] args) { double data[] = { 32, 54, 67.5, 29, 35 }; __________ System.out.println(str); }

String str = Arrays.toString(data);

Consider the following code snippet: public class RewardPointsAccount { private int currentRewardPointBalance; private int level1Cutoff = 15000; public void process(boolean isEliteCustomer) { int level1Cutoff = 15000; if (isEliteCustomer) { this.level1Cutoff = 12500; . . . } } Which of the following statements is true?

The code within the process method changes the value of the instance variable level1Cutoff for elite customers.

Which one of the following statements is correct about the given code snippet? int[] somearray = new int[6]; for (int i = 1; i < 6; i++) { somearray[i] = i + 1; }

The for loop initializes all the elements except the 1st one.

What will have to change if the instance variables month and day of the Date class are replaced by the single instance integer variable dayOfTheYear?

The implementation of the methods will change,but the interface will not need to change.

Consider the following code snippet: Employee anEmployee = new Programmer(); anEmployee.increaseSalary(2500); If the Programmer class inherits from the Employee class, and only the Employeeclass has an implementation of the increaseSalary method, which statement is correct?

The increaseSalary method of the Employee class will be executed

The use of the static keyword in a method declaration implies which of the following?

The method cannot be invoked on an instance of an object.

Suppose an object is intended to store its current position in a 2D array using private instance variables and a mutator: private int row; private int column;​ public void moveLeft() { column--;​ } What problem could be encountered with the method moveLeft(), which is intended to move the current position of the object to the left 1 column on the grid?

The method might cause the instance variables to go out of the bounds of the 2D array.

Which of the following is a good indicator that a class is overreaching and trying to accomplish too much?

The public interface refers to multiple concepts.

Which of the following statements about comparing objects is correct?

The purpose of the equals method is to compare whether 2 objects have the same contents.

Which of the following can potentially be changed by a Java method?

The state of an object reference parameter's attribute

If a subclass defines the same method name and the same parameter types for a method that appears in its superclass, which statement is true?

The subclass method overrides the superclass method

Consider the following code snippet: Employee anEmployee = new Programmer();String emp = anEmployee.toString(); Assume that the Programmer class inherits from the Employeeclass and only the Employee class has an implementation of thetoString() method. Which of the following statements is correct?

The toString() method of the Employeeclass will be used when this code is executed.

Consider the following code snippet: Vehicle aVehicle = new Auto(4, "gasoline"); String s = aVehicle.toString(); Assume that the Auto class inherits from the Vehicle class, and neither class has an implementation of the toString() method. Which of the following statements is correct?

The toString() method of the Object class will be used when this code is executed.

What (if anything) is wrong with the following code snippet, which searches for the searchedValue variable in the array data? String[] data = { "abc", "def", "ghi", "jkl" }; String searchedValue = "ghi"; int pos = 0; boolean found = false; while (pos < data.length) { if (data[pos].equals(searchedValue)) { found = true; } else { found = false; } pos++; } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); }

There is a logic error.

Why is a static variable also referred to as a class variable?

There is a single copy available to all objects of the class.

Consider the following code snippet: public class Score { private String name; . . . public boolean equals(Object otherScore) { Score other = (Score) otherscore; return name.equals(other.name); } . . . } What is wrong with this code?

There is nothing wrong with this code.

When you implement equals in a subclass, you should first call equals in the superclass. Why?

To check whether the superclass instance variables match

Which of the following constitutes a common reason for creating a static method?

To encapsulate a computation that involves only numbers

Which class category has static methods and constants, but no objects?

Utility class

When an object of type Auto is constructed, what will be printed by the constructors from this inheritance hierarchy?

Vehicle Land Auto

In which of the following cases could a static variable be declared as something other than private?

When implementing static constants

If a class has an abstract method, which of the following statements is FALSE?

You can construct an object from this class

Assume the method doSomething has been defined as follows: public static int[] doSomething(int[] values) { int[] result = new int[values.length - 1]; for (int i = 0; i < result.length; i++) { result[i] = values[i] + values[i + 1]; } return result; } What is printed by the statements below? int[] nums = { 3, 18, 29, -2 } ;System.out.print(Arrays.toString(doSomething(nums)));

[21, 47, 27]

Assume the method createSomething has been defined as follows: public static int[] createSomething(int start, int size) { int[] result = new int[size]; for (int i = 0; i < result.length; i++) { result[i] = start; start++; } return result; } What is printed by the statement below? System.out.print(Arrays.toString(createSomething(4, 3)));

[4, 5, 6]

What will be printed by the statements below? ArrayList<String> names = new ArrayList<String>(); names.add("Annie"); names.add("Bob"); names.add("Charles"); names.set(2, "Doug"); names.add(0, "Evelyn"); System.out.print(names);

[Evelyn, Annie, Bob, Doug]

Consider the following 2-dimensional array. Which expression gives the number of elements in the 3rd row? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } };

counts[2].length

The integer array numbers will be filled with values from the Scanner object in. If there are more input values than there are spaces in the array, only enough values to fill the array should be read. The integer variable currentSize should be set to the number of values read. Partial code to do this is given below: int[] numbers = new int[100]; Scanner in = new Scanner(System.in); int currentSize = 0; while (/* Put condition here */) { int value = in.nextInt(); numbers[currentSize] = value; currentSize++; } What condition will complete this code?

currentSize < numbers.length && in.hasNextInt()

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String[] data2; In Java 6 and later, which statement copies the data array to the data2 array?

data2 = Arrays.copyOf(data, data.length);

Select the statement that reveals the logic error in the following method public static double minimum(double[] data) { double smallest = 0.0; for (int i = 0; i < data.length; i++) { if (data[i] < smallest) { smallest = data[i]; } } return smallest; }

double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

What reserved word in a method definition ensures that subclasses cannot override the method?

final

Assume the array of integers values has been created and process is a method that has a single integer parameter. Which of the following is equivalent to the loop below? for (int val : values) { process(val); }

for (int i = 0; i < values.length; i++) { int val = values[i]; process(val); }

Why is the following code, which assumes ChoiceQuestion is a subclass of Question, considered poor strategy? if (q instanceof ChoiceQuestion) { // Do the task the ChoiceQuestion way } else if (q instanceof Question) { // Do the task the Question way }

instanceof should NOT be used for type tests that can be solved by using polymorphism.

Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList?

int max = aList.get(0); for (int count = 1; count < aList.size();count++) { if (aList.get(count) > max) { max = aList.get(count); } }

Which one of the following statements is a valid initialization of an array named some array of 10 elements?

int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Which one of the following is the correct definition for initializing data in a 2-dimensional array with 3 rows and 2 columns?

int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

Which class does NOT require an import statement at the beginning of the program that uses it?

java.lang.Double

Which is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = names; friends.add("Harry");

names final size is 3 friends final size is 3

Assume the variable numbers has been declared to be an array that has at least 1 element. Which of the following represents the last element in numbers?

numbers[numbers.length - 1]

Consider the following code snippet: public class Coin { private String name; . . . public boolean equals(Object otherCoin) { return name.equals(otherCoin.name); } . . . } What is wrong with this code?

otherCoin must be cast as a Coin object before using the equals method

A subclass of Message, ExcitedMessage, is defined that will behave like Message, except that it will add 2 exclamation points (!!) to the end of the message. Sample code that uses ExcitedMessage is shown below. ExcitedMessage greeting = new ExcitedMessage("Hello"); System.out.print(greeting.getMessage()); // will print "Hello!!" Which ExcitedMessage constructor will give this behavior?

public ExcitedMessage(String line) { super(line + "!!"); }

Consider the following code snippet: public abstract class Machine { public abstract void setRPMs(); . . . } You wish to create a concrete subclass named PolisherMachine. Which of the following is the correct way to declare this subclass?

public class PolisherMachine extends Machine { public void setRPMs() { . . . } }

Consider the partial class below: public class Thing { private int number; private char letter; { number = -10; letter = 'Z'; } ... } Which code is equivalent to the code above?

public class Thing { private int number = -10; private char letter = 'Z'; ... }

Suppose you wish to write a method that returns the sum of the elements in a partially filled array. Which is the best choice for a method header?

public int sum(int[] values, int currSize)

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?

public static final double

Which one of the following is a correct declaration for a method named passAList with the array list num of size 5 as a parameter?

public static void passAList(ArrayList num[5])

Which one of the following is a valid signature of a method with an integer 2-dimensional array parameter of size 10 x 10?

public void func(int[][] arr)

Suppose an object is intended to store its current position in a 2D array using private instance variables: private int row; private int column; Which code represents a method that would move the current position of the object 1 column to the right on the grid?

public void moveRight() { column++; }

Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String s = programmer.toString(); Assume that the Employee class has NOT implemented its own toString()method. What value will s contain when this code is executed?

s will contain the class name of the programmer object followed by a hash code.

Insert the missing code in the following code fragment. This fragment is intended to call the Vehicle class's method public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Motorcycle extends Vehicle { . . . public Motorcycle() { __________; } }

setVehicleClass(2.0)

Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 && num1.size() > 3) { num1.remove(num1.size() - 1); } } System.out.println("size is : " + num1.size()); What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input?

size is: 4

Which of the following names would be considered to be qualified?

studentOne.getGraduationYear()

Complete the code in the Auto class constructor to store the type data.

super(type)

Complete the code in the Auto class method named displayAutoType to return the type data

super.displayinfo()

When an array reading and storing input runs out of space ________.

the array could be "grown" using the new command and the copyOf method.

The binary search is more efficient than the linear search, providing _________.

the elements of the array are ordered.

To test whether an object belongs to a particular type, use __________

the instanceof operator

It may be necessary to "grow" an array when reading inputs because __________.

the number of inputs may not be known in advance.

Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for (int val : values) { if (__________) max = val; }

val > max

Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for (int current = 1; current < values.length; current++) { if (__________) max = values[current]; }

values[current] > max


संबंधित स्टडी सेट्स

B Law Chapter 1 The Nature of Law

View Set

Ch 37 PrepU: Assessment and Management of Patients With Allergic Disorders

View Set

Life Insurance CH. 2 (Types of Policies)

View Set

Chapter 9, Motivation and emotion

View Set