OOP Flashcards
- What is the JVM?
The JVM (Java Virtual Machine) is like a virtual computer that runs Java programs. It translates Java code into instructions that the computer can understand. It also manages memory and ensures that the program runs smoothly. The JVM allows Java programs to work on different types of devices and operating systems without needing to be rewritten.
- What does Java compilation mean?
Java compilation is the process of converting human-readable Java code into a format that can be understood and executed by the Java Virtual Machine (JVM), enabling platform-independent execution of Java programs.
- What is Java bytecode?
Java bytecode is a low-level, platform-independent representation of Java code. It is generated during the compilation process and can be executed by the JVM.
- What is the difference between the JRE and the JDK?
JRE (Java Runtime Environment) is responsible for executing Java applications, while the JDK (Java Development Kit) includes the JRE and additional tools needed for developing Java applications, such as compilers and debuggers. The JRE is used for running Java programs, while the JDK is used for both developing and running Java programs.
- What is the
Iterable
interface?
The Iterable
interface is the foundation of classes that represent a collection of elements that can be iterated (looped) through.
The main purpose of the Iterable interface is to provide a standardized way to iterate over the elements of a collection using an enhanced for loop (also known as a “for-each” loop) or by using iterators.
- What is the
Collection
interface?
The Collection interface in Java is the root interface of the Java Collections Framework hierarchy. It represents a group of objects, known as elements, and provides a set of methods for working with these elements like adding, removing, and querying elements in a collection.
- What is the
Map
interface?
- The
Map
interface in Java represents a collection of key value pairs, where each key is unique. It provides a way to associate values with keys and retrieve values based on their corresponding keys. TheMap
interface does not inherit from the Collection interface but is an independent interface in the Java Collections Framework.
- Compare sets, lists, and queues in Java.
- Sets:
- Collection of unique elements. No specific order.
- Ensures uniqueness.
- Key implementations: HashSet, TreeSet, LinkedHashSet.
- Lists:
- Ordered collection of elements.
- Allows duplicates.
- Maintains insertion order.
- Accessible by index.
- Key implementations: ArrayList, LinkedList, Vector.
- Queues:
- Follows First-In-First-Out (FIFO) order.
- Elements added at the end, removed from the beginning.
- Key implementations: LinkedList, ArrayDeque, PriorityQueue.
- Useful for task scheduling, event handling, and algorithms like breadth-first search.
- Compare
ArrayList
andLinkedList
in Java.
- ArrayList provides efficient random access and retrieval by index but slower for insertion and removal in the middle.
- LinkedList offers fast insertion and removal at the beginning and end but slower for random access.
- Are sets sorted in Java?
- In general, sets in Java do not guarantee a specific order of elements.
- The Set interface itself does not define any order for its implementations.
- What control statements are available in Java?
- Conditional Statements
- Looping Statements : for, while, do-while, foreach
- Jump Statements: break, continue, return, throw
- Branching Statements: switch
- Compare the different looping constructs in Java.
- for loop: Used when the number of iterations is known or can be determined.
- while loop: Used when the number of iterations is not known in advance and depends on a condition.
- do-while loop: Similar to the while loop, but the condition is evaluated after the loop body.
- foreach loop: Designed specifically for iterating over arrays or collections.
- Compare the different conditional constructs in Java.
- if statement: Used for basic conditional branching.
- if-else statement: Allows for branching between two blocks of code.
- if-else if-else statement: Enables branching between multiple conditions.
- switch statement: Provides multiple branching options based on the value of a variable or expression.
- What is a
while
loop?
A while
loop is a control flow statement in Java that allows for repeated execution of a block of code as long as a given condition remains true. It is used when the number of iterations is not known in advance and depends on the condition being evaluated.
- How do you manually break out of a loop?
In Java, you can manually break out of a loop using the break
statement.
The break
statement allows you to exit the loop prematurely, regardless of the loop condition.
- What does the
var
keyword mean?
The var keyword in Java is a feature introduced in Java 10 that allows for local variable type inference. It allows you to declare variables without explicitly specifying their types, and the compiler infers the type based on the assigned value. The var keyword can only be used for local variables within method bodies, loops, or other blocks.
- What are lambda expressions? How are they used in Java development?
In Java, lambda expressions are primarily used in functional interfaces, which are interfaces with a single abstract method (SAM). Instead of implementing the interface explicitly with a separate class or anonymous inner class, you can use lambda expressions to provide a concise implementation of the abstract method.
- What are primitive types in Java? Give some examples.
Primitive types in Java are basic data types that represent the most fundamental types of data.
They are not objects and do not have methods or properties like objects do.
Example:
- boolean isTrue = true
- int i = 42
- char c = ‘A’
- What is the difference between primitive types and reference types?
The main difference between primitive types and reference types in Java lies in how they are stored, treated, and interacted with in the program. Here are the key distinctions:
1. Primitive types:
- Represent basic data types in Java and are predefined by the language itself (e.g., int, boolean, float,char).
- Store their values directly in memory, typically on the stack, with a fixed size.
- Have default values if not explicitly initialized (e.g., 0 for numeric types, false for boolean).
- Are assigned by value, meaning the value is copied to the assigned variable. Comparison checks the equality of values.
- Do not have methods or properties since they are not objects.
- Have corresponding wrapper classes (e.g., Integer, Boolean) that provide object representations and additional functionality.
2. Refrence types:
- Represent complex objects that are created using classes or interfaces defined by the programmer or language(e.g., String, ArrayList).
- Store a reference (memory address) to the actual object in memory, typically on the heap, allowing for dynamic memory allocation.
- Have a default value of null if not explicitly initialized, indicating that they don’t reference any object.
- Are assigned by reference, meaning the reference (memory address) is copied to the assigned variable. Comparison checks if the references point to the same object, not the equality of values.
- Can have methods and properties, as they are objects that can be instantiated from classes or interfaces.
- Do not have wrapper classes since they are already objects.
- What is a class in Java?
In Java, a class is a fundamental building block and a blueprint for creating objects. It serves as a template that defines the properties (attributes) and behaviors (methods) that objects of that class can have.
- What is an object in Java?
In Java, an object is a fundamental entity created from a class. It represents a specific instance of that class and encapsulates its own set of data (attributes) and behaviors (methods). Objects are the main building blocks used to interact with and manipulate data in Java programs.
- What is a constructor?
In Java, a constructor is a special method that is used to initialize objects of a class. It is called automatically when an object is created using the new keyword. The purpose of a constructor is to ensure that the newly created object is properly initialized and in a valid state.
- What is an
enum
in Java?
In Java, an enum is a special data type used to define a set of constants. It represents a fixed number of predefined values, often referred to as enumeration constants or enumerators. enum provides a way to define a collection of related values that have a specific meaning or purpose.
- Explain the difference between a class and an enum.
1. Purpose:
- Class: A class is a blueprint or template for creating objects. It defines the structure, behavior, and state of objects.
- Enum: An enum is used to represent a fixed set of predefined values or constants. It provides a way to define a collection of related values.
2. Instantiation:
- Class: Objects are created from classes using the new
keyword. Each object has its own set of attributes and can have its own state.
- Enum: Enum constants are predefined and finite. They are automatically instantiated and have a fixed number of instances determined by the defined constants.
3. Extensibility:
- Class: Classes can be extended by creating subclasses that inherit the properties and behaviors of the parent class. Inheritance allows for code reuse and the creation of hierarchical relationships between classes.
- Enum: Enums cannot be extended or subclassed. The set of enum constants is fixed and cannot be modified once defined.
4. Usage:
- Class: Classes are used to model complex entities, encapsulate data and behavior, and implement various features and functionalities in an object-oriented program.
- Enum: Enums are typically used to represent a limited set of related constants or options, such as days of the week, months, status codes, or choices for an enumeration.
- Explain the difference between a class and a record.
The main difference between a class and a record in Java is that a class is a general-purpose blueprint for creating objects with customizable attributes and behaviors, while a record is a special type of class primarily used for immutable data storage and automatic generation of common methods like constructors, getters, setters, and equals()/hashCode().
- What are interfaces? Why should we use them?
Interfaces in Java are a way to define contracts or sets of rules that classes must follow. They allow for abstraction, polymorphism, multiple inheritance of types, code reusability, modularity, and facilitate API design and collaboration. Interfaces provide a foundation for designing reliable, maintainable, and flexible software systems by promoting consistent behavior and enabling interchangeable components.
- What is inheritance?
Inheritance is a mechanism in object-oriented programming where a subclass can inherit attributes and methods from a superclass. It promotes code reuse, creates a hierarchical structure, and allows for specialization and customization of behavior.
- Is multiple inheritance allowed in Java?
No, Java does not support multiple inheritance of classes, where a class can inherit from multiple parent classes.
- What is a static class member?
A static class member in Java is a variable or method that belongs to the class itself, not to individual instances. It is shared among all instances, accessed using the class name, and stored in a common memory location. It is useful for defining shared data or behavior associated with the class.
- Can a static method use non-static members?
No, a static method in Java cannot directly access or use non-static members of a class. Static methods are not associated with any particular instance of a class, so they cannot access instance-specific data or behaviors.
- What does the
final
keyword mean in Java?
In Java, the final
keyword is used to denote that a variable, method, or class is immutable or cannot be overridden or extended, depending on its usage.
- What does the
abstract
keyword mean in Java?
The abstract
keyword in Java is used to declare abstract classes and methods, providing a framework for creating related subclasses and enforcing a common structure and behavior.