JAVA Flashcards

1
Q

What is JAVA?

A

Java is a multi-platform, object-oriented programming language.

I use it to automate functional tests to improve testing efficiency.

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

What are the JAVA components?

A
  • 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.).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can you explain the main method in JAVA?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are Local, Instance, and Static variables?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the String class in JAVA?

A

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.

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

What is the String pool?

A

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.

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

Difference between ‘==’ and .equals() for Strings?

A
  • ==: Compares references equality.(memory addresses).
  • .equals(): Compares the content of the strings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Difference between String, StringBuffer, and StringBuilder?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are JAVA Loops?

A

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.

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

What are JAVA Operators?

A
  • Arithmetic: +, -, *, /, %
  • Assignment: =, +=, -=, *=, /=, %=
  • Comparison: >, <, >=, <=, ==, !=
  • Logical: &&, ||
  • Unary: ++, – (pre/post increment/decrement)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an if statement in JAVA?

A

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.

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

What is a JAVA Constructor?

A

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.

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

Can you have multiple constructors in a class?

A

Yes, by using method overloading (same name, different parameters).

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

What is the meaning of the static keyword?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Difference between a block, static block, and method?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an Array?

A

An array is a fixed-size collection of elements of the same type, accessed by index starting at 0.

17
Q

What is the difference between a class and object?

A
  • Class is: Blueprint defining properties and methods.
  • Object is: Instance of a class with its own state and behavior.
18
Q

What is object-oriented programming?

A

A programming example based on objects and classes to model real-world entities and relationships.

19
Q

What are the pillars of Object-Oriented Programming in JAVA?

A
  1. Encapsulation: Bundles data and methods, hides internal state.
  2. Inheritance: Classes inherit properties and methods from other classes.
  3. Polymorphism: Objects can be treated as instances of their parent class.
  4. Abstraction: Hides complex details, shows only essential features.
20
Q

What do you know about inheritance?

A

-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

21
Q

What is the purpose of inheritance?

A

Purpose: The purpose of inheritance is to allow code reusability.

22
Q

When do you use the static keyword?

A

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.

23
Q

What are the access modifiers in Java?

A

-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)