Assignment 3

Due Feb 10 on line

Part of the homework for CS:2820, Spring 2021
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Simple multiple choice questions, 1 point each:

  1. We model a road network using a directed graph. Suppose we have intersections A and B, and there is a two-way street between them. We model this street with:
    a) an edge connecting vertex A with vertex B.
    b) two edges, one from B to A, and one from A to B. — correct;
    c) an edge from vertex A to vertex B.
    d) an edge from vertex B to vertex A.
    e) a vertex representing the street and edges from it to A and B.

    a) is wrong because it describes an undirected edge or is the same as c), which is wrong for the same reason d) is wrong, because both describe just one-way links when the street is a two way link.

    e) descibes something very complex, where both streets and intersections are vertices in the graph; it must be wrong because edges only go from streets to intersectins, making it impossible to find which streets lead away from an intersection.

  2. In the epidemic model, the two key objects, in the informal sense, are people and places. This suggests we will have class Person and class Place in our code. Given what has been said about these, which of the following is most likely to be a central part of the declaration of class Person;?
    a) Collection <Place> whereHaveIBeen;
    b) Place whereAmI — correct;
    c) Collection <Person> whoDoIKnow;
    d) Collection <Person> whoHaveIMet;
    e) Person BestFriend;

    b) is right because knowing where a person is at any moment is central to all other things you might do with a person. Where the person is right now determines (based on who else is there now) whether and how likely that person is to get infected right now. As we advance the simulation, where the person is will change, allowing the person to spread the infeciton or get infected.

    In contrast, all the others might be relevant to some refinement of the model. a) and d) involve keeping historical records. This might be interesting, but to generate these records, you need to start with where the person is at each time. c) and e) are the kinds of things you might want to use to plan a person's future actions. Nothing we have discussed suggests plans of that complexity but we might want to add such planning in the future.

  3. Which of the following is a plausible event in a discrete-event simulation of a highway network?
    a) a vehicle passes through an intersection.
    b) a vehicle passes down a road.
    c) a stop light stays green for 30 seconds.
    d) an intersection is occupied so incoming vehicles must wait.
    e) none of the above. — correct;

    It must be e), none of the above. None of the other alterntives match the crucial feature of an event: That it describes something that takes place at a single instant. In contrast, a), b) and c) describe things that take time, passing through an intersection may only take a second, while all the others are longer. d) is not an event because it describes a state that may be caused by one or more other vehicles passing through an intersection.

  4. Java class Scanner is a monster. We'll be using it quite a bit to pick apart input from text files. Which of the following services is not included in class Scanner? (This is a research question, you'll need to go looking.)
    a) recognize that the next bit of input is a word.
    b) get the next word from the input.
    c) get the next integer from the input.
    d) recognize a misspelled words in the input. — correct;
    e) recognize that the next bit of input matches some pattern.

    Scanners do everything but d). Perhaps you could write a pattern that describes some set of anticipated misspellings of a word, but people's ability to misspell seems to be unbounded and everyone these days has experienced spelling recognizers that do a far more remarkable job than you could do with just an enumeration of expected misspellings.

  5. This is legal in Java: int i = ~((4 << 1) & 0;
    The above should be: int i = ~((4 << 1) & 0);
    You may need to look up some of the operators in this code to figure it out. Note that all of these operators are also legal in Python expressions. Which of the following is not true of this expression?
    a) The value of (4 << 1) is irrelevant.
    b) i ends up equal to -1 because ~0 is -1.
    c) (4 << 1) is the same as 2*4.
    d) The & operator is the same as &&. — correct;
    e) Anything & 0 is zero.

    All of the above are true except d), which is absolutely false. The && operator is a boolean and, requiring the two operands to be boolean values, either true or false. In contrast, & operator is a bitwise operator that applies to integer values, where 12&5 is 4. (Note that in C and C++, there is no boolean type, boolean values are represented by integers, so both & and && take integer operands. Even so, they do very different things, serving the same purpose they do in Java.