Practice

To create a list to store integers, use

a. ArrayList<Object> list = new ArrayList<Integer>();
b. ArrayList<Integer> list = new ArrayList<Integer>();
c. ArrayList<int> list = new ArrayList<int>();
d. ArrayList<Number> list = new ArrayList<Integer>();



Question 2

If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed?

Select one:
a. ABCD
b. ABDC
c. DCAB
d. DCBA




Question 3

What code is missing to complete the following method for sorting a list?

public static void sort(double[] list) {
___________________________;
}

public static void sort(double[] list, int high) {
   if (high > 1) {
      // Find the largest number and its index
      int indexOfMax = 0;
      double max = list[0];
      for (int i = 1; i <= high; i++) {
          if (list[i] > max) {
              max = list[i];
              indexOfMax = i;
         }
      }

// Swap the largest with the last number in the list
list[indexOfMax] = list[high];
list[high] = max;

// Sort the remaining list
sort(list, high - 1);
}
}

Select one:
a. sort(list)
b. sort(list, list.length)
c. sort(list, list.length - 1)
d. sort(list, list.length - 2)



Question 4

In what way can a Set be distinguished from other types of Collections?
“A Set cannot contain duplicate elements.”

Select one:
True
False



Question 5

Which of the following statements are true?

Select one or more:

a. Recursive methods usually takes more memory space than non-recursive methods.
b. A recursive method can always be replaced by a non-recursive method.
c. In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve.
d. Recursive methods run faster than non-recursive methods.



Question 6

Which of the following are true?

Select one or more:

a. A stack can be viewed as a special type of list, where the elements are accessed, inserted, and deleted only from the end, called the top, of the stack.
b. A queue represents a waiting list. A queue can be viewed as a special type of list, where the elements are inserted into the end (tail) of the queue, and are accessed and deleted from the beginning (head) of the queue.
c. Since the insertion and deletion operations on a stack are made only at the end of the stack, using an array list to implement a stack is more efficient than a linked list.
d. Since deletions are made at the beginning of the list, it is more efficient to implement a queue using a linked list than an array list.



Question 7

Suppose the rule at the office is that the workers who arrive later will leave earlier. Which data structure is appropriate to store the workers?

Select one:
a. Stack
b. Queue
c. Array List
d. Linked List



Question 8

To declare an interface named A with two generic types, use

a. public interface A<E> { ... }
b. public interface A<E, F> { ... }
c. public interface A(E) { ... }
d. public interface A(E, F) { ... }



Question 9

To create a list to store integers, use

a. ArrayList<Object> list = new ArrayList<Integer>();
b. ArrayList<Integer> list = new ArrayList<Integer>();
c. ArrayList<int> list = new ArrayList<int>();
d. ArrayList<Number> list = new ArrayList<Integer>();



Question 10

What is the printout of the following code?

List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
for (int i = 0; i < list.size(); i++)
     System.out.print(list.remove(i));

Select one:
a. ABCD
b. AB
c. AC
d. AD
e. ABC



Question 11

The server listens for a connection request from a client using the following statement:

Select one:
a. Socket s = new Socket(ServerName, port);
b. Socket s = serverSocket.getSocket()
c. Socket s = new Socket(ServerName);
d. Socket s = serverSocket.accept()



Question 12

Which of the following code is correct to obtain hour from a Calendar object cal?

Select one:
a. cal.get(Calendar.HOUR);
b. cal.getHour();
c. cal.hour();
d. cal.get(Hour);




The next two questions refer to this method:

    public void switchMethod(String input){
        switch(input){
            case "apple":
                System.out.println("sauce");
                break;
            case "orange":
                System.out.println("julius");
            case "banana":
                System.out.println("split");
                break;
            case "pear":
                System.out.println("juice");
            default:
                System.out.println(input);
        }



Question 13

What is the output to System out after the following method call: switchMethod("orange");

Select one:
a. sauce
b. julius
c. split
d. juice
e. None of the Above



Question 14

What is the output to System out after the following method call: switchMethod("strawberry");

Select one:
a. sauce
b. julius
c. juice
d. strawberry
e. None of the Above




The next two questions refer to the following:

1 public int factorial(int number){
2    if(number==1) {
3        return 1;
4    } else {
5        return number*(factorial(number-1));
6    }
7 }



Question 15

On which line is the base case for this recursive function?

Select one:
a. 1
b. 2
c. 4
d. 5
e. None of the Above



Question 16

On which line is the recursive call for this recursive function?

Select one:
a. 1
b. 2
c. 4
d. 5
e. None of the Above




The next two questions refer to the following:

Cell class reference:

public class Cell {
    public Cell(){
    }
    public char content;  // The character in this cell.
    public Cell next;     // Pointer to the cell to the right of this one.
    public Cell prev;     // Pointer to the cell to the left of this one.
}

The following method is supposed to move one node to the right in a doubly linked list of Cells but it contains a flaw.

The global variable 'currentCell' is a pointer to a Cell in the list.

1    public void moveRight() {
2           if (currentCell.next == null) {
3                Cell newCell = new Cell();
4                newCell.content = ' ';
5                newCell.next = null;
6                newCell.prev = currentCell;
7                currentCell.next = newCell;
8                currentCell = currentCell.next;
9                }
10        }



Question 17

True or False: If 'currentCell' is at the head of the list this moveRight() method will perform its duties correctly.

Select one:
True 
False



Question 18

True or False: If 'currentCell' is in the middle of the list this moveRight() method will perform its duties correctly.

Select one:
True
False




Question 19

Sample class reference:

public class Sample {

    private int number;
    private String color;

    public Sample(int number) {
        this.number = number;
        this.color = "blue";
    }
 
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Sample other = (Sample) obj;
        if ((this.color == null) ? (other.color != null) : !this.color.equals(other.color)) {
            return false;
        }
        return true;
    }
}

Given the above code for the Sample class what is the output of the following program:

public static void main(String args[]){
        List<Sample> list = new ArrayList();
        Sample sample1 = new Sample(1);
        Sample sample2 = new Sample(2);
        list.add(sample1);
        Boolean contains;
        contains = list.contains(sample2);
        System.out.println("Contains: " + contains);
    }

Select one:
a. Contains: true
b. Contains: false
c. Contains: null



Question 20

True or False: A Set is used to store objects in a particular order.

Select one:
True
False



Question 21

True or False: The following two statements will always yield the same value when a and b are Strings:

    1) a.equals(b);
    2) a==b;

Select one:
True
False



Question 22

Overloaded methods must be differentiated by:

Select one:
a. method name
b. data type of arguments
c. method signature
d. None of the above



Question 23

True or False: An Interface contains method declarations as well as implementations.

Select one:
True
False



Question 24

True or False: A class that implements an interface may only implement a few of that interface's method declarations.

Select one:
True
False



Question 25

For the following list:

int[] list = { 4, 8, 10, 6, 2, 8, 5 };

What would the list look like after one pass of the outer loop of the bubble sort algorithm?

Select one:
a. { 2, 4, 5, 6, 8, 8, 10 }
b. { 4, 8, 8, 6, 2, 10, 5 }
c. { 4, 8, 6, 2, 8, 5, 10 }
d. None of the above



Question 26

True or False: Every 'try' block must end with a 'finally' block.

Select one:
True
False



Question 27

If A and B are logical expressions: !(A && B) is equivalent to:

Select one:
a. (A || B)
b. (!A || !B)
c. (!A && !B)
d. None of the above




No comments:

Post a Comment