CSC EXAM 2 MATERIAL
Which of the following statements about class properties is correct? A. A setter method is used to assign the value of a class property. B. A setter method is used to retrieve the value of a class property. C. A getter method is used to assign the value of a class property. D. You cannot assign values to class properties.
A. A setter method is used to assign the value of a class property.
Which of the following statements using data values in a class is NOT correct? A. You can use the argument of any instance method in any other method. B. You can use a global constant's value in an instance method. C. You can use an instance variable's data value in an instance method. D. You can use the argument of an instance method in that method.
A. You can use the argument of any instance method in any other method.
Given the ArrayStack class implementation discussed in section 16.3 (partially shown below), select the statements needed to complete the push method. public class ArrayStack{ private Object[] elements; private int currentSize; public ArrayStack(){ final int INITIAL_SIZE = 10; elements = new Object[INITIAL_SIZE]; currentSize = 0; } public void push(Object element){ growIfNecessary(); ________________ ________________ } } Select one: A. elements[currentSize] = element; currentSize++; B. currentSize++; elements[currentSize] = element; C. elements[currentSize - 1] = element; currentSize++; D. elements[currentSize + 1] = element; currentSize++;
A. elements[currentSize] = element; currentSize++;
Insert the missing code in the following code fragment. This fragment is intended to remove a node from the head of a linked list: public class LinkedList{ . . . public Object removeFirst(){ if (first == null) { throw new NoSuchElementException(); } Object element = first.data; ________________ ________________ } . . . } Select one: A. first = first.next; return element; B. first.next = first; return element; C. first = element.next; return element; D. first = element.next; return null;
A. first = first.next; return element;
You are creating a class named Vessel. Which statement correctly declares a constructor for this class? A. public Vessel() B. public void Vessel() C. new Vessel() D. void Vessel()
A. public Vessel()
Given the partial LinkedList class declaration below, select a statement to complete the size method, which is designed to return the number of list elements. public class LinkedList{ class Node{ public Object data; public Node next; } private Node first; . . . public int size(){ int count = 0; Node temp = first; while (temp != null){ count++; _____________________ } return count; } } Select one: A. temp = temp.next; B. temp = first.next; C. first = temp.next; D. first = first.next;
A. temp = temp.next;
If the postorder traversal of an expression tree is 8, 2, +, 5, /, what is the numeric result of that Reverse Polish Notation expression? Select one: A. 8 B. 2 C. 7 D. 15
B. 2
A balanced binary tree with 260 nodes has a height of approximately ____. Select one: A. 13 B. 8 C. 12 D. 10
B. 8
Which of the following statements about breadth-first and depth-first traversal is NOT correct? Select one: A. Depth-first search goes deeply into the tree and then backtracks when it reaches the leaves. B. Breadth-first and depth-first search only work on a binary tree. C. Breadth-first search first visits all nodes on the same level before visiting the children. D. Depth-first search uses a stack to track the nodes that it visits.
B. Breadth-first and depth-first search only work on a binary tree.
A stack can be implemented as a sequence of nodes in a linked list or an array list. Which of the following statements about this is correct? Select one: A. If implementing the stack as a linked list, the least expensive approach is to add and remove elements at the end. B. If implementing the stack as an array list, the least expensive approach is to add and remove elements at the end. C. If implementing the stack as an array list, there is no cost difference whether adding and removing elements at the beginning or the end. D. If implementing the stack as a linked list, there is no cost difference whether adding and removing elements at the beginning or the end.
B. If implementing the stack as an array list, the least expensive approach is to add and remove elements at the end.
Consider the following binary search tree: ----- J ----- | | -- E -- M -- | | | -- C G -- -- P | | | A H N Which of the following sequences correctly represents a preorder traversal of this tree? Select one: A. J, E, M, C, G, P, A, H, N B. J, E, C, A, G, H, M, P, N C. A, C, H, G, E, N, P, M, J D. A, C, E, G, H, J, M, N, P
B. J, E, C, A, G, H, M, P, N
Given the partial LinkedList class declaration below, select an expression to complete the empty method, which is designed to return true if the list contains no elements. public class LinkedList{ class Node{ public Object data; public Node next; } private Node first; . . . public boolean empty(){ return ________________________ ; } } Select one: A. first != null B. first == null C. first.data == null D. first.next == null
B. first == null
Insert the missing code in the following code fragment. This fragment is intended to add a new node to the head of a linked list: public class LinkedList{ . . . public void addFirst(Object element){ Node newNode = new Node(); newNode.data = element; _________ _________ } . . . } Select one: A. first = newNode; newNode.next = first; B. newNode.next = first; first = newNode; C. first = newNode.next; newNode.next = first; D. first = newNode.next; newNode = first;
B. newNode.next = first; first = newNode;
Which of the following statements about trees is NOT correct? Select one: A. The root of the tree does not have a parent. B. A tree is composed of nodes. C. A node can have multiple parents. D. A node can have multiple child nodes.
C. A node can have multiple parents.
Given the following class definition, which of the following are considered part of the class's public interface? public class CashRegister{ public static final double DIME_VALUE == 0.1; private static int objectCounter; public void updateDimes (int dimes) { . . . } private boolean updateCounter (int counter) { . . . } } A. DIME_VALUE and objectCounter B. objectCounter and updateCounter C. DIME_VALUE and updateDimes D. updateDimes and updateCounter
C. DIME_VALUE and updateDimes
In a linked list data structure, when does the reference to the first node need to be updated? I inserting into an empty list II deleting from a list with one node III deleting an inner node Select one: A. I B. II C. I & II D. III
C. I & II
Which operations from the array list data structure could be used in the implementation of the push and pop operations of a stack data structure? I addLast II addFirst III removeFirst Select one: A. I and III B. I and II C. II & III D. None of the above
C. II & III
Which of the following statements about the three tree traversal schemes studied is correct? Select one: A. Inorder traversal is used for copying file directories. B. Postorder traversal is used for copying file directories. C. Postorder traversal is used for removing file directories by removing subdirectories first. D. Inorder traversal is used for evaluating arithmetic expression trees on a stack-based calculator.
C. Postorder traversal is used for removing file directories by removing subdirectories first.
You have created a Rocket class which has a constructor with no parameters. Which of the following statements will construct an object of this class? A. myRocket.new(Rocket); B. Rocket.new(myRocket); C. Rocket myRocket = new Rocket(); D. Rocket myRocket;
C. Rocket myRocket = new Rocket();
Consider the following code snippet: public class Employee { private String empName; public String getEmpName() { . . . } } Which of the following statements is correct? A. The getEmpName method can be accessed only by methods of another class. B. The getEmpName method cannot be accessed at all. C. The getEmpName method can be accessed by any user of an Employee object. D. The getEmpName method can be accessed only by methods of the Employee class.
C. The getEmpName method can be accessed by any user of an Employee object.
Consider the following tree diagram: ---- * ---- | | ---- + ---- -- - -- | | | | | - * - 5 4 6 1 | | 2 3 Which arithmetic expression is represented by this tree? Select one: A. 2 * 3 + 5 + 4 * 6 - 1 B. 2 * (3 + 5 + 4) * (6 - 1) C. [(2 * 3) + 5 + 4] * (6 - 1) D. 2 * 3 + 5 + 4 - 6 - 1
C. [(2 * 3) + 5 + 4] * (6 - 1)
Which statement illustrates the invocation of a static method? A. deposit(100).mySavings; B. double s = 100.sqrt(); C. double s = Math.sqrt(100); D. mySavings.deposit(100);
C. double s = Math.sqrt(100);
You should declare all instance variables as ___. A. class B. protected C. private D. public
C. private
Consider the following tree diagram: ---- C ---- | | --- R --- --- P --- | | | | | | -- H - N U D- B L -- | | | | M X K T What is the height of this tree? Select one: A. 7 B. 3 C. 6 D. 4
D. 4
Consider the following binary search tree diagram: ---- H ---- | | -- D -- S -- | | | A F W Consider the following addNode method for inserting a newNode into a binary search tree: public void addNode(Node newNode){ int comp = newnode.data.compareTo(data); if (comp < 0){ if (left == null) {left = newNode;} else { left.addNode(newNode); } } else{ if (right == null) {right = newNode;} else { right.addNode(newNode); } } } Which nodes will be visited in order to insert the letter E into this tree, calling addNode on the root of the tree? Select one: A. H only B. H and D only C. H, D, and A D. H, D, and F
D. H, D, and F
What is included in a linked list node? I a reference to the next node II an array reference III a data element Select one: A. I Incorrect B. II C. II and III D. I and III
D. I and III
You wish to traverse a binary search tree in sorted order. Arrange the following actions in the correct order to accomplish this. I Print the right subtree recursively II Print the root III Print the left subtree recursively Select one: A. I, II, III B. II, III, I C. III, I, II D. III, II, I
D. III, II, I
Which of the following statements about classes is correct? A. When programmers work with an object of a class, they must understand only how the object stores its data. B. When programmers work with an object of a class, they must understand only how the object's methods are implemented. C. When programmers work with an object of a class, they must understand how the object stores its data and how its methods are implemented. D. When programmers work with an object of a class, they do not need to know how the object stores its data or how its methods are implemented
D. When programmers work with an object of a class, they do not need to know how the object stores its data or how its methods are implemented
Given the partial ArrayList class declaration below, select an expression to complete the empty method, which is designed to return true if the list contains no elements. public class ArrayList{ private Object[] elements; private int currentSize; public ArrayList(){ final int INITIAL_SIZE = 10; elements = new Object[INITIAL_SIZE]; currentSize = 0; } public boolean empty(){ return ________________________ ; } } Select one: A. elements.length == 0 B. elements.currentSize == 0 C. elements[0] == null D. currentSize == 0
D. currentSize == 0
You have created a Coin class and a Purse class. Which of the following constructors will correctly initialize an array list named coins in the Purse class? A. public Purse{ coins = new ArrayList<Coin>(); } B. public Purse(){ coins = new ArrayList<Coin>; } C. public void Purse(){ coins = new ArrayList<Coin>(); } D. public Purse () { coins = new ArrayList<Coin> (); }
D. public Purse () { coins = new ArrayList<Coin> (); }