Test
All Mix EECS 2030
Crea una cuenta gratis para terminar este test
Guarda tu progreso y consulta tu resultado final.
¿Ya tienes cuenta? Inicia sesión
Preguntas del test (39)
1Consider 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();
Which of the following is legal but considered bad style?- Aa.x = 1;
- Ba.y = 1
- CA.x = 1;
- DA.y = 1;
2What is true about the expression x.compareTo(y) if x is an instance of a type that implements the
comparable interface?- Ax.compareTo(y) returns a postive integer if x is less than y
- Bx.compareTo(y) returns a postive integer if x is equal to y
- Cx.compareTo(y) returns a postive integer if x is greater than y
- Dx.compareTo(y) must return 0 if x is equal to y
3What is the difference between x.equals(y) and x == y for two references x and y?
- Ax == y does not compile
- Bequals compares equality of state and == compares equality of addresses
- Cequals compares equality of addresses and == compares equality of state
- Dthere is no difference
4What are the parts of a method signature?
- Amethod name and return type
- Bmethod name and list of parameter types
- Cmethod name, list of parameter types, and return type
- Dmethod name, modifiers, list of parameter types, and return type
5Consider the following code fragment:
/**
* This method does something
*
* @param i an integer
* @pre. i>=0
*/
public void method(int i) {
/* method body not shown */
}- AIn the method body, the implementer has to throw an exception when i is smaller than zero
- BIn 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. - C. In the method body, the implementer has to set i to zero whenever the value of i is smaller
than zero. - DNone of the above.
6 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;
}
Which of the following properties does the above equals method NOT satisfy? For all non-null references x, y and z,- Ax.equals(y) returns true if and only if y.equals(x) returns true
- Bx.equals(null) returns false
- Cx.equals(x) returns true
- Dif x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) returns
true.
7Which of the following is required for objects of a class to be considered immutable?
- Athe class should be declared as final.
- Bthe class should have no public mutators
- Cclass methods should not return references to class fields.
- Dall of the above.
- Esome of the above
8Suppose that an implementer creates a class that defines no constructors in the class body. Which one
of the following statements is correct?- Athe class must be a utility class
- Bthe class does not compile
- Cthe class compiles but is not useable.
- Dthe compiler adds a default (no-argument) constructor.
9Which statement is true for a static attribute (or field) of a class X?
- AThe class X has one copy of the attribute for every x object.
- BThe class X has the only copy of the attribute
- CEach X object has its own copy of the attribute.
- DOnly objects of classes that extend X have a copy of the attribute
10What is a class invariant?
- Aa condition that the method promises will be true immediately after the method finishes running
- Ba condition that the method must check at the beginning of the method
- Ca condition that the user (client) must ensure is true before calling the method
- Da condition that is always true immediately after a constructor or method finishes running
11What is an obligatory method?
- Aa method that can be invoked using any object reference
- Ba method that the client must invoke, such as a constructor
- Ca method that the implementer must override.
- Da method that the implementer must not override.
12What makes up a unit test for a method in Java?
- Aarguments to the method and the expected result of running the method with the arguments
- Barguments to the method that do not satisfy the preconditions of the method
- Ca program that prints out the result of running the method
- Da program that records the result of running the method
13What is the main purpose of testing?
- Ato find errors in your code
- Bto make sure that every line of code is necessary
- Cto make sure that every line of code is run at least once
- Dto prove that your code is correct
14Consider 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
}- A89
- B90
- C100
- D101
- Eall of the above
- F89,90
- G89,90,100
15Consider 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"
Page 4 of 25
EECS2030 Sample Exam Questions Fall 2018-19
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());
}
SEE TEST CASE Pg: 5/25- A1
- B2
- C3
- D4
- E5
- F6
- Gall of the cases
- Hnone of the cases
16Consider 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:
Figgure: 6/26- A1
- B2
- C3
- D4
- E5
- F6
- Gall of choice should be used
- Hnone of choice should be used
17Suppose 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:
see 7/26
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?- Ayou forgot to test the case where x has only negative values
- Byou forgot to test the case where x has only one value
- Cyou forgot to test the case where x has only positive values
- Dyou forgot to test the case where x has values all greater than 10
- Eyou forgot to test the case where x has values all less than 10
- Fyou forgot to test the case where x has some values equal to 10
- Gyour friends did not use your code correctly
18Consider 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;
}
}
Which UML diagram best describes the relationship between LineSegment and Point2?- AA
- BB
- CC
- DD
19In the LineSegment class from the previous question, the constructor contains the line:
this.start = p1;
Which choice below best completes the following statement: “this.start is for p1”- Aan alias
- Ba shallow copy
- Ca deep copy
- Da privacy leak
- Enone of these
20When does a privacy leak occur?
- Awhen the client passes a reference to an object to a constructor or method
- Bwhen an object returns the value of one of its primitive fields
- Cwhen an object returns a reference to one of its immutable fields
- Dwhen an object returns a reference to one of its mutable fields
21Suppose 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?- Aa constructor
- Ba copy constructor
- Ca method that returns a Widget that is equal to another Widget
- Dan overridden equals(Object obj) method
- Enone of the above
2222> Consider the following class
9/26- Aan alias for other.cards
- Ba shallow copy of other.cards
- Ca deep copy of other.cards
- Dnone of the above
23. 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));
}
}- Afalse because deckCards contains different cards than copyCards
- Bfalse because deckCards returns a reference to a different object than copyCards
- Ctrue because deckCards contains the same cards as copyCards
- Dtrue because deckCards returns a reference to the same object as copyCards
24Consider 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?- Aany reasonable implementation of getCards will always cause the program to print true
- BgetCards has a privacy leak
- CgetCards returns a shallow copy of this.cards
- DgetCards returns a deep copy of this.cards
- Eit is impossible to implement getCards so that the program prints true
25What is an interface?
- Aan abstract class
- Ban abstract class that other classes may extend
- Ca class
- Da class that other classes may extend
- Ea declaration of an API
26 Java supports single inheritance; what does the term single inheritance mean?
- Aevery class has exactly one parent class
- Bevery class has exactly one child class
- Cevery class implements exactly one interface
- Devery class inherits from Object
27Consider the following classes related by inheritance:
11/25- AA a = new B();
- BB b = new A();
- CC c = new B();
- DC c = new A();
- EB and C
- FB, C, and D
- Gnone of the above are legal
28Consider the following classes related by inheritance:
Which of the following statements is the most accurate and complete regarding the class C?
12/26- AC inherits all of the fields and methods from A
- BC inherits all of the fields and methods from B
- CC inherits all of the fields and methods from A and B
- DC inherits all of the fields and methods from A and B and may add its own fields and methods
- EC is made up of an A subobject
- FC is made up of an B subobject
- GC is made up of an A subobject and a B subobject
29Consider the following two classes related by inheritance:
12/25- Asuper(size);
- Bthis(size);
- Cthis.setSize(size);
- Dthis.size = size;
30The postcondition of the Lion version of setSize is incomplete (ends with
/* MISSING POSTCONDITION */); which of the following can be used to complete the postcondition so that Lion is substitutable for AdultCat?- Aan IllegalArgumentException is thrown
- Bthe method returns false
- Cthe size of this lion is set to sz
- Dthe size of this lion is set to 8
31Can the AdultCat constructor be safely implemented like so?:
public AdultCat(int size) {
this.setSize(size);
}- Ano, the postcondition of setSize does not guarantee that the class invariant is true
- Bno, setSize is not a final method
- Cyes, setSize can be overridden by child classes
- Dyes, using setSize minimizes code duplication
32In plain English, what is the meaning of a big-O complexity O(g(n))?
- Aan estimate of best-case runtime operations of a method to solve a problem of size n
- Ban estimate of the worst-case runtime operations of a method to solve a problem of size n
- Can estimate of the average runtime operations of a method to solve a problem of size n
- Dan estimate of the best-case memory usage for a method to solve a problem of size n
- Ean estimate of the average memory usage for a method to solve a problem of size n
33If an algorithm has complexity O(n
2
) then which of the following statements is the most correct?- Adoubling the size of the problem does not affect the amount of time needed for the algorithm
to solve the problem - Bdoubling the size of the problem doubles the amount of time needed for the algorithm to solve
the problem - Cdoubling the size of the problem increases the amount of time needed for the algorithm to
solve the problem by 1 unit of time - Ddoubling the size of the problem quadruples the amount of time needed for the algorithm to
solve the problem
- Adoubling the size of the problem does not affect the amount of time needed for the algorithm
34. 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?- AO(1)
- BO(log(n))
- CO(n)
- DO(nlog(n))
- E. O(n
2
)
35Whenever a recursive method is called, the method:
- Aalways calls itself at least once
- Balways calls itself zero or more times
- Calways returns a value
- Dnever contains a loop
36In plain English, what is the meaning of a recurrence relation T(n)?
- AT(n) is the big-O complexity of a recursive method
- BT(n) is the number of elementary operations required by the base case of the method
- CT(n) is the number of elementary operations required by the recursive case of the method
- DT(n) is the number of elementary operations required by the method to solve a problem of
size n
37Consider 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;
}- AT(0) = T(1) = 0
- BT(0) = T(1) = c for some constant postive integer c
- CT(n) = c for some constant postive integer c
- DT(n) = O(n) where n is the number of elements in the list
389. 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- AT(n) = d for some constant postive integer d
- BT(n) ≈ T(n − 1) + d for some constant postive integer d
- CT(n) ≈ T(n/2) + d for some constant postive integer d
- DT(n) ≈ 2T(n/2) + d for some constant postive integer d
- ET(n) ≈ 2T(n/2) + O(n) for some constant postive integer d
- FT(n) ≈ 2T(n/2) + O(n
2
) for some constant postive integer d
39What do you expect the Big-O complexity of mergesort to be?
- AO(1)
- BO(log(n))
- CO(n)
- DO(nlog(n))
- EO(n
2
)
Información
- Descripción
- Test sobre All Mix EECS 2030, creado por Muhammad Khan el 09/04/2020.
- Autor
- Muhammad Khan
- Idioma
- English
- Visitas
- 5
- Valoración
- Aún sin valoraciones Valora este test