Chapter 2 - Elementary Programming Flashcards
What are “identifiers”?
Identifiers are names for naming elements such as variables, constants, methods, classes, packages in a program
What are the naming rules for identifiers?
- Can be letters, digits, underscores and dollar signs
- Must begin with letter or underscore.
- Can not be a reserved word.
What is a named constant?
A named constant represents permanent data that never changes
How is a named constant declared?
By using the keyword “final”
What are the integer types in Java?
byte, short, int, and long. Representing different sizes in ascending order
What are the floating-point types in Java?
They are float and double, representing two different presicions
How can you convert a value from one type to another?
You can do this explicitly by using the (type)value notation
What does it mean to widen a type?
Casting a variable of a type with a small range to a variable of a type with a larger range
What does it mean to narrow a type?
Casting a variable of a type with a large range to a variable of a type with a smaller range
what is “char”?
A reserved word, and a type that represents a single character
What is the UNIX epoch?
Midnight of January 1, 1970 is known as the UNIX epoch. System.currentTimeMillis() returns the number of milliseconds since the UNIX epoch
Which of the following are correct ways to declare variables? A. int length; int width; B. int length, width; C. int length; width; D. int length, int width;
A and B are correct
To improve readability and maintainability, you should declare ____ instead of using literal values such as 3.14159
Constants
Which of these data types requires the most amount of memory? A. long B. int C. short D. byte
long
If a number is too large to be stored in a variable of the float type, it \_\_\_ A. causes overflow B. causes underflow C. causes no error D. cannot happen in Java
It causes overflow
Analyze the following code:
public class Test { public static void main(String[] args) { int n = 10000 * 10000 * 10000; System.out.println("n is " + n); } }
A. The program displays n is 1000000000000
B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program is aborted.
C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow.
D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program continues to execute because Java does not report errors on underflow.
Correct answer is C:
The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow.
Which of the following expression results in a value 1? A. 2 % 1 B. 15 % 4 C. 25 % 5 D. 37 % 6
D. 37 % 6
-24 % 5 is \_\_\_ A. 1 B. -1 C. 4 D. -4
D. -4
-24 % -5 is \_\_ A. 1 B. -1 C. 4 D. -4
D. -4
Math.pow(2, 3) returns \_\_ A. 9 B. 8 C. 9.0 D. 8.0
D. 8.0
Math.pow() returns a double value
Analyze the following code.
public class Test { public static void main(String[] args) { int month = 09; System.out.println("month is " + month); } }
A. The program displays month is 09
B. The program displays month is 9
C. The program displays month is 9.0
D. The program has a syntax error, because 09 is an incorrect literal value.
D. The program has a syntax error, because 09 is an incorrect literal value.
Explanation: Any numeric literal with the prefix 0 is an octal value. But 9 is not an octal digit. An octal digit is 0, 1, 2, 3, 4, 5, 6, or 7.
The expression 4 + 20 / (3 - 1) * 2 is evaluated to
A. 4 B. 20 C. 24 D. 9 E. 25
C. 24
To assign a double variable d to a float variable x, you write
x = (float)d;
Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to \_\_ A. 66 B. B C. A1 D. Illegal expression
C. A1