Junaid Khan
Quiz von , erstellt am more than 1 year ago

Set of Final Question

155
0
0
Junaid Khan
Erstellt von Junaid Khan vor fast 6 Jahre
Schließen

EECS 2030

Frage 1 von 39

1

Interfaces and Inheritance- What is an interface?

Wähle eine der folgenden:

  • an abstract class

  • an abstract class that other classes may extend

  • a class

  • a class that other classes may extend

  • a declaration of an API

Erklärung

Frage 2 von 39

1

Interfaces and Inheritance- Java supports single inheritance; what does the term single inheritance mean?

Wähle eine der folgenden:

  • every class has exactly one parent class

  • every class has exactly one child class

  • every class implements exactly one interface

  • every class inherits from Object

Erklärung

Frage 3 von 39

1

Interfaces and Inheritance- Consider the following classes related by inheritance:
See Figure: pg 11 of 28
Question 27
Assuming A, B, and C all have public no-argument constructors, which of the following variable definitions are legal?

Wähle eine der folgenden:

  • A a = new B();

  • B b = new A(); (*)

  • C c = new B(); (**)

  • C c = new A(); (***)

  • B (*)and C (**)

  • B(*) C(**) D(***)

  • None of the expression is legal

Erklärung

Frage 4 von 39

1

Interfaces and Inheritance-Consider the following classes related by inheritance:
Which of the following statements is the most accurate and complete regarding the class C?

Wähle eine der folgenden:

  • C inherits all of the fields and methods from A

  • C inherits all of the fields and methods from B

  • C inherits all of the fields and methods from A and B 11/28 Q28

  • C inherits all of the fields and methods from A and B and may add its own fields and methods

  • C is made up of an A subobject

  • C is made up of an B subobject

  • C is made up of an A subobject and a B subobject

Erklärung

Frage 5 von 39

1

12/28 Q29.
Consider the following two classes related by inheritance:
public class AdultCat {
private int size; /* INVARIANT: this.size > 0 && this.size <= 10 */
public AdultCat(int size) {
/* IMPLEMENTATION NOT SHOWN */
}
/**
* Sets the size of this cat to be equal to sz. If sz is greater than 10
* then the size of this cat is set to some value between 1 and 10.
*
* @param sz the desired size of this cat
* @pre. sz is greater than 0
*/
public void setSize(int sz) {
/* IMPLEMENTATION NOT SHOWN */
}
}
public class Lion extends AdultCat {
public Lion(int size) {
/* MISSING STATEMENT */
}
/**
* Sets the size of this cat to be equal to sz. If sz is greater than 10
* then /* MISSING POSTCONDITION */
*
* @param sz the desired size of this cat
* @pre. /* MISSING PRECONDITION */
*/
@Override
public void setSize(int sz) {
/* IMPLEMENTATION NOT SHOWN */
}
}

What can the line with the comment /* MISSING STATEMENT */ be replaced with in the Lion constructor?

Wähle eine der folgenden:

  • super(size);

  • this(size);

  • this.setSize(size);

  • this.size = size;

Erklärung

Frage 6 von 39

1

** REFER TO Q29.
Interfaces and Inheritance
- The precondition of the Lion version of setSize is incomplete (ends with
/* MISSING PRECONDITION */); which of the following can be used to complete the precondition
so that Lion is substitutable for AdultCat?

Wähle eine der folgenden:

  • size is greater than 0 and less than 10

  • size is greater than 0 and less than 11

  • size is greater than 1

  • size is greater than -1

  • size is not equal to 0

Erklärung

Frage 7 von 39

1

Interfaces and Inheritance-
**REfere to Q29**
Can the AdultCat constructor be safely implemented like so?:
public AdultCat(int size) {
this.setSize(size);
}

Wähle eine der folgenden:

  • no, the postcondition of setSize does not guarantee that the class invariant is true

  • no, setSize is not a final method

  • yes, setSize can be overridden by child classes

  • yes, using setSize minimizes code duplication

Erklärung

Frage 8 von 39

2

Class Features-What is an obligatory method?

Wähle eine der folgenden:

  • a method that can be invoked using any object reference.

  • a method that the client must invoke, such as a constructor.

  • a method that the implementer must override.

  • a method that the implementer must not override

Erklärung

Frage 9 von 39

2

Class Features-What is a class invariant?

Wähle eine der folgenden:

  • a condition that the method promises will be true immediately after the method finishes running

  • a condition that the method must check at the beginning of the method

  • a condition that the user (client) must ensure is true before calling the method

  • . a condition that is always true immediately after a constructor or method finishes running

Erklärung

Frage 10 von 39

2

Class Features-Which statement is true for a static attribute (or field) of a class X?

Wähle eine der folgenden:

  • The class X has one copy of the attribute for every x object.

  • The class X has the only copy of the attribute

  • Each X object has its own copy of the attribute

  • Only objects of classes that extend X have a copy of the attribute.

Erklärung

Frage 11 von 39

2

Class Features- Suppose that an implementer creates a class that defines no constructors in the class body. Which one
of the following statements is correct?

Wähle eine der folgenden:

  • the class must be a utility class

  • the class does not compile.

  • the class compiles but is not useable.

  • the compiler adds a default (no-argument) constructor.

Erklärung

Frage 12 von 39

2

Class Features- Which of the following is required for objects of a class to be considered immutable?

Wähle eine der folgenden:

  • the class should be declared as final

  • the class should have no public mutators

  • all fields in the class should be private

  • class methods should not return references to class fields

  • all of the above

  • some of the above

Erklärung

Frage 13 von 39

2

Class Features- The class MyBoolean has a single attribute named value of type boolean. Consider the following
implementation of the equals method:

public boolean equals(Object object) {
boolean equals;
if (object != null && this.getClass() == object.getClass()) {
MyBoolean other = (MyBoolean) object;
equals = this.value && other.value;
}
else {
equals = false;
}
return equals;
}

Wähle eine der folgenden:

  • x.equals(y) returns true if and only if y.equals(x) returns true

  • x.equals(null) returns false

  • x.equals(x) returns true

  • if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) returns
    true.

Erklärung

Frage 14 von 39

2

Class Feature- Consider the following code fragment:
/**
* This method does something
*
* @param i an integer
* @pre. i>=0
*/
public void method(int i) {
/* method body not shown */
}
Which of the following statements is correct?

Wähle eine der folgenden:

  • In the method body, the implementer has to throw an exception when i is smaller than zero

  • In the method body, the implementer has to take the absolute value of i to ensure that its
    value is greater than or equal to zero.

  • In the method body, the implementer has to set i to zero whenever the value of i is smaller
    than zero

  • None of the above

Erklärung

Frage 15 von 39

2

Class Features- What are the parts of a method signature?

Wähle eine der folgenden:

  • method name and return type

  • method name and list of parameter types

  • method name, list of parameter types, and return type

  • method name, modifiers, list of parameter types, and return type

Erklärung

Frage 16 von 39

2

Class Features- What is the difference between x.equals(y) and x == y for two references x and y?

Wähle eine der folgenden:

  • x == y does not compile

  • equals compares equality of state and == compares equality of addresses

  • equals compares equality of addresses and == compares equality of state

  • there is no difference

Erklärung

Frage 17 von 39

2

Class Features-What is true about the expression x.compareTo(y) if x is an instance of a type that implements the
comparable interface?

Wähle eine der folgenden:

  • x.compareTo(y) returns a postive integer if x is less than y

  • x.compareTo(y) returns a postive integer if x is equal to y

  • x.compareTo(y) returns a postive integer if x is greater than y

  • x.compareTo(y) must return 0 if x is equal to y

Erklärung

Frage 18 von 39

2

Class Features- Consider the following class:
public class A {
public static int x = 0;
public int y;
public A() {
this.y = 0;
}
}
Suppose that you have a reference of type A:
A a = new A();

Wähle eine der folgenden:

  • a.x = 1;

  • a.y = 1

  • A.x = 1;

  • A.y = 1;

Erklärung

Frage 19 von 39

2

Testing- What makes up a unit test for a method in Java?

Wähle eine der folgenden:

  • arguments to the method and the expected result of running the method with the arguments

  • arguments to the method that do not satisfy the preconditions of the method

  • a program that prints out the result of running the method

  • a program that records the result of running the method

Erklärung

Frage 20 von 39

2

Testing- What is the main purpose of testing?

Wähle eine der folgenden:

  • to find errors in your code

  • to make sure that every line of code is necessary

  • to make sure that every line of code is run at least once

  • to prove that your code is correct

Erklärung

Frage 21 von 39

2

Testing- Consider the following method that determines if a numeric grade is equivalent to a letter grade of A+:
/**
* Returns true if the specified grade is equivalent to a letter grade
* of A+. A numeric grade greater than or equal to 90 is an A+.
*
* @param grade a numeric grade
* @returns true if grade is equivalent to A+ and false otherwise
* @pre. grade is less than or equal to 100
*/
public static boolean isAPlus(int grade) {
// IMPLEMENTATION NOT SHOWN
}
Which of the following values of grade would be considered as boundary cases for the method isAPlus?

Wähle eine der folgenden:

  • 89

  • 90

  • 100

  • 101

  • all of the above

  • A and B

  • A, B, and C

Erklärung

Frage 22 von 39

2

Testing - Consider the following class that represents a temperature in degrees Celcius or degrees Fahrenheit:
public class Temperature {
private double degrees;
private String units; // INVARIANT: this.units is equal to "C" or "F"

public Temperature() {
this.degrees = 0.0;
this.units = "C";
}
/**
* Changes the units of this temperature to the specified units
* if the specified units is equal to "C" or "F", otherwise leaves
* the current units of this temperature unchanged.
*
* @param the desired units of this temperature
*/
public void setUnits(String units) {
if (units == "C" || units == "F") {
this.units = units;
}
}
public void getUnits() {
return this.units;
}
}
Consider the following unit test for setUnit:
@Test
public void test_setUnits() {
String units = /* SEE TABLE BELOW */
String expected = /* SEE TABLE BELOW */
Temperature t = new Temperature();
t.setUnits(units);
assertEquals(expected, t.getUnits());
}
Test case units expected
See test cases on pg 5/28 question 15

Which test case fails?

Wähle eine der folgenden:

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • all of the test cases fail

  • none of the test cases fail

Erklärung

Frage 23 von 39

2

Testing - Consider the following class that represents a two-dimensional point having an x-coordinate and a
y-coordinate.
public class Point2 {
private int x;
private int y;
public Point2(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
// implementation not shown
}
}
equals is implemented using the guidelines discussed in class, and two points are considered equal if
their x components are equal and their y components are equal. Consider the following unit test cases
for equals:

See table: question 16 pg 6/28

Wähle eine der folgenden:

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • all of the choices should be used

  • none of the choices should be used

Erklärung

Frage 24 von 39

2

Testing- Suppose that you want to compute the average of all of the values that are greater than 10 in a list x.
You implement the following method:
/**
* Returns the average of all values greater than 10 in the list x.
*
* @param x a list
* @return the average of all values greater than 10 in the list x
* @pre. x.size() > 0
*/
public static double avg(List<Double> x) {
double avg = 0.0;
int n = 0;
for (int i = 0; i < x.size(); i++) {
double xi = x.get(i);
if (xi > 10.0) {
n = n + 1;
avg = avg + xi;
}
}
return avg / n;
}
After testing your code, you are convinced that it is correct and you give it to your friends to use in
their project. A couple of weeks later, your friends tell you that they did poorly on their project because
of your code. What went wrong?

Wähle eine der folgenden:

  • you forgot to test the case where x has only negative values

  • you forgot to test the case where x has only one value

  • you forgot to test the case where x has only positive values

  • you forgot to test the case where x has values all greater than 10

  • you forgot to test the case where x has values all less than 10

  • you forgot to test the case where x has some values equal to 10

Erklärung

Frage 25 von 39

2

Aggregation and composition- Consider the following class that represents a line segment connecting two points (its start point and
its end point):
public class LineSegment {
private Point2 start;
private Point2 end;
public Line(Point2 p1, Point2 p2) {
this.start = p1;
this.end = p2;
}
}

See the diagram

Wähle eine der folgenden:

  • a

  • b

  • c

  • d

Erklärung

Frage 26 von 39

2

Aggregation and composition- In the LineSegment class from the previous question(q18), the constructor contains the line:
this.start = p1;
Which choice below best completes the following statement: “this.start is for p1”

Wähle eine der folgenden:

  • an alias

  • a shallow copy

  • a deep copy

  • a privacy leak

  • none of the aboe

Erklärung

Frage 27 von 39

2

Aggregation and composition- When does a privacy leak occur?

Wähle eine der folgenden:

  • when the client passes a reference to an object to a constructor or method

  • when an object returns the value of one of its primitive fields

  • when an object returns a reference to one of its immutable fields

  • when an object returns a reference to one of its mutable fields

Erklärung

Frage 28 von 39

2

Aggregation and composition- Suppose that you want to make a shallow copy of a List<Widget> object where Widget is some
class. What public feature does Widget need to provide for you to make a shallow copy of the list?

Wähle eine der folgenden:

  • a constructor

  • a copy constructor

  • a method that returns a Widget that is equal to another Widget

  • an overridden equals(Object obj) method

  • none of the above

Erklärung

Frage 29 von 39

2

Aggregation and composition- Consider the following class
public class DeckOfCards {
private Set<Card> cards; /* the only field in DeckOfCards */
public DeckOfCards() {
/* IMPLEMENTATION NOT SHOWN but sets this.cards to be equal to
standard deck of 52 playing cards */
}
public DeckOfCards(DeckOfCards other) {
this.cards = new HashSet<>();
for (Card card : other.cards) {
this.cards.add(card);
}
}
public Set<Card> getCards() {
/* IMPLEMENTATION NOT SHOWN but returns either:
an alias to this.cards OR
a new copy of this.cards */
}
}

Wähle eine der folgenden:

  • an alias for other.cards

  • a shallow copy of other.cards

  • a deep copy of other.cards

  • none of the above

Erklärung

Frage 30 von 39

2

Aggregation and composition- Consider the following class that uses DeckOfCard from Question 22:
public class Test {
public static void main(String[] args) {
DeckOfCards deck = new DeckOfCards();
DeckOfCards copy = new DeckOfCards(deck);
Set<Card> deckCards = deck.getCards();
Set<Card> copyCards = copy.getCards();
System.out.println(deckCards.equals(copyCards));
}
}

Wähle eine der folgenden:

  • false because deckCards contains different cards than copyCards

  • false because deckCards returns a reference to a different object than copyCards

  • true because deckCards contains the same cards as copyCards

  • true because deckCards returns a reference to the same object as copyCards

Erklärung

Frage 31 von 39

2

Aggregation and composition- Consider the following class that uses DeckOfCard from Question 22:
public class Test {
public static void main(String[] args) {
DeckOfCards deck = new DeckOfCards();
Set<Card> deckCards = deck.getCards();
System.out.println(deckCards == deck.getCards());
}
}
Suppose that the program above prints true; what is the most likely statement regarding the method
getCards?

Wähle eine der folgenden:

  • any reasonable implementation of getCards will always cause the program to print true

  • getCards has a privacy leak

  • getCards returns a shallow copy of this.cards

  • it is impossible to implement getCards so that the program prints true

Erklärung

Frage 32 von 39

2

Complexity and Recursion-In plain English, what is the meaning of a big-O complexity O(g(n))?

Wähle eine der folgenden:

  • an estimate of best-case runtime operations of a method to solve a problem of size n

  • an estimate of the worst-case runtime operations of a method to solve a problem of size n

  • an estimate of the average runtime operations of a method to solve a problem of size n

  • an estimate of the best-case memory usage for a method to solve a problem of size n

  • an estimate of the worst-case memory usage for a method to solve a problem of size n

  • an estimate of the average memory usage for a method to solve a problem of size n

Erklärung

Frage 33 von 39

2

Complexity and Recursion- If an algorithm has complexity O(n^2) then which of the following statements is the most correct?

Wähle eine der folgenden:

  • doubling the size of the problem does not affect the amount of time needed for the algorithm
    to solve the problem

  • doubling the size of the problem doubles the amount of time needed for the algorithm to
    solve the problem

  • doubling the size of the problem increases the amount of time needed for the algorithm to
    solve the problem by 1 unit of time

  • doubling the size of the problem quadruples the amount of time needed for the algorithm to
    solve the problem

Erklärung

Frage 34 von 39

2

Complexity and Recursion- Consider the following implementation of the set method on a singly linked-list:
/**
* Sets the element stored at the given index in this linked list.
* Returns the old element that was stored at the given index.
*
* @param index the index of the element to set
* @param elem the element to store in this linked list
* @return the old element that was stored at the given index
* @throws IndexOutOfBoundsException if (index < 0) || (index > size)
*/
public char set(int index, char elem) {
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
}
Node n = this.head;
for (int i = 0; i < index; i++) {
n = n.next;
}
char oldData = n.data;
n.data = elem;
return oldData;
}
What is the Big-O complexity for this method?

Wähle eine der folgenden:

  • O(1)

  • O(log(n))

  • O(n)

  • O(nlog(n))

  • O(n^2)

Erklärung

Frage 35 von 39

2

Complexity and Recursion- Whenever a recursive method is called, the method:

Wähle eine der folgenden:

  • always calls itself at least once

  • always calls itself zero or more times

  • always returns a value

  • never contains a loop

Erklärung

Frage 36 von 39

2

Complexity and Recursion- In plain English, what is the meaning of a recurrence relation T(n)?

Wähle eine der folgenden:

  • T(n) is the big-O complexity of a recursive method

  • T(n) is the number of elementary operations required by the base case of the method

  • T(n) is the number of elementary operations required by the recursive case of the method

  • T(n) is the number of elementary operations required by the method to solve a problem of
    size n

Erklärung

Frage 37 von 39

2

Complexity and Recursion- What do you expect the Big-O complexity of mergesort to be?

Wähle eine der folgenden:

  • O(1)

  • O(log(n))

  • O(n)

  • O(nlog(n))

  • O(n^2)

Erklärung

Frage 38 von 39

2

Consider the following recursive implementation of a merge sort (pseudocode only):
public static LinkedList<Integer> mergeSort(LinkedList<Integer> t) {
if (t.size() <= 1) {
return t;
}
LinkedList<Integer> left = mergeSort(t.sublist(0, t.size() / 2) );
LinkedList<Integer> right = mergeSort(t.sublist(t.size()/2 + 1, t.size() );
LinkedList<Integer> result = merge(left,right);
return result;
}

Wähle eine der folgenden:

  • T(0) = T(1) = 0

  • T(0) = T(1) = c for some constant postive integer c

  • T(n) = c for some constant postive integer c

  • T(n) = O(n) where n is the number of elements in the list

Erklärung

Frage 39 von 39

2

What is the recurrence relation for mergesort for the recursive case where n is the number of elements
in the list? Note: you may assume the merge method has a complexity of O(n); and the value of n/2 is
computed using integer division

Wähle eine der folgenden:

  • T(n) = d for some constant postive integer d

  • T(n) ≈ T(n − 1) + d for some constant postive integer d

  • T(n) ≈ T(n/2) + d for some constant postive integer d

  • T(n) ≈ 2T(n/2) + d for some constant postive integer d

  • T(n) ≈ 2T(n/2) + O(n) for some constant postive integer d

  • T(n) ≈ 2T(n/2) + O(n^2) for some constant postive integer d

Erklärung