JAVA Flashcards
What is JAVA?
Java is a multi-platform, object-oriented programming language.
I use it to automate functional tests to improve testing efficiency.
What are the JAVA components?
- JVM (Java Virtual Machine): An abstract machine that runs Java programs by converting bytecode to machine code.
- JRE (Java Runtime Environment): Provides libraries and JVM to run Java applications. JRE = JVM + Library Classes.
- JDK (Java Development Kit): Used for developing Java applications. JDK = JRE + Development Tools (compiler, archiver, etc.).
Can you explain the main method in JAVA?
The main method is the entry point for Java applications.
To explain each keyword:
- public: Accessible by any class.
- static: Belongs to the class, not instances.
- void: No return value.
- main: Method name, recognized by JVM.
- String[] args: Parameters passed to the main method.
What are Local, Instance, and Static variables?
- Local Variable: Defined inside a method, accessible only within it.
- Instance Variable: Defined in a class, outside methods. Each object has its own copy.
- Static Variable: Shared by all instances of a class. Defined with the static keyword.
What is the String class in JAVA?
A String represents a sequence of characters and is immutable (cannot be changed once created). Modifying a String creates a new object.
String methods can be found
here.
What is the String pool?
A special memory area where string literals are stored for memory efficiency and performance. If a string already exists in the pool, a new one is not created.
Difference between ‘==’ and .equals()
for Strings?
- ==: Compares references equality.(memory addresses).
- .equals(): Compares the content of the strings.
Difference between String, StringBuffer, and StringBuilder?
- String: Immutable, not thread-safe, good for infrequent changes.
- StringBuilder: Mutable, not thread-safe, faster for single-threaded use.
- StringBuffer: Mutable, thread-safe, slower due to synchronization, good for multi-threaded use.
What are JAVA Loops?
Loops allow repeated execution of code based on conditions:
- for: Fixed number of iterations.
- enhanced for (for-each): Iterates over arrays/collections.
- while: Repeats while condition is true.
- do-while: Executes at least once before checking the condition.
- break: Exits the loop.
- continue: Skips the current iteration.
What are JAVA Operators?
- Arithmetic: +, -, *, /, %
- Assignment: =, +=, -=, *=, /=, %=
- Comparison: >, <, >=, <=, ==, !=
- Logical: &&, ||
- Unary: ++, – (pre/post increment/decrement)
What is an if statement in JAVA?
An if statement in Java checks a condition and executes code only if the condition is true.
_Below answer is if you want give more detail.
The different if statements in Java are:
if: Executes code if the condition is true.
if-else: Executes one block if the condition is true, another if false.
else-if: Checks multiple conditions in sequence.
nested if: An if statement inside another if.
switch: Handles multiple conditions by matching values.
What is a JAVA Constructor?
A constructor initializes an object’s state when it’s created. It has the same name as the class and no return type.
Types:
- Default Constructor: No parameters, initializes to default values.
- Parameterized Constructor: Takes parameters to initialize the object.
- No-Argument Constructor: Like default, but explicitly defined.
Can you have multiple constructors in a class?
Yes, by using method overloading (same name, different parameters).
What is the meaning of the static keyword?
static keyword is used for memory management primarily with classes and methods. various uses are:
- Static Variables: Shared among all class instances.
- Static Methods: Belong to the class, can be called without creating an object.
- Static Blocks: Run once when the class is loaded, used for initialization.
Difference between a block, static block, and method?
- Block: is a group of statements enclosed within braces {}
- Static Block: is a block of code inside a class that is executed only once when the class is loaded into memory
- Method: is a collection of statements grouped together to perform an operation.
What is an Array?
An array is a fixed-size collection of elements of the same type, accessed by index starting at 0.
What is the difference between a class and object?
- Class is: Blueprint defining properties and methods.
- Object is: Instance of a class with its own state and behavior.
What is object-oriented programming?
A programming example based on objects and classes to model real-world entities and relationships.
What are the pillars of Object-Oriented Programming in JAVA?
- Encapsulation: Bundles data and methods, hides internal state.
- Inheritance: Classes inherit properties and methods from other classes.
- Polymorphism: Objects can be treated as instances of their parent class.
- Abstraction: Hides complex details, shows only essential features.
What do you know about inheritance?
-Definition: Inheritance is a key feature of Object-Oriented Programming in Java.
-Purpose: It allows superclasses and child classes to share common data and methods.
-Types of Inheritance in Java:
1.Single Inheritance
2.Multi-Level Inheritance
4.Hierarchical Inheritance
What is the purpose of inheritance?
Purpose: The purpose of inheritance is to allow code reusability.
When do you use the static keyword?
Usage: The static keyword in Java is used to create variables and methods that belong exclusively to the class.
Example:
vehicle_identification_number
Pi - public final static
Benefits:
-Maintains a single copy exclusive to the class.
-Improves memory management.
-Commonly used for constants and utility methods.
What are the access modifiers in Java?
-Public: Accessible throughout the project. (Most accessible)
-Protected: Accessible within the package and subclasses. (2nd most accessible)
-Private: Scope is only within the class. (Least accessible)
-Default: Package-level access (accessible within the same package). (3rd most accessible)