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