Week 3 - Intermediate Java - Handling Problems, Exceptions and Errors, Collections Flashcards
Describe the difference between exceptions and errors in Java.
In the java compilation process, the compiler checks that any checked exceptions that could be thrown are handled. If this is not the case, the compiler cannot compile the code. This is an example of a compilation error - not an exception.
Exceptions are never thrown during the compilation process - they can only be thrown when the code is executing (running). Compilation errors generally occur due to improper syntax, like calling a method that doesn’t exist, forgetting a semicolon on a line, using the wrong data type, using a reserved keyword incorrectly, and as we’ve shown, not handling checked exceptions properly.
Exceptions are never …..
thrown during the compilation process - they can only be thrown when the code is executing (running).
Compilation errors generally occur due to….(4)
improper syntax,
like calling a method that doesn’t exist,
forgetting a semicolon on a line,
using the wrong data type, using a reserved keyword incorrectly,
and as we’ve shown, not handling checked exceptions properly.
The exception class hierarchy starts with the….
Throwable class which inherits from Object.
The Exception and Error classes both extend
Throwable class
An Error represents something that…
went so horribly wrong with your application that you should not attempt to recover from
When the source code fails before it finishes compiling, this is known as a…?
Compilation error
What class is a general class which provides an abstraction for all exceptions?
Exception
Exceptions that require mandatory handling are called
checked exceptions, The compiler will check that such exceptions are handled by the program.
The _____ class represents all possible objects that can be thrown by a throw statement and caught by a catch clause in a try..catch statement.
Throwable
Subclasses of the class Exception which are not subclasses of ______ are checked exceptions.
RuntimeException
If an error is predictable but unpreventable and reasonable to recover from, a checked exception should be used
True
Since Java has exception-handling capability, programmers do not need to write code to handle exceptions.
False
Exception handling can be done in one of two ways:
The first way is to place the statement in a try statement that has a catch clause that handles the exception.
The second way is to declare that the subroutine can throw the exception. This is done by adding a “throws” clause to the subroutine heading, which alerts any callers to the possibility that the exception might be generated when the subroutine is executed. The caller will, in turn, be forced either to handle the exception in a try statement or to declare the exception in a throws clause in its own header.
Exception-handling is mandatory for…
any exception class that is not a subclass of either Error or RuntimeException.
When risky code is written that has the possibility of throwing an exception, it can be dealt with in one of two ways:
Handling means that the risky code is placed inside a try/catch block
Declaring means that the type of exception to be thrown is listed in the method signature with the throws keyword. This is also called “ducking” the exception - you let the code which calls the method deal with it.
If the exception is not handled anywhere in the program, it will propagate up through the
call stack until it is handled by the JVM which then terminates the program.
When an exceptional condition occurs in the course of a Java program, a special class called an Exception can be thrown,…
which indicates that something went wrong during the execution of the program.
If an exception is NOT handled anywhere in the program, what will occur?
The exception propagates up through the call stack until it is handled by the JVM which will then terminate the program.
Exception handling is done via…?
A try-catch block
“Ducking” or declaring an exception is done with which keyword?
throws
Code that executes at the end of a try-catch block is designated with which keyword?
finally
When risky code is written that has the possibility of throwing an exception, it can be dealt with in one of two ways:
Handling means that the risky code is placed inside a try/catch block
Declaring means that the type of exception to be thrown is listed in the method signature with the throws keyword. This is also called “ducking” the exception - you let the code which calls the method deal with it.
What is troubleshooting?
Troubleshooting is a process that helps people identify issues or problems occurring in a system.
_____ is a troubleshooting technique where you get more info on the specifications, configuration setup, and events that occurred.
Drilling down
Troubleshooting occurs at a lower level than debugging.
False
Troubleshooting can be applied to any system.
True
The Collections framework in Java is a
set of classes and interfaces that implement commonly used data structures. A collection is a single object which acts as a container for other objects.
The important interfaces in the Collections API are:
Iterable - guarantees the collection can be iterated over
List - an ordered collection
Set - a collection does not contain duplicates
Queue - a collection that operates on a first-in-first-out (FIFO) basis
Map - contains key/value pairs. Does not extend Iterable.
Iterable
guarantees the collection can be iterated over
List
List - an ordered collection
This interface represents a collection of elements whose elements are arranged sequentially ordered.
List maintains an order of elements means the order is retained in which we add elements, and the same sequence we will get while retrieving elements.
We can insert elements into the list at any location. The list allows storing duplicate elements in Java.
ArrayList, vector, and LinkedList are three concrete subclasses that implement the list interface.
Set - a collection does not contain duplicates
Set - a collection does not contain duplicates
This interface represents a collection of elements that contains unique elements. i.e, It is used to store the collection of unique elements.
Set interface does not maintain any order while storing elements and while retrieving, we may not get the same order as we put elements. All the elements in a set can be in any order.
Set does not allow any duplicate elements.
HashSet, LinkedHashSet, TreeSet classes implements the set interface and sorted interface extends a set interface.
It can be iterated by using Iterator but cannot be iterated using ListIterator.
Queue
Queue - a collection that operates on a first-in-first-out (FIFO) basis
A queue is an ordered of the homogeneous group of elements in which new elements are added at one end(rear) and elements are removed from the other end(front). Just like a queue in a supermarket or any shop.
This interface represents a special type of list whose elements are removed only from the head.
LinkedList, Priority queue, ArrayQueue, Priority Blocking Queue, and Linked Blocking Queue are the concrete subclasses that implement the queue interface.
Map
Map - contains key/value pairs. Does not extend Iterable.
SortedSet Interface
This interface extends a set whose iterator transverse its elements according to their natural ordering.
TreeSet implements the sorted interface.
Deque Interface
A deque (double-ended queue) is a sub-interface of queue interface.
This interface was added to the collection framework in Java SE 6.
Deque interface extends the queue interface and uses its method to implement deque. The hierarchy of the deque interface is shown in the below figure.
Deque hierarchy in JavaIt is a linear collection of elements in which elements can be inserted and removed from either end. i.e, it supports insertion and removal at both ends of an object of a class that implements it.
LinkedList and ArrayDeque classes implement the Deque interface.
Map Interface
Map interface is not inherited by the collection interface. It represents an object that stores and retrieves elements in the form of a Key/Value pairs and their location within the Map are determined by a Key.
The hierarchy of the map interface is shown in the below figure.Map hierarchy in Java
Map uses a hashing technique for storing key-value pairs.
It doesn’t allow to store the duplicate keys but duplicate values are allowed.
HashMap, HashTable, LinkedHashMap, TreeMap classes implements Map interface.
SortedMap Interface
This interface represents a Map whose elements are stored in their natural ordering. It extends the Map interface which in turn is implemented by TreeMap classes.
A List is a collection that is
ordered and preserves the order in which elements are inserted into the list. Contrary to sets, duplicate entries are allowed. Also, elements are accessed by their index, which begins with 0 for the first element in the list.
An ArrayList is a concrete class which implements
List. It is a data structure which contains an array within it, but can resize dynamically.
A LinkedList implements both the
List and Queue interfaces, so it has all methods in both interfaces. The data structure is composed of nodes internally, each with a reference to the previous node and the next node - i.e. a doubly-linked list.
Which of these is NOT a characteristic of the List interface?
It does not preserve the order in which elements are inserted.
Duplicate entries are allowed.
Elements are accessed by index
List inherits operations from Collections as well as adding some of its own.
It does not preserve the order in which elements are inserted.
Which of these is not an additional operation provided by the List interface?
eliminate()
get()
set()
addAll()
eliminate()
Which of these is not an additional operation provided by the List interface?
firstIndexOf()
A ______ comprises a sequence of nodes with each node containing a reference to its successor and can be used to implement Stacks and Queues.
LinkedList
A ______ is more efficient for random access and less efficient for inserting/removing elements when compared to ______
ArrayList, LinkedList
Select the incorrect choice regarding LinkedList.
LinkedList is index driven.
LinkedList is faster at middle of the list retrieval/addition.
LinkedList implements List.
LinkedList implements Deque.
LinkedList is index driven.
______ is a List implementation that also implements the Queue and Deque interfaces.
LinkedList
Wrapper classes are
classes that let you treat primitives as Objects.
Boxing is the….
process of converting a primitive to its wrapper class
Java has a feature called autoboxing which will automatically convert primitives to wrapper classes implicitly.