Select an appropriate expression to complete the following method, which is designed to return the number of elements in the parameter array numbers. If a value appears more than once, it should be counted exactly once. public static int countElementsOnce(int[] numbers) { Set values = new HashSet(); for (int num: numbers) { values.add(num); } ______________________ }
Author: Anonymous
Consider the following class: public class Stock implements…
Consider the following class: public class Stock implements Comparable { private String name; private double price; // other methods go here public int compareTo(Object otherObject) { Stock otherStock = (Stock) otherObject; __________________________________; } } Which is the best statement to use to complete the compareTo() method?
Consider the following code snippet: public class Vehicle {…
Consider the following code snippet: public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Motorcycle extends Vehicle { . . . public void setModelName(String model) { . . . } public void setVehicleClass(double numberAxles) { . . . } } Which of the following statements is NOT correct?
It is common for certain classes to keep track of how many t…
It is common for certain classes to keep track of how many times certain events have occurred over the life of an object. This is done using
Consider the classes shown below: public class Parent { publ…
Consider the classes shown below: public class Parent { public void doSomething(){/* Implementation not shown */} } public class Child extends Parent { public void doAnotherThing(){/* Implementation not shown */} } Which lines in the following code will compile without error? Child kid = new Child(); kid.doSomething(); // line 1 kid.doAnotherThing(); // line 2
Which of the following represents a good strategy regarding…
Which of the following represents a good strategy regarding cohesion and class dependencies?
Consider the following code snippet: public class Vehicle {…
Consider the following code snippet: public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Auto extends Vehicle { . . . public void setVehicleClass(int numberAxles) { . . . } } Which of the following statements is correct?
Which type of method modifies the object on which it is invo…
Which type of method modifies the object on which it is invoked?
Consider the classes shown below: public class Parent { priv…
Consider the classes shown below: public class Parent { private int value = 100; public int getValue() { return value; } } public class Child extends Parent { private int value; public Child(int number) { value = number; } public int getValue() { return value; } } What is the output of the following lines of code? Child kid1 = new Child(-14); Child kid2 = new Child(21); System.out.println(kid1.getValue() + ” ” + kid2.getValue());
Consider the following recursive code snippet: public int my…
Consider the following recursive code snippet: public int mystery(int n, int m) { if (n == 0) { return 0; } if (n == 1) { return m; } return m + mystery(n – 1, m); } What parameter values for n would cause an infinite recursion problem in the following method?