Chapter 2 Flashcards
What must the filename match in a Java source file with a public class?
The public class name must match the filename.
What is the entry point of a Java program?
The main method.
What is the required signature of the main method in Java?
public static void main(String[] args) { }
What is the purpose of curly braces { } in Java?
Curly braces define the scope of classes and methods.
What are the two types of comments in Java?
Single-line comments (//) and multi-line comments (/* */).
What must every Java statement end with?
: A semicolon (;).
What are some Java elements that do not require semicolons?
Comments, class headers, method headers, and curly braces.
What happens if a Java file has a public class?
The class name must match the filename.
What is Java’s language case sensitivity rule?
Java is case-sensitive, meaning Variable and variable are different.
What file extension must Java source files have?
.java
What is the rule regarding braces in Java code?
For every opening brace {, there must be a closing brace }.
What does the line System.out.println(“Hello World!”); do in Java?
It prints “Hello World!” to the console
Can Java programs run without a main method?
No, every Java application must have a main method.
What should the file name be for a class named Simple in a Java program?
Simple.java
What is the console window that starts a Java application typically known as?
The console window is typically known as the standard output device.
What is the standard input device typically in Java?
The standard input device is typically the keyboard.
How does Java send information to the standard output device?
Java sends information to the standard output device by using a Java class stored in the standard Java library, which is accessed through the Java API.
What is the standard Java library commonly referred to as?
The standard Java library is commonly referred to as the Java API.
What does the line System.out.println(“Programming is great fun!”); do?
This line prints the string “Programming is great fun!” to the console and moves the cursor to the next line.
What is the System class used for in Java?
The System class provides methods and objects that perform system-level tasks, such as handling input and output.
What is the purpose of the out object in the System class?
The out object is used to handle output operations, such as printing to the console.
How do you pronounce the line System.out.println(“Programming is great fun!”);?
It is pronounced as “System dot out dot println”.
What happens when multiple println() statements are used?
When multiple println() statements are used, each one prints its output on a new line because the println() method adds a newline character.
How is the print() method different from println()?
The print() method prints the output without adding a newline character at the end, so the next output will appear on the same line.
What will this code output?
System.out.print(“These lines will be”);
System.out.print(“printed on”);
System.out.println(“the same line.”);
The output will be: “These lines will beprinted onthe same line.” because print() does not add spaces or newlines, so the words run together.
What is an escape sequence in Java?
An escape sequence is a special character used in strings to represent characters that cannot be typed directly, such as a newline character (\n).
Even though escape sequences are comprised of two characters, how are they treated by the compiler?
Escape sequences are treated by the compiler as a single character.
What is the output of the following code?
System.out.print(“These are our top sellers:\n”);
System.out.print(“\tComputer games\n\tCoffee\n “);
System.out.println(“\tAspirin”);
These are our top sellers:
Computer games
Coffee
Aspirin
What does the + operator do in Java?
The + operator can be used as both a concatenation operator (for strings) and an addition operator (for numbers).
What will this line of code output?
System.out.println(“Hello “ + “World”);
“Hello World”
Explanation: The + operator concatenates the two strings.
How can you concatenate strings and variables in Java?
You can use the + operator to concatenate strings and variables, like so:
System.out.println(“The value is: “ + 5);
How do you handle long string literals that span multiple lines in Java?
Long string literals should be concatenated with the + operator, like so:
System.out.println(“These lines are “ +
“now ok and will not “ +
“cause the error as before.”);
What is the rule for variable naming conventions in Java?
Variable names should start with a lowercase letter and use camelCase.
For example: int caTaxRate.
What is the naming convention for class names in Java?
Class names should be in Title Case, with each word starting with a capital letter.
For example: public class BigLittle.
What should Java variable names be like?
Variable names should be descriptive to make the code more readable and maintainable.
For example, double salesTaxRate is more descriptive than double tr.
What are the 8 Java primitive data types?
byte
short
int
long
float
double
boolean
char
What is the difference between integer and floating-point data types in Java?
Integer data types (e.g., byte, short, int, long) hold whole numbers, while floating-point data types (e.g., float, double) can hold numbers with decimal points.
What is the default type for floating-point literals in Java?
The default type for floating-point literals is double.
How can you assign a floating-point value to a float variable in Java?
to assign a floating-point value to a float variable, append an F or f to the literal, like this:
float number = 23.5F;
What will happen if you try to assign a double value to a float variable without casting?
You will get an error because a double value has higher precision than a float.
Can a string literal span multiple lines in Java?
No, a string literal cannot span multiple lines in Java without causing a syntax error. It must be concatenated or broken into multiple strings.
How do you format complex strings using concatenation in Java?
You can use the + operator to concatenate strings, numbers, and escape sequences, like so:
System.out.println(“The following will be printed “ +
“in a tabbed format: \n\tFirst = “ + 5 * 6 + “, “ +
“\n\tSecond = “ + (6 + 4) + “,” +
“\n\tThird = “ + 16.7 + “.”);
What is an identifier in Java?
An identifier is a programmer-defined name used for classes, variables, and methods. It cannot be a Java reserved keyword.
How should you format floating-point literals in Java?
Floating-point literals can be written in scientific notation using the letter E for exponent notation.
For example: 4.728197E4 is equivalent to 47281.97.
What happens if you try to use a currency symbol or comma in a number literal in Java?
You will get an error. Java literals cannot contain embedded currency symbols or commas.
For example:
grossPay = 1257.00; // Correct
grossPay = $1,257.00; // ERROR!
What happens when a floating-point number is represented in scientific notation in Java?
Java uses E notation to represent floating-point numbers in scientific notation.
For example: 47,281.97 is written as 4.728197E4.
What is the char data type used for in Java?
The char data type is used to represent a single character.
What are char literals enclosed in?
char literals are enclosed in single quotes (e.g., ‘a’, ‘Z’, ‘\n’).
What is the difference between char literals and String literals?
char literals are enclosed in single quotes (e.g., ‘a’), while String literals are enclosed in double quotes (e.g., “Hello”).
What two values can the boolean data type hold in Java?
The boolean data type can hold two values: true and false.
Can a boolean variable hold a value other than true or false?
No, a boolean variable can only hold the values true or false.
How are characters stored internally in Java?
Characters are stored as numbers using Unicode, which consists of 65,536 individual characters.
What is the first 256 characters of the Unicode character set compatible with?
The first 256 characters of the Unicode character set are compatible with the ASCII character set.
What is the purpose of the assignment operator = in Java?
The assignment operator = is used to assign a value to a variable. The left side must be a variable, and the right side must be a literal or an expression of compatible type.
What happens if you try to use an uninitialized local variable in Java?
Trying to use an uninitialized local variable will result in a syntax error when the code is compiled.
What is an example of a binary operator in Java?
An example of a binary operator is the arithmetic operator +, which takes two operands.
How does integer division work in Java?
When performing integer division, the result is truncated (i.e., any decimal part is discarded). For example, 1 / 2 results in 0
What is the order of operations in Java when using arithmetic expressions?
The order of operations follows standard precedence rules (parentheses first, then multiplication/division, then addition/subtraction). Parentheses are evaluated first, and operations inside them are executed before others.
What are combined assignment operators in Java?
Combined assignment operators (e.g., +=, -=, *=, /=) perform an arithmetic operation and assign the result to a variable in one step.
Why should literal values be replaced with constants in Java?
Constants make programs more readable and maintainable by avoiding magic numbers and providing a single place to update values.
How do you declare a constant in Java?
A constant is declared using the final keyword and must be initialized before use. By convention, constants are written in uppercase letters with underscores separating words.
Example: final int MAX_SPEED = 100;
What is the String class used for in Java?
The String class is used to store a sequence of characters (strings), as Java does not have a primitive data type for strings.
How can a String object be created in Java?
A String object can be created by directly assigning a string literal or using the new keyword.
Example: String message = “Hello”; or String message = new String(“Hello”);
What does it mean that strings in Java are immutable?
Strings in Java are immutable, meaning their value cannot be changed once created. Operations on strings create new string objects.
What is the scope of a local variable in Java?
The scope of a local variable starts from its declaration and ends when the method in which it was declared finishes executing.
How is a variable referencing an object different from primitive types?
A variable referencing an object stores the memory address of the object, while primitive variables store the actual value.
What is the purpose of Javadoc comments in Java?
Javadoc comments are used to generate HTML documentation for your code.
How do you generate Javadoc documentation for your Java file?
Run the javadoc program with the source file as an argument, e.g., javadoc Comment3.java. This will generate an index.html file and other related documentation files.
What are some of the whitespace characters in Java that are ignored by the compiler?
Space, tab, newline, carriage return, and form feed. These characters are ignored by the compiler but affect code readability and structure.
What is the best practice for indentation in Java code?
Use 2-4 spaces for each level of indentation and avoid tab characters, as they can vary in size between applications.
How do you read input from the keyboard in Java?
Use the Scanner class. Example:
import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
What is the purpose of the JOptionPane class in Java?
JOptionPane is used to display dialog boxes for messages or user input.
How do you display a message using JOptionPane?
Use the showMessageDialog method. Example:
JOptionPane.showMessageDialog(null, “This is a message.”);
How do you create an input dialog box with JOptionPane?
Use the showInputDialog method. Example:
String name = JOptionPane.showInputDialog(“Enter your name:”);
What happens if the user clicks the “Cancel” button in a JOptionPane input dialog?
If “Cancel” is clicked, the method returns null.
How do you stop the execution of a program when using JOptionPane?
Use System.exit(0); to stop the execution of the program.
What does the argument 0 in System.exit(0) signify?
The argument 0 signifies that the program ended successfully.
How do you convert a string to an integer in Java?
Use Integer.parseInt(String) to convert a string to an integer. Example:
int age = Integer.parseInt(userInput);
What method is used to convert a string to a double in Java?
Use Double.parseDouble(String) to convert a string to a double.
What is the advantage of using parse methods (e.g., Integer.parseInt(), Double.parseDouble()) in Java?
These methods allow you to convert strings (which may represent numbers) into numeric types like int or double.