test Qs part1
test Qs part2
test Qs part3
test Qs part4
100

The equals method is defined in the Object class. Which of the following is correct to override it in the String class?

  1. public static boolean equals(String other)

  2. public static boolean equals(Object other)

  3. public boolean equals(String other)

  4. 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 **

100

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.


100

Which of the following is used to describe a stack?

  1. LIFO (last-in-first-out)

  2. FIFO (first-in-first-out)

  3. deque

  4. queue

1. LIFO (last-in-first-out)

100

Which of the following is used to describe a queue?

  1. LIFO (last-in-first-out)

  2. FIFO (first-in-first-out)

  3. deque

  4. stack

2. FIFO (first-in-first-out)

200

For the following expression, what is the order of the growth (expressed in Big O notation)?

3n4 + 27

O(n4)

200

For the following expression, what is the order of the growth (expressed in Big O notation)?

n + log(n) + 191n2

O(n2)

200

An ArrayList is always more efficient than a Linked List for which one of the following operations:

  1. Insert/delete an element in the middle of the list.

  2. Insert/delete an element in the beginning of the list.

  3. Insert/delete an element at the end of the list.

  4. Retrieve an element given the index.

4. Retrieve an element given the index.

200

For the following expression, what is the order of the growth (expressed in Big O notation)?

n log(n) + 7n

O(n log n)

300

 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]

300

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)

300

Why is the analysis often for the worst case?

  1. Best-case is not representative of real-world situations.

  2. Average-case analysis is hard to determine the relative probabilities and distributions of various input instances for many problems.

  3. You can show that the algorithm will never be slower than the worst-case.

  4. All of the above

4. All of the above

300

 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 *

  1. O(n3), O(n2), O(n), O(log n), O(1)

  2. O(1), O(log n), O(n), O(n2), O(n3)

  3. O(n3), O(n2), O(n log n), O(n), 0(1)

  4. 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? 

400

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

400

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)

400

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]

400

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]]

500

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;

}

500

 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);

}

500

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();

}

}

500

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

M
e
n
u