Topic 2.1: Working With Java Data Types - Working With Java Data Types: Declare and initialize variables (including casting of primitive data types) Flashcards

1
Q

What is the risk of data loss when performing casting?

A

When casting from a larger data type to a smaller data type, there is a risk of data loss or truncation.

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

The order of types for this is:

byte -> short -> char -> int -> long -> float -> double

A

List the order of types for widening casting from smallest to largest.

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

What is
dynamic method dispatch?

A

This is the process of determining the appropriate method implementation based on the actual object type at runtime.

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

this is the ability of objects of different classes to be treated as objects of a common superclass and exhibit different behavior based on their specific implementation.

A

What is
polymorphism?

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

What are the risks involved in casting between compatible data types?

A

There are no risks of data loss or precision issues when casting between compatible data types since the target data type can accommodate the values of the source data type.

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

What is
polymorphism?

A

this is the ability of objects of different classes to be treated as objects of a common superclass and exhibit different behavior based on their specific implementation.

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

Can you access methods defined in a subclass using a reference of the superclass?

A

Yes, if the methods are defined in the subclass or the common superclass, you can access them using the reference of the superclass.

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

What are the size, range, and default values associated with the
float data type in Java?

A

This data type has a size of 32 bits (4 bytes) and a range Sufficient for storing 6 to 7 decimal digits. Its default value is 0.0f.

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

This data type has a size of 32 bits (4 bytes) and a range Sufficient for storing 6 to 7 decimal digits. Its default value is 0.0f.

A

What are the size, range, and default values associated with the
float data type in Java?

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

What role do data types play in ensuring data integrity?

A

these enforce constraints and provide data validation. They prevent incompatible operations, such as performing arithmetic on non-numeric data or assigning incorrect values.

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

How can you check the type compatibility before performing a cast?

A

You can use the instanceof operator for this situation

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

How is
method lookup
determined in Java?

A

This in Java is determined at runtime based on the actual type of the object, rather than the declared type of the variable.

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

What is narrowing casting in Java?

A

Narrowing casting involves manually converting a larger type to a smaller size type.

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

this refers to the concept of ensuring that the operations performed on variables are compatible with their declared data types. It prevents potential errors and unexpected behavior by catching type-related issues during compile-time rather than at runtime.

A

What is type safety?

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

these include:

  • Memory Efficiency: Choosing an appropriate data type optimizes memory usage.
  • Data Integrity: Data types enforce constraints and prevent incompatible operations.
  • Performance: Proper data types can improve program performance.
A

name 3 reasons why choosing the right data type important in programming?

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

This in Java is determined at runtime based on the actual type of the object, rather than the declared type of the variable.

A

How is
method lookup
determined in Java?

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

This data type has a size of 64 bits (8 bytes) and a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Its default value is 0L.

A

What are the size, range, and default values associated with the
long data type in Java?

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

The order of types for this is:

double -> float -> long -> int -> char -> short -> byte.

A

List the order of types for narrowing casting from largest to smallest.

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

What are the size, range, and default values associated with the
long data type in Java?

A

This data type has a size of 64 bits (8 bytes) and a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Its default value is 0L.

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

Give an example of reference casting in Java.

A
Animal animal = new Dog(); 
Dog dog = (Dog) animal; // Casting Animal to Dog
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Narrowing casting involves manually converting a larger type to a smaller size type.

A

What is narrowing casting in Java?

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

these are significant in programming because they determine the kind of data that can be stored in variables and the operations that can be performed on that data.

They ensure data integrity, optimize memory usage, and enable the compiler to perform type checking, resulting in more reliable and efficient code.

A

Why are data types significant in programming?

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

This refers to automatically converting a smaller type to a larger type size.

A

What is widening casting in Java?

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

this will result in a compile-time error because the compiler looks for the method in the superclass and cannot find it.

A

What happens if you try to
access a specific method of a subclass without casting, using a reference of the superclass?

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

Is narrowing casting performed automatically or manually?

A

Narrowing casting is performed manually by explicitly specifying the target data type in parentheses.

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

This data type represents a boolean value, which can be either true or false. Its size is not precisely defined. Its default value is false.

A

What are the size, range, and default values associated with the
boolean data type in Java?

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

Why is type safety important in Java?

A

Java is a statically typed language, and explicit declaration of data types promotes type safety. It helps catch potential errors at compile-time, preventing unexpected behavior and enhancing code reliability.

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

This data type has a size of 16 bits (2 bytes) and a range from -32,768 to 32,767. Its default value is 0.

A

What are the size, range, and default values associated with the
short data type in Java?

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

List the order of types for narrowing casting from largest to smallest.

A

The order of types for this is:

double -> float -> long -> int -> char -> short -> byte.

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

The double data type has a size of 64 bits (8 bytes) and a range Sufficient for storing 15 decimal digits. Its default value is 0.0d.

A

What are the size, range, and default values associated with the
double data type in Java?

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

these enforce constraints and provide data validation. They prevent incompatible operations, such as performing arithmetic on non-numeric data or assigning incorrect values.

A

What role do data types play in ensuring data integrity?

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

What is widening casting in Java?

A

This refers to automatically converting a smaller type to a larger type size.

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

How is primitive and reference casting performed in Java?

A

this is performed by explicitly specifying the target data type in parentheses before the value being cast.

34
Q

Give an example of data loss during casting.

A

Example:

double largerDouble = 3.99;
int smallerInt = (int) largerDouble; // Casting from double to int

35
Q

Is widening casting performed automatically or manually?

A

Widening casting is performed automatically by the compiler.

36
Q

This data type has a size of 8 bits (1 byte) and a range from -128 to 127. Its default value is 0.

A

What are the size, range, and default values associated with the
byte data type
in Java?

37
Q

this is performed by explicitly specifying the target data type in parentheses before the value being cast.

A

How is primitive and reference casting performed in Java?

38
Q

Is explicit casting syntax required for casting between compatible data types?

A

No, explicit casting syntax is not required. The casting is performed implicitly by the compiler when the target data type can accommodate the range and precision of the source data type.

39
Q

What is widening or upcasting?

A

This refers to the implicit casting from a smaller range to a larger range of compatible data types.

40
Q

What happens if you try to
access a specific method of a subclass without casting, using a reference of the superclass?

A

this will result in a compile-time error because the compiler looks for the method in the superclass and cannot find it.

41
Q

What are the size, range, and default values associated with the
char data type in Java?

A

This data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive). Its default value is ‘\u0000’.

42
Q

What are 3 consequences of using uninitialized variables in Java?

A
  • Unpredictable behavior: Using uninitialized variables can result in unpredictable behavior due to unknown or arbitrary values stored in the variable, leading to incorrect calculations or comparisons.
  • Compilation errors: The Java compiler may generate an error when uninitialized variables are used because it violates language rules and can cause runtime errors.
  • Bugs and logical errors: Uninitialized variables can introduce bugs and logical errors, affecting program flow and producing incorrect results.
43
Q

What is the
purpose of casting between compatible data types?

A

The purpose of this is to ensure compatibility and store or manipulate values with a smaller range or precision in a data type that can accommodate larger values.

44
Q

Can you access methods specific to a subclass using a reference of the superclass?

A

No, you cannot access methods specific to a subclass using a reference of the superclass directly. Casting to the subclass type is required to access those methods.

45
Q

What is type safety?

A

this refers to the concept of ensuring that the operations performed on variables are compatible with their declared data types. It prevents potential errors and unexpected behavior by catching type-related issues during compile-time rather than at runtime.

46
Q

provide 4 reasons on why its important to initialize variables in Java?

A

Initializing variables is important in Java because it ensures:
* they have a well-defined starting value
* avoids unpredictable behavior
* prevents compilation errors
* and helps maintain program correctness and reliability.

47
Q

The purpose of this is to ensure compatibility and store or manipulate values with a smaller range or precision in a data type that can accommodate larger values.

A

What is the
purpose of casting between compatible data types?

48
Q

List the order of types for widening casting from smallest to largest.

A

The order of types for this is:

byte -> short -> char -> int -> long -> float -> double

49
Q

How does choosing the right data type improve memory efficiency?

A

Choosing a data type that accurately represents the range of possible values helps optimize memory usage. Using smaller data types conserves memory resources.

50
Q

What are the size, range, and default values associated with the
boolean data type in Java?

A

This data type represents a boolean value, which can be either true or false. Its size is not precisely defined. Its default value is false.

51
Q

name 3 reasons why choosing the right data type important in programming?

A

these include:

  • Memory Efficiency: Choosing an appropriate data type optimizes memory usage.
  • Data Integrity: Data types enforce constraints and prevent incompatible operations.
  • Performance: Proper data types can improve program performance.
52
Q

What happens if you attempt to cast incompatible primitive data types?

A

If you attempt to cast incompatible data types, a compile-time error will occur.

53
Q

Give an example of primitive casting in Java.

A
int num1 = 10; 
double num2 = (double) num1; // Casting int to double
54
Q

What is the risk when casting objects in a class hierarchy?

A

The risk in this situation is a ClassCastException if the actual object is not compatible with the target type.

55
Q

This refers to the implicit casting from a smaller range to a larger range of compatible data types.

A

What is widening or upcasting?

56
Q

Can you give an example of narrowing casting?

A

Example of narrowing casting:

double largerDouble = 3.14; 
int smallerInt = (int) largerDouble;
57
Q

this situation can result in a ClassCastException at runtime if the actual object being referenced is not compatible with the target type.

A

What can happen if
incorrect reference casting is performed?

58
Q

This data type has a size of 32 bits (4 bytes) and a range from -2,147,483,648 to 2,147,483,647. Its default value is 0.

A

What are the size, range, and default values associated with the
int data type in Java?

59
Q

This involves converting a value from a smaller range to a larger range in Java.

A

What is
casting between compatible data types?

60
Q

What can happen if
incorrect reference casting is performed?

A

this situation can result in a ClassCastException at runtime if the actual object being referenced is not compatible with the target type.

61
Q

Can you give an example of widening casting?

A

Example of widening casting:

int smallerNumber = 5; 
long largerNumber = smallerNumber;
62
Q

How does selecting the appropriate data type enhance program performance?

A

Properly chosen data types can improve program performance. Using smaller data types can result in faster calculations and reduce memory access times.

63
Q

What is the purpose of using casting to access specific methods of a subclass?

A

Casting allows you to access the specific methods of a subclass that are not present in the superclass, but caution should be exercised to ensure compatibility and prevent runtime errors.

64
Q

What are the size, range, and default values associated with the
byte data type
in Java?

A

This data type has a size of 8 bits (1 byte) and a range from -128 to 127. Its default value is 0.

65
Q

You can use the instanceof operator for this situation

A

How can you check the type compatibility before performing a cast?

66
Q

Give an example of a ClassCastException during casting.

A

Example:

Animal animal = new Animal(); 
Dog dog = (Dog) animal; // ClassCastException at runtime
67
Q

The risk in this situation is a ClassCastException if the actual object is not compatible with the target type.

A

What is the risk when casting objects in a class hierarchy?

68
Q

What are the size, range, and default values associated with the
int data type in Java?

A

This data type has a size of 32 bits (4 bytes) and a range from -2,147,483,648 to 2,147,483,647. Its default value is 0.

69
Q

Why are data types significant in programming?

A

these are significant in programming because they determine the kind of data that can be stored in variables and the operations that can be performed on that data.

They ensure data integrity, optimize memory usage, and enable the compiler to perform type checking, resulting in more reliable and efficient code.

70
Q
  1. Give an example of casting from int to long.
  2. Give an example of casting from float to double.
  3. Give an example of casting from byte to int.
A
// 1
int smallerNumber = 10; 
long largerNumber = smallerNumber; // Casting int to long

// 2
float smallerFloat = 3.14f; 
double largerDouble = smallerFloat; // Casting float to double

// 3
byte smallerByte = 5; 
int largerInt = smallerByte; // Casting byte to int
71
Q

What are the size, range, and default values associated with the
short data type in Java?

A

This data type has a size of 16 bits (2 bytes) and a range from -32,768 to 32,767. Its default value is 0.

72
Q

What is
casting between compatible data types?

A

This involves converting a value from a smaller range to a larger range in Java.

73
Q
Animal animal = new Dog(); 
Dog dog = (Dog) animal; // Casting Animal to Dog
A

Give an example of reference casting in Java.

74
Q

Give an example of checking type compatibility using the instanceof operator.

A

Example:

Animal animal = new Dog(); 
if (animal instanceof Dog) { 
    Dog dog = (Dog) animal; // Safe casting 
}
75
Q

This data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive). Its default value is ‘\u0000’.

A

What are the size, range, and default values associated with the
char data type in Java?

76
Q

What are the two types of
casting
in Java?

A

The two types of casting in Java are primitive casting and reference casting.

77
Q

What is
**casting **
in Java and what is its purpose?

A

This in Java refers to the process of converting a value of one data type to another.

Its purpose is to ensure compatibility between different data types

78
Q

This is the process of determining the appropriate method implementation based on the actual object type at runtime.

A

What is
dynamic method dispatch?

79
Q

What are the size, range, and default values associated with the
double data type in Java?

A

The double data type has a size of 64 bits (8 bytes) and a range Sufficient for storing 15 decimal digits. Its default value is 0.0d.

80
Q

Example:

Animal animal = new Dog(); 
if (animal instanceof Dog) { 
    Dog dog = (Dog) animal; // Safe casting 
}
A

Give an example of checking type compatibility using the instanceof operator.

81
Q

This in Java refers to the process of converting a value of one data type to another.

Its purpose is to ensure compatibility between different data types

A

What is
**casting **
in Java and what is its purpose?