CS final Flashcards
Which can cause the actions of the setUp() method to not be reflected at the start of each test class:
Initialized variables were changed in one of
the test methods
the variables initialized in setUp were also declared locally within setUp
if a tearDown() method is used
none- the actions of the setUp() method happen only once
Correct answer:
the variables initialized in setUp were also declared locally within setUp
Write a test method that will cover every case of the following method
public Hokie createHokie(String name) {
if (name.equals(“”)) {
return null;
}
return new Hokie(name);
}
public void testCreateHokie(){ assertNull(createHokie(“”)); assertNull(!createHokie(“Hannah”);}
public void testCreateHokie(){ assertEquals(null,createHokie(“”)); assertNotEquals(“Hannah”,createHokie(“Hannah”);}
public void testCreateHokie(){ assertNull(createHokie(“”)); assertNotNull(createHokie(“Hannah”);}
public void testCreateHokie(){ assertEquals(null,createHokie(“”)); assertEquals(“Hannah”,createHokie(“Hannah”);}
public void testCreateHokie(){ assertNull(createHokie(“”)); assertNotNull(createHokie(“Hannah”);}
What code would you use to test if the sum() method of the MyCalculator class is throwing the correct exception?
public int sum(String num1String, String num2String) {
int sum = 0;
try {
int num1 = Integer.parseInt(num1String);
int num2 = Integer.parseInt(num2String);
sum = num1 + num2;
}
catch (NumberFormatException nfe) {
throw new NumberFormatException();
}
return (sum);
}
}
NumberFormatException myNFE = null;
try {
calc.sum(“2hello”, “3”);
}
catch (NumberFormatException nfe) {
nfe = myNfe;
}
assertNotNull(myNFE);
NumberFormatException myNFE = null;
try {
calc.sum(“2”, “0”);
}
catch (NumberFormatException nfe) {
myNFE = nfe;
}
assertNotNull(myNFE);
ArithmeticException myAE = null;
try {
calc.sum(“2”, “0”);
}
catch (ArithmeticException ae) {
myAE = ae;
}
assertNotNull(ae);
NumberFormatException myNFE = null;
try {
calc.sum(“2hello”, “3”);
}
catch (NumberFormatException nfe) {
myNFE = nfe;
}
assertNotNull(myNFE);
NumberFormatException myNFE = null;
try {
calc.sum(“2hello”, “3”);
}
catch (NumberFormatException nfe) {
myNFE = nfe;
}
assertNotNull(myNFE);
Consider the Movie class:
class Movie {
String genre, title;
int year;
public Movie() {
}
public void setMovie(String title, String genre, int year)
{
this.title = title;
this.genre = genre;
this.year = year;
}
public void setYear(int year)
{
this.year = year;
}
public void print() {
System.out.println(title + “ “ + genre + “ movie in “ + year);
}
}
What is the output from the following code snippet?
Movie recentFilm = new Movie();
recentFilm.setMovie(“The Little Mermaid”, “musical”, 2023);
Movie classicFilm = recentFilm;
classicFilm.setYear(1989);
recentFilm.print();
The Little Mermaid musical movie in 2023
The Little Mermaid musical movie in 1989
The Little Mermaid musical movie in 1989, 2023
The Little Mermaid musical movie in 1989 2023
The Little Mermaid musical movie in 1989
The simplified UML diagram above shows the relationships among Java classes Bird, Crow, and Duck.
Suppose Bird has a land(Location place) method, but we want Ducks to makeSplash() just after they land and then behave like other Birds. Assuming Ducks have a makeSplash() method, we should
Group of answer choices
Define a land method in Duck that just consists of makeSplash(), i.e.
public void land(Location place) {
this.makeSplash();
}
, Not Selected
Define a land method in Duck by copying the land code from Bird then adding in makeSplash() at the start, i.e.
public void land(Location place) {
// [paste the body of Bird’s land method here]
this.makeSplash();
}
Define a land method in Duck that just consists of makeSplash() and Bird.land(place); i.e.
public void land(Location place) {
Bird.land(place);
this.makeSplash();
}
Define a land method in Duck that just consists of makeSplash() and super.land(place)
public void land(Location place) {
super.land(place); this.makeSplash();
}
Define a land method in Duck that just consists of makeSplash() and this.land(place), i.e.
public void land(Location place) {
this.land(place); this.makeSplash();
Define a land method in Duck that just consists of makeSplash() and super.land(place)
public void land(Location place) {
super.land(place); this.makeSplash();
}
Given the following 2 definitions for Computer and Notebook:
public class Computer {
private String manufacturer; private String processor; private int ramSize; private int diskSize; private double processorSpeed; public Computer(String manuf, String proc, int ram, int disk, double prcSpeed){ manufacturer = manuf; processor = proc; ramSize = ram; diskSize = disk; processorSpeed = prcSpeed; } public int getRamSize(){ return ramSize; } public int getDiskSize(){ return diskSize; } public double getProcessorSpeed(){ return processorSpeed; } public String toString(){ return "manufacturer: " + manufacturer + " " + "\nprocessor: " + processor + " "+ "\nramSize: " + ramSize + " " + "\ndiskSize: " + diskSize + " " + "\nprocessorSpeed: " + processorSpeed; }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Notebook extends Computer {
private double screenSize; private double weight; public Notebook( String manuf, String proc, int ram, int disk, double prcSpeed, double ScrnSz, double wt){ super(manuf, proc, ram, disk, prcSpeed); screenSize = ScrnSz; weight = wt; } public double getScreenSize(){ return screenSize; } public double getWeight(){ return weight;
}
}
And:
Computer workComputer = new Computer(“Acme”, “Intel”, 2, 160, 2.4);
Notebook yourComputer = new Notebook(“DellGate”, “AMD”, 4, 240, 1.8, 15.0, 7.5);
Which line of code doesn’t compile?
yourComputer .getRamSize();
workComputer.getScreenSize();
yourComputer .getScreenSize();
workComputer.getRamSize();
workComputer.getScreenSize();
Suppose you have an array of seven int values called test containing the following array:
{8, 14, 0,12, 10,9,20}
Trace the execution of the following code:
int x = 0;
int y = 0;
for (int i = 0; i < test.length; i++)
{
if (test[i] % 4 == 0)
{
x++;
y += test[i];
}
}
What is the value of x when this loop is done?
35
3
40
7
4
4
Given the following code:
for (int i=0; i < values.length; i++) {
if (values[i] == searchedValue){
found = true;
} else {
found = false;
}
}
if (found) {
System.out.println(“found “ + searchedValue);
} else {
System.out.println (“not found”);
}
If the initial values are:
int[] values = { 3, 6, 10, 12, 100, 200, 250, 500 };
int searchedValue = 10;
boolean found = false;
What is output?
not found
found 10
not found
The use of a generic data type is preferred over using Object as a general class in a collection because…
It allows the data type to be determined later by the client of the class.
It ensures that all the members of the collection are objects related by inheritance.
It can contain objects of type OrderedPair.
It ensures that all the members of the collection are objects related by inheritance.
for (int i=0; i < values.length; i++) {
if (values[i] == searchedValue){
found = true;
} else {
found = false;
}
}
if (found) {
System.out.println(“found “ + searchedValue);
} else {
System.out.println (“not found”);
}
If the initial values are:
int[] values = { 3, 6, 10, 12, 100, 200, 250, 500 };
int searchedValue = 250;
boolean found = false;
What is output?
not found
found 250
not found
For an ArrayBag when would the add method return false?
when there was not a duplicate of the entry already in the bag
when addition of a new item was successful
when the addition of a new item was not successful
when there was a duplicate of the entry already in the bag
when the addition of a new item was not successful
A fixed size array
can waste memory
has a limited capacity
all of these
prevents expansion when the bag becomes full
all of these
Which behavior(s) change the contents of a bag?
contains()
add()
toArray()
getFrequencyOf()
add()
Which of the following 5 implementations could not be used for contains in and ArrayBag as implemented in the course?
public boolean contains(T anEntry)
{
int i = 0;
while ((i <= numberOfEntries - 1))
{
if (anEntry.equals(contents[i]))
{
return true;
}
i++;
}
return false;
}
public boolean contains(T anEntry)
{
int i = 0;
while ((i <= numberOfEntries))
{
if (anEntry.equals(contents[i]))
{
return true;
}
i++;
}
return false;
}
public boolean contains(T anEntry)
{
return getFrequencyOf(anEntry) > 0;
}
public boolean contains(T anEntry)
{
boolean found = false;
int i = 0;
while ( !found && (i < numberOfEntries))
{
if (anEntry.equals(contents[i]))
{
found = true;
}
i++;
}
return found;
}
public boolean contains(T anEntry)
{
return getIndexOf(anEntry) > -1;
}
public boolean contains(T anEntry)
{
int i = 0;
while ((i <= numberOfEntries))
{
if (anEntry.equals(contents[i]))
{
return true;
}
i++;
}
return false;
}
Which of the following 5 implementations could not be used for getFrequency?
public int getFrequencyOf(T anEntry)
{
int i = 0;
int count = 0;
while( i < numberOfEntries)
{
if(anEntry.equals(contents[i]))
{
count++;
}
i++;
}
return count;
}
public int getFrequencyOf(T anEntry)
{
int count = 0;
for(int i = 0; i < numberOfEntries; i++)
{
if(anEntry.equals(contents[i]))
{
count++;
}
}
return count;
}
public int getFrequencyOf(T anEntry)
{
int count = 0;
for(int i = numberOfEntries - 1; i >= 0; i–)
{
if(anEntry.equals(contents[i]))
{
count++;
}
}
return count;
}
public int getFrequencyOf(T anEntry)
{
int count = 0;
for(int i = numberOfEntries - 1; i > -1; i–)
{
if(anEntry.equals(contents[i]))
{
count++;
}
}
return count;
}
public int getFrequencyOf(T anEntry)
{
int i = 0;
int count = 0;
while(anEntry.equals(contents[i]))
{
i++;
count++;
}
return numberOfEntries - count;
}
public int getFrequencyOf(T anEntry)
{
int i = 0;
int count = 0;
while(anEntry.equals(contents[i]))
{
i++;
count++;
}
return numberOfEntries - count;
}
The most efficient approach to dealing with a gap left in an array after removing an entry from a bag to correspond with the approaches used in this course, would be to
replace the entry being removed with the last entry in the array and replace the last entry with null
replace the entry being removed with the first entry in the array and replace the first entry with null
shift subsequent entries and replace the duplicate reference to the last entry with null
replace the entry being removed with null
replace the entry being removed with the last entry in the array and replace the last entry with null
Which of the following would not be an alternate way to write remove() method for an ArrayBag as implemented in the course
public T remove()
{
T result = null;
if (numberOfEntries > 0)
{
result = contents[numberOfEntries -1];
contents[numberOfEntries - 1] = null;
numberOfEntries–;
}
return result;
}
public T remove()
{
int lastEntry = numberOfEntries-1
return removeEntry(lastEntry);
}
public T remove()
{
T result = removeEntry(numberOfEntries-1);
return result;
}
public T remove()
{
T result = removeEntry(getIndexOf(null));
return result;
}
public T remove()
{
T result = removeEntry(getIndexOf(null));
return result;
}
Which method removes all entries of a bag?
remove()
none of these
clear()
delete()
clear()
Which of the following would be a way to write remove() method for an ArrayBag?
private T removeEntry(int givenIndex)
{ T result = null; if (!isEmpty() && (givenIndex >= 0)) { result = contents[givenIndex]; contents[givenIndex] = contents[numberOfEntries - 1]; contents[numberOfEntries - 1] = null; numberOfEntries--; } return result; }
public T remove()
{
T result = removeEntry(getIndexOf(null)); return result;
}
public T remove()
{
T result = null; if (numberOfEntries > 0) { contents[numberOfEntries - 1] = null; numberOfEntries--; } return result;
}
public T remove()
{
T result = removeEntry(numberOfEntries-1); return result;
}
public T remove()
{
return removeEntry(numberOfEntries--);
}
public T remove()
{
T result = removeEntry(numberOfEntries-1); return result;
}
Given:
class Link {
public Link next; //Point to next node in list
public Object data; //Value for this node
//Constructors
public Link(Object dataIn, Link nextIn) {
this.data = dataIn; this.next = nextIn;
}
public Link(Link nextIn) {
this.data = null; this.next = nextIn;
}
Object getData() { // Return the data field
return data;
}
void setData(Object newData) { // Set the data field
data = newData;
}
Link getNext() { // Return the next field
return next;
}
void setNext(Link newNext) { // Set the next field
next = newNext;
}
}
Given a linked chain of Integers reference by the variable head which contains data 20, 30, 10, 5 in that order. Consider the impact of the execution of the following lines of code:
Link p = head;
Link q = head.next;
Link r = q.next;
Integer myVal = q.data
What is the value of r.data?
20
5
30
10
10
Given:
Link head = new Link(null);
head.data = new Integer(20);
head.next = new Link(new Integer(30), null);
head.next.next = new Link(new Integer(10), null);
Link temp = head.next.next;
Which line of code would add another node to the end of the chain?
temp.data = new Link(new Integer(45), null);
temp.next.next = new Link(new Integer(45), null);
temp.next.data = new Link(new Integer(45), null);
temp = new Link(new Integer(45), null);
temp.next = new Link(new Integer(45), null);
temp.next = new Link(new Integer(45), null);
Given:
Link head = new Link(null);
head.data = new Integer(10);
head.next = new Link(new Integer(5), null);
head.next.next = new Link(new Integer(20), null);
Link temp = head.next.next;
temp.next = new Link(new Integer(30), null);
What would the linked chain look like if the following code was executed next?
temp.next.next = head;
head.next = null;
head = temp;
Head -> 10 ->5 -> 20
Head -> 20 ->30 -> 10
Head -> 10 ->5 -> 20 -> 30
Head -> 20 ->30 -> 10 -> 5
Head -> 20 ->30 -> 10
The following code will create a series of Link nodes, with the first one pointed to by head. What order will the Link nodes be in after this code is executed?
Link head = new Link(“K”, null);
head.next = new Link(“D”, new Link(“R”, null));
head.next.next.next = new Link(“F”, null);
Link c = head;
head = c.next;
head.next.next.next = c;
c.next = null;
head ->K->D->R->F
head ->K->R->F->D
head ->D->R->F->K
head ->K->D->F ->R
head ->K->D->R->F
Which loop starts at the first node of a non-empty linked chain and loops all the way through the chain?
while (curr.next != null)
Link curr = head.next; curr = curr.next;
Link curr = head.next;
while (curr != null)
curr = curr.next;
while (curr != null)
Link curr = head; curr = curr.next;
while (curr.next != null)
Link curr = head; curr = curr.next;
Link curr = head;
while (curr != null)
curr = curr.next;
Link curr = head;
while (curr != null)
curr = curr.next;
Which loop correctly starts at the second node of a non-empty linked chain and loops all the way through the chain?
while (curr.next != null)
Link curr = head; curr = curr.next;
Link curr = head;
while (curr != null)
curr = curr.next;
Link curr = head.next;
while (curr != null)
curr = curr.next;
While (curr != null)
Link curr = head; curr = curr.next;
while (curr != null)
Link curr = head.next; curr = curr.next;
Link curr = head.next;
while (curr != null)
curr = curr.next;
The efficient version of the remove method that has no parameter in the linked implementation of a bag
removes a random element in the chain
removes the last element in the chain
none of these
removes the first element in the chain
removes the first element in the chain
What does the”new” operator do in the following code from the LinkedBag add method:
Node newNode = new Node(newEntry);
all of these
a new node is created
a new object is instantiated
the JRE allocates memory for a node object
all of these
Which of the following is true about the following code:
Node newNode;
newNode.data = newEntry;
It causes an exception
a new object is instantiated
a new node is created
the JRE allocates memory for a node object
It causes an exception
What is the time complexity for adding an entry to an array-based bag ADT that is not a fixed length?
O(1)
O(n^2)
Negligible
O(n)
O(n)
What is the time complexity for adding an entry to a linked-based bag ADT?
O(n)
O(n^2)
negligible
O(1)
O(1)
For large values of n which statement is true?
(1000 n ) / 2 behaves like n^2
(1000 n ) / 2 behaves like n
all of these
(1000 n ) / 2 behaves like 500
(1000 n ) / 2 behaves like n
Imagine you have an empty stack of strings named stack. List the contents of the stack from top to bottom after the following operations have executed. (Assume that the pop method returns the element that was popped.)
stack.push(“K”);
stack.push(“P”);
stack.push(“G”);
stack.push(“R”);
String str = stack.pop();
stack.pop();
stack.push(“V”);
stack.push(str);
P V G P
G V P K
K P V R
R V P K
R V P K
If the top of the stack is the last element in a fixed-size array, what is the complexity for push(newEntry) in big Oh notation?
O(1)
O(n^2)
O(n)
O(logn)
Correct answer:
O(1)
If the top of the stack is the first entry of the array what is the complexity for pop() in big Oh notation?
O(1)
O(n)
O(n^2)
O(logn)
O(n)
In the course array-based implementation of a Stack ADT, the last entry in the the array is
Always the top of the stack
One slot after the bottom of the stack
One slot afer the top of the stack
Always the bottom of the stack
Always the top of the stack
In the demonstrated linked-chain implementation of a StackADT, when a node is popped from the stack
the second to last node in the chain will now be the top
the original first node in the chain will no longer be referenced
all of these
the original first node in the chain will have a new value
Correct answer:
the original first node in the chain will no longer be referenced
In the course linked-chain implementation of a StackADT, when a node is pushed onto the stack
the original first node will no longer be referenced
the original first node will now be the second node
the original first node will have a new value
the second to last node will now be the top
Correct answer:
the original first node will now be the second node
In the course linked-chain implementation of a StackADT, the efficiency of push and pop
Is better for push
Is better for pop
Depends on n
Is the same
is the same
Consider the design of a system used to track and manage the items in a library.
From discussions with librarians and other stakeholders you identified the following possible classes and fields.
Book
Fields of the Book class: library ID, name, description, data of acquisition, status, title, ISBN, category, authors, publisher, publication date
DVD
Fields of the DVD class: library ID, name, description, data of acquisition, status, title, actors, directors, genre, rating, length
Recognizing that Books, DVDs, and other forms of library media all contain similar pieces of data/information that must be tracked and managed, you decide to create a Media class (see below) and use inheritance since it improves code re-usability.
Media
Fields of the Media class: library ID, name, description, data of acquisition, status
Which would be the most appropriate way to utilize inheritance within your UML design?
Book as a superclass, with Movie as a subclass of Book, and Media as a standalone class
Media, Book, and Movie each as standalone class (not related by inheritance)
Movie as a superclass, with Book as a subclass of Movie, and Media as a standalone class
Media as a superclass, with Book and Movie as subclasses of Media
Media as a superclass, with Book and Movie as subclasses of Media
Consider the design of a system used to track and manage items in a running store.
From discussions with the owner and other stakeholders you identified the following possible classes and fields.
Fields of the Product class: product ID, name, price, size
Shoes: product ID, name, price, size, gender, width, brand, purpose
Clothes: product ID, name, price, size, gender, category, season
Which would be the most appropriate way to utilize inheritance within your UML design?
Item as a superclass, with Clothes as a subclass of Item, and Shoes as a standalone class
Clothes as a superclass, with Shoes as a subclass of Clothes, and Product as a standalone class
Product as a subclass, with Clothes and Shoes as superclasses of Product
Product as a superclass, with Clothes and Shoes as subclasses of Product
Dog, Reptile, and Product each as standalone class (not related by inheritance)
Correct answer:
Product as a superclass, with Clothes and Shoes as subclasses of Product
Consider the design of a system used to track and manage students at a university.
From discussions with administrators and other stakeholders you identified the following possible classes and fields.
Student
Fields of the Student class: student ID, name, gender, race, ethnicity, full-time status, GPA
Undergraduate student: student ID, name, gender, race, ethnicity, full-time status, GPA, majors, minors, hours completed
Graduate student: student ID, name, gender, race, ethnicity, full-time status,GPA, department, advisor, degree pursuing
Which would be the most appropriate way to utilize inheritance within your UML design?
Student as a subclass, with Undergraduate STudent and Graduate student as superclasses of Student
Student as a superclass, with Undergraduate student as a subclass of Student, and Graduate Student as a standalone class
Graduate Student as a superclass, with Student as a subclass of Graduate Student, and Undergraduate as a standalone class
Student, Undergraduate student, and Graduate student each as standalone class (not related by inheritance)
Student as a superclass, with Undergraduate Student and Graduate Student as subclasses of Student
Student as a superclass, with Undergraduate Student and Graduate Student as subclasses of Student
Which of the following could be considered an example of a non-functional requirement for this scenario?
The system shall allow users to search for a product item
The system shall notify users when an item has been successfully added to the shopping cart
The system shall process and complete user-submitted requests within 10 seconds
The system shall allow ABC support staff to look up an order
The system shall allow registered customers to place an order
The system shall allow users to add an item to the shopping cart
The system shall process and complete user-submitted requests within 10 seconds