Chapter 2 Flashcards

1
Q

What are identifiers in Java?

A

Identifiers are the names that identify elements such as classes, methods, and variables in a program.

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

What can an identifier be made up of?

A

An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign ($).

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

How must an identifier start in Java?

A

An identifier must start with a letter (A-Z, a-z), an underscore (_), or a dollar sign ($). It cannot begin with a digit.

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

Is Java case sensitive when it comes to identifiers?

A

Yes, Java is case sensitive. Total, total, and TOTAL are different identifiers.

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

What is the convention for naming class identifiers in Java?

A

By convention, class names use title case (e.g., Lincoln).

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

What is the convention for naming constants in Java?

A

By convention, constants are written in upper case (e.g., MAXIMUM).

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

What is lower camel case in Java?

A

Lower camel case is when the first letter of each word is uppercase, except the first one (e.g., dayOfWeek).

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

What is the convention for naming method and variable identifiers in Java?

A

Methods and variables start with a lowercase letter (e.g., myMethod()).

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

What do variables do in Java programs?

A

Variables store data in memory and are used to “remember” information in the program.

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

What are the components of a variable in Java?

A

A variable has a name, data type, and value.

Example: int count = 1;

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

What must a variable declaration specify in Java?

A

A variable declaration must specify the variable’s name and the type of information it will hold.

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

What is an example of declaring multiple variables in one statement?

A

```java
int total = 0, count = 0, temp = 0, result = 0;
~~~

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

What are global variables in Java?

A

Global variables exist throughout the entire program’s life and are accessible from any part of the program.

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

What are local variables in Java?

A

Local variables exist only temporarily, until the block or bracket in which they were declared is closed.

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

What is the general programming practice regarding global variables in Java?

A

It is generally good programming practice to avoid using global variables, but sometimes they are necessary.

This is for the sake of preserving memory

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

what does it mean to initialize a variable?

A

it means to give it its initial value

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

Why do we need to specify a data type when creating a variable in Java?

A

A data type is required so the system can reserve the appropriate amount of memory for the variable.

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

What are the two basic groupings of data types in Java?

A

The two basic groupings of data types are:
- Primitive Data Types
- Class Data Types (e.g., Strings)

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

What are primitive data types in Java?

A

Primitive data types, also known as ‘Built In’ data types, are the building blocks of data manipulation in Java. They are stored in memory directly with their value.

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

What is the default value for primitive data types in Java?

A

The default value for primitive data types is 0.

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

Are primitive data types in Java case sensitive?

A

Yes, primitive data types in Java are all lowercase. String is not a primitive type.

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

How many primitive data types are there in Java?

A

There are 8 primitive data types: byte, char, short, int, long, float, double, and boolean.

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

What are the two categories of primitive numeric data types in Java?

A

The two categories are:
- Integers: byte, short, int, long
- Floating-point numbers: float, double

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

What is the difference between float and double in Java?

A

float: Stores decimal numbers with single precision (e.g., 3.14159).
double: Stores decimal numbers with double precision and more decimal places.

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

What is the purpose of the char data type in Java?

A

The char data type is used to store individual characters such as ‘a’, ‘b’, ‘c’, etc.

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

What does the boolean data type represent in Java?

A

The boolean data type can store two possible values: true or false.

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

What is the size of byte, short, int, and long in terms of bits?

A

byte: 8 bits
short: 16 bits
int: 32 bits
long: 64 bits

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

What is the size of float and double in terms of bits?

A

float: 32 bits
double: 64 bits

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

What is the range of values for the byte, short, int, and long types?

A

byte: -128 to 127
short: -32,768 to 32767
int: -2,147,483,648 to 2,147,483,647
long: Less than -9 x 10^18 , around 9 x 10^18

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

What are the ranges for the float and double data types?

A

float: +/- 3.4 x 10^38 with 7 significant digits
double: +/- 1.7 x 10^308 with 15 significant digits

31
Q

What is a numeric literal in programming?

A

A literal is a constant that appears directly in a program.

Example: int numberOfYears = 34;

32
Q

What happens if you assign a value that exceeds the range of a variable’s data type?

A

If the value exceeds the range, it will cause a compile error.

For example, byte b = 128; causes a compile error because byte can only hold values from -128 to 127.

33
Q

How can floating-point literals be written in Java?

A

Floating-point literals can be written in scientific notation in the form of:

a x 10^b

34
Q

Can a char variable hold multiple characters?

A

No, a char variable holds only one character, while a String object can hold multiple characters.

35
Q

What does the boolean data type represent?

A

The boolean data type represents a true or false condition.

36
Q

What can a boolean variable be used to represent?

A

A boolean variable can represent any two states, such as a light bulb being on or off.

37
Q

What is a String data type in Java?

A

A String is a data type that represents a sequence of characters surrounded by quotes.

38
Q

How does Java treat everything inside a String?

A

Everything inside a String is printed, including mathematical operations.

Example: String subjects = "We are learning Java and Data structures";

39
Q

What are the two ways to create a String object in Java?

A
  1. By string literal: String name = "Alex"; (does not create a new instance)
  2. By the new keyword: String s = new String("Hello"); (creates a new object)
40
Q

What operator is used for string concatenation in Java?

A

The plus (+) operator is used to concatenate two or more strings.

41
Q

How do you concatenate two strings in Java?

A

You can concatenate strings using the + operator.

Example:
String firstName = "John"; String lastName = "Allen"; String fullName = firstName + lastName;

42
Q

Can a string literal be broken across two lines in a program?

A

No, a string literal cannot be broken across two lines in a program.

43
Q

Can numeric values be concatenated with a string?

A

Yes, numeric values can be concatenated with a string using the + operator.

Will add values if in paratheses

44
Q

How does the + operator behave when one operand is a string and the other is a number?

A

If one operand is a string and the other is a number, the + operator performs string concatenation.

45
Q

How does the + operator behave when both operands are numeric?

A

If both operands are numeric, the + operator performs arithmetic addition when enclosed by parantheses.

otherwise they are concatenated

46
Q

What happens if you try to change the value of a constant in Java?

A

The compiler will issue an error if you try to change the value of a constant.

47
Q

How do you declare a constant in Java?

A

You declare a constant in Java using the final modifier.

Example: final int MIN_HEIGHT = 69;

48
Q

Why are constants useful in programming?

A

Constants are useful for three important reasons:
1. They give meaning to unclear literal values.
2. They facilitate program maintenance.
3. They formally establish that a value should not change.

49
Q

How do constants make program maintenance easier?

A

Constants facilitate program maintenance by ensuring that if a constant is used in multiple places, its value needs to be updated only in one place.

50
Q

What actually happens when you declare a variable in Java?

A

When you declare a variable, you give it a name, and the compiler allocates a memory location for that variable, based on its type. The memory location is called the variable’s address.

51
Q

Where are expressions commonly found in an assignment statement?

A

Expressions are usually on the right side of an assignment statement.

52
Q

List the five arithmetic operators in Java.

A

+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Remainder)

53
Q

What happens if either operand in an arithmetic expression is a floating point?

A

The result will be a floating point.

54
Q

What is a relational expression? Give an example.

A

A relational expression compares values. Example: boolean isEqual = (5 == 5);

55
Q

What is a logical expression? Give an example.

A

A logical expression combines boolean values. Example: boolean result = (5 > 3) && (2 < 4);

56
Q

What is the order of precedence for evaluating expressions in Java?

57
Q

What is JShell?

A

JShell is a command-line tool for quickly evaluating expressions and executing statements.

58
Q

What is JShell commonly known as?

A

It is commonly known as REPL, which stands for Read-Evaluate-Print-Loop.

59
Q

What is data conversion in Java?

A

Data conversion is the process of converting data from one type to another, often for computation purposes.

60
Q

Does data conversion change the type of a variable?

A

No, data conversion does not change the type of a variable; it only converts a value during a computation.

61
Q

Why must data conversions be handled carefully?

A

To avoid losing information, especially in narrowing conversions.

62
Q

What are the two types of conversions in Java?

A

Widening conversions (safe, small to large type) and Narrowing conversions (may lose information, large to small type).

63
Q

What are the three ways data conversion can occur in Java?

A

Assignment conversion, promotion, and casting.

64
Q

What is assignment conversion?

A

When a value of one type is assigned to a variable of another type, but only widening conversions can happen this way.

65
Q

Can Boolean values be converted to other types in Java?

A

No, Boolean values cannot be cast to other types.

66
Q

What is promotion in Java?

A

When operators in expressions automatically convert operands to a compatible type.

67
Q

What is widening conversion?

A

A type conversion where a smaller data type is converted into a larger data type automatically.

68
Q

List an example of widening conversion.

A

byte → short → int → long → float → double

69
Q

What is an example of automatic type conversion in Java?

A

```java
int i = 100;
long l = i; // automatic conversion
float f = l; // automatic conversion
~~~

70
Q

What is narrowing conversion?

A

A type conversion where a larger data type is converted into a smaller data type, which may result in data loss.

71
Q

List an example of narrowing conversion.

A

double → float → long → int → short → byte

72
Q

What is an example of explicit type casting in Java?

A

```java
double d = 100.04;
long l = (long) d; // explicit casting
int i = (int) l; // explicit casting
~~~

73
Q

What is casting in Java?

A

A technique to convert one data type to another by placing the target type in parentheses before the value.

74
Q

Give an example of casting for floating point division.

A

```java
float result = (float) total / count;
~~~