Chapter 2 Flashcards

1
Q

What must the filename match in a Java source file with a public class?

A

The public class name must match the filename.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the entry point of a Java program?

A

The main method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the required signature of the main method in Java?

A

public static void main(String[] args) { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of curly braces { } in Java?

A

Curly braces define the scope of classes and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two types of comments in Java?

A

Single-line comments (//) and multi-line comments (/* */).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What must every Java statement end with?

A

: A semicolon (;).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some Java elements that do not require semicolons?

A

Comments, class headers, method headers, and curly braces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if a Java file has a public class?

A

The class name must match the filename.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Java’s language case sensitivity rule?

A

Java is case-sensitive, meaning Variable and variable are different.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What file extension must Java source files have?

A

.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the rule regarding braces in Java code?

A

For every opening brace {, there must be a closing brace }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the line System.out.println(“Hello World!”); do in Java?

A

It prints “Hello World!” to the console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can Java programs run without a main method?

A

No, every Java application must have a main method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What should the file name be for a class named Simple in a Java program?

A

Simple.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the console window that starts a Java application typically known as?

A

The console window is typically known as the standard output device.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the standard input device typically in Java?

A

The standard input device is typically the keyboard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does Java send information to the standard output device?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the standard Java library commonly referred to as?

A

The standard Java library is commonly referred to as the Java API.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does the line System.out.println(“Programming is great fun!”); do?

A

This line prints the string “Programming is great fun!” to the console and moves the cursor to the next line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the System class used for in Java?

A

The System class provides methods and objects that perform system-level tasks, such as handling input and output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the purpose of the out object in the System class?

A

The out object is used to handle output operations, such as printing to the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you pronounce the line System.out.println(“Programming is great fun!”);?

A

It is pronounced as “System dot out dot println”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What happens when multiple println() statements are used?

A

When multiple println() statements are used, each one prints its output on a new line because the println() method adds a newline character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How is the print() method different from println()?

A

The print() method prints the output without adding a newline character at the end, so the next output will appear on the same line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What will this code output?

System.out.print(“These lines will be”);
System.out.print(“printed on”);
System.out.println(“the same line.”);

A

The output will be: “These lines will beprinted onthe same line.” because print() does not add spaces or newlines, so the words run together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is an escape sequence in Java?

A

An escape sequence is a special character used in strings to represent characters that cannot be typed directly, such as a newline character (\n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Even though escape sequences are comprised of two characters, how are they treated by the compiler?

A

Escape sequences are treated by the compiler as a single character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

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

A

These are our top sellers:
Computer games
Coffee
Aspirin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What does the + operator do in Java?

A

The + operator can be used as both a concatenation operator (for strings) and an addition operator (for numbers).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What will this line of code output?

System.out.println(“Hello “ + “World”);

A

“Hello World”
Explanation: The + operator concatenates the two strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How can you concatenate strings and variables in Java?

A

You can use the + operator to concatenate strings and variables, like so:

System.out.println(“The value is: “ + 5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How do you handle long string literals that span multiple lines in Java?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is the rule for variable naming conventions in Java?

A

Variable names should start with a lowercase letter and use camelCase.
For example: int caTaxRate.

34
Q

What is the naming convention for class names in Java?

A

Class names should be in Title Case, with each word starting with a capital letter.
For example: public class BigLittle.

35
Q

What should Java variable names be like?

A

Variable names should be descriptive to make the code more readable and maintainable.
For example, double salesTaxRate is more descriptive than double tr.

36
Q

What are the 8 Java primitive data types?

A

byte
short
int
long
float
double
boolean
char

37
Q

What is the difference between integer and floating-point data types in Java?

A

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.

38
Q

What is the default type for floating-point literals in Java?

A

The default type for floating-point literals is double.

39
Q

How can you assign a floating-point value to a float variable in Java?

A

to assign a floating-point value to a float variable, append an F or f to the literal, like this:

float number = 23.5F;

40
Q

What will happen if you try to assign a double value to a float variable without casting?

A

You will get an error because a double value has higher precision than a float.

41
Q

Can a string literal span multiple lines in Java?

A

No, a string literal cannot span multiple lines in Java without causing a syntax error. It must be concatenated or broken into multiple strings.

42
Q

How do you format complex strings using concatenation in Java?

A

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

43
Q

What is an identifier in Java?

A

An identifier is a programmer-defined name used for classes, variables, and methods. It cannot be a Java reserved keyword.

44
Q

How should you format floating-point literals in Java?

A

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.

45
Q

What happens if you try to use a currency symbol or comma in a number literal in Java?

A

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!

46
Q

What happens when a floating-point number is represented in scientific notation in Java?

A

Java uses E notation to represent floating-point numbers in scientific notation.
For example: 47,281.97 is written as 4.728197E4.

47
Q

What is the char data type used for in Java?

A

The char data type is used to represent a single character.

48
Q

What are char literals enclosed in?

A

char literals are enclosed in single quotes (e.g., ‘a’, ‘Z’, ‘\n’).

49
Q

What is the difference between char literals and String literals?

A

char literals are enclosed in single quotes (e.g., ‘a’), while String literals are enclosed in double quotes (e.g., “Hello”).

50
Q

What two values can the boolean data type hold in Java?

A

The boolean data type can hold two values: true and false.

51
Q

Can a boolean variable hold a value other than true or false?

A

No, a boolean variable can only hold the values true or false.

52
Q

How are characters stored internally in Java?

A

Characters are stored as numbers using Unicode, which consists of 65,536 individual characters.

53
Q

What is the first 256 characters of the Unicode character set compatible with?

A

The first 256 characters of the Unicode character set are compatible with the ASCII character set.

54
Q

What is the purpose of the assignment operator = in Java?

A

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.

55
Q

What happens if you try to use an uninitialized local variable in Java?

A

Trying to use an uninitialized local variable will result in a syntax error when the code is compiled.

56
Q

What is an example of a binary operator in Java?

A

An example of a binary operator is the arithmetic operator +, which takes two operands.

57
Q

How does integer division work in Java?

A

When performing integer division, the result is truncated (i.e., any decimal part is discarded). For example, 1 / 2 results in 0

58
Q

What is the order of operations in Java when using arithmetic expressions?

A

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.

59
Q

What are combined assignment operators in Java?

A

Combined assignment operators (e.g., +=, -=, *=, /=) perform an arithmetic operation and assign the result to a variable in one step.

60
Q

Why should literal values be replaced with constants in Java?

A

Constants make programs more readable and maintainable by avoiding magic numbers and providing a single place to update values.

61
Q

How do you declare a constant in Java?

A

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;

62
Q

What is the String class used for in Java?

A

The String class is used to store a sequence of characters (strings), as Java does not have a primitive data type for strings.

63
Q

How can a String object be created in Java?

A

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

64
Q

What does it mean that strings in Java are immutable?

A

Strings in Java are immutable, meaning their value cannot be changed once created. Operations on strings create new string objects.

65
Q

What is the scope of a local variable in Java?

A

The scope of a local variable starts from its declaration and ends when the method in which it was declared finishes executing.

66
Q

How is a variable referencing an object different from primitive types?

A

A variable referencing an object stores the memory address of the object, while primitive variables store the actual value.

67
Q

What is the purpose of Javadoc comments in Java?

A

Javadoc comments are used to generate HTML documentation for your code.

68
Q

How do you generate Javadoc documentation for your Java file?

A

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.

69
Q

What are some of the whitespace characters in Java that are ignored by the compiler?

A

Space, tab, newline, carriage return, and form feed. These characters are ignored by the compiler but affect code readability and structure.

70
Q

What is the best practice for indentation in Java code?

A

Use 2-4 spaces for each level of indentation and avoid tab characters, as they can vary in size between applications.

71
Q

How do you read input from the keyboard in Java?

A

Use the Scanner class. Example:

import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);

72
Q

What is the purpose of the JOptionPane class in Java?

A

JOptionPane is used to display dialog boxes for messages or user input.

73
Q

How do you display a message using JOptionPane?

A

Use the showMessageDialog method. Example:

JOptionPane.showMessageDialog(null, “This is a message.”);

74
Q

How do you create an input dialog box with JOptionPane?

A

Use the showInputDialog method. Example:

String name = JOptionPane.showInputDialog(“Enter your name:”);

75
Q

What happens if the user clicks the “Cancel” button in a JOptionPane input dialog?

A

If “Cancel” is clicked, the method returns null.

76
Q

How do you stop the execution of a program when using JOptionPane?

A

Use System.exit(0); to stop the execution of the program.

77
Q

What does the argument 0 in System.exit(0) signify?

A

The argument 0 signifies that the program ended successfully.

78
Q

How do you convert a string to an integer in Java?

A

Use Integer.parseInt(String) to convert a string to an integer. Example:

int age = Integer.parseInt(userInput);

79
Q

What method is used to convert a string to a double in Java?

A

Use Double.parseDouble(String) to convert a string to a double.

80
Q

What is the advantage of using parse methods (e.g., Integer.parseInt(), Double.parseDouble()) in Java?

A

These methods allow you to convert strings (which may represent numbers) into numeric types like int or double.