exam review Flashcards
What type of browser is required to run applets?
Applets run in a Java-enabled web browser.
How does Java treat case sensitivity?
Java is case sensitive; identifiers like variable, method, and class names must be written with consistent letter casing.
How is Java generally categorized in terms of execution?
Java is known as an interpreted language because it compiles to byte code and then is interpreted (or just-in-time compiled) by the JVM.
What does the term “statement” mean in Java programming?
A statement is a coding instruction that can span multiple lines and ends with a semicolon.
How is data stored in memory, particularly for variables?
Data is stored in memory, where a variable name represents a memory location (e.g., int var1 = 0; stores 0 in a memory address and var1 represents that address). Declarations (e.g., int var;) and assignments (e.g., var = 0;) are separate operations.
What is the file extension for Java source code and what does it contain?
Java source code uses the .java extension and contains one or more classes. Only one public class is allowed, and it must have the same name as the file.
What file is produced after compiling Java source code and what is checked at that stage?
The compiler produces .class files (byte code) and checks for syntax errors during compilation.
How does the JVM execute a Java program?
The JVM interprets the .class (byte code) file and executes the code by emulating a microprocessor since the CPU cannot run byte code directly.
What is mandatory for every Java application?
Every Java application must have a main method, although not every class needs one.
What are three important Java keywords and their roles?
public (accessible to the JVM and other classes), static (belongs to the class rather than an instance), and void (indicates no return value).
What are the main characters/symbols used in Java and what are they for?
- // for single line comments
- /* … */ for block comments
- /** … */ for Javadoc comments
- () for method headers and conditionals
- {} to enclose blocks (classes, methods, loops)
- ”” for strings and ‘’ for chars
- ; to end a statement
What are some essential escape sequences in Java?
- \t for a tab
- \b for backspace
- \r for carriage return
- \ to print a backslash
- ' or " to print quotes
What does the standard Java library provide?
It is a collection of pre-built classes accessed through the Java API. For example, the System class provides methods like System.out.println() and System.out.printf(), and the Math class provides constants (PI, E) and methods for trigonometry, exponentiation, rounding, etc.
How do you import and use the Scanner and Random classes from java.util?
Use Scanner to read input: e.g., Scanner keyboard = new Scanner(System.in); and call methods like .next() or .nextLine() (with char ch = s1.charAt(0); for a character). Use Random to generate numbers: e.g., Random rand = new Random(); int num = rand.nextInt(n); where n is the upper bound (non-inclusive).
Can string literals span multiple lines in Java?
No, string literals cannot span lines unless you break them up and concatenate multiple strings.
What are identifiers and what are the rules for them?
Identifiers (names for classes, methods, variables) may contain letters, numbers, underscores, and dollar signs, must not start with a number, are case sensitive, and cannot include spaces.
What are the primitive data types in Java?
Primitive data types include byte, short, int, long (1/2/4/8 bytes respectively), float and double (4/8 bytes with about 7/15 decimal precision), boolean, and char.
What is a special rule for assigning literals to long and float types?
For a long literal, you must append l or L (e.g., 100L). For a float literal, append f or F (e.g., 23.5f) since decimal literals default to double.
How are characters stored in Java?
Characters (char) are stored as numbers (Unicode), using 2 bytes per char. The first 256 characters are compatible with ASCII.
What are the three variable types based on their scope in Java?
- Local variables: declared inside a method/block (no default value).
- Instance variables: non-static variables declared in a class (unique to each object).
- Class variables: declared as static, shared across all instances of the class.
What happens during integer division in Java?
Integer division truncates decimals. For example, 1/2 evaluates to 0; to get a floating-point result, you must cast one operand to a float/double or use a literal with a decimal.
What is casting and how does it differ between implicit and explicit casting?
Implicit casting (widening conversion) automatically converts to a larger type (e.g., int to long). Explicit casting (narrowing conversion) converts to a smaller type and may cause data loss (e.g., casting a double to an int truncates the decimal portion).
How can you convert between strings and numbers in Java?
- To convert a string to a number, use methods like Integer.parseInt(string).
- To convert a number to a string, you can concatenate it with an empty string (e.g., number + “”).
How are objects referenced compared to primitive data types?
Variables that reference objects store the memory address of the object, whereas primitives hold the actual value.
case condition:
// statements
break;
…
default:
// default statements
} Test conditions can be of type char, byte, short, int, or String.