Primitive Types Flashcards

Primitive types are fundamental building blocks for creating data structures. (59 cards)

1
Q

What are primitive types?

A

Primitive types are the simplest, indivisible data values stored directly in memory. In computing, they represent basic hardware-level data (like numbers or single characters). In Java, primitive types are built-in, non-object values with fixed sizes and efficient storage.

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

What are the three types of primitive data?

A

Depends on the programming language, but usually:

Numeric (int, float)

Character (char)

Boolean (true/false)

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

How many bits does each primitive type use?

A

A (Java — fixed sizes except boolean):

byte — 8 bits (−128 to 127)
short — 16 bits (signed)
int — 32 bits (signed)
long — 64 bits (signed)
float — 32 bits (IEEE 754)
double — 64 bits (IEEE 754)
char — 16 bits (UTF-16 code unit; may form surrogate pairs for some Unicode characters)
boolean — size not specified by Java; conceptually 1 bit, but actual storage depends on the JVM/implementation (often 1 byte in arrays, promoted to int for operations)

B (General computing — common conventions):

boolean — conceptually 1 bit (true/false)
integer sizes vary by language/platform (common modern conventions): byte 8, short 16, int 32, long 64
floating point: float 32-bit, double 64-bit (IEEE 754)
character storage depends on encoding: ASCII/UTF-8 uses 8-bit code units; UTF-16 uses 16-bit code units; Unicode code points may require more than one unit.

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

What are the numeric primitive data types in Java?

A

A: Java has 6 numeric primitive types:

Integral types (whole numbers):
byte — 8 bits
short — 16 bits
int — 32 bits
long — 64 bits

Floating-point types (decimal numbers):
float — 32 bits (IEEE 754)
double — 64 bits (IEEE 754)

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

What is the boolean data type?

A

Boolean = primitive logical type with two values: true/false. Conceptually 1 bit, but Java does not define its size. Used for conditions and control flow.

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

Example of using a byte in Java and how many bits does the Java byte use?

A

```java
byte b = 100;
System.out.println(b); // prints 100
~~~
- Size: 8 bits
- Range: -128 to 127

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

Example of using a short in Java and how many bits does the Java short use?

A

```java
short s = 20000;
System.out.println(s); // prints 20000
~~~
- Size: 16 bits
- Range: -32,768 to 32,767

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

Example of using an int in Java and how many bits does the Java int use

A

```java
int i = 100000;
System.out.println(i); // prints 100000
~~~
- Size: 32 bits
- Range: -2,147,483,648 to 2,147,483,647

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

Example of using a long in Java and how many bits does the Java long use

A

```java
long l = 10000000000L;
System.out.println(l); // prints 10000000000
~~~
- Size: 64 bits
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

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

Example of using a float in Java and how many bits does the Java float use

A

```java
float f = 3.14f;
System.out.println(f); // prints 3.14
~~~
- Size: 32 bits
- Single-precision decimal numbers

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

Example of using a double in Java and how many bits does the Java double use

A

```java
double d = 3.14159265359;
System.out.println(d); // prints 3.14159265359
~~~
- Size: 64 bits
- Double-precision decimal numbers

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

Example of using a char in Java and how many bits does the Java char use

A

```java
char c = ‘A’;
System.out.println(c); // prints A
~~~
- Size: 16 bits
- Stores a single Unicode character

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

Example of using a boolean in Java and how many bits does the Java boolean use

A

```java
boolean flag = true;
System.out.println(flag); // prints true
~~~
- Values: true or false

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

What are the 8 primitive data types in Java?

A

Type Size Range / Values Description
byte 8 bits -128 to 127 Small integers
short 16 bits -32,768 to 32,767 Larger integers than byte
int 32 bits -2³¹ to 2³¹-1 Standard integer type
long 64 bits -2⁶³ to 2⁶³-1 Large integers
float 32 bits Approx. ±3.4e38, 7 decimal digits Single-precision floating-point
double 64 bits Approx. ±1.7e308, 15 decimal digits Double-precision floating-point
char 16 bits ‘\u0000’ to ‘\uffff’ (Unicode) Single character
boolean 1 bit* true or false Logical values

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

What is the size and range of byte in Java?

A

Size: 8 bits
Range: -128 to 127

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

What is the size and range of short in Java?

A

Size: 16 bits
Range: -32,768 to 32,767

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

What is the size and range of int in Java?

A

Size: 32 bits
Range: -2,147,483,648 to 2,147,483,647

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

What is the size and range of long in Java?

A

Size: 64 bits
Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

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

What is the size and range of float in Java?

A

Size: 32 bits
Range: approximately ±3.40282347E+38F
Single-precision decimal numbers

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

What is the size and range of double in Java?

A

Size: 64 bits
Range: approximately ±1.79769313486231570E+308
Double-precision decimal numbers

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

What is the size and valid values of char in Java?

A

Size: 16 bits
Range: 0 to 65,535 (Unicode characters)
Example: ‘A’, ‘Ω’, ‘\n’

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

What is the size and valid values of boolean in Java?

A

Size: JVM dependent (typically 1 bit)
Values: true or false

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

Primitive Data Type (Definition)

A

The most basic, fundamental data type built into a programming language, representing a single, simple value.

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

Alternative Definition for what is primitive data type.

A

Small blocks of data that are fundamental to the Data Structures.

25
What is the relationship between primitive types and Data Structures
They are the fundamental building blocks used to construct all complex data structures (like arrays, lists, and objects).
26
Explain Primitive Type Category: Integers
Represent whole numbers (positive, negative, or zero). Examples: int, long, short.
27
Explain Primitive Type Category: Floating-Point
Represent real numbers with a fractional/decimal part. Examples: float, double.
28
Explain Primitive Type Category: Character
Represents a single character (letter, digit, or symbol). Example: char.
29
Explain Primitive Type Category: Logical
Represents a truth value; can only be true or false. Example: boolean (or bool).
30
Example of an Integer value
100, -5, or 0
31
Example of a Floating-Point value
3.14159 or -0.001
32
Example of a Character value
'A', '7', or '?'
33
Example of a Boolean value
true or false
34
Are strings (like "Hello") primitive types?
Not always. In many languages (like Java or Python), they are technically objects or composite types (an array of chars), even if they feel like primitives.
35
What does 'Blocks of data' imply?
It suggests a fixed, contiguous area of memory of a specific size (e.g., 4 bytes for an integer).
36
What is the opposite of a primitive type?
Composite, Complex, or Non-Primitive types (also called Reference types), such as Objects, Arrays, and Classes.
37
Why are primitives memory efficient?
They usually correspond directly to basic hardware operations and store only the value, not extra metadata or references.
38
What is Data Structure: Linked List Node
Contains primitive data (like an int value) and a reference (pointer) to the next node.
39
What is Data Structure: Class/Object
A collection of fields, many of which are primitive types (e.g., int age, boolean isActive).
40
What is the Hardware Connection with the primitive types.
Operations on primitive types often map directly to instructions for the computer's processor (CPU).
41
What does 'atomic' mean in the primitive type context?
Indivisible. A primitive type cannot be broken down into smaller data components.
42
What is the Storage Location (Commonly) for primitive types.
Primitive types are often stored on the 'stack', which is very fast for access and management.
43
What is the Non-Primitive Storage (Commonly)?
Objects and complex types are often stored on the 'heap', and a reference (pointer) to them is stored on the stack.
44
In languages like Java or C#, is the String data type primitive or non-primitive (reference)?
Non-primitive (or Reference). String is a class that represents an object in the memory heap.
45
What is the main reason why String is considered an object and not a primitive type (like int or boolean)?
Because it is a class, it has associated methods (e.g., .length(), .toUpperCase()). Primitive types store the value directly and have no methods.
46
What does the String data type fundamentally represent?
A sequence of characters (char).
47
What is a key characteristic of Strings in most object-oriented programming languages (like Java)?
Immutability. Once created, a String cannot be changed. Any "modification" actually results in the creation of a new String object.
48
What is the default value of a reference data type (like a String) when it is not initialized?
null (which means the variable does not point to any object in memory).
49
In languages like Java, what is the main difference between a primitive type (e.g., int) and a non-primitive / reference type (e.g., String) in terms of functionality and default value?
1. Functionality: Primitive types only store the value and have no methods. Non-primitive types (Objects) are classes and have methods to manipulate data. 2. Default Value: The default value for a primitive is a specific number or boolean (e.g., 0 for int, false for boolean). The default value for a non-primitive is null.
50
How much memory does a boolean variable occupy?
Theoretically: It only needs 1 bit (to store a 0 or 1). Practically: Most systems (like the JVM for Java) reserve 1 byte (8 bits) for it, because it is inefficient for a CPU to access a single bit from byte-addressable memory.
51
Explain the three key concepts that define primitive data types (e.g., int, boolean) in contrast to reference types (objects).
1. Atomic: Primitives represent indivisible values. They are not composed of other types and store only a single, fundamental piece of data. 2. Predefined (Fixed Size): Every primitive type has a fixed, guaranteed size defined by the system (e.g., long is always 64 bits). This ensures their range and memory usage are predictable. 3. Immutable Value: Primitive values cannot be changed. If you have the value 5, it will always be 5. Changing the variable (x = 10;) only makes it point to a new primitive value (10), leaving the old value unaltered.
52
What does it mean that a primitive type is atomic?
Definition: Primitive types represent indivisible values. Explanation: They are not composed of other types and store only a single fundamental piece of data. Example: int x = 42; → 42 is atomic; it cannot be split into smaller components.
53
What does it mean that a primitive type is predefined (fixed size)?
Definition: Each primitive type has a fixed, guaranteed size determined by the system. Explanation: This ensures that their range and memory usage are predictable. Example: int → 32 bits long → 64 bits boolean → JVM-dependent, but logically 1 bit
54
What does it mean that a primitive type has an immutable value?
Definition: Primitive values cannot be changed once created. Explanation: Assigning a new value to a variable does not alter the old value; it just points the variable to a new primitive value. Example: int x = 5; x = 10; // x now points to 10; the original value 5 remains unchanged
55
How do primitive types differ from objects in terms of atomicity?
Primitive: Fixed, predefined size (e.g., int = 32 bits, long = 64 bits) Object: Variable size depending on fields and references
56
How do primitive types differ from objects in terms of size?
Primitive: Yes, each type has a fixed, predefined size (e.g., int = 32 bits, long = 64 bits) Object: No, object size can vary depending on fields and references
57
How do primitive types differ from objects in terms of mutability?
Primitive: Immutable – changing a variable points it to a new value; old value remains unchanged Object: Usually mutable – object fields can be modified after creation
58
How do primitive types and objects differ in memory storage?
Primitive: Usually stored on the stack if local; inside objects, on the heap Object: Stored on the heap, accessed via references on the stack
59
How do primitive types and objects differ in how they store data?
Primitive: Stores raw values directly Object: Stores references; the object contains fields (primitives or objects)