Moment 2 Flashcards

1
Q

How to access the Scanner class?

A

Use an import declaration:

import java.util.Scanner;

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

What happens if the user enters a string when the program requires an integer?

A

An error will occur and the program will terminate.

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

A Java application is a?

A

A Java application is a computer program that executes when you use the java command to launch the Java Virtual Machine (JVM).

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

What is white space?

A

Blank lines (like line 3), space characters and tabs can make programs easier to read. Together, they’re known as white space. The compiler ignores white space.

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

What are Keywords?

A
Keywords are reserved for use by Java and are spelled with all lowercase letters. Ex: 
The "class" keyword introduces a class declaration and is immediately followed by the class name
Example code: 
"public class Welcome1 { "
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A public class must be placed in a ?

A

file that has a filename of the form ClassName.java, so class Welcome1 is stored in the file Welcome1.java.

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

What is a identifier?

A

A class name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces.

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

What is a Method?

A

Methods perform tasks and can return information when they complete their tasks.

EX: Keyword void indicates that this method will not return any information.

“public static void main(String[] args) {“
The parentheses, after the identifier main, indicate that it’s a program building block (is a method).

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

What is the “Welcome to Java Programming!” in the ex below called?

System.out.println(“Welcome to Java Programming!”);

A

a string—also known as a character string or a string literal. White-space characters in strings are not ignored by the
compiler. Strings cannot span multiple lines of code—later we’ll show how to conveniently deal with long strings.

The string in the parentheses is the method’s argument.

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

What is the “System.out.” part from the ex below called?

System.out.println(“Welcome to Java Programming!”);

A

An object.

This particular on: is known as the standard output object. It allows a program to display information in the command window from which the program executes.

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

What is a statement?

A

The entire (se ex below), including System.out.println, the argument “Welcome to Java Programming!” in the parentheses and the semicolon (;), is called a statement. A method typically contains statements that perform its task. Most statements end with a semicolon.

System.out.println(“Welcome to Java Programming!”);

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

What characters will make a string appear on several lines?

A

\n

The backslash () is an escape character, which has special meaning to System.out’s print and println methods.

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

What are the common escape sequences for a string?

A

\n Newline. Position the screen cursor at the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor at the beginning of the current
line—do not advance to the next line. Any characters output after the carriage
return overwrite the characters previously output on that line.
\ Backslash. Used to print a backslash character.
" Double quote. Used to print a double-quote character. For example,
System.out.println(“"in quotes"”);
displays “in quotes”.

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

Calling a method is also referred to as

A

invoking a method.

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

What are format specifier for println?

A

Each format specifier is a placeholder for a value and specifies the type of data to output. Format specifiers also may include optional formatting information. Format specifiers begin with a percent sign (%) followed by a character that represents the data type.

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

All Java variables must be declared with?

A

a name and a type before they can be used.

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

What is the function of a variable’s name?

A

A variable’s name enables the program to access the variable’s value in memory.

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

What is the function of a variable’s type?

A

A variable’s type specifies what kind of information is stored at that location in memory.

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

What does the variable type “Scanner” do?

A

A Scanner enables a program to read data (e.g., numbers and strings) for use in a program. The data can come from many sources, such as the user at the keyboard or a file on disk. Before using a Scanner, you must create it and specify the source of the data.

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

Why should you choose meaningful variable names?

A

Choosing meaningful variable names helps a program to be self-documenting (i.e., one can understand the program simply by reading it rather than by reading associated documentation or creating and viewing an excessive number of comments).

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

Portions of statements that contain calculations are called?

A

expressions.

In fact, an expression is any portion of a statement that has a value.

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

int sum = number1 + number2;

In the preceding statement, the addition operator is a binary operator, because?

A

it has two operands—number1 and number2.

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

A condition is an?

A

expression that can be true or false.

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

An “If” selection statement helps a program to?

A

It allows a program to make a decision based on a condition’s value.

For example, the condition “grade is greater than or equal to 60” determines whether a student passed a test. If an if statement’s condition is true, its body executes. If the condition is false, its body does not execute.

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

Conditions in if statements can be formed by using?

A

the equality operators (== and !=) and relational operators (>, = and <=)

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

What is the meaning of “x != y”

A

x is not equal to y

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

What is the meaning of “x == y”

A

x is equal to y

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

What is the meaning of “x >= y”

A

x is greater than or equal to y

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

A syntax error occurs when?

A

the compiler encounters code that violates Java’s language rules. It’s similar to a grammar error in a natural language.

(p. 89)

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

Blank lines, space characters and tab characters are known as?

A
white space (p. 90).
White space makes programs easier to read and is normally ignored by the compiler.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

By convention, all class names in Java begin with a?

A

capital letter and capitalize the first letter of each word they include (e.g., SampleClassName).

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

A public class declaration must be saved in a?

A

file with the same name as the class followed by the “.java” filename extension.

(p. 90)

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

Java is case sensitive, which means?

A

It’s uppercase and lowercase letters are distinct.

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

The body of every class declaration is delimited by?

A

braces, { and }.

p. 91

35
Q

Method main is the starting point of every Java application and must begin with

A

public static void main(String[] args)
otherwise, the JVM will not execute the application.
(p. 91)

36
Q

Keyword void indicates?

A

that a method will perform a task but return no information.

p. 91

37
Q

Statements instruct the computer to?

A

perform actions.

38
Q

You compile a program with the command?

A

javac.

If the program contains no syntax errors, a class file (p. 93) containing the Java bytecodes that represent the application is created. These bytecodes are interpreted by the JVM when you execute the program.

39
Q

To run an application, type?

A

“java” followed by the name of the class that contains method main.

40
Q

System.out.print displays?

A

its argument and positions the output cursor immediately after the last character displayed.
(p. 94)

41
Q

A backslash () in a string is an?

A

escape character.
Java combines it with the next character to form an escape sequence —\n represents the newline character.

(p. 95)

42
Q

System.out.printf method displays

A

formatted data.

p. 96; f means “formatted”

43
Q

Method printf’s first argument is a?

A

format string containing fixed text and/or format specifiers. Each format specifier indicates the type of data to output and is a placeholder for a corresponding argument that appears after the format string.

(p. 96)

44
Q

Format specifiers begin with a?

A

percent sign (%) and are followed by a character that represents the data type. The format specifier %s is a placeholder for a string.

(p. 97)

45
Q

The %n format specifier is a?

A

portable line separator.
You cannot use %n in the argument to System.out.print or System.out.println; however, the line separator output by System. out.println after it displays its argument is portable across operating systems.
(p. 97)

46
Q

An import declaration helps the compiler locate a

A

class that’s used in a program.

p. 98

47
Q

A variable’s name enables the program to?

A

access the variable’s value in memory.

48
Q

A Scanner enables a program to?

A

read data that the program will use.
Before a Scanner can be used, the program must create it and specify the source of the data.
(package java.util; p. 98)

49
Q

The expression new Scanner(System.in) creates a?

A

Scanner that reads from the standard input object (System.in; p. 99)—normally the keyboard.

50
Q

What is a prompt?

A

A prompt directs the user to take a specific action.

(p. 99)

51
Q

The int values you use in a program may not contain commas; however, for readability, you can use?

A

You can place underscores in numbers (e.g., 60_000_000).

52
Q

The format specifier %d is a?

A

placeholder for an int value.

p. 100

53
Q

The assignment operator, = , enables the program to?

A

give a value to a variable.

p. 101

54
Q

The arithmetic operators are?

A
\+ (addition)
- (subtraction) 
* (multiplication)
/ (division)
% (remainder) 
(p. 102)
55
Q

Arithmetic expressions must be written in?

A

straight-line form

p. 103

56
Q

An “if” statement begins with keyword if followed by?

A

a condition in parentheses and expects one statement in its body. You must include braces around multiple-statement bodies.

57
Q

A(n)__________ and a(n)_________ begin and end the body of every method.

A

left brace ({), right brace (})

58
Q

You can use the_______________ statement to make decisions.

A

if

59
Q

_________begins an end-of-line comment.

A

//

60
Q

__________ , ____________ and __________ are called white space.

A

Space characters, newlines and tabs.

61
Q

_________________are reserved for use by Java.

A

Keywords

62
Q

Java applications begin execution at method ___________________ .

A

main

63
Q

Methods ______________ , _____________ and _______________ display information in a command window.

A

System.out.print,
System.out.println
System.out.printf.

64
Q

State whether each of the following is true or false. If false, explain why.

Comments cause the computer to display the text after the // on the screen when the program executes.

A

False. Comments do not cause any action to be performed when the program executes.
They’re used to document programs and improve their readability.

65
Q

State whether each of the following is true or false. If false, explain why.

All variables must be given a type when they’re declared.

A

True.

66
Q

State whether each of the following is true or false. If false, explain why.

Java considers the variables number and NuMbEr to be identical.

A

False. Java is case sensitive, so these variables are distinct.

67
Q

State whether each of the following is true or false. If false, explain why.

The remainder operator (%) can be used only with integer operands.

A

False. The remainder operator can also be used with noninteger operands in Java.

68
Q

State whether each of the following is true or false. If false, explain why.

The arithmetic operators *, /, %, + and - all have the same level of precedence.

A

False. The operators *, / and % have higher precedence than operators + and -.

69
Q

State whether each of the following is true or false. If false, explain why.

The identifier _ (underscore) is valid in Java 9.

A

False. As of Java 9, _ (underscore) by itself is no longer a valid identifier.

70
Q

Write statements to accomplish each of the following tasks:

Declare variables c, thisIsAVariable, q76354 and number to be of type int and initialize each to 0.

A

int c = 0;
int thisIsAVariable = 0;
int q76354 = 0;
int number = 0;

71
Q

Write statements to accomplish each of the following task.

Prompt the user to enter an integer.

A

System.out.print(“Enter an integer: “);

72
Q

Write statements to accomplish each of the following tasks:

Input an integer and assign the result to int variable value. Assume Scanner variable input can be used to read a value from the keyboard.

A

int value = input.nextInt();

73
Q

Write statements to accomplish each of the following tasks:

Print “This is a Java program” on one line in the command window. Use method System.out.println.

A

System.out.println(“This is a Java program”);

74
Q

Write statements to accomplish each of the following tasks:

Print “This is a Java program” on two lines in the command window. The first line should end with Java. Use method System.out.printf and two %s format specifiers.

A

System.out.printf(“%s%n%s%n”, “This is a Java”, “program”);

75
Q

Write statements to accomplish each of the following tasks:

If the variable number is not equal to 7, display “The variable number is not equal to 7”.

A

if (number != 7) {
System.out.println(“The variable number is not equal to 7”);
}

76
Q

Identify and correct the errors in each of the following statements:

if (c < 7); {
System.out.println(“c is less than 7”);
}

A

Error: Semicolon after the right parenthesis of the condition (c < 7) in the if. As a result, the output statement executes regardless of whether the condition in the if is true.
Correction: Remove the semicolon after the right parenthesis.

77
Q

Identify and correct the errors in each of the following statements:

if (c => 7) {
System.out.println(“c is equal to or greater than 7”);
}

A

Error: The relational operator => is incorrect.
Correction: Change => to >=.

78
Q

Write declarations, statements or comments that accomplish each of the following tasks:

State that a program will calculate the product of three integers.

A

// Calculate the product of three integers

79
Q

Write declarations, statements or comments that accomplish each of the following tasks:

Create a Scanner called input that reads values from the standard input.

A

Scanner input = new Scanner(System.in);

80
Q

Write declarations, statements or comments that accomplish each of the following tasks:

Prompt the user to enter the first integer.

A

System.out.print(“Enter first integer: “);

81
Q

Write declarations, statements or comments that accomplish each of the following tasks:

Read the first integer from the user and store it in the int variable x.

A

int x = input.nextInt();

82
Q

Write declarations, statements or comments that accomplish each of the following tasks:

Compute the product of the three integers contained in variables x, y and z, and store the result in the int variable result.

A

int result = x * y * z;

83
Q

Write declarations, statements or comments that accomplish each of the following tasks:

Use System.out.printf to display the message “Product is” followed by the value of the variable result.

A

System.out.printf(“Product is %d%n”, result);