# 07 - Casting and Range of Variables

1. Casting is the action of _______________________ two different data types such as converting an int to a double and vice versa.

differentiating between

seperating

converting between

concatenating

2. This example shows that we have converted the double value 15.23 into an integer, which left only the whole number and the decimal places to be cut off.
```public class CastingExample {

// this converts 15.23 into an integer
public static int exampleVariableOne = (int) 15.23;
public static double exampleVariableTwo = exampleVariableOne;

public static void main(String[] args) {
System.out.println(exampleVariableOne);
System.out.println(exampleVariableTwo);
}
}```

FALSE

TRUE

3. Even though it is possible to convert an int to a double without casting, it is best practice to always use casting for precision and thorough logic.

FALSE

TRUE

4. When you convert an int to a double, a _____________ will be added to match with the structure of a double. For example, double varOne = (double) 15; assigns 15.0 to varOne.

decimal point

special character

space

real number

5. When casting (e.g from a double to an integer) it is possible to lose:

binary bits

precision

range

exponential value

6. What is printed as a result of executing this code segment?
```double piValue = 3.14159;
int circumference = (int)(2 * piValue * radius);
System.out.println(circumference);```

31416

31415

31415.9

30000

7. Variables that are declared inside a method are called _______________ because they can only be utilized and referenced in the method itself.

local

bifocal

global

referenced

8. In the following example, we cannot print exampleVariableThree inside the main method because ?
```public class LocalVariableExample {

public static int exampleVariableOne = 10;
public static int exampleVariableTwo = 6;

public static void main(String[] args) {
// System.out.println(exampleVariableThree) will cause an error
// because exampleVariableThree is a local variable
}

public static int add(int x, int y) {
// this is a local variable
int exampleVariableThree = x + y;
return exampleVariableThree;
}
}```

it exists as a static int which has not yet been converted in the add() method

it exists only as a local variable in the add() method.

it exists only as a global variable in the add() method

it exists as an integer and has not been cast in the add() method

9. A local variable is not visible to any other method besides the one in which it exists. If you have a variable that will be used in multiple classes, you must make sure it is ____________________.

beside the method

outside the method

inside the method

outside the curly brackets of the bracket

10. If you are referencing a local variable outside of the method it exists in, you will get a ___________ due to access control and visibility.

method encryption error

casting error

compile-time error

global value error

11. Which variables in the code below are local variables?
```public class TestClass()
{
int varOne = 100;
int varTwo = 25;

public static void main (String[] args)
{
int varThree = 5;
System.out.println("Hello World!");
exampleMethod(5);
}

public static int exampleMethod(int paramOne)
{
int varFour  = 50;
System.out.println(paramOne);
System.out.println(varFour);
}
}```

only paramOne

varOne, varTwo, varFour

varThree, varFour, paramOne

varOne,varTwo, paramOne

12. In the above code segment, paramOne is a parameter, and parameter are also considered local variables.

TRUE

FALSE

13. In the code segment above, varOne is a field variable. Field variables are _____________________________.

declared separate to the program

declared as a member of the main method only

declared outside any given class (e.g. similar to global variables)

declared as a member of a class

14. Field variables, are similar to local variables and they can NOT be called in any of the methods that exist in the same class.

FALSE

TRUE

15. Instance variables are non-static fields and therefore declared without _____________________________.

any casting

the Java reserved word static.

any curly brackets around them

a trailing semi-colon

16. There are two categories of field variables: _______ variables and ________ variables.

numeric / non numeric

instance / class

integer / string

finite / infinite

17. There are two field variables in this code segment. If you add the values held in these variables together, you would get:
```public class TestClass()
{
int varOne = 100;
int varTwo = 25;

public static void main (String[] args)
{
int varThree = 5;
System.out.println("Hello World!");
exampleMethod(5);
}

public static int exampleMethod(int paramOne)
{
int varFour  = 50;
System.out.println(paramOne);
System.out.println(varFour);
}
}```

100

55

100

125

18. Casting a double value to an int causes the digits to the right of the decimal point to be _____________.

updated

concatenated

truncated

19. Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. This suggests that?..

an int value must never be cast to double unless it is cast back at some point

an int value must be in the range from Integer.INFINITE to INFINITE

an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_ VALUE inclusive.

an int value can be in any range whatsoever

20. If an expression would evaluate to an int value outside of the allowed range, an _______________ occurs. This could result in an incorrect value within the allowed range.

integer overflow

int data type error

casting error

fixed range concatenation

21. ___________ is the process of providing value to a variable at declaration time. Any attempt of setting a variable's value after its declaration is called assignment

Modularization

Initialization

Localization

Globalization

22. Automatic type conversion is done when either both sides (left and right) are of same type or the left hand (destination) side is larger in size. The latter is called widening.

TRUE

FALSE

23. Why will the following code result in a compilation error?
```/* StaticTypedDemo.java */
public class StaticTypedDemo
{
public static void main(String[] args)
{
byte s = 90;
s = s + 2; // Type mismatch: cannot convert from int to byte
System.out.println(s);
}
}```

Because, in the statement s = s + 2; the term 's' is actually a floating point which cannot be type cast

Because, in the statement s = s + 2; the intermediate term s + 2 is automatically promoted to int and therefore, cannot be assigned to a byte without the use of a cast.

Because, in the statement s = s+2; the 2 is a literal and has not been explicitly cast, which causes the error

Because, in the statement s = s + 2; the term s+2 is not an int and therefore can only be assigned to a byte with the use of a cast

24. Fill in the blanks in the following paragraph.
```Data members or fields or variables of a class that are declared
non-static, but outside a method, constructor or any block are
known as _______________. _____________________ are created when an
object of a class is created by using new keyword.```

global variables

static variables

field variables

instance variables

25. And here's a bonus question on expression evaluation: What is printed as the result of executing this code segment?
```double varOne;
int varTwo = 56;
int varThree = 25;
varOne = varTwo / varThree;
System.out.println(varOne);```