CS 165 Last Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What does the super keyword do?

The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: ✦ To call a superclass constructor ✦ To call a superclass method

When and how do you use the instance of operator?

Use the instanceof operator to test whether an object is an instance of a class:

What is method overloading?

When a subclass inherits a method from the super class but the parameter is different.

How do you do open addressing?

When colliding with a location in the hash table that is already occupied - Probe for some other empty, open, location in which to place the item. The sequence of locations that you examine Linear probing uses a constant step, and thus probes loc, (loc+step)%size, (loc+2*step)%size, etc. In the sequel we use step=1 for linear probing examples

What does offer(e) and add(e) do in a priority queue?

offer(E e) and add(E e) - inserts the element into the priority queue based on the priority order

How do you declare an interface?

public interface InterfaceName { constant declarations; abstract method signatures; } Example: public interface Edible { /** Describe how to eat */ public abstract String howToEat(); }

How do you make an add method in an ArrayList implementation?

public void add(int index, E e) { // Ensure the index is in the right range if (index < 0 || index > size) throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); ensureCapacity(); // Move the elements to the right after the specified index for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; // Insert new element to data[index] data[index] = e; // Increase size by 1 size++; }

What does poll(), and remove() do in a priority queue?

remove() and poll() - removes the head of the queue (which is the highest priority) and returns it

How would you build a heap out of the array [3,1,6,5,2,4]

3 is the root node and then go top to bottom left to right. 3 has 1 and 6 as children 5 and 2 is 1s children and 4 is 6s child.

What gets printed? class Temp { Temp() { this(5); System.out.println("The Default constructor"); } Temp(int x) { this(5, 15); System.out.println(x); } Temp(int x, int y) { System.out.println(x * y); } public static void main(String args[]) { new Temp(); } }

75, 5, The Default constructor

What is a class?

A blueprint of your code Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.

What is a collection?

A collection is a container object that holds a group of objects, often referred to as elements. The Java Collections Framework supports three types of collections, named lists, sets, and maps.

What is a complete binary tree?

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

What is a constructor with no parameters called?

A constructor with no parameters is referred to as a no-arg constructor.

What does the list interface do in Java?

A list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index.

What makes a heap a max heap?

A maximum heap (maxheap) is a complete binary tree that satisfies the following: It is a leaf, or it has the heap property: Its root contains a key greater or equal to the keys of its children Its left and right sub-trees are also maxheaps

What makes a heap a min heap?

A minheap has the root less or equal children, and left and right sub trees are also minheaps

What is a queue in java?

A queue is a first-in/first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned priorities. When accessing elements, the element with the highest priority is removed first.

What is an objects reference?

A reference is an address that indicates where an object's variables and methods are stored. You aren't actually using objects when you assign an object to a variable or pass an object to a method as an argument. You aren't even using copies of the objects. Instead, you're using references to those objects.

Difference between and Static non static (instance) methods?

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static.

What is a hash function?

A typical hash function first converts a search key to an integer value called a hash code, and then compresses the hash code into an index to the hash table.

When does dynamic binding occur?

At runtime

• Which of the following does not apply to an class that implements Comparable: A. Must implement compareTo B. Must implement equals C. Allows for a natural ordering of objects D. Can be stored in a variable of type Comparable E. Can be stored in an array of type Comparable

B. Must implement equals

• Which of the following does not apply to an concrete class: A. Has a constructor B. Has only concrete methods C. Has some abstract methods D. Constructor used for instantiation E. May have instance variables

C. Has some abstract methods

How are instance variables and methods specified?

From omitting the static keyword and being outside of main.

Can you have an abstract class without abstract methods?

YES A class that contains abstract methods must be abstract. However, it is possible to define an abstract class that contains no abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass.

Can an array of interface type store any class that is implementing the interface?

YES.

Can a static method be inherited, can a static method be overridden?

Yes they can be inherited, a static method cannot be overridden.

Do two examples of double hashing

Yi.

Do one example of separate chaining hashing.

Yo.

Why do we hash?

You can use hashing to implement a map or a set to search, insert, and delete an element in O(1) time which is QUICKER than a search trees O(logn) time.

Do two examples of quadratic probing.

Yuh.

What is a no-arg constructor with an empty body called?

a no-arg constructor with an empty body is implicitly defined in the class. This constructor, called a DEFAULT CONSTRUCTOR, is provided automatically only if no constructors are explicitly defined in the class.

What is a list in java?

List is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. Creating List Objects: List is an interface, we can create instance of List in following ways: List a = new ArrayList(); List b = new LinkedList(); List c = new Vector(); List d = new Stack();

Can a nonabstract subclass thats extending a abstract superclass not implement one of the defined methods in the superclass?

NO in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass.

Can you create objects from an abstract class?

NO An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses, and which initialize the instance variables of the super class. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class.

Are superclass's Constructor Inherited?

No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. They can only be invoked from the subclasses' constructors, using the keyword super. If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked.

How does the Array.sort method sort numbers and strings?

Numbers: Least to greatest Strings: In alphabetical order

Difference between pass by value for reference and primitive types.

Passing by value for primitive type value (the value is passed to the parameter) Passing by value for reference type value (the value is the reference to the object)

What is polymorphism?

Polymorphism means that a variable of a supertype can refer to a subtype object. For instance, let's consider a class Animal and let Cat be a subclass of Animal. So, any cat IS animal. Here, Cat satisfies the IS-A relationship for its own type as well as its super class Animal.

How would you write a search method for a BST Tree

Start from the root (current), while current does not equal null, compare e to a current to currents element. If its less set it to left, If its greater set it to right. Else return true because element is found. Return false outside everything.

What is a hash table?

The array that stores the values is called a hash table. The function that maps a key to an index in the hash table is called a hash function.

What is the difference between implements and extends?

The distinction is that implements means that you're using the elements of a Java Interface in your class, and extends means that you are creating a subclass of the class you are extending. You can only extend one class in your new class, but you can implement as many interfaces as you would like.

What does the equals method do?

The equals() method compares the contents of two objects. The default implementation of the equals method in the Object class is as follows: public boolean equals(Object obj) { return this == obj; } For example, the equals method is overridden in the Circle class. public boolean equals(Object o) { if (o instanceof Circle) { return radius == ((Circle)o).radius; } else return false; }

Select the correct definition of the usage of a Java abstract class. A. An abstract class provides shared code and data for a set of classes that share attributes and behaviors. B. An abstract class is similar to an interface in that it specifies functionality, but has no actual code or data. C. An abstract class differs from an interface in that it must implement every method that it contains. D. An abstract class can be instantiated, but code for its abstract methods might be missing.

A. An abstract class provides shared code and data for a

How do you add to a priority queue?

Add the value to the spot that keeps it a complete tree and then swap from there. Do two examples of these online.

What is the technical H of T definition of a complete binary tree?

All nodes at level h - 2 and above have h-1: two children each, and When a node at level h - 1 has children, h: all nodes to its left at the same level have two children each, and When a node at level h - 1 has one child, it is a left child So the leaves at level h go from left to right

Can a private method be overriden?

An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

What is an interface?

An interface is a classlike construct that contains only constants and abstract methods. In many ways, an interface is similar to an abstract class, but the intent of an interface is to specify common behavior for objects. For example, you can specify that the objects are comparable, edible, cloneable using appropriate interfaces.

What is an object?

An object is a member or an "instance" of a class. An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings.

How do you declare an object?

ClassName objectRefVar; Example: Circle myCircle;

Draw out the java collection hierarchy.

Collection--->(List, Set, Map) Set--->(HashSet, LinkedHashSet) List--->(LinkedList, ArrayList, Vector) Map--->(HashMap, LinkedHashMap, Hashtable)

What is a collision in hashing?

Collision: two keys map to the same index

comparable vs. comparator in java?

Comparable - Implemented with compareTo - Defines the natural order for the object Comparator - Implemented with compare() - Defines a different order for some purpos

What is a constructor?

Constructors are a special kind of methods that are invoked to construct objects. Circle() { } Circle(double newRadius) { radius = newRadius; }

How do you do a heap sort?

Create a max heap, remove largest item, place item in sorted partition. Do two examples of this

Which of the following does apply to an interface (pre-1.8): A. Has a constructor B. Has some concrete methods C. Has some abstract methods and some concrete methods D. Has only abstract methods E. A class may only implement 1 interface.

D. Has only abstract methods

What must you do to demote an object from a superclass to an object from a sub class?

Explicit casting must be used when casting an object from a superclass to a subclass. This type of casting may not always succeed. Apple x = (Apple)fruit; Orange x = (Orange)fruit;

How do you delete from a priority queue?

Delete the node at the top, switch it with the last leaf and then swap from there. Do two examples of these online?

Which is the incorrect statement about implicit or explicit constructor chaining? Please select the single incorrect answer. A. Implicit chaining is when the compiler inserts default constructors. B. Explicit chaining requires the programmer to insert constructors. C. When chaining, the super class constructor must be called first. D. The programmer can explicitly chain default constructors. E. Implicit chaining applies when there is no default constructor.

E. Implicit chaining applies when there is no default constructor.

Which of the following does not apply to an abstract class: A. Has a constructor B. May have some concrete methods C. May have some abstract methods D. Constructor not used for instantiation of abstract class E. Required to contain abstract methods

E. Required to contain abstract methods

public class Dog extends Animal { public Dog () { System.out.println("Dog") ; } public class Animal { public Animal(String name) { System.out.println("Animal") ; }} A.Dog B.Dog Animal C.Animal Dog D.Animal E.None of the above

E.None of the above

What is efficient about an Array, ArrayList, and LinkedList?

Efficiency • ArrayList - Random access through an index • LinkedList - Insertion or deletion of elements at any location • Array - If your application does not require insertion or deletion of elements, the most efficient data structure is the array.

How do you make an insert method for a BST Tree.

If the root is null create a new node, else create a new tree node named parent and set it to null and create a new tree node named current and set it to root. Compare e to current and if its less than 0 set parent to current and then set Current to current.left, if its greater set parent to current and current to current.right. Else return false. Compare e to the parent element and then add then create a new node for parents left and right. Add 1 to size and return return true.

Do two examples of linear probing.

If you did it write your good.

What is hashing?

If you know the index of an element in the array, you can retrieve the element using the index in O(1) time. So, can we store the values in an array and use the key as the index to find the value? The answer is yes if you can map a key to an index. The array that stores the values is called a hash table. The function that maps a key to an index in the hash table is called a hash function. Hashing is a technique that retrieves the value using the index obtained from key without performing a search.

A priority queue has reference based implementation. What does that mean?

It has a head and links that references on another just like a linked list (visualize a linked list with data and reference points). Reference-based implementation Sorted in descending order Highest priority value is at the beginning of the linked list remove() returns the item that pqHead references and changes pqHead to reference the next item. offer(E e) must traverse the list to find the correct position for insertion.

What is method overriding?

Its when the subclass inherits a method from its super class (same signature and everything) but the body of the methods modified to do something else.

What does the comparable interface do in Java?

Java Comparable interface is used to order the objects of user-defined class.This interface is found in java.lang package and contains only one method named compareTo(Object). It provide single sorting sequence only i.e. you can sort the elements on based on single data member only. For example it may be rollno, name, age or anything else.

How do you do a pre order traversal for a BST Tree?

if root is null return, print the roots element then recursively call roots left and right child.

How do you do an in-order traversal for a BST tree?

if root is null return, recursively call inorder for roots left child, roots element, and roots right child in that order.

How do you do a post order traversal for a BST tree?

if root is null return, recursively call roots left and right child and then print roots element.


Set pelajaran terkait

MOR 345 Restorative Art Lesson 1.4

View Set

Chapter 24: Fluid, Electrolyte, and Acid-Base Balance

View Set

Chapter 11: Nursing Management: Patients With Chronic Obstructive Pulmonary Disease and Asthma

View Set

Pediatrics Test 2 Study Questions

View Set

prep u fundamentals exam 2 - CH 27, ch 24, 31, 33

View Set

Study Cards: Metric Measurement, Metric Conversion

View Set

3.07 Unit Test: American Free Enterprise and U.S. Government Involvement. Econ

View Set

Social Studies True or False Questions

View Set