test2 Flashcards
What is the Java Virtual Machine (JVM) and what role does it play?
The JVM is an abstract machine that executes Java bytecode. It provides platform independence, memory management (via garbage collection), security, and manages runtime processes.
What is the Java Runtime Environment (JRE) and how does it differ from the JVM and JDK?
The JRE contains the JVM and the core libraries required to run Java applications, whereas the JDK (Java Development Kit) includes the JRE along with development tools like the compiler and debugger.
What is the entry point of a Java program?
The entry point is the main method, defined as: public static void main(String[] args) (or with varargs: public static void main(String… args)).
What is a class in Java?
A class is a blueprint that defines the structure (fields) and behavior (methods) for objects. It encapsulates data and functions to operate on that data.
What is an object in Java?
An object is an instance of a class that occupies memory and has a state (via instance variables) and behavior (via methods).
What are the main components of a Java class?
The components include fields (instance and static variables), methods, constructors, nested classes/interfaces, and static initialization blocks.
What types of variables exist in Java?
There are instance variables (fields), local variables (declared within methods or blocks), and class (static) variables, which are shared among all instances.
What are the default values for uninitialized instance variables?
Numeric types default to 0 (or 0.0), boolean defaults to false, char defaults to \u0000 (null character), and object references default to null.
What are the eight primitive data types in Java?
byte, short, int, long, float, double, char, and boolean.
What are wrapper classes and why are they important?
Wrapper classes (e.g., Integer, Double) provide object representations for primitives. They allow primitives to be used in collections and provide utility methods for conversion and manipulation.
What are autoboxing and unboxing?
Autoboxing is the automatic conversion of a primitive to its corresponding wrapper object, while unboxing converts a wrapper object back to its primitive type.
What kinds of operators exist in Java?
Java operators include arithmetic (+, -, *, /, %), relational (==, !=, >, <, >=, <=), logical (&&,
How does integer division behave in Java?
Integer division truncates any fractional part. For example, 7 / 2 yields 3.
What is the difference between == and equals() when comparing objects?
== checks for reference equality (same memory location), while equals() typically checks for logical equality (same content), as defined by the class’s implementation.
How do you perform type casting in Java and what happens when you cast a double to an int?
Implicit casting occurs automatically when converting a smaller to a larger type; explicit casting is needed for incompatible types. Casting a double to an int truncates the decimal portion.
What are the primary control flow statements in Java?
Control flow statements include conditionals (if, if-else, switch), loops (for, while, do-while), and branch statements (break, continue, return).
How do if/else statements work in Java?
They evaluate a boolean condition and execute a block if true; an optional else block executes if false. Multiple conditions can be chained with else if.
How does a switch statement operate, and what types can be used?
A switch statement evaluates an expression and executes the matching case block. Since Java 7, switch statements can work with strings, as well as integral types and enums.
When is a for loop preferred and what is its syntax?
A for loop is preferred when the number of iterations is known. Syntax: for (initialization; condition; update) { /* code */ }
What is the difference between a for loop and a while loop?
A while loop checks its condition before executing the loop body, making it ideal when the iteration count is unknown.
What is unique about the do-while loop?
The do-while loop executes the loop body at least once before evaluating the condition.
How can an infinite loop occur and why might it be used?
An infinite loop (e.g., while(true)) never terminates. It can be used in server applications or event loops where the program must continuously run until explicitly stopped.
What are the purposes of the break and continue statements in loops?
break exits the loop immediately, while continue skips the remainder of the current iteration and proceeds with the next iteration.
What is a method in Java and why are methods important?
A method is a block of code that performs a specific task. Methods promote modularity, code reuse, and readability.
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
System.out.printf("%4d", i * j);
}
System.out.println();
}
@Override
public String toString() {
return "MyClass details...";
}