Week 3 - Intermediate Java - Handling Problems, Exceptions and Errors, Collections Flashcards

1
Q

Describe the difference between exceptions and errors in Java.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Exceptions are never …..

A

thrown during the compilation process - they can only be thrown when the code is executing (running).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Compilation errors generally occur due to….(4)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The exception class hierarchy starts with the….

A

Throwable class which inherits from Object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The Exception and Error classes both extend

A

Throwable class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

An Error represents something that…

A

went so horribly wrong with your application that you should not attempt to recover from

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When the source code fails before it finishes compiling, this is known as a…?

A

Compilation error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What class is a general class which provides an abstraction for all exceptions?

A

Exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Exceptions that require mandatory handling are called

A

checked exceptions, The compiler will check that such exceptions are handled by the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

Throwable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Subclasses of the class Exception which are not subclasses of ______ are checked exceptions.

A

RuntimeException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If an error is predictable but unpreventable and reasonable to recover from, a checked exception should be used

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Since Java has exception-handling capability, programmers do not need to write code to handle exceptions.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Exception handling can be done in one of two ways:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Exception-handling is mandatory for…

A

any exception class that is not a subclass of either Error or RuntimeException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When risky code is written that has the possibility of throwing an exception, it can be dealt with in one of two ways:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

If the exception is not handled anywhere in the program, it will propagate up through the

A

call stack until it is handled by the JVM which then terminates the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

When an exceptional condition occurs in the course of a Java program, a special class called an Exception can be thrown,…

A

which indicates that something went wrong during the execution of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If an exception is NOT handled anywhere in the program, what will occur?

A

The exception propagates up through the call stack until it is handled by the JVM which will then terminate the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Exception handling is done via…?

A

A try-catch block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

“Ducking” or declaring an exception is done with which keyword?

A

throws

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Code that executes at the end of a try-catch block is designated with which keyword?

A

finally

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

When risky code is written that has the possibility of throwing an exception, it can be dealt with in one of two ways:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is troubleshooting?

A

Troubleshooting is a process that helps people identify issues or problems occurring in a system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
_____ is a troubleshooting technique where you get more info on the specifications, configuration setup, and events that occurred.
Drilling down
26
Troubleshooting occurs at a lower level than debugging.
False
27
Troubleshooting can be applied to any system.
True
28
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.
29
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.
30
Iterable
guarantees the collection can be iterated over
31
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.
32
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.
33
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.
34
Map
Map - contains key/value pairs. Does not extend Iterable.
35
SortedSet Interface
This interface extends a set whose iterator transverse its elements according to their natural ordering. TreeSet implements the sorted interface.
36
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.
37
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.
38
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.
39
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.
40
An ArrayList is a concrete class which implements
List. It is a data structure which contains an array within it, but can resize dynamically.
41
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.
42
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.
43
Which of these is not an additional operation provided by the List interface? eliminate() get() set() addAll()
eliminate()
44
Which of these is not an additional operation provided by the List interface?
firstIndexOf()
45
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
46
A ______ is more efficient for random access and less efficient for inserting/removing elements when compared to ______
ArrayList, LinkedList
47
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.
48
______ is a List implementation that also implements the Queue and Deque interfaces.
LinkedList
49
Wrapper classes are
classes that let you treat primitives as Objects.
50
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.
51
Unboxing is the...
reverse - converting a wrapper class to its primitive.
52
Wrapper classes have static helper methods like
.parseX() and .valueOf() for explicit primitive conversion.
53
What is the correct name for the wrapper class for the int type?
Integer
54
What is the process of converting a primitive to its wrapper class?
Boxing
55
What is the process of converting a wrapper class to its primitive?
Unboxing
56
What is the Java process that implicitly converts a primitive to its wrapper class?
Autoboxing
57
The Queue places objects on a “waiting list”, typically based on
First-In-First-Out (FIFO)
58
What is a queue data structure useful for?
Useful for storing objects prior to processing Elements are added to the tail of the queue Elements can be "popped" off the frot of the queue
59
The Deque Interface extends the...
Queue interface
60
Deque is useful for...
Short for “double-ended queue” Pronounced “deck” Supports element insertion and removal from both ends of the queue Can be used to implement a stack, with Last-In-First-Out (LIFO) behavior
61
Queue implementations include: (4)
LinkedList ArrayDeque PriorityQueue ArrayBlockingQueue
62
A ______ supports the insert and remove operations using a first-in first-out (FIFO) order.
Queue
63
______ is a resizable array implementation of Deque that is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.
ArrayDeque
64
______ is an array-backed collection that acts as a "bounded buffer", in which a fixed-sized array holds elements inserted by producer threads and extracted by consumer threads. Attempts to put an element into a full queue will result in the operation blocking; attempts to take an element from an empty queue will similarly block.
ArrayBlockingQueue
65
______ is a List implementation that also implements the Queue and Deque interfaces
LinkedList
66
Description of the Set Interface data structure?
The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.
67
A HashSet implements Set and is backed by a HashMap. It:
Guarantees no ordering when iterating Allows one null value Allows fast insertion and traversal Does not maintain order in which you insert elements
68
What is a TreeSet?
A TreeSet is a Set whose elements maintain sorted order when inserted. Internally, it is backed by a Sorted Tree. Insertion and removal of elements is slow, because the elements must maintain sorted order. It cannot contain any null values, since null cannot be compared to any object.
69
Which of the following is NOT a method provided by the Set Interface? delete() contains() isEmpty() iterator()
delete()
70
Which of the following is the main distinction with the Set interface?
Duplicate elements are not allowed in a Set
71
Which of the following is NOT an implementation of the Set interface?
StackSet
72
Which of the following is NOT a method provided by the Set Interface? length() add() remove() size()
length()
73
HashMap is a Map which:
Stores elements in key-value pairs Insertion/Retrieval of element by key is fast Tradeoff is that it does not maintain the order of insertion Permits one null key and null values
74
HashMap is a Map which:
Stores elements in key-value pairs Insertion/Retrieval of element by key is fast Tradeoff is that it does not maintain the order of insertion Permits one null key and null values
75
TreeMap is a Map whose:
Keys are stored in a Sorted Tree structure Main benefit is that keys are always in a sorted order Insertion/Retrieval are slow Cannot contain null keys as null cannot be compared for sorting
76
HashTable is an older, thread-safe implementation of a
HashMap. It does not allow null keys or null values
77
A Map is useful if you have to
search, update or delete elements on the basis of a key.
78
Map implements the Collection interface.
False
79
The Map interface defines an iterator() method.
False
80
Syntax for hashmap
Map map=new HashMap();
81
Define the access modifiers in Java
Access modifiers are simply keywords in Java that provides accessibility of a class and its member. They set the access level to methods, variable, classes and constructors. public, protected default, private
82
There are 4 types of access modifiers available in Java
public default protected private
83
Define public access modifiers
The member with public modifiers can be accessed by any classes. The public methods, variables or class have the widest scope.
84
Define default access modifiers
default: When we do not mention any access modifier, it is treated as default. It is accessible only within same package
85
Define protected access modifiers
protected: The protected modifier is used within same package. It lies between public and default access modifier. It can be accessed outside the package but through inheritance only
86
Define private access modifiers
private: The private methods, variables and constructor are not accessible to any other class. It is the most restrictive access modifier. A class except a nested class cannot be private.
87
____ modifier allows access by any class.
public
88
____ modifier allows access outside the package but through inheritance only.
protected
89
____ is the most restrictive access modifier.
private
90
Java provides a number of non-access modifiers to achieve many other functionalities. (3)
The static modifier for creating class methods and variables. The final modifier for finalizing the implementations of classes, methods, and variables. The abstract modifier for creating abstract classes and methods.
91
Java provides a number of non-access modifiers to achieve many other functionalities. (3)
The static modifier for creating class methods and variables. The final modifier for finalizing the implementations of classes, methods, and variables. The abstract modifier for creating abstract classes and methods.
92
Static Variables:
The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class. Static variables are also known as class variables. Local variables cannot be declared static.
93
Static Methods
The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.
94
Final Variables
A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed. So, the state of the object can be changed but not the reference. With variables, the final modifier often is used with static to make the constant a class variable.
95
Final Methods
A final method cannot be overridden by any subclasses. As mentioned previously, the final modifier prevents a method from being modified in a subclass. The main intention of making a method final would be that the content of the method should not be changed by any outsider.
96
Final Classes
The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class.
97
Abstract Methods
An abstract method is a method declared without any implementation. The methods body (implementation) is provided by the subclass. Abstract methods can never be final or strict. Any class that extends an abstract class must implement all the abstract methods of the super class, unless the subclass is also an abstract class. If a class contains one or more abstract methods, then the class must be declared abstract. An abstract class does not need to contain abstract methods. The abstract method ends with a semicolon. Example: public abstract sample();
98
Abstract Class
An abstract class can never be instantiated. If a class is declared as abstract then the sole purpose is for the class to be extended A class cannot be both abstract and final (since a final class cannot be extended). If a class contains abstract methods then the class should be declared abstract. Otherwise, a compile error will be thrown. An abstract class may contain both abstract methods as well normal methods.
99
The _____ keyword is used to create variables that will exist independently of any instances created for the class
static explanation: In object-oriented programming, the static keyword is used to define a variable or a method that is associated with the class itself, rather than with any specific instance of the class. This means that the variable or method can be accessed using the class name, rather than an object reference. For example, let's say you have a class called Car. You might define a static variable called numCars to keep track of the total number of cars created, like this: class Car { static int numCars = 0; // Other class members here... } In this case, the numCars variable belongs to the Car class as a whole, rather than to any particular instance of Car. It's declared as static, which means that it's not tied to any specific object, and can be accessed using the class name (Car.numCars) rather than an instance (myCar.numCars). Static variables are useful when you want to keep track of some information that's related to the class as a whole, rather than to any specific instance. They're also used to create constants, which are variables that can't be changed after they're initialized. Static methods work in a similar way. They belong to the class as a whole, rather than to any specific instance, and can be accessed using the class name. They're often used for utility functions that don't require any state information from an instance of the class.
100
A class can be both abstract and final.
False
101
How often can a final variable be initialized?
Only once
102
An abstract class can have both abstract methods and non-abstract methods. T/F?
True
102
An abstract class can have both abstract methods and non-abstract methods. T/F?
True
103
What is a static member in Java?
A static member in Java is a member of a class that is not associated with an instance of a class, but rather belongs to the class itself.
104
How is the static keyword used in Java?
The static keyword in Java is mainly used for memory management and is used to share the same variable or method of a given class. It can be applied to variables, methods, blocks, and nested classes.
105
What can the static keyword be applied to in Java?
The static keyword can be applied to blocks, variables, methods, and classes.
106
What is a non-access modifier in Java?
A non-access modifier in Java is a keyword that is used to modify the default behavior of a class or its members. The static keyword is a non-access modifier in Java.
107
What is the purpose of the static keyword in Java?
The purpose of the static keyword in Java is mainly for memory management and sharing the same variable or method of a given class.
108
What is a constant variable in Java?
A constant variable in Java is a variable that cannot be changed after it is initialized.
109
What type of methods are often declared as static methods?
Utility functions that don't require any state information from an instance of the class are often declared as static methods.
110
How can you access a static member in Java?
You can access a static member in Java using the class name, rather than an object reference.
111
The static keyword is a non-access modifier in Java that is applicable for the following:
Blocks Variables Methods Classes
112
When a member is declared static, it can be accessed before any
objects of its class are created, and without reference to any object.
113
A _________ is a member of a class that is not associated with an instance of a class.
static member
114
Static variables can be created at the class level only
True
115
Define the term variable scope.
When a variable is declared in a Java program, it is attached to a specific scope within the program, which determines where the variable resides.
116
The different scopes of a variable in Java are:
Instance, or object, scope Class, or static, scope Method scope Block scope
117
Instance scope means that the variable is
attached to individual objects created from the class. When an instance-scoped variable is modified, it has no effect on other, distinct objects of the same class.
118
Class, or static, scope
Resides on the class definition itself.
119
Method scope
Declared within a method block; only available within the method in which they are declared.
120
Block scope
Only exist within the specific control flow block (for, while, etc.)
121
When a variable is attached to individual objects created from the class, it has what type of scope?
Instance, or object, scope
122
When a variable resides in the class definition itself, it has what type of scope?
Class, or static, scope
123
When the scope of a variable declared within a method block, it has what type of scope?
Method scope
124
When a variable only exists within the specific control flow block, it has what type of scope?
Block scope