Chapter 2 - Elementary Programming Flashcards

1
Q

What are “identifiers”?

A

Identifiers are names for naming elements such as variables, constants, methods, classes, packages in a program

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

What are the naming rules for identifiers?

A
  1. Can be letters, digits, underscores and dollar signs
  2. Must begin with letter or underscore.
  3. Can not be a reserved word.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a named constant?

A

A named constant represents permanent data that never changes

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

How is a named constant declared?

A

By using the keyword “final”

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

What are the integer types in Java?

A

byte, short, int, and long. Representing different sizes in ascending order

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

What are the floating-point types in Java?

A

They are float and double, representing two different presicions

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

How can you convert a value from one type to another?

A

You can do this explicitly by using the (type)value notation

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

What does it mean to widen a type?

A

Casting a variable of a type with a small range to a variable of a type with a larger range

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

What does it mean to narrow a type?

A

Casting a variable of a type with a large range to a variable of a type with a smaller range

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

what is “char”?

A

A reserved word, and a type that represents a single character

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

What is the UNIX epoch?

A

Midnight of January 1, 1970 is known as the UNIX epoch. System.currentTimeMillis() returns the number of milliseconds since the UNIX epoch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
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

A and B are correct

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

To improve readability and maintainability, you should declare ____ instead of using literal values such as 3.14159

A

Constants

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Which of these data types requires the most amount of memory?
A. long
B. int
C. short
D. byte
A

long

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
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
A

It causes overflow

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

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.

A

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.

17
Q
Which of the following expression results in a value 1?
A. 2 % 1
B. 15 % 4
C. 25 % 5
D. 37 % 6
A

D. 37 % 6

18
Q
-24 % 5 is \_\_\_
A. 1
B. -1
C. 4
D. -4
A

D. -4

19
Q
-24 % -5 is \_\_
A. 1
B. -1
C. 4
D. -4
A

D. -4

20
Q
Math.pow(2, 3) returns \_\_
A. 9
B. 8
C. 9.0
D. 8.0
A

D. 8.0

Math.pow() returns a double value

21
Q

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.

A

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.

22
Q

The expression 4 + 20 / (3 - 1) * 2 is evaluated to

A. 4
B. 20
C. 24
D. 9
E. 25
A

C. 24

23
Q

To assign a double variable d to a float variable x, you write

A

x = (float)d;

24
Q
Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to \_\_
A. 66
B. B
C. A1
D. Illegal expression
A

C. A1