Question
Amelia and Pat are working on a programming project for a restaurant. They are considering a hierarchy or classes that includes, among other classes, MenuItem,
-
Amelia and Pat are working on a programming project for a restaurant. They are considering a hierarchy or classes that includes, among other classes, MenuItem, FoodItem, and Dessert. Pat, who likes to eat his dessert first, is proposing to make both FoodItem and Dessert direct subclasses of MenuItem; Amelia's proposal is to make FoodItem a subclass of MenuItem and Dessert a subclass of FoodItem.
Pat's design Amelia's Design MenuItem
MenuItem
Dessert
FoodItem
FoodItem
Dessert
Which of the following is an advantage of Pat's design as compared to Amelia's?
-
Both FoodItem and Dessert can reuse public methods of MenuItem.
-
Both FoodItem and Dessert type of object can be passed to a method that accepts a MenuItem as a parameter.
-
Pat's design better reflects the IS-A relationships between the three classes.
-
None of the three
-
I only
-
II only
-
I and II only
-
I, II and III
-
-
Consider the following code segment.
int[] nums = new int [51]; for (int k = 0; k < nums.length; k++) nums[k] = 1; for (int k = 3; k <= 50; k += 3) nums[k] = 0; for (int k = 5; k <= 50; k += 5) nums[k] = 0;
How many elements in the array nums have the value 0 after this code has been executed?
-
23
-
25
-
26
-
27
-
28
-
-
Consider the following method.
public int countSomething(int[] p) { int count = 0; for (int i = 0; i < p.length; i++) { count++; int j = p[i] while (j != i) { j = p[j]; count++; } } return count; }
Given
int[] arr = {0, 2, 3, 1};
what will countSomething(arr) return?
-
2
-
3
-
5
-
10
-
13
-
-
public class Point { private int x, y; public Point(int _x, int _y) { x = _x; y = _y; } public int getX() { return x; } public int getY() { return y; } public void move(int dx, int dy { x += dx; y += dy; } } public class Polygon { private ArrayList
vertices; public Polygon() { vertices = new ArrayList (); } public void add(Point p) { vertices.add(p); } /* Returns the x-coordinate of the k-th vertex * (counting from 0) */ public int getX(int k) { return < missing expression >; } /** Moves every vertex of the polygon horizontally by dx * and vertically by dy */ public void move(int dx, int dy) { < missing code > } } -
Which of the following could replace <missing expression> in the getX method of the Polygon class?
-
vertices[k].x
-
vertices.getX(k)
-
vertices.get(k).x
-
vertices.get(k).getX()
-
vertices.get(k+1).getX()
-
-
Which of the following could replace <missing code> in Polygon's move method for it to work as specified?
for (int k = 0; k < vertices.size; k++) { Point p = vertices.get(k); p.x += dx; p.y += dy; }
for (int k = 0; k < vertices.size(); k++) vertices.get(k).move(dx, dy);
for (Point p : vertices) p.move(dx, dy);
-
I only
-
II only
-
I and II only
-
II and III only
-
I, II, and III
-
Suppose class C has a private int data field value:
public class C { private int value; < Other fields, constructors, and methods not shown > }
Suppose we have a method
public static int compare(C x, C y) { return x.value - y.value; }
and we need to find "home" for it: place it into some class. Where can we place this method so that it compiles with no errors?
-
Only into C
-
Only into C or any subclass of C
-
Only into C or any superclass of C
-
Into any class
-
This method will always cause a syntax error, no matter what class we place it in
-
-
Classes Salsa and Swing implement an interface Dance. If both calls
perform(new Salsa()); perform(new Swing());
are valid, which of the following headers of the perform method(s) in the class Dancer will compile successfully?
Two methods:
public void perform(Salsa dance) public void perform(Swing dance)
public void perform(Dance dance)
public void perform(Object dance)
-
I only
-
II only
-
I and II only
-
II and III only
-
I, II, and III
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started