Lecture2b-variables-Identifiers-data-types-assignments Flashcards

1
Q

What is an identifier in Java?

A

A name used for variables, classes, or methods.

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

What are reserved words in Java?

A

Special words (e.g., int, double, class) that cannot be used as variable names.

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

What are the three main parts of a variable?

A
  1. Name (Identifier)
  2. Data Type (e.g., int, double)
  3. Value (Stored data)

Example: int count = 1;

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

What is the difference between local and global variables?

A

Local Variable: Exists only inside the method where it’s declared.
Global Variable: Exists throughout the entire program.

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

What are Java’s 8 primitive data types?

A
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Java’s integer data types?

A

byte: -128 to 127
short: -32,768 to 32,767
int: -2,147,483,648 to 2,147,483,647
long: Very large numbers

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

What are Java’s floating-point data types?

A

float – Stores decimal numbers with 7 digits of precision.
double – Stores decimal numbers with 15 digits of precision.

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

What values can a boolean hold?

A

true or false

Example:
```java
boolean isJavaFun = true;
~~~

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

How do you declare a char in Java?

A
java  
char letter = 'A';  

✔ Uses single quotes (‘ ‘)
✔ Stores one character
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you declare a String in Java?

A
java  
String name = "Java";  

✔ Strings use double quotes (“ “)
✔ Strings are objects, not primitive types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the + operator do with strings?

A

Joins (concatenates) strings.

Example:

java  
System.out.println("Hello " + "World!");  

🔹 Output: Hello World!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare a constant?

A

Use final to prevent changes.

java  
final int MAX_SPEED = 120;  

✔ Constants use ALL UPPERCASE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an assignment statement in Java?

A

Stores a value in a variable using =.

Example:

java  
int x = 5;  

🔹 x is assigned the value 5.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does Java handle division and remainder operations?

A

Integer division (/) drops decimals (e.g., 7 / 2 = 3)
Modulus (%) gives the remainder (e.g., 7 % 2 = 1)

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

How do you calculate exponents in Java?

A

Use Math.pow(base, exponent).

Example:
```java
System.out.println(Math.pow(2, 3)); // Output: 8.0
~~~

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

What are ++ and -- used for?

A

x++ → Adds 1 (x = x + 1)
x-- → Subtracts 1 (x = x - 1)

17
Q

What are Java’s compound operators?

A

+= (Addition assignment)
-= (Subtraction assignment)
*= (Multiplication assignment)
/= (Division assignment)
%= (Modulus assignment)

Example:
```java
x += 5; // Equivalent to x = x + 5;
~~~

18
Q

What are the two types of data conversion?

A
  1. Widening Conversion (Safe) – Converts small to large (e.g., int → double).
  2. Narrowing Conversion (Unsafe) – Converts large to small (e.g., double → int).

Example of casting:
```java
double num = 10.5;
int x = (int) num; // x = 10
~~~

19
Q

What are the three types of data conversion in Java?

A
  1. Assignment Conversion: Assigning a smaller type to a larger one automatically (e.g., int → double).
  2. Promotion: Automatic conversion in expressions (e.g., int + double = double).
  3. Casting: Manually converting types using (type) (e.g., (int) 3.53).
20
Q

What is JShell in Java?

A

A Read-Evaluate-Print Loop (REPL) that allows testing Java code interactively.

Example usage:
```sh
jshell> int x = 5;
x ==> 5

jshell> x + 3;
$2 ==> 8

jshell> /exit
~~~

21
Q

What is casting in Java, and how is it used?

A

Casting is manually converting one data type into another using (type).

22
Q

What is Widening Cast in Java?

A

Widening Cast is a safe, automatic conversion from a small to a large type.

Example:
```java
int num = 10;
double d = num; // Implicit casting (int to double)
~~~

23
Q

What is Narrowing Cast in Java?

A

Narrowing Cast is an explicit, risky conversion from a large to a small type.

Example:
```java
double d = 10.99;
int num = (int) d; // Explicit casting (double to int), drops decimals
~~~

24
Q

When is casting used in Java?

A

Casting is used when Java does not automatically convert types.

25
Q

What are numeric literals in Java?

A

A numeric literal is a constant value written directly in code.

26
Q

What is an integer literal?

A

An integer literal is a whole number written directly in code.

Example: int years = 34; // 34 is an integer literal

27
Q

What is a floating-point literal?

A

A floating-point literal is a number that has a decimal point written directly in code.

Example: double pi = 3.14159; // 3.14159 is a floating-point literal

28
Q

What is a caution regarding integer literals?

A

Integer literals must fit within the variable’s type (byte, short, int, long).