java Flashcards

1
Q

JDK vs JRE vs JVM

A
  • Java Development Kit contains compiler and debugger tools to compile the Java source code, also includes Java Runtime Environment and Java Virtual Machine
  • The JRE provides an environment and in that environment the JVM runs the compiled byte code. It contains both the library and the JVM.
  • The JVM creates a layer of abstraction from the Java Code and the computer environment. It is used to convert the java bytecode into machine readable code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Class vs Object

A
  • An object is an instantiation of a class

- The class is the blueprint that provides the states and behaviors its instantiated objects will have

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

What are the class members? (JAVA)

A
  • Instance variables
  • Methods
  • Constructors
  • Inner class
  • static/instance blocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the types of variables? (JAVA)

A
  • Instance variable
  • Local variable
  • Static variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the scopes of the variables? (JAVA)

A
  • Class - inside class
  • Method - inside method
  • Block - inside for or if statements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the Access Modifiers? (JAVA)

A
  • Public - globally accessible
  • Protected - accessible within the package and its subclasses
  • Default - accessible only within the package
  • Private - accessible only within that class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the Non-Access Modifiers? (JAVA)

A
  • Static - variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves
  • Final - define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value.
  • Abstract - purpose of extending
  • Synchronized - used for threads; accessed by only one thread at a time
  • Volatile - used for threads; lets JVM know that a thread accessing a variable must merge its own private copy with the master copy in memory
  • Transient - used in serialization; field that is transient will not be sent at all and will appear as null on the other end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Constructor? (JAVA)

A

A method that creates an instance of a class based on the parameters of the constructor

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

What are the types of Constructors? (JAVA)

A
  • Default (created by the JVM for you if no constructor is specified)
  • No args
  • Other
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 4 pillars of Object Oriented Programming?

A
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

. What is Abstraction? How to achieve it? (JAVA)

A
  • Abstraction is focused on the concept of reusability which is achieved using generalization or specialization

ACHIEVE WITH:
- Abstract class

  • Using a framework, such as Spring
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Abstract Class vs Interface (JAVA)

A
  • Can only extend one abstract class
  • Can implement as many interfaces as you would like
  • If a programmer wants to have method definitions or contain variables that are not static and final, they would use an abstract class. The methods in an abstract class can also be overridden, while in an interface, all the methods must be implemented by the class that implements the interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Polymorphism? How to achieve it?

A
  • Polymorphism is the concept that in Java, objects can have the same states and behaviors, so they are interchangeable

ACHIEVE WITH:

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

What is Inheritance? How to achieve?

A
  • Inheritance is the concept that classes can share states and behaviors through extension.
  • Achieve by extending a class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Encapsulation? How to achieve?

A
  • Encapsulation is a technique to control an object’s accessibility to its states. You can achieve this control using access modifiers. The access modifiers are public, protected, default, and private.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a hashcode? (JAVA)

A
  • A hashcode is an identifier for an object that has been converted to an integer.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Final vs Finally vs Finalize (JAVA)

A
  • Final is a non-access modifier that marks your variable as the value that it is initialized
  • Finally refers to the finally clause in a try, catch, finally block. Finally will always
    execute, even if there is an exception.
  • Finalize - a method that the garbage collector invokes when the JVM figures out
    that a particular instance should be garbage collected.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the keyword “Static” mean? (JAVA)

A
  • Static is a non-access modifier that means you can use that method or variable from a class without instantiating that class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is an Exception? What class does it extend?

A
- Exception extends Throwable, which has Exception and Errors as subclasses and
RuntimeException, which is a subclass of Exception.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

String vs StringBuilder vs StringBuffer

A
  • String - Immutable (cannot be changed or extended); stored in string pool; thread
    safe
  • StringBuilder - Mutable; for strings changing rapidly; do not extend string because string is final
  • StringBuffer - Mutable; synchronized, much slower; do not extend string because string is final; thread safe, useful for threads/strings being manipulated by multiple
    threads
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is Serialization?

A
  • Serialization is the process where java objects are converted to byte streams
  • Must use the serializable interface to be marked for serialization.
22
Q

What are the Data Structures in Java?

A

Data Structures are a way to store and organize data so that it can be used efficiently

Types of Data Structure
- Array - one block of memory; constant access time

  • ArrayList - dynamically resizable; object
  • Vector - dynamically resizable; object
  • LinkedList - uses nodes
  • Set - unique values; sortable
  • Queue - FIFO
  • Tree - binary search tree
  • Map - key value; not sortable
  • Deque - accessible from both ends
  • Stack - LIFO
23
Q

What is a Generic?

A
- A Generic is a template which enables programmers to specify generic methods
and classes with a single method declaration, a set of related methods, or with a
single class declaration
  • An example of a Generic is using the diamond syntax, and an example of using the diamond syntax is when you create a reference to a List of some object type and
    initialize an ArrayList.
24
Q

What is a Wrapper Class?

A
  • A Wrapper Class is meant to encapsulate primitive data types to turn them into
    objects using an operation of the JVM called autoboxing. You can also unbox an
    object to a primitive.

EX: Character, Byte, Integer, Float, …

25
Q

What is a Collection? What data structures does it provide?

A

A Collection - a group of individual objects represented as a single unit; an interface with static methods.

  • Iterable -> Collection -> Set, List, Queue
26
Q

What is a Set? What are some types of sets?

A

An interface which extends collection. A set is an unordered collection of objects
which can only contain unique values.

  • HashSet
  • LinkedSet
  • TreeSet
27
Q

What is a List? What are some types of lists?

A
  • An interface that implements Collection
  • An ordered collection of objects in which duplicate values can be stored

TYPES:

  • ArrayList
  • LinkedList
  • Vector
  • Stack
28
Q

What is a Thread?

A
  • A thread is a part of a process that runs a path of the program in execution. It runs asynchronous to the main method thread.
29
Q

What is Singleton? How to make one?

A
- A Singleton is a class that can only have one instance of that class at a time. Its
purpose is to limit the number of objects to only one
  • To design a Singleton class, you make the constructor
    private and write a static accessor method that gets your Singleton.
30
Q

What are Reflections?

A
  • Reflections refers to the accessing and manipulation of a class at runtime.
  • Reflection is generally used in frameworks to operate on objects without having those objects be of any particular class.
31
Q

What is the difference between ‘Collection’ vs ‘Collections’

A
  • Collection is the interface that implements Iterable. It marks a class as a Collection and therefore also Iterable.
  • Collections is the java util package that provides static methods available to
    Collection classes.
32
Q

What are the data types in Java? (8)

A
  • Boolean - true or false
    Default false
    1 bit
  • Char - character type whose values are 16-bit Unicode characters
    Default \u0000
    16 bits
  • Byte
    Default 0
    8 bits
  • Short
    Default 0
    16 bits
  • Int
    Default 0
    32 bits
  • Long
  • Default 0
  • 64 bits
  • Float
    Default 0.0
    32 bits
  • Double
    Default 0.0
    64 bits
33
Q

How do you invoke a static method?

A
- A static method can be invoked within the class that it is declared in without needing to
create an instance of that class to invoke the method.
- Outside the defining class, a static method is called by prefixing it with its class
name.
34
Q

What is an Interface?

A
  • An interface is a class that other classes can implement.
35
Q

. Can I force garbage collection?

A
  • No, you cannot force the garbage collector to scan the heap.
36
Q

What is the finally block?

A
  • The finally block is the block that executes after a try-catch block.
    A finally block will
    always execute, even if an exception is thrown.
37
Q

Comparable vs Comparator

A
  • Comparable is an interface that compares an object based on natural ordering
    (numbers and letters). It uses a convention of positive number for greater than the
    compared element, negative number for less than the compared element, and 0 for
    equal.
  • Comparator is an interface that implements Comparable and implements its
    compareTo() method for custom ordering. Comparator allows you to implement your
    own rules.
38
Q

How do I start a thread implementing Runnable?

A
  • Invoke the run method to start a thread implementing Runnable.
  • The Runnable object needs to be passed through the thread constructor and have
    invoke start.
39
Q

Thread methods. (run, start, sleep, wait, notify, notifyAll, join).

A

Run() - Executes starts the thread of execution.

Start() - Creates a new thread and calls run().

Sleep() - static method of a thread, does not release resource locks.

Wait() - method of an object, releases resource locks.

Notify() - wakes up only a single thread from a wait state to use a resource.

NotifyAll() - wakes up all threads waiting for a resource.

Join()- causes the current thread to wait for another thread to finish.

40
Q

Difference between the run and start method?

A
  • When you call run() it executes the run() method.

- When you call start() it instantiates a new thread and calls run.

41
Q

What is a deadlock?

A
  • Deadlock is when two or more threads are waiting for resources that the other(s)
    have.
42
Q

What is shadowing?

A
  • Shadowing is when two variables or objects with the same name and type have overlapping scopes.
  • When this happens the field with the larger scope is hidden.
43
Q

How do you insert elements in a Map?

A
  • Invoke the maps put() method.
44
Q

What is synchronization?

A
  • Synchronization is the process of making resources thread-safe
  • Done using the synchronize keyword
  • Makes object only accessible by one thread
45
Q

When is an object ready for garbage collection?

A
  • An object is ready for garbage collection when there is no longer any reference to it
    in memory.
  • This typically happens when the object goes out of scope
46
Q

What is Starvation?

A
  • Occurs when two threads are sharing resources: producer is creating resources and
    the consumer is using them
  • When the producer creates so fast that the consumer cannot keep up, it reaches a
    point where it can no longer produce, causing it to starve on runtime
47
Q

InputStream vs Reader

A
  • InputStream is a byte stream (used for serialized data).

- Reader is a character stream (used for reading character data (files)).

48
Q

What is the parent of all exceptions?

A

The hierarchy is:

  • Throwable
  • Exception
  • RuntimeException
  • Error
49
Q

How do I make an object Serializable?

A
  • Implement the Serializable Interface

- serializable is a marker interface (has no methods)

50
Q

Annotations. (JUNIT)

A
  • @Test - Denotes a method as a test.
  • @Before - Runs before every test.
  • @BeforeClass - Runs once before the class is run.
  • @After - Runs after each test.
  • @AfterClass - Runs once after the tests are done.
  • @Ignore - ignores a test temporarily
51
Q

How do you test your application automatically?

A
  • Certain processes and applications can use hooks to run tests when a commit or push is
    made as well (Jenkins, husky).