Java Chapter 2 – Flashcards

Unlock all answers in this set

Unlock answers
question
Java provides simple data types for representing integers, real numbers, characters, and Boolean types. These types are known as _ or fundamental types.
answer
primitive data types
question
Java provides simple data types for representing integers, real numbers, characters, and Boolean types. These types are known as primitive data types or _.
answer
fundamental types
question
Real numbers (i.e., numbers with a decimal point) are represented using a method known as _ in computers.
answer
floating-point
question
So, the real numbers are also called _
answer
floating-point numbers
question
The _ word _ indicates that radius and area are floating-point values stored in the computer.
answer
reserved, double
question
The method of reviewing how a program works is called _; helpful for understanding how programs work, and they are useful tools for finding errors in programs.
answer
tracing a program
question
The plus sign (+) has two meanings: one for addition and the other for concatenating (combining) strings. The plus sign for concatenating is called a _.
answer
string concatenation operator
question
Java uses System.out to refer to the standard output device and System.in to the standard input device. By default, the output device is the display monitor and the input device is the _.
answer
keyboard
question
The syntax _ creates an object of the Scanner type. The syntax Scanner input declares that input is a variable whose type is Scanner.
answer
new Scanner(System.in)
question
The syntax __ declares that input is a variable whose type is Scanner.
answer
Scanner input
question
An object may invoke its methods. To invoke a method on an object is to ask the object to _.
answer
perform a task
question
This statement reads a number from the keyboard and assigns the number to radius.
answer
double radius = input.nextDouble();
question
Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in, as follows:
answer
Scanner input = new Scanner(System.in);
question
Import the class Scanner (use the code instruction)
answer
import java.util.Scanner;
question
System.out.print("Enter a number for radius: "); Now enter the code to except user Radius entry:
answer
double radius = input.nextDouble();
question
System.out.print("Enter a number for radius: ") is known as a _
answer
prompt
question
_ moves to the beginning of the next line after displaying the string, but _ does not advance to the next line when completed.
answer
println, print
question
The Scanner class is in the __.
answer
java.util package
question
There are two types of import statements: _ import and _ import
answer
specific, wildcard
question
The specific import specifies _ in the import statement.
answer
a single class
question
The _ import imports all the classes in a package by using the asterisk as the wildcard.
answer
wildcard
question
For example, the following statement imports all the classes from the package java.util.
answer
import java.util.*;
question
The information for the classes in an imported package is not read in at compile time or runtime unless the class is used in the program. The import statement simply tells the compiler where to locate the classes. There is _ between a specific import and a wildcard import declaration.
answer
no performance difference
question
Line 9 prompts the user to enter three numbers. The numbers are read in lines 10-12. You may enter three numbers _, then press the Enter key, or enter each number followed by a press of the Enter key, as shown in the sample runs of this program.
answer
separated by spaces
question
An identifier must start with a letter, an underscore (_), or _. It cannot start with _.
answer
$, a digit
question
An identifier cannot be a _
answer
reserved word
question
An identifier cannot be true, false, or _.
answer
null
question
An identifier _ be of any length.
answer
can
question
An _ is a sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($).
answer
identifier
question
Identifiers are the names that identify the elements such as _, and variables in a program.
answer
classes, methods
question
$2, ComputeArea, area, radius, and print, 2A and d+4. Which would be detected by the compiler as illegal identifiers?
answer
2A, d+4
question
area, Area, and AREA are all _.
answer
different identifiers (Java is case sensitive)
question
By convention, the _ character should be used only in mechanically generated source code
answer
$
question
The _ tells the compiler to allocate appropriate memory space for the variable based on its data type.
answer
variable declaration
question
The syntax for declaring a variable is __
answer
datatype variableName;
question
Declare count to be an integer variable
answer
int count;
question
Declare radius to be a double variable
answer
double radius;
question
Declare interestRate to be a double variable
answer
double interestRate;
question
Declare i, j, and k as int variables on one line
answer
int i, j, k;
question
draw with one line: int count; count = 1;
answer
int count = 1;
question
draw with one line: int i = 1, int j = 2;
answer
int i = 1, j = 2;
question
Every variable has a _; the part of the program where the variable can be referenced.
answer
scope
question
After a variable is declared, you can assign a value to it by using an __.
answer
assignment statement
question
In Java, the equal sign =() is used as the __.
answer
assignment operator
question
The syntax for assignment statements is as follows:
answer
variable = expression;
question
An _ represents a computation involving values, variables, and operators that, taking them together, evaluates to a value
answer
expression
question
In mathematics, x = 2 * x + 1 denotes an equation. However, in Java, x = 2 * x + 1 is an __ that evaluates the expression 2 * x + 1 and _ the result to x.
answer
assignment statement, assigns
question
In Java, an assignment statement is essentially an expression that evaluates to the value to be assigned to the variable on the left side of the assignment operator. For this reason, an assignment statement is also known as an _.
answer
assignment expression
question
A _ is an identifier that represents a permanent value.
answer
named constant
question
syntax for declaring a constant
answer
final datatype CONSTANTNAME = value;
question
The word _ is a Java keyword for declaring a constant.
answer
final
question
Declare a constant for pi 3.14
answer
final double PI = 3.14; (remember capitals)
question
name variables and methods, naming convention
answer
lowercase
question
class name, naming convention
answer
Capitalized
question
constants, naming convention
answer
CAPITALIZED
question
Java provides _ primitive data types for numeric values, characters, and Boolean values.
answer
8
question
IEEE 754 is a standard approved by the _ for representing floating-point numbers on computers.
answer
Institute of Electrical and Electronics Engineers
question
Java uses the _-bit for the float type and _-bit for the double type.
answer
32, 64
question
Java uses the 32-bit IEEE 754 for the _ type and the 64-bit IEEE 754 for the _ type.
answer
float, double
question
Java uses four types for integers:
answer
byte, int, long, short
question
Java uses two types for floating-point numbers: float and double. The double type is twice as big as float, so the double is known as _ and float as single precision. Normally, you should use the _ type, because it is more accurate.
answer
double precision, double
question
reads an integer of the byte type.
answer
nextByte()
question
reads an integer of the short type.
answer
nextShort()
question
reads an integer of the int type
answer
nextInt()
question
reads an integer of the long type.
answer
nextLong()
question
reads a number of the float type.
answer
nextFloat()
question
reads a number of the double type.
answer
nextDouble()
question
Scanner input = new Scanner(System.in); System.out.print("Enter a byte value: "); ????
answer
byte byteValue = input.nextByte();
question
When both operands of a division are integers, the result of the division is the _ and the fractional part is truncated. For example, 5 / 2 yields 2, not 2.5.
answer
quotient, 2
question
To perform a float-point division, one of the operands must be a _. For example, 5.0 / 2 yields 2.5.
answer
floating-point number
question
The % operator, known as _ or _ operator
answer
remainder, modulo
question
3 % 7 yields
answer
3
question
-7 % 3 yields
answer
-1
question
-26 % -8 yields
answer
-2
question
20 % -13 yields
answer
7
question
The operand on the left is the _and the operand on the right is the _. Therefore, 7 % 3 yields 1
answer
dividend, divisor
question
Remainder is very useful in programming. For example, an even number _ is always 0 and an odd number _ is always 1. Thus, you can use this property to determine whether a number is even or odd.
answer
% 2
question
has only one operand; has two.
answer
unary operator, binary operator
question
- operator in -5 is a _ operator to negate number 5, whereas the - operator in 4 - 5 is a _ operator for subtracting 5 from 4.
answer
unary, binary
question
The _ method can be used to compute a**b. The pow method is defined in the __ in the Java API.
answer
Math.pow(a, b), Math class
question
Here, a and b are _ for the _ method and the numbers 2 and 3 are actual values used to invoke the method. For example, System.out.println(Math.pow(2, 3)); // Displays _
answer
parameters, pow, 8.0
question
byte, short, int, long, float, and double. Which of these data types requires the least & most amount of memory?
answer
byte, double
question
If today is Saturday, it will be Saturday again in 7 days. Suppose you and your friends are going to meet in 10 days. What day is in 10 days? You can find that the day is Tuesday using the following expression:
answer
(6 + 10) % 7 is 2 (Day 2 in a week is Tuesday, Day 0 is Sunday; Day 6 in a week is Saturday; a week has 7 days)
question
A _ is a constant value that appears directly in a program.
answer
literal
question
A float value has _ number of significant digits and a double value has _ number of significant digits.
answer
7-8, 15-17
question
Floating-point literals are written with __.
answer
a decimal point
question
Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a __ type value, not a _ type value.
answer
double, float
question
You can make a number a float by appending the letter _, and you can make a number a double by appending the letter _.
answer
f/F, d/D
question
The _ type values are more accurate than the _ type values (float or double)
answer
double, float
question
Integer Literals: The statement byte b = 128, for example, will cause a compile error, because the range for a byte value is from _.
answer
-128 to 127
question
An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) and 231 - 1 (2147483647). To denote an integer literal of the long type, append the letter _ to it.
answer
L/l
question
By default, an integer literal is a __
answer
decimal integer number
question
By default, an integer literal is a decimal integer number. To denote a binary integer literal, use a leading _.
answer
0B (/ob (zero B))
question
34 and 0.305 are _ in the following statements: int numberOfYears = 34; double weight = 0.305;
answer
literals (numeric literals)
question
Floating-point literals can be written in scientific notation in the form of _
answer
a * 10**b
question
A special syntax is used to write scientific notation numbers. For example, 1.23456 * 10**2 is written as _ or 1.23456E+2 and 1.23456 * 10-2 as 1.23456E-2. E (or e) represents an exponent and can be in either lowercase or uppercase.
answer
1.23456E2
question
A special syntax is used to write scientific notation numbers. For example, 1.23456 * 10**2 is written as 1.23456E2 or 1.23456E+2 and 1.23456 * 10**-2 as _. E (or e) represents an exponent and can be in either lowercase or uppercase.
answer
1.23456E-2
question
To improve readability, Java allows you to use underscores between two digits in a _. For example, the following literals are correct. However, 45_ or _45 is incorrect. The underscore must be placed between two digits.
answer
number literal
question
Be careful when applying division. Division of two integers yields an integer in Java. 5/9 is translated to _ instead of 5 / 9 in line 11, because 5 / 9 yields 0 in Java.
answer
5.0 / 9
question
You can invoke _ to return the current time.
answer
System.currentTimeMillis()
question
The currentTimeMillis method in the System class returns the current time in milliseconds elapsed since midnight, January 1, _ GMT
answer
1970
question
The currentTimeMillis method in the System class returns the current time in milliseconds elapsed since midnight, January 1, 1970 GMT, as shown in Figure 2.2. This time is known as the _; the point when time starts, and 1970 was the year when the UNIX operating system was formally introduced.
answer
UNIX epoch
question
// Obtain the total milliseconds since midnight, Jan 1, 1970
answer
long totalMilliseconds = System.currentTimeMillis();
question
// Obtain the total seconds since midnight, Jan 1, 1970
answer
long totalSeconds = totalMilliseconds / 1000;
question
// Compute the current second in the minute in the hour
answer
long currentSecond = totalSeconds % 60;
question
// Obtain the total minutes
answer
long totalMinutes = totalSeconds / 60;
question
// Compute the current minute in the hour
answer
long currentMinute = totalMinutes % 60;
question
// Obtain the total hours
answer
long totalHours = totalMinutes / 60;
question
// Compute the current hour
answer
long currentHour = totalHours % 24;
question
// Display results (time)
answer
System.out.println(time); (longer string than this just an example)
question
The operators +, -, *, /, and % can be combined with the assignment operator to form _.
answer
augmented operators
question
The += is called the _. count += 1; count = count + 1;
answer
addition assignment operator
question
-=
answer
Subtraction assignment
question
*=
answer
Multiplication assignment
question
Division assignment
answer
/=
question
%=
answer
Remainder assignment (There are no spaces in the augmented assignment operators.)
question
The augmented assignment operator is __ after all the other operators in the expression are evaluated. x /= 4 + 5.5 * 1.5; is same as x = x / (4 + 5.5 * 1.5);
answer
performed last
question
The _ operator (++) and _ operator (- -) are for incrementing and decrementing a variable by 1.
answer
increment, decrement
question
i = 3 i++; // i becomes _
answer
4
question
i++ is pronounced as i __ and i-- as i _
answer
plus plus, minus minus
question
i++ is pronounced as i plus plus and i—— as i minus minus. These operators are known as _ (or postincrement) and _ (or postdecrement), because the operators ++ and -- are placed after the variable.
answer
postfix increment, postfix decrement
question
++i & --j: These operators are known as _ (or preincrement) and _ (or predecrement).
answer
prefix increment, prefix decrement
question
int j = ++i; (assume i = 1) // j is _, i is _
answer
2, 2 (Increment var by 1, and use the new var value in the statement)
question
int j = i++; (assume i =1) // j is _, i is _
answer
1, 2 (Increment var by 1, but use the original var value in the statement)
question
int j = --i; (assume i = 1) // j is _, i is _
answer
0, 0
question
int j = i--; // j is _, i is _ assume i = 1
answer
1, 0
question
int i = 10; int newNum = 10 * i++; System.out.print("i is " + i + ", newNum is " + newNum); i is _, newNum is _
answer
11, 100 (In this case, i is incremented by 1, then the old value of i is used in the multiplication. So newNum becomes 100.)
question
int newNum = 10 * i; i = i + 1; write with postfix
answer
int newNum = 10 * i++;
question
i = i + 1; int newNum = 10 * i; write with pre-fix
answer
int newNum = 10 * (++i);
question
int i = 10; int newNum = 10 * (++i); System.out.print("i is " + i + ", newNum is " + newNum); i is _ newNum is _
answer
11, 110 (i is incremented by 1, and the new value of i is used in the multiplication. Thus newNum becomes 110.)
question
double x = 1.0; double y = 5.0; double z = x-- + (++y); After all three lines are executed, y becomes _, z becomes _, and x becomes _.
answer
6.0, 7.0, 0.0.
question
Floating-point numbers can be converted into integers using _.
answer
explicit casting
question
If an integer and a floating-point number are involved in a binary operation, Java automatically converts the integer to a _
answer
Floating point value (so, 3 * 4.5 is same as 3.0 * 4.5)
question
You can always assign a value to a numeric variable whose type supports a larger range of values; thus, for instance, you can assign a long value to a float variable. You cannot, however, assign a value to a variable of a type with a smaller range unless you use _
answer
type casting
question
_ is an operation that converts a value of one data type into a value of another data type.
answer
Casting
question
Casting a type with a small range to a type with a larger range is known as _
answer
widening a type
question
Casting a type with a large range to a type with a smaller range is known as _.
answer
narrowing a type (Java will automatically widen a type, but you must narrow a type explicitly)
question
System.out.println((int)1.7); displays _
answer
1
question
When a double value is cast into an int value, the fractional part is truncated. System.out.println((double)1 / 2); displays
answer
0.5 (because 1 is cast to 1.0 first, then 1.0 is divided by 2)
question
System.out.println(1 / 2); displays
answer
0 (because 1 and 2 are both integers and the resulting value should also be an integer)
question
Casting is necessary if you are assigning a value to a variable of a _ type range, such as assigning a double value to an int variable. A _ error will occur if casting is not used in situations of this kind.
answer
smaller, compile (However, be careful when using casting, as loss of information might lead to inaccurate results)
question
double d = 4.5; int i = (int)d; what is i? what is d worth?
answer
4, 4.5 (Casting does not change the variable being cast. For example, d is not changed)
question
int sum = 0; sum += 4.5 sum becomes _ after this statement
answer
4
question
int sum = 0; sum += 4.5 in one line, "sum = .... + ..."
answer
sum = (int)(sum + 4.5)
question
tax * 100 is 1185.3 (int)(tax * 100) is _
answer
1185
question
System.in is the standard input device, connected to the keyboard by default. You will be accessing it through methods of a _
answer
scanner object
question
System.out is the standard output device, connected to the console by _. You have been using its print and println methods for display.
answer
by default
question
A primitive data type is one that is _ to a language. A primitive data type has _-component values as opposed to object types, to be treated later in this subject, which normally have multi-component values. A single-component value is stored in a single location in memory, and that location is associated with an identifier.
answer
built-in, single
question
The Java primitive data types consist of the _ data types (Liang section 2.9), the _data type (Liang section 4.3) and the _ data type (Liang section 3.2).
answer
numeric, character, boolean
question
You don't need to remember the details of range and storage size, which you might use for reference. You do need to remember the ordering of the integer data types, with respect to range (_ ; _ ; _ ; _ ) and the ordering of the floating-point types with respect to both precision and range (;, ;).
answer
byte short int long, float double (basil fauldy)
question
Note that both the addition of 1 to the maximum integer value and the subtraction of 1 from the minimum integer value are said to be cases of _. The condition of underflow arises only with floating-point calculations and will not be of concern to us in this subject.
answer
overflow
question
char ch1 = 'A'; System.out.println(ch1 + " + 4 is " + (char)(ch1 + 4)); displays
answer
A + 4 is E
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New