The equals method is defined in the Object class. Which of the following is correct to override it in the String class?
public static boolean equals(String other)
public static boolean equals(Object other)
public boolean equals(String other)
public boolean equals(Object other)
4. public boolean equals(Object other)
** Overridden methods must have exactly the same method header as in the parent class **
Which of the following statements are true?
Override the equals(Object) method inherited from the Object class whenever possible.
Override the toString() method inherited from the Object class whenever possible.
A public default no-arg constructor is assumed if no constructors are defined explicitly.
You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.
ALL of them are true.
Which of the following is used to describe a stack?
LIFO (last-in-first-out)
FIFO (first-in-first-out)
deque
queue
1. LIFO (last-in-first-out)
Which of the following is used to describe a queue?
LIFO (last-in-first-out)
FIFO (first-in-first-out)
deque
stack
2. FIFO (first-in-first-out)
For the following expression, what is the order of the growth (expressed in Big O notation)?
3n4 + 27
O(n4)
For the following expression, what is the order of the growth (expressed in Big O notation)?
n + log(n) + 191n2
O(n2)
An ArrayList is always more efficient than a Linked List for which one of the following operations:
Insert/delete an element in the middle of the list.
Insert/delete an element in the beginning of the list.
Insert/delete an element at the end of the list.
Retrieve an element given the index.
4. Retrieve an element given the index.
For the following expression, what is the order of the growth (expressed in Big O notation)?
n log(n) + 7n
O(n log n)
What is displayed when the following code fragment is executed?
list<Integer> list - new ArrayList<Integer> {
list.add(0);
list.add(1);
list.add(2);
list.add(1,4);
list.set(2,30);
System.out.println(list);
}
[0, 4, 30, 2]
What is the Big-O time estimate of the following method in terms of n?
public int doSomething (int n) {
int sum = 0;
if (n % 2 == 1) {
for (int i = 0; i < n % 2; i++) {
sum++;
}
else {
sum = sum + n;
}
return sum;
}
O(1)
Why is the analysis often for the worst case?
Best-case is not representative of real-world situations.
Average-case analysis is hard to determine the relative probabilities and distributions of various input instances for many problems.
You can show that the algorithm will never be slower than the worst-case.
All of the above
4. All of the above
Which of the following Big-O categories are correctly sorted from fastest to slowest? The algorithm that executes the fastest should be on the left.
* all the "n2" and "n3" are squares/cubes *
O(n3), O(n2), O(n), O(log n), O(1)
O(1), O(log n), O(n), O(n2), O(n3)
O(n3), O(n2), O(n log n), O(n), 0(1)
O(1), O(n log n), (n), O(n2), O(n3)
2. O(1), O(log n), O(n), O(n2), O(n3)
*Bonus: Where would O(n log n) go?
Consider the following Java statements:
int num = -5;
int result = 1;
Java.util.Stack<Integer> mystack
= new Java.util.Stack<Integer>();
while (num < 0) {
myStack.push(num + 3);
num++;
}
while (!myStack.isEmpty()) {
Integer value = myStack.pop();
result = result + value;
}
System.out.println(result);
What numerical value is displayed when this code executes?
1
What is the Big-O time estimate of the following method in terms of n?
public int doSomething (int n) {
int sum = 0;
if (n % 2 == 1) {
for (int i = 0; i < n * n; i++) {
sum++;
}
else {
sum = sum + n;
}
return sum;
}
O(n2)
What is displayed when the following code fragment is executed?
public class Test {
public static void main (String[] args) throws Exception {
List<Integer> list = new Arraylist();
list.add(1);
list.add (2);
list.add(1);
list.add(3);
remove(list, 1);
System.out println(list):
}
public static void remove (List List, int value) {
int k = 0;
while (k < list.size()) {
if (list.get(k).equals(value)) {
list.remove(k);
}
k++;
}
}
}
[2, 3]
What is the output of the following code?
public class Test {
public static void main(String[] args) {
List<Student> list = new Arraylist ();
list. add(new Student("Peter", 65));
list. add (new Student ("Jill",50));
list. add (new Student ("Sarah", 34)):
Collections.sort(list);
System.out.print(list + " ");
Collections.sort(list, new StudentComparator1());
System.out.println(list);
}
static class StudentComparator1 Implements Comparator<Student> {
public int compare (Student sl, Student s2) {
return s1.age - s2.age;
static class Student implements Comparable<Student> {
String Name;
int age;
Student (String name, int age) {
this.name = name;
this.age = age;
}
public int compareTo (Student e) {
return this. name. compareTo (s.name) ;
}
public String toString() {
return "[" + name + ", " + age + "]";
}
}
}
[[Jill, 50], [Peter, 65], [Sarah, 34]] [[Sarah, 34], [Jill, 50], [Peter, 65]]
Complete the implementation for the method countLines that has a File object as a parameter and returns the number of lines that contain text and whitespace in the file.
The method heading is:
public int countLines (File file) throws FileNotFoundException {
public int countLines (File file) throws FileNotFoundException {
// create an instance of Scanner with the file
Scanner sc = new Scanner(file);
int count = 0;
// read each line and count number of lines
while(sc.hasNextLine()) {
sc.nextLine();
count++;
}
return count;
}
Suppose that you want an operation for the ADT list that adds an array of items to the end of this List. Write an implementation of this method for the linked chain implementation of the interface List<E>. The interface is given on the last page.
The header of the method must be:
public void addAll (E[] items) {
public void addAll (E[] items){
for(E item : items)
append(item);
}
Suppose that you want an operation for the ADT list that copies the elements from one list (rightList) to another list (leftList). The goal is to create a new list, not a reference to the existing list. Write an implementation of this method for the linked chain implementation of the interface List<E>. The interface is given on the last page.
The header of the method must be:
public void copy (List<E> leftList, List<E> rightList) {
public void copy (List<E> leftList, List<E> rightList){
rightList.moveToFront();
while(!rightList.isAtEnd()){
E element = rightList.getValue();
leftList.append(element);
rightList.next();
}
}
Consider the following Java statements:
int num = 4;
int result = 1;
Java.util.Stack<Integer> mystack
= new Java.util.Stack<Integer>();
while (num > 0) {
myStack.push(num);
num--;
}
while (!myStack.isEmpty()) {
Integer value = myStack.pop();
result = result + value;
}
System.out.println(result);
What numerical value is displayed when this code executes?
11