Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

  1. 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?

    1. Both FoodItem and Dessert can reuse public methods of MenuItem.

    2. Both FoodItem and Dessert type of object can be passed to a method that accepts a MenuItem as a parameter.

    3. Pat's design better reflects the IS-A relationships between the three classes.

    1. None of the three

    2. I only

    3. II only

    4. I and II only

    5. I, II and III

  2. 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?

    1. 23

    2. 25

    3. 26

    4. 27

    5. 28

  3. 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?

    1. 2

    2. 3

    3. 5

    4. 10

    5. 13

  4. 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 > } }

  5. Which of the following could replace <missing expression> in the getX method of the Polygon class?

    1. vertices[k].x

    2. vertices.getX(k)

    3. vertices.get(k).x

    4. vertices.get(k).getX()

    5. vertices.get(k+1).getX()

  6. 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);

    1. I only

    2. II only

    3. I and II only

    4. II and III only

    5. I, II, and III

  7. 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?

    1. Only into C

    2. Only into C or any subclass of C

    3. Only into C or any superclass of C

    4. Into any class

    5. This method will always cause a syntax error, no matter what class we place it in

  8. 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)

    1. I only

    2. II only

    3. I and II only

    4. II and III only

    5. I, II, and III

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

Explain the essential features of life cycle costing.

Answered: 1 week ago