Java Fundamentals - Chapter 2 Review (Parts of a Java Program, Print/API, Variables and Literals, Primitives, Arithmetic, Conversions, final, String, Scope, Comments, Style, Input, Dialog Boxes, Common Errors)

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/36

flashcard set

Earn XP

Description and Tags

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.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

37 Terms

1
New cards

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.

2
New cards

What is the file extension for Java source files?

.java

3
New cards

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.

4
New cards

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.

5
New cards

What does the keyword public denote in a class declaration?

It is an access specifier indicating unrestricted access to the class.

6
New cards

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.

7
New cards

What is the purpose of the opening and closing braces { and } in Java programs?

They enclose the body of a class or a method.

8
New cards

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.

9
New cards

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.

10
New cards

What is the purpose of the semicolon at the end of Java statements?

It marks the end of a complete programming statement.

11
New cards

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.

12
New cards

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.

13
New cards

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.

14
New cards

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 ".

15
New cards

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.

16
New cards

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.

17
New cards

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

18
New cards

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.

19
New cards

What is the general format of a variable declaration in Java?

DataType VariableName; (ends with a semicolon) declares the variable and its type.

20
New cards

What is scope, and what are local variables?

Scope is the program region where a variable is accessible; local variables exist inside a method.

21
New cards

What are the three types of comments in Java, and how are they written?

Single-line (// …), multi-line (/* … /), and documentation comments (/* … */).

22
New cards

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.

23
New cards

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

24
New cards

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.

25
New cards

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

26
New cards

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.

27
New cards

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.

28
New cards

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.

29
New cards

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.

30
New cards

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.

31
New cards

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.

32
New cards

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.

33
New cards

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.

34
New cards

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.

35
New cards

What is the rule about matching braces and parentheses in Java?

For every opening brace or parenthesis, there must be a corresponding closing one.

36
New cards

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;.

37
New cards

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