Preview

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

  concatenating

  converting between

  seperating

 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.

  TRUE

  FALSE

 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.

  real number

  decimal point

  special character

  space

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

  range

  precision

  exponential value

  binary bits

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

  31415

  30000

  31416

  31415.9

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

  global

  referenced

  local

  bifocal

 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(add(exampleVariableOne, exampleVariableTwo));
    // 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 only as a global variable in the add() method

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

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

  it exists only as a local variable 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 ____________________.

  inside the method

  beside the method

  outside 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

  global value error

  compile-time error

  casting 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);
  }
}

  varOne,varTwo, paramOne

  varThree, varFour, paramOne

  only paramOne

  varOne, varTwo, varFour

 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 as a member of a class

  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)

 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.

  TRUE

  FALSE

 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.

  integer / string

  instance / class

  numeric / non numeric

  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

  125

  55

  100

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

  truncated

  concatenated

  added to

  updated

 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 be in the range from Integer.MIN_VALUE to Integer.MAX_ VALUE inclusive.

  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 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

  fixed range concatenation

  casting error

 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

  Localization

  Modularization

  Initialization

  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+2 is not an int and therefore can only be assigned to a byte with the use of a cast

  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

 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

  field variables

  static 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);