Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CMSC 132 Final Exam Questions with 100% Correct Answers | Verified | Latest Update 2024, Exams of Advanced Education

CMSC 132 Final Exam Questions with 100% Correct Answers | Verified | Latest Update 2024 What are the two types of abstraction? Procedural and Data Abstraction What is procedural abstraction? Procedural abstraction is a type of abstraction in which the user is aware of what actions are being performed, but they are not told how the action is performed. What is data abstraction? Data abstraction is the concept in object-oriented programming that deals with hiding the complex implementation details of data structures and exposing only the necessary and relevant aspects to the user. What is the difference between data abstraction and procedural abstraction? Data abstraction deals with the nature and manipulation of data, hiding its complexities, while procedural abstraction focuses on the process or method of achieving a task, hiding the steps or algorithms involved. Both are used to simplify the design of software and enhance readability, maintainability, and reusability.

Typology: Exams

2023/2024

Available from 07/11/2024

professoraxel
professoraxel 🇺🇸

3.7

(26)

9.1K documents

1 / 27

Toggle sidebar

Related documents


Partial preview of the text

Download CMSC 132 Final Exam Questions with 100% Correct Answers | Verified | Latest Update 2024 and more Exams Advanced Education in PDF only on Docsity! CMSC 132 Final Exam Questions with 100% Correct Answers | Verified | Latest Update 2024 What are the two types of abstraction? Procedural and Data Abstraction What is procedural abstraction? Procedural abstraction is a type of abstraction in which the user is aware of what actions are being performed, but they are not told how the action is performed. What is data abstraction? Data abstraction is the concept in object-oriented programming that deals with hiding the complex implementation details of data structures and exposing only the necessary and relevant aspects to the user. What is the difference between data abstraction and procedural abstraction? Data abstraction deals with the nature and manipulation of data, hiding its complexities, while procedural abstraction focuses on the process or method of achieving a task, hiding the steps or algorithms involved. Both are used to simplify the design of software and enhance readability, maintainability, and reusability. What is an abstract data type? Abstract data type (ADT) is an entity that has values and operations. More formally, an abstract data type is an implementation of interfaces (a set of methods). Note that it is “abstract” because it does not provide any details surrounding how these various operations are implemented. An abstract data type is a blueprint for the data structure. It defines the data and the operations that can be performed on the data, but it does not specify how these operations will be implemented. An example of an abstract data type is a queue, which supports the operation of inserting items to the end of the queue as well as the operation of retrieving items from the front of the queue. Note, again, that we are not concerned with how these operations should be performed internally. What is the difference between procedural abstraction and encapsulation Procedural abstraction is when the user knows how to use a method, but not the exact method implementation, while encapsulation is hiding variables using private Encapsulation: Think of the BankAccount as a secure vault. You have some controls (methods) to interact with what's inside (the data), but you can't directly access or see the contents (internal state). Procedural Abstraction: Consider the process of depositing money as a black box. You put the .shuffle() and .sort() What is this variable initialization called without a generic? List myL = new ArrayList(); Raw type. You can cast the elements in a raw type to another object without getting a runtime error. Java will permit us to add any type to the list. So B b = (B) myL.get(0); Will compile without an error - unlike if we did pass in a type other than B. T/F Iterable is a class. False. It is an interface How do you iterate over a collection in Java? If a collection implements Iterable, we can use a combination of .iterator and .hasNext() to iterate over a collection. ArrayList<Integer> arr = new ArrayList<Integer>(); Iterable<Integer> arrIterator = arr.iterator(); while(arrIterator.hasNext()) { System.out.println(arrIterator.next()); } What is an automatic alternative to using an Iterator? Using a forEach loop (enhanced for loop) Using forEach(T object : objects) uses the iterator by default. ArrayList<Integer> arr = new ArrayList<Integer>(); for(int arrValue : arr) { System.out.println(arrValue)); } What method does the Comparable interface implement? .compareTo() How do you use Comparable in a class? public class Student implements Comparable<Student> { What are the three steps to an equals method? What are the three things you have to implement? The equals method signature should take in an Object obj 1. You must first check if obj == this. This checks if the object being compared is the current object. 2. You must second check if obj is not even an instance of the current class. This would mean that it is definitely not equal. 3. You should then cast obj into the current class type, and then compare the equals of the subvariable, with that new casted type. T/F Super() must be the first statement in the subclass constructor. True. It must be the first statement in the subclass constructor. If not explicitly called, the compiler automatically inserts a call to the no- argument constructor of the superclass. What happens if there is no default constructor in a super class? If there is no default constructor in your superclass, Java can not automatically insert super(), because of that, you need to manually type out the super() constructor initialization. This causes a compile time error. T/F Subclasses inherit everything from their superclasses False. They inherit everything that isn't private. When you extend a superclass, you can not access any of the private fields. Since you can't access private fields, what is an alternative keyword instead of private that would allow a subclass to access a field in the superclass. Protected Protected allows us to limit the visibility of a variable, while still permitting subclasses to access that variable. What is Early Binding? The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. False. You can only extend one class in Java Name some exceptions in Java UnsupportedOperationException("error"); This is if you are yet to implement a method. ClassCastException ArrayStoreException when adding wrong type to an array What extra identifier do you specify on top of a try catch black to run code whether or not there is an exception? Finally block. The finally block runs whether or not you ran into an exception, if if you run into an exception in the catch block, the finally block will run after it. What are the two types of errors, and what are their differences? Checked Exceptions and Unchecked Exceptions. A checked exception may occur even if your code works perfectly fine, for example opening a file that does not exist. An unchecked exception is usually more serious, if your code is unchecked. These may be like NullPointerException or something. What class should you extend to make an exception? Exception class (checked exception). This must be used in a throw catch block. T/F an outer class can not access the private variables of an inner calss False. An outer class distinguishes itself from extending classes by allowing the ability to access private members. Can an inner class extend its outer class? True. T/F Anonymous classes can't be referenced later on True. They are not given names, so you can not extend them later on. What is the difference between a static nested class and inner classes? Static nested classes can be initialized without a reference to the outer class, which is not the case with inner classes. T/F You need to initialize the outer class to use a static nested class. False. This is one of the important distinctions of static nested classes and inner classes, they do not need an instance of their outer class to be used. What are the three ways to copy an object in Java? Reference, Shallow, and Deep Copy. Reference copy is when you set two objects equal to each other, like x = y, this only copies their reference in memory and any change to x is reflected in y. Reference copy copies reference. Shallow copy copies the structure of an object, but not the actual data. You can think of it as making a new copy of primitives, but maintaining references to mutable fields. This is what .clone does. Deep copying makes a unique, legit new copy that is unreferenced to the original object, and is not effected at all by when the first object is changed. What class implements the 'clone()' method? Cloneable. To override the Object class' default clone method, you extend Cloneable and override the clone method with your own implementation. What is the process of a clone method? Explain what happens inside of a clone method and have a good understanding of what it does. Inside of a try catch block, define a new object with the same type as the current object, which is also what you want to return. You can either set it to null and build on it, or directly cast super.clone() into the current object type. You have created a shallow copy, now, you can add specific implementation for specific variables if you want them to be deep copied, or have some other cloning behavior for those values. Now you can return that object. In your catch block, check if there is a CloneNoteSupportedException, this will only be thrown if the Cloneable class is not extended, you can throw an assertion here. Code a clone method for an object now. Flip to see the answer Tada! Trick question, but false. When you create an ArrayList and initialize it with an ArrayList of a specific type, say an ArrayList of Dogs into Animals it causes a compile error, meaning your editor will have a red line under it telling you it's wrong. When you create an Array of Dogs and pass it into an Array of Animals, then add a Cat, you actually get a runtime ArrayStoreException, not a compile error. To summarize when Collection errors occur with different types, you get a compilation error when initializing the ArrayList, which works fine with an Array, but you get a runtime exception when you try to add a different value to it. Here List<Animal> animals = new ArrayList<Dog>(); // Compilation error Animal[] animals = new Dog[1]; animals[1] = new Cat(); // Throws ArrayStoreException at runtime What is a collection in programming in simple terms? What is a collection? A collection is a group of individual objects represented as a single unit. What is a data structure? A data structure is a class that is used to represent and store information in a specific manner. Keyword is specific. What class do Set, Queue, Tree, etc all extend? Collection Everything in the collections framework implements the Collection interface. This means that a declaration like Collection<String> l = new ArrayList<String>() is perfectly valid. Does a queue have an iterator? What about lists, queues, or sets? If something has an iterator, it extends Iterable. List, Queue, Set, etc all extend Collection, which extends Iterable. (that was a hard question) Draw a LinkedList What are the two entities stored in a LinkedList node? The data stored in that node, and the reference to the data in the next node. T data Node next In memory, where are are nodes stored? The heap Write a toString() method for a LinkedList, flip the card to see the code. Write an delete method for a LinkedList, flip the card to see the code. Are circular arrays more commonly used in queues or stacks? Queues. This is due to their First In First Out nature. What is the name for the condition for when recursion stops? Base case or Stopping case Where are recursive calls stored in a memory diagram? The call stack Classes can only extend more than one other class if at least one of those classesis an abstract class. False, under no circumstances ever can you extend more than one class. Downcasting can be done implicitly. False, you always have to purposely do it, or your code will not compile Unchecked exceptions happen when your code in unchecked, and you should fix an underlying problem in your code. Checked exceptions happen in correct, checked code, but require try-catch for instances where a user might have a missing file or class, etc. Checked Exceptions are usually checked at compile time and represent conditions a user might want to catch. Checked exceptions are typically used for recoverable conditions and where a client can take some useful action concerning the exception. You would want to wrap these in a try catch block. IOException, SQLException, ClassNotFoundException Encourages developers to proactively handle exceptions which can lead to more robust and error-resistant code. Unchecked Exceptions are not checked at compile time, but instead at run time. You fix these by writing proper code NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticE xception, OutOfMemoryError. They can be caught, but it's often a better practice to fix the underlying problem that caused the exception rather than to handle it. What does putting final before a method do? Avoids the method from being overridden, not overloaded. Will this code compile final int x = 8; int y = 21; x = (y + 7) / 4; False. It will not compile Explain what putting the final keyword does for classes, methods, and variables. When you add final to a class, you avoid classes from extending it When you add final to a method, you avoid it from being overriden, When you add final to a variable, you avoid it from being changed. What does calling ".values()" on an enum type do? It returns an Array of the enum possible values What is the difference between Comparable and Comparator Comparable is an interface for a collection to implement that contains a single method, compareTo(). CompareTo is used withen an instance of a class, and therefore only takes one parameter - the other object. int compareTo(T o) Comparator is an interface that carries out the comparison using compareTo() when given two objects Why do the wrapper classes for primitives exist? In order to make collections of these values. Collections can only collect objects, not primitives. How do you initialize an Integer wrapper class using the built in Integer methods? Integer x = new Integer.valueOf(7) How do you initialize a new ArrayList in Java? ArrayList<Cat> catList = new ArrayList<>(); Practice initializing an enum with a constructor Tada! Do enums have static members? What about instance members? Yes, enums can have both How would you call the iterator for this Arraylist list = new ArrayList<>(); Iterator listIterator = new list.iterator(); Explain how you would use the Iterator and Iterable interfaces in Java when writing a collection. You would first implement Iterable in the collection itself. This allows you to override the .iterator() method, which returns Iterator. You then define an instance sub class that implements Iterator. This allows you to define the currentIndex, and add the hasNext() and next() methods. How do you get an array length in Java? Int length = arr.length; T/F the next method in an iterator in Java returns the currentIndex first, then the next element. True T/F you can call remove() many times after calling next() FALSE. If you use next(), you can only call remove() one time, and you can only use remove() once you have called the next() method. T/F A regular non abstract class can have abstract methods False. The entire class should be abstract. If the sorted result has the duplicate numbers in the same order, for example, 5 was a couple spaces behind 5', and now in the sorted array it is is 5, 5', we say it is a stable sorting algorithm. But for example, if it appeared as 5', 5, then it is is an unstable sorting algorithm. A common way to make standard sorts stable is by only checking if the values being compared are greater than, not greater than or equal to. This means two equal elements wont be tampered with. T/F An abstract class must include at least one abstract method. False. A class can be labled as abstract without have any abstract methods An enum can have a public constuctor False What is the thread that renders UI called in Java? Event Dispatch Thread T/F Lambda expressions do not have variable types for their methods True. You do not define the variable when passing in variables into a lambda expression. How do you get the length of an array in Java? .length, not .length() What is the best sorting strategy for small list Insertion sort What are the two sorting algorithms for large sets Quick sort (in place, but not stable) and Merge sort (stable, but not inplace) What are the complexities for each sorting algorithm For the larger sorts, O(n * log n) O(n * log n) for merge sorts O(n * log n) for quick sorts on average, but O(n^2) worst case if the pivot is always the minimum or maximum For the smaller sort (insertion sort), O(n^2) O(n^2) for insertion sort For the non comparison sorts, O(n) Radix sort is O(n) Comparison sort is O(n) Explain the process for merge sort, insertion sort, and quick sort. Insertion sorts is used on smaller lists. You start with the first two elements, if they are out of order, then rearrange them. Then choose the next two, while overlapping with the previous value and the next one. If they are in correct order already, move on. If they are not, then swap them, and keep swapping until it is in the correct spot. It is inplace and stable with a runtime of O(n^2) Merge sorts are a sorting algorithm used on larger lists. You start by halving the array until you get the individual elements left. Then, you merge sort each value with its neighboring value, and keep doing that with each set until it grows bigger and bigger, until it becomes the original size and correct order. It is not inplace, but is stable with a complexity of O(n * log n). Quick sort is another sorting algorithm used on larger lists. You start by choosing a pivot (left for this class), and then swap it. It is inplace, but not stable with a complexity of O(n * log n) with O(n^2) being the worst complexity. What is the process for implementing merge sort? Merge sort is usually run recursively alongside java's merge function. The merge method takes in two arrays, and then returns them in sorted order by comparing first element in the first array with the other array elements. You split the array into two halves. Run mergesort on each of the two halves to return two ordered halves. And then merge