Java - Intermediate Flashcards

1
Q

JDK

A

JDK (Java Development Kit) is a software development environment that provides tools and libraries necessary for developing Java applications.

It includes the:
1. compiler,
2. debugger,
4. and other development utilities.

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

JRE

A
  • JRE (Java Runtime Environment)
  • is an implementation of the JVM (Java Virtual Machine)
  • along with libraries and other files required to run Java applications.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

JVM

A
  • JVM (Java Virtual Machine)
  • an abstract computing machine that executes Java bytecode.
  • provides a runtime environment for Java applications (to run independently of the underlying hardware and operating system.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the concept of polymorphism in Java

A
  • it allows a subclass to override a method of its superclass, providing its own implementation.
  • Polymorphism enables:
    1. code flexibility
    2. and reusability.

For Example:

class Shape {
    void draw() {
        System.out.println("Drawing a shape");
    }
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a circle");
    }
}

class Rectangle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape circle = new Circle();
        Shape rectangle = new Rectangle();

        circle.draw();     // Output: Drawing a circle
        rectangle.draw();  // Output: Drawing a rectangle
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different types of collections available in Java?

A
  1. List
  2. Set
  3. Map
  4. Queue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List

A
  1. Ordered
  2. Allows duplicates
  • Example:
    1. ArrayList,
    2. LinkedList
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Set

A
  1. Unordered
  2. Does not allow duplicates
    - Example:
    1. HashSet
    2. TreeSet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Map

A
  • An interface for a collection that stores data in key value pairs
  • The way it orders depends on the implementation type:
    1. HashMap doesn’t guarantee order
    2. TreeMap sorts based on natural order of the keys
  • Example:
    1. HashMap,
    2. TreeMap,
    3. LinkedHashMap,
    4. ConcurrentHashMap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Queue

A

Follows the FIFO (First-In-First-Out) principle.

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

Explain the concept of generics in Java

A
  • Generics allow the use of type parameters to create generic classes and methods.
  • provide compile-time type checking, ensuring that the correct types are used at compile time and preventing type-related errors at runtime
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the benefits of Generics?

A
  • compile-time type safety,
  • and thus preventing type-related errors at runtime
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ArrayList

A

ArrayList is the List implemented for a resizable array.

  1. Dynamic Size
  2. Can access elements by index
  3. Ordered
  4. Duplicates allowed
  5. Null elements allowed
  6. Resizes by a factor of 1.5 or 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

LinkedList

A
  1. double linked - knows prev and next node
  2. dynamically sized
  3. ordered
  4. duplicates allowed
  5. nulls allowed

Sequential access (walking the list) is better than random access.

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

When should you use an ArrayList?

A
  1. When you need fast access to elements by index
  2. and when the list size doesn’t change frequently.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When should you use LinkedLists?

A
  1. When you need to add or remove an element from the beginning or end,
  2. When you don’t know the size of the list you’re going to be working with.
  3. When you will be frequently adding or removing elements from the list. (ArrayLists have to shift each element forward or backward in the list whereas a linked list only has to update it’s prev/next pointers).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain the concept of threads in Java

A
  1. A thread represents an independent path of execution within a program.
  2. It allows multiple tasks to be executed concurrently.
17
Q

What is synchronization in Java?

A

Ensures that only one thread can access a shared resource at a time.

18
Q

Serialization

A
  • object -> byte stream
  • which can be stored in a:
    1. file,
    2. sent over a network,
    3. or persisted in a database.
19
Q

Deserialization

A

byte code -> object

20
Q

How do you serialize and desserialize a class?

A
  • The Serializable interface is used to mark a class as serializable
  • and the ObjectOutputStream is used to serialize an object
  • and ObjectInputStream is used to deserialize an object.
21
Q

What are annotations in Java?

A

Annotations provide metadata about program elements, such as:
1. classes,
2. methods,
3. or fields.

22
Q

From Java 8 How do you handle dates and times in Java?

A
  • In Java 8 and later versions, the Date and Time API (java.time package) was introduced.
  • It provides classes like:
    1. LocalDate,
    2. LocalTime,
    3. LocalDateTime,
    4. and ZonedDateTime

for handling dates, times, and time zones.

23
Q

What are the advantadges of the Date and Time API in Java 8?

A
  1. improved functionality,
  2. immutability - once an object is created it’s state cant be changed
  3. thread safety,
  4. and better support for operations like
    1. parsing,
    2. formatting,
    3. and calculations.
24
Q

What are lambda expressions in Java?

A
  1. anonymous functions
  2. uses a more concise arrow syntax.
  3. helpful for things like implementing Functional Interfaces (an interface with only one method) instead of using anonymous inner classes.
25
Q

Explain the concept of reflection in Java

A

Reflection is a feature in Java that allows you to examine and modify the structure and behavior of classes at runtime.

It provides the ability to dynamically create new objects, invoke methods, and access fields, even for private members.

26
Q

What is a use case for reflection in Java?

A
  • Framework Dependency Injection: Inspecting parameters in constructors and dynamically loading class instances.
  • Framework Aspect Oriented Programming: Identify and intercept method invocations on objects so that various aspects such as logging, exception handling, etc… can be dynamically applied at runtime.
27
Q

Explain the concept of dependency injection in Java

A
  • Manage dependencies by injecting them as parameters in a constructor or method rather than creating them internally.
  • Helps achieve loose coupling between components promoting:
    1. easier testing
    2. and maintainability.
28
Q

What are the benefits of Dependency Injection?

A
  1. Loose coupling
  2. Improved reusability (used in more ways without having to change code).
  3. Testability
  4. Configurability (change configuration without changing the code)
  5. Inversion of Control (allows for dependencies to be provided from an external source such as a framework allowing the code to focus on what it’s supposed to do)
29
Q

Why should you use StringBuilder instead of concatenation?

A

StringBuilder does not create a new string object for each concatenation making it a better use of memory and more performant.

30
Q

Explain the concept of inner classes in Java

A
  • Classes defined within another class.
  • They have access to the members of the enclosing class (including private members).
31
Q

When could you use an inner class in Java?

A
  1. Encapsulation: Inner classes can encapsulate implementation details within the scope of the enclosing class.
  2. Implementing interfaces: i.e. functional interfaces can be implemented using an anonymous inner class.