1/36
A set of practice flashcards covering core concepts from Java Fundamentals Chapter 2: program structure, printing, variables and literals, primitive data types, arithmetic operators, type conversion, final constants, the String class, scope, comments, style, input, dialog boxes, and common errors.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the purpose of Code Listing 2-1 (Simple.java) in 2.1 The Parts of a Java Program?
It shows a simple Java program with a class, a main method, and a System.out.println call that prints a message.
What is the file extension for Java source files?
.java
What command compiles a Java source file named Simple.java, and what file is produced?
javac Simple.java creates Simple.class, which contains bytecode; the program is run with java Simple.
In Java, what does the class header public class Simple signify?
It marks the beginning of a class definition; Simple is the class name; public is an access specifier.
What does the keyword public denote in a class declaration?
It is an access specifier indicating unrestricted access to the class.
What is a method header, and what does public static void main(String[] args) define?
It marks the start of a method; main is the entry point; public, static, void are modifiers; (String[] args) is the parameter list.
What is the purpose of the opening and closing braces { and } in Java programs?
They enclose the body of a class or a method.
What is a string literal and how is it printed in the example?
A sequence of characters in double quotes; System.out.println prints the string without quotation marks.
What are the opening and closing braces used for in Java, and why must they match?
They define blocks (class or method bodies); every opening brace must have a corresponding closing brace.
What is the purpose of the semicolon at the end of Java statements?
It marks the end of a complete programming statement.
What is the difference between the print and println methods?
print outputs text without advancing to a new line; println outputs text and moves to the next line.
What is an escape sequence, and give examples used to control output?
A backslash followed by a character that has special meaning; e.g., \n for newline, \t for tab.
How is System.out.println related to the System class and its out object?
System is a Java API class; out is a static member of System; println is a method of out that prints to the console.
What is a literal in Java, and what types of literals are mentioned in the notes?
A value written in code; examples include integer literals like 20 and string literals like "Today we sold ".
What is an identifier in Java, and what are naming conventions for variables and classes?
A programmer-defined name; variables use lowerCamelCase, classes use UpperCamelCase.
What does the final keyword do, and what is a named constant?
final declares a constant whose value cannot be changed; named constants are often in uppercase.
What is Math.PI and where does it come from?
A predefined named constant in the Math class representing the value of pi (approximately 3.14159…).
What is the difference between primitive data types and the String class in Java?
Primitive types hold actual values; String is a class type that holds a reference to a String object.
What is the general format of a variable declaration in Java?
DataType VariableName; (ends with a semicolon) declares the variable and its type.
What is scope, and what are local variables?
Scope is the program region where a variable is accessible; local variables exist inside a method.
What are the three types of comments in Java, and how are they written?
Single-line (// …), multi-line (/* … /), and documentation comments (/* … */).
What is the purpose of documentation comments and how do you generate HTML documentation from them?
They describe code for javadoc; run javadoc on the source file to generate HTML docs.
What are the combined assignment operators, and what do they do?
Operators like +=, -=, *=, /=, %= combine assignment with arithmetic (e.g., x += 5 is x = x + 5).
What is widening conversion and narrowing conversion in Java?
Widening is automatic conversion to a larger type; narrowing is conversion to a smaller type and may lose data; use cast to narrow.
When both operands of the division operator are integers, what type of division is performed?
Integer division; the fractional part is discarded unless one operand is floating-point (e.g., 5.0/2).
What is the difference between a string literal and a string variable in Java?
A string literal is a sequence in double quotes; a string variable holds a reference to a String object.
How do you read keyboard input in Java using the Scanner class?
Import java.util.Scanner; declare Scanner keyboard = new Scanner(System.in); then use methods like nextInt, nextLine, etc.
What issue arises when mixing nextLine with nextInt/nextDouble, and how can you fix it?
After reading a number, a newline remains in the buffer; fix by consuming it with an extra nextLine or by input order.
What is JOptionPane used for, and what are showMessageDialog and showInputDialog?
JOptionPane provides dialog boxes; showMessageDialog shows a message; showInputDialog prompts for input and returns a String.
What does System.exit(0) do, and what caution should you exercise with it?
It terminates the JVM; can disrupt program flow, so use only when necessary.
How do you convert a String to a number in Java using wrapper classes?
Use methods like Integer.parseInt or Double.parseDouble on the string.
What String class methods are highlighted in the notes, such as length, charAt, toLowerCase, and toUpperCase?
length returns the number of characters; charAt(index) returns a char at position; toLowerCase() and toUpperCase() return new strings.
What are the rules and examples for valid identifiers in Java?
Identifiers start with a letter, underscore, or $, and may contain letters, digits, underscores, or $. Case-sensitive; no spaces.
What is a self-documenting program and why is it important?
Code with clear naming and structure that communicates its purpose; aids readability and maintenance.
What is the rule about matching braces and parentheses in Java?
For every opening brace or parenthesis, there must be a corresponding closing one.
What is the role of the import statement when using Scanner and when using JOptionPane?
Import statements bring in library classes; e.g., import java.util.Scanner; or import javax.swing.JOptionPane;.
What is the difference between a primitive type and a class type when storing data?
Primitives hold the actual value; class types hold references to objects (e.g., String).