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 compiler, debugger, 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.

JRE does not include development tools.

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

JVM

A

JVM (Java Virtual Machine) is an abstract computing machine that executes Java bytecode.

It 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

Polymorphism is the ability of an object to take on different forms.

In Java, it allows a subclass to override a method of its superclass, providing its own implementation.

Polymorphism enables code flexibility and reusability.

It allows you to write code that can work with objects of different classes, as long as they are related through inheritance or interface implementation.

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

List

Set

Map

Queue

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

List

A

An ordered collection that allows duplicate elements. Example: ArrayList, LinkedList.

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

Set

A

An unordered collection that does not allow duplicate elements. Example: HashSet, TreeSet.

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

Map

A

A collection that stores key-value pairs. Example: HashMap, TreeMap.

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

Queue

A

A collection that follows the FIFO (First-In-First-Out) principle. Example: LinkedList, PriorityQueue.

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

Explain the concept of generics in Java and their benefits.

A

Generics allow the use of type parameters to create generic classes and methods.

They enable the creation of reusable code that can work with different types.

Benefits of generics include compile-time type safety, eliminating the need for explicit type casting, and enabling code reuse.

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

ArrayList

A

a resizable array

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

LinkedList

A

a doubly-linked list

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

When would you list an ArrayList vs a LinkedList

A

ArrayList provides fast random access but slower insertions and deletions, while LinkedList provides fast insertions and deletions but slower random access.

Use ArrayList when you need fast access to elements by index and when the list size doesn’t change frequently.

Use LinkedList when you frequently add or remove elements from the list, particularly from the beginning or middle, and when you don’t require random access to elements.

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

Explain the concept of threads in Java

A

A thread in Java represents an independent path of execution within a program. It allows multiple tasks to be executed concurrently

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

What is synchronization in Java?

A

Synchronization is a mechanism in Java to control access to shared resources by multiple threads to ensure thread safety.

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

How do you ensure thread safety?

A

The synchronized keyword can be used to create synchronized blocks or methods.

It ensures that only one thread can execute the synchronized code block or method at a time.

Synchronization can prevent race conditions and data corruption when multiple threads access and modify shared data simultaneously.

17
Q

Serialization

A

the process of converting an object into a byte stream, which can be stored in a file, sent over a network, or persisted in a database.

18
Q

Deserialization

A

reconstructing an object from the serialized byte stream.

19
Q

How do you serialize and deserialize an object?

A

The Serializable interface is used to mark a class as serializable,

the ObjectOutputStream and ObjectInputStream classes are used for serialization and deserialization.

20
Q

What are annotations in Java?

A

Annotations provide metadata about program elements, such as classes, methods, or fields.

21
Q

How do you handle dates and times in Java in Java 8 and later versions?

A

In Java 8 and later versions, the Date and Time API (java.time package) was introduced.

It provides classes like LocalDate, LocalTime, LocalDateTime, and ZonedDateTime for handling dates, times, and time zones.

22
Q

What advantadges does the Date and Time API bring in Java 8?

A

offers improved functionality, immutability, thread safety, and better support for operations like parsing, formatting, and calculations.

23
Q

What are lambda expressions in Java?

A

Lambda expressions are anonymous functions that can be used to provide a concise and functional way of representing behavior in Java.

24
Q

How do Lambda expressions simplify functional programming?

A

They simplify functional programming by allowing the representation of behavior as data and enabling the use of functional interfaces (interfaces with a single abstract method) like Predicate, Consumer, and Function.

Lambda expressions eliminate the need for creating anonymous inner classes for implementing functional interfaces and provide a more expressive and compact way of writing code.

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

Explain the use of streams in terms of input/output?

A

The java.io package provides classes for working with streams. InputStream and OutputStream are used for byte-oriented I/O, while Reader and Writer are used for character-oriented I/O.

Streams allow reading or writing data from various sources like files, network sockets, or in-memory buffers.

27
Q

What is the purpose of equals()

A

The equals() method is used to compare the equality of objects.

It is typically overridden to provide a custom implementation based on the object’s properties.

28
Q

What is the purpose of hashCode()

A

The hashCode() method is used to generate a hash code for an object.

It is used in hash-based data structures like HashMap and HashSet for efficient storage and retrieval of objects.

29
Q

Explain the concept of dependency injection

A

Dependency injection is a design pattern that allows the separation of object creation and dependency resolution from the class itself.

In dependency injection, dependencies are provided to a class from an external source (e.g., through constructor parameters or setters) rather than the class creating them itself.

30
Q

What are the benefits of dependency injection?

A

Benefits of dependency injection include improved modularity, testability, and flexibility in configuring and managing dependencies.

31
Q

What’s the benefit of using StringBuilder over just concatenating strings?

A

The advantage of StringBuilder over string concatenation using the “+” operator is that StringBuilder avoids unnecessary string object creations and performs better when concatenating multiple strings in a loop or in performance-critical scenarios.

32
Q

Explain the concept of inner classes in Java

A

Inner classes are classes defined within another class. They have access to the members of the enclosing class, including private members.

33
Q

When would you use inner classes?

A

Encapsulation: Inner classes can encapsulate implementation details within the scope of the enclosing class.

Callbacks and event handling: Inner classes can be used to define event listeners or callback handlers.

Iterators and data structures: Inner classes can be used to implement custom iterators or data structures within a class.

34
Q

How do you handle concurrency in Java?

A

Concurrency in Java is handled using mechanisms like locks and synchronized blocks to ensure thread safety.

35
Q

What is a lock?

A

Locks provide a way to control access to shared resources by allowing only one thread to acquire the lock at a time.

36
Q

What is a synchronized block?

A

Synchronized blocks use the synchronized keyword to acquire an intrinsic lock on an object, ensuring that only one thread can execute the block at a time.