All Flashcards
What are the steps to round a positive double to the nearest integer using casting?
- Add 0.5 to the double. 2. Cast the result to an int.
What are the steps to round a negative double to the nearest integer using casting?
- Subtract 0.5 from the double. 2. Cast the result to an int.
What are the steps to convert from double to int?
- Use explicit casting: (int) doubleValue.
What are the steps to calculate the average of three doubles and round it to the nearest integer?
- Calculate the average: (num1 + num2 + num3) / 3.0. 2. Add 0.5 to the average. 3. Cast the result to an int.
What are the steps to handle potential integer overflow?
- Check if the result exceeds Integer.MAX_VALUE or Integer.MIN_VALUE. 2. If it does, handle the overflow appropriately (e.g., throw an exception or use a larger data type).
What is casting?
Converting a variable from one data type to another.
What is widening conversion?
Automatic conversion from a smaller data type to a larger one (e.g., int to double).
What is narrowing conversion?
Manual conversion from a larger data type to a smaller one (e.g., double to int). Requires explicit casting.
What is integer overflow?
Occurs when an integer exceeds its maximum or minimum possible value.
What does truncation mean in casting?
Removing the decimal part of a floating-point number when converting it to an integer.
What is Integer.MAX_VALUE?
The largest possible value for an int.
What is Integer.MIN_VALUE?
The smallest possible value for an int.
What is the syntax for casting a double to an int?
(int) doubleValue
What is explicit casting?
Manually converting one data type to another using the cast operator.
What is implicit casting?
Automatic conversion performed by the compiler without explicit casting.
What does the following code output?
java
double x = 7.9;
int y = (int) x;
System.out.println(y);
7
What does the following code output?
java
double a = 3.1;
double b = 2.8;
int c = (int) (a + b);
System.out.println(c);
5
What does the following code output?
java
double num = -2.3;
int rounded = (int) (num - 0.5);
System.out.println(rounded);
-2
What does the following code output?
java
int max = Integer.MAX_VALUE;
System.out.println(max + 1);
-2147483648
What does the following code output?
java
double value = 5.5;
int result = (int) (value + 0.5);
System.out.println(result);
6
What does the following code output?
java
double value = -5.5;
int result = (int) (value - 0.5);
System.out.println(result);
-6
Identify the error in the following code:
java
double x = 10.5;
int y = x;
Cannot implicitly convert from double to int. Requires explicit casting: int y = (int) x;
What does the following code output?
java
int a = 5;
double b = 2.0;
double result = a / b;
System.out.println(result);
2.5
What does the following code output?
java
double a = 5.0;
int b = 2;
double result = a / b;
System.out.println(result);
2.5
What does the following code output?
java
int a = 5;
int b = 2;
double result = (double) a / b;
System.out.println(result);
2.5