Test 2 Flashcards

1
Q

What happens if I declare a specific type for a list interface

A

declare a specific type → need to define
many List interfaces, one for each specific type
creates a lot of duplicate code

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

what happens if I declare a list with elements are simply Object

A

lose the safety of compile-time type checking (need to cast) cant do anything with the list because it needs to be casted to type

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

type parameter

A

a variable that takes a type as its value and
is used to make a generic class or interface more specific

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

what type is List<E></E>

A

generic type

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

List<String></String>

A

parameterized type

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

List

A

raw type

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

what can you do with Parametrized types

A

Can declare variables of parameterized types
Can construct objects of parameterized types
Can invoke methods on objects of parameterized types
Can define methods that take or return parameterized types

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

what can’t raw types do

A

never declare or instantiate!

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

what can generics extend

A

Generic types can extend/implement other generic types
We could not define…
public class MyList<T> implements List<E>
Generic types can extend non-generic types</E></T>

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

what can non generics extend

A

Non-generic types can also extend/implement generic types

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

Invariance

A

must use the exact type specified

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

Contravariance

A

can use a less specific type than declared

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

Covariance

A

can use a more specific type than declared

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

Pros of covariance

A

more flexible

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

invariance

A

more safe (less prone to failing at runtime)

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

boolean add(E elem) (.add)

A

adds elem to the end of the List and returns true, if its an element it return false

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

add(int idx, E elem)

A

adds elem to the List at index idx

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

E set(int idx, E elem) (.set)

A

places the element at index idx in the
List, replacing what was there previously

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

E get(int idx)

A

returns the element at index idx in the List

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

int indexOf(E elem)

A

returns the index of elem in the List

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

boolean contains(Object obj)

A

returns true if obj is in the
List; false otherwise

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

E remove(int idx)

A

removes element at index idx from the List

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

boolean remove(Object obj)

A

removes obj from the List

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

if remove could be boolean remove or normal remove what does java go with

A

removes number at that idx

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

what do u do when u want to remove a certain value from a list but that number is also a valid index

A

.remove(Integer.valueOf());

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

If we define a generic type (class or interface) by declaring one or more type
parameters, what is the scope of those type parameter(s)?

A

The scope would be the entire class or a single method.

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

There are only two circumstances under which it may be a good design to use a
raw type. What are those circumstances?

A

a. When you want to access a static field or method of generic class
because it enables you to quickly create non empty lists
b. When you want to use instanceOf to perform type checking

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

what are javas 3 primary ways of creating a constructor

A
  1. Default constructors (zero args) – create an empty container: List<Integer> myList = new ArrayList<Integer>(); 2.Copy constructors – create a shallow copy of a given container 3.Static “factory” methods – quick means of building a new
    non-empty container
    List<Integer> myList = List.
    of(1, 3, 5, 7, 9)</Integer></Integer></Integer>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

difference between generic methods and types

A

Generic methods offer greater flexibility. Generic types offer better consistency & type safety generic types are a good fit for when
you want a consistent type across
multiple methods that share state

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

If we define a generic method by declaring one or more type parameters, what is
the scope of those type parameter(s)?

A

The scope of the type parameter is simply the single method for which it is
defined

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

describe one situation in which a generic method would be the better design

A

An example would be if we had a generic copier
method this way we could use one copier object once to copy lists of different
type inputs. If we used a generic class we would have to instantiate a different
copier for each type of list we wanted to copy

32
Q

Describe one situation in which a generic class would be the better design

A

Generic classes are better for consistency and type safety. An example would be
a Map class when the values for K and V are type parameters for the entire
class, having Java’s static type checking guarantees the argument type will
match the parameter type and the return value is assigned to the right type.

33
Q

pros and cons of LVT

A

Pros of explicit declaration:
● Code is more “self-documenting”
● Can be used in all cases
● Have full control over the choice of
numeric type (e.g. int vs. short)
Cons of LVTI:
● Code is less self-documenting
● Can only be used in select cases
● Can’t indicate that you want a byte or
short (will always get an int)*

34
Q

pros and cons of explicit declaration

A

Pros of explicit declaration:
● Code is more “self-documenting”
● Can be used in all cases
● Have full control over the choice of
numeric type (e.g. int vs. short)

Cons of explicit declaration:
● Code might take slightly longer to write
● Less adaptable to interface changes

35
Q

Two kinds of type inference in modern Java:

A
  1. Local variable type inference (LVTI), introduced in Java 10
  2. Generic type inference (diamond operator), introduced in Java 7
36
Q

generic type inference

A

● Pros:
○ Code is faster to write
○ Code is more adaptable to changes, in interfaces and locally
○ Code is arguably more readable (shorter, but without any loss of
information) if declaration & construction are in the same line
● Cons:
○ Code is less readable if declaration & construction statements are
not located near each other Verdict: use whenever declaration
& construction are near each other!

37
Q

what happens when LVTI and generic type is combined

A

var myList = new ArrayList<>();
● Problem:
○ var infers the type from the righthand side; finds ArrayList,
but no type parameter
○ <> infers the type from the lefthand side; no type is found
● Result: type is ArrayList<object></object>

38
Q

constraints on wildcards

A

New constraints, in order to preserve type safety:
1. Can’t put anything besides null into a Collection<?>
2. Can’t assume anything about the types of the objects you
take out of a Collection<?>
3. Can’t directly instantiate an object with a wildcard

39
Q

generic methods vs wildcards

A

generic methods offer
greater consistency
Wildcards offer
greater flexibility

40
Q

PECS principle:

A

“producer
extends, consumer super” A ○ producer = a collection that we’re taking elements out of
○ A consumer = a collection that we’re putting elements into
○ Use upper bounds (extends) when you want a producer
○ Use lower bounds (super) when you want a consumer
○ If you want both, need to use a specific (non-wildcard) type

41
Q

what is the point of type erasure

A

java uses type erasure to make raw types and parameterized
types work together seamlessly

42
Q

Limitations on defining generic types & methods:

A
  1. Cannot use a type parameter T to construct an object of type T
  2. Cannot use a type parameter T to construct an array of type T[]
    Limitations
    public static <T> T constructObject() {
    return new T();
    }
    public static <T> T[] constructArray() {
    return new T[10];
    }</T></T>
43
Q

Limitations on using parameterized types

A

Cannot use instanceof to check parameterized types
List<Integer> myList = new ArrayList<Integer>();
boolean isIntList = myList instanceof List<Integer>;
List<T> myList = new ArrayList<T>();
boolean isList = myList instanceof List<T>;</T></T></T></Integer></Integer></Integer>

44
Q

Compile-time errors

A

errors that are detected by the compiler (syntax & type safety)

45
Q

Logical errors

A

not detected by the compiler or the JVM, but
cause the program to do the wrong thing

46
Q

Runtime errors

A

errors that are detected by the JVM ■ Error only occurs for certain inputs to the program
■ Program reads file; error only occurs if the file is missing or
malformed

47
Q

Checked

A

must be handled

48
Q

Unchecked

A

do not need to be handled

49
Q

Java’s compiler requires that every checked exception is
handled in one of two ways:

A
  1. A function can catch an exception and handle it appropriately
  2. A function can throw an exception to its caller
    Checked Exceptions
50
Q

what are errors

A

Errors are typically
non-recoverable issues;
typically originate in the JVM

51
Q

what are exceptions

A

Exceptions are issues that
are potentially recoverable;
can originate in code or JVM

52
Q

runtime exceptions

A

RuntimeExceptions are unchecked (no handling
required); often the result of programmer error

53
Q

what are types of unchecked exceptions

A

IOException, TimeoutExpection all other Exceptions are
checked (must be handled);
typically not the result of
programmer error

54
Q

there are 3 types of Throwable objects in Java:

A

errors: typical point of origin? JVM Typically caused by programmer error? No
Typically recoverable? No
Need to be handled? No

Runtime
Exceptions:Typical point of origin? JVM
Typically caused by programmer error? Yes
Typically recoverable? No
Need to be handled? No

All Other
Exceptions
Typical point of origin?: Code
Typically caused by programmer error?: No
Typically recoverable?: Maybe
Need to be handled?: Yes

55
Q

pros and cons of exceptional control flow

A

● Pros of using exceptional control flow:
○ Forces you to think about what errors may occur
○ Forces you to handle errors in a structured way
○ Seamless integration with the JVM
● Cons of using exceptional control flow:
○ Code is slightly bulkier, but no worse than checking return values
○ Slight time overhead
○ Can be abused out of laziness

56
Q

When not to use exceptional control flow?

A

Don’t use to handle unchecked exceptions!
These are generally caused by poor programming.

57
Q

role of device drivers

A

The OS uses device drivers to
communicate with I/O devices

58
Q

memory vs storage

A

Memory = short-term; contents are lost when power is lost
■ Contains data used by running programs
Stroage = long-term; contents persist when power is lost
■ Contains data that appears to be organized as files

59
Q

hard disk

A

a circular disk + a fixed arm that reads from the disk;
disk spins to the correct location for the arm to read
has no notion of files

60
Q

solid state drive

A

non-volatile memory: sequence of fixed-size storage
cells like “normal” memory (DRAM), but persistent

61
Q

what is a file system

A

an abstraction, which
imposes structure on the data in storage by:
○ Dividing the space on the HDD/SSD into blocks
○ Maintaining an indexing system for organizing files

62
Q

what are the options to read a file

A

Read the entire contents into a single String:
String contents = Files.readString(Paths.get(path));
○ Use a Reader to perform more granular operations

63
Q

buffered vs unbuffered

A

In unbuffered I/O, each
read/write goes to the OS,
triggering a potentially
slow hardware access. In buffered I/O, a buffer
serves as an intermediary
between the OS and the
Reader/Writer, allowing
multiple read/writes to be
combined into one
hardware access

64
Q

call stack

A

the in-progress function calls that have led to the
current point

65
Q

frame

A

One frame per function = an abstraction encapsulating the value
of each of the function’s local variables

66
Q

breakpoint

A

a custom stopping point that you can place in the
program to pause execution when running in debug mode. Set a breakpoint by clicking in the lefthand margin next to the line
you’d like to stop at

67
Q

why Modern operating systems typically provide a graphical user
interface (GUI)

A

so that you can run programs with a single click
● This GUI is an abstraction; when you click on an icon to run a
program, a command is sent to the OS telling it:
1. The path (location) of the program to be run
2. Which arguments to use as inputs to that program

68
Q

Running Your Program from the Command Line

A

public class Main {
public static void main (String[] args) {
System.
out.println(args[0]);
}
}java Main hello
compiles Main.java, generating Main.classjavac Main.java
runs Main.main() with args = [“hello”], printing “hello”

69
Q

Two types of RNGs:

A
  1. Pseudo random number generators (PRNGs), AKA deterministic
    random number generators (DRNGs)
    ○ Numbers only appear to be random
  2. Hardware random number generators (HRNGs), AKA true
    random number generators (TRNGs)
    ○ Numbers are genuinely random
70
Q

PRNGs
pros and cons

A

● Pro:
○ Fast!
● Con:
○ Not truly random; a poorly-designed PRNG
may exhibit:
■ A short period
■ Correlation of successive values
■ Lack of uniformity of distribution

Because of this lack of true randomness,
PRNGs are less secure: if the seed is known,
the entire sequence can be reproduced

71
Q

when are PRNGs cryptically secure

A

A PRNG is a CSPRNG iff it’s been proven that:
○ If an attacker knows all of the generated bits up to a point
○ But the attacker does not know the seed
○ Then the attacker cannot guess the next bit with probability
significantly higher than 50%

72
Q

pros and cons of HRNGs

A

● Pro: non-deterministic!
● Con: slow!
○ The rate at which entropy can be harvested from a natural source
depends on the underlying physical phenomena
○ Some algorithms may request random numbers faster than they
can be generated; this causes the HRNG to block

73
Q

hybrids of PRNG and HRNG

A

Hybrid approaches can be used to strike a balance of between
non-determinism and efficiency, e.g.:
1. Use an HRNG by default, but fall back on a PRNG when the
desired rate of generation exceeds the HRNG’s capabilities
2. Use a PRNG, but periodically re-seed it with a number generated
by an HRNG

74
Q

create a variable that will get a random number between 2 values

A

randNum = (Math.random() * (upper - lower)) + lower;
Randomness in Java

75
Q

process

A

A process encapsulates an instance of a running program However, each process believes that:
1. It’s the only process
2. It has exclusive access to resources such as memory and CPU
3. All of its operations get executed back-to-back

76
Q

how do process protect against malicious attacks

A

The OS runs in kernel mode; has complete power
○ Processes run in user mode; the hardware bans certain
operations from being performed in user mode
■ Processes need to ask the OS for help with these operations,
at which point it can safeguard against malicious behavor The OS gives each process a private virtual address space
○ Physical address = real (hardware) memory address
○ Virtual address = fake address used within a process

77
Q

advantages of virtual address

A

Security: OS is involved in the “address translation” process →
process can’t access memory that doesn’t belong in it
○ Convenience: OS can present the process with a contiguous
virtual address space, even if the physical pages are scattered