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
Q

_____ is a troubleshooting technique where you get more info on the specifications, configuration setup, and events that occurred.

A

Drilling down

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

Troubleshooting occurs at a lower level than debugging.

A

False

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

Troubleshooting can be applied to any system.

A

True

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

The Collections framework in Java is a

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.

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

The important interfaces in the Collections API are:

A

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.

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

Iterable

A

guarantees the collection can be iterated over

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

List

A

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.

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

Set - a collection does not contain duplicates

A

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.

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

Queue

A

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.

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

Map

A

Map - contains key/value pairs. Does not extend Iterable.

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

SortedSet Interface

A

This interface extends a set whose iterator transverse its elements according to their natural ordering.
TreeSet implements the sorted interface.

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

Deque Interface

A

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.

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

Map Interface

A

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.

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

SortedMap Interface

A

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.

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

A List is a collection that is

A

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.

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

An ArrayList is a concrete class which implements

A

List. It is a data structure which contains an array within it, but can resize dynamically.

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

A LinkedList implements both the

A

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.

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

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.

A

It does not preserve the order in which elements are inserted.

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

Which of these is not an additional operation provided by the List interface?

eliminate()
get()
set()
addAll()

A

eliminate()

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

Which of these is not an additional operation provided by the List interface?

A

firstIndexOf()

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

A ______ comprises a sequence of nodes with each node containing a reference to its successor and can be used to implement Stacks and Queues.

A

LinkedList

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

A ______ is more efficient for random access and less efficient for inserting/removing elements when compared to ______

A

ArrayList, LinkedList

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

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.

A

LinkedList is index driven.

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

______ is a List implementation that also implements the Queue and Deque interfaces.

A

LinkedList

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

Wrapper classes are

A

classes that let you treat primitives as Objects.

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

Boxing is the….

A

process of converting a primitive to its wrapper class
Java has a feature called autoboxing which will automatically convert primitives to wrapper classes implicitly.

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

Unboxing is the…

A

reverse - converting a wrapper class to its primitive.

52
Q

Wrapper classes have static helper methods like

A

.parseX() and .valueOf() for explicit primitive conversion.

53
Q

What is the correct name for the wrapper class for the int type?

A

Integer

54
Q

What is the process of converting a primitive to its wrapper class?

A

Boxing

55
Q

What is the process of converting a wrapper class to its primitive?

A

Unboxing

56
Q

What is the Java process that implicitly converts a primitive to its wrapper class?

A

Autoboxing

57
Q

The Queue places objects on a “waiting list”, typically based on

A

First-In-First-Out (FIFO)

58
Q

What is a queue data structure useful for?

A

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
Q

The Deque Interface extends the…

A

Queue interface

60
Q

Deque is useful for…

A

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
Q

Queue implementations include: (4)

A

LinkedList
ArrayDeque
PriorityQueue
ArrayBlockingQueue

62
Q

A ______ supports the insert and remove operations using a first-in first-out (FIFO) order.

A

Queue

63
Q

______ 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.

A

ArrayDeque

64
Q

______ 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.

A

ArrayBlockingQueue

65
Q

______ is a List implementation that also implements the Queue and Deque interfaces

A

LinkedList

66
Q

Description of the Set Interface data structure?

A

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
Q

A HashSet implements Set and is backed by a HashMap. It:

A

Guarantees no ordering when iterating
Allows one null value
Allows fast insertion and traversal
Does not maintain order in which you insert elements

68
Q

What is a TreeSet?

A

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
Q

Which of the following is NOT a method provided by the Set Interface?
delete()
contains()
isEmpty()
iterator()

A

delete()

70
Q

Which of the following is the main distinction with the Set interface?

A

Duplicate elements are not allowed in a Set

71
Q

Which of the following is NOT an implementation of the Set interface?

A

StackSet

72
Q

Which of the following is NOT a method provided by the Set Interface?
length()
add()
remove()
size()

A

length()

73
Q

HashMap is a Map which:

A

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
Q

HashMap is a Map which:

A

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
Q

TreeMap is a Map whose:

A

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
Q

HashTable is an older, thread-safe implementation of a

A

HashMap. It does not allow null keys or null values

77
Q

A Map is useful if you have to

A

search, update or delete elements on the basis of a key.

78
Q

Map implements the Collection interface.

A

False

79
Q

The Map interface defines an iterator() method.

A

False

80
Q

Syntax for hashmap

A

Map<Integer,String> map=new HashMap<Integer,String>();

81
Q

Define the access modifiers in Java

A

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
Q

There are 4 types of access modifiers available in Java

A

public
default
protected
private

83
Q

Define public access modifiers

A

The member with public modifiers can be accessed by any classes. The public methods, variables or class have the widest scope.

84
Q

Define default access modifiers

A

default: When we do not mention any access modifier, it is treated as default. It is accessible only within same package

85
Q

Define protected access modifiers

A

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
Q

Define private access modifiers

A

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
Q

____ modifier allows access by any class.

A

public

88
Q

____ modifier allows access outside the package but through inheritance only.

A

protected

89
Q

____ is the most restrictive access modifier.

A

private

90
Q

Java provides a number of non-access modifiers to achieve many other functionalities. (3)

A

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
Q

Java provides a number of non-access modifiers to achieve many other functionalities. (3)

A

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
Q

Static Variables:

A

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
Q

Static Methods

A

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
Q

Final Variables

A

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
Q

Final Methods

A

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
Q

Final Classes

A

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
Q

Abstract Methods

A

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
Q

Abstract Class

A

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
Q

The _____ keyword is used to create variables that will exist independently of any instances created for the class

A

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
Q

A class can be both abstract and final.

A

False

101
Q

How often can a final variable be initialized?

A

Only once

102
Q

An abstract class can have both abstract methods and non-abstract methods. T/F?

A

True

102
Q

An abstract class can have both abstract methods and non-abstract methods. T/F?

A

True

103
Q

What is a static member in Java?

A

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
Q

How is the static keyword used in Java?

A

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
Q

What can the static keyword be applied to in Java?

A

The static keyword can be applied to blocks, variables, methods, and classes.

106
Q

What is a non-access modifier in Java?

A

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
Q

What is the purpose of the static keyword in Java?

A

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
Q

What is a constant variable in Java?

A

A constant variable in Java is a variable that cannot be changed after it is initialized.

109
Q

What type of methods are often declared as static methods?

A

Utility functions that don’t require any state information from an instance of the class are often declared as static methods.

110
Q

How can you access a static member in Java?

A

You can access a static member in Java using the class name, rather than an object reference.

111
Q

The static keyword is a non-access modifier in Java that is applicable for the following:

A

Blocks
Variables
Methods
Classes

112
Q

When a member is declared static, it can be accessed before any

A

objects of its class are created, and without reference to any object.

113
Q

A _________ is a member of a class that is not associated with an instance of a class.

A

static member

114
Q

Static variables can be created at the class level only

A

True

115
Q

Define the term variable scope.

A

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
Q

The different scopes of a variable in Java are:

A

Instance, or object, scope
Class, or static, scope
Method scope
Block scope

117
Q

Instance scope means that the variable is

A

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
Q

Class, or static, scope

A

Resides on the class definition itself.

119
Q

Method scope

A

Declared within a method block; only available within the method in which they are declared.

120
Q

Block scope

A

Only exist within the specific control flow block (for, while, etc.)

121
Q

When a variable is attached to individual objects created from the class, it has what type of scope?

A

Instance, or object, scope

122
Q

When a variable resides in the class definition itself, it has what type of scope?

A

Class, or static, scope

123
Q

When the scope of a variable declared within a method block, it has what type of scope?

A

Method scope

124
Q

When a variable only exists within the specific control flow block, it has what type of scope?

A

Block scope