Java Questions 41 - 50  «Prev  Next»

Java Questions 42

  1. Where do the methods, variables, and objects of Java programs live?
    Answer:
    They live in one of two places.
    1. Stack: primitive variables and types live on the stack.
    2. heap: Objects live on the heap
  2. Which 3 types should you focus on when studying Java? Answer:
    1. Instance variables represent an instance of the class
    2. Local variables are defined within a method or block
    3. Objects live on the heap

    There are 3 reference types. 1) interface, 2) Class and 3) Arrays
  3. Where do instance variables and local variables reside?
    Answer:
    1. Instance variables and objects live on the heap.
    2. Local variables live on the stack
  4. What are three ways to represent an integer number in the Java language?
    Answer:
    Values of the integral types byte, short, int, and long can be created from int literals.
    Values of type long that exceed the range of int can be created from long literals. Integer literals can be expressed by these number systems:
    1. Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day
    2. Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F
    3. Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later)
  5. Give an example of a decimal literal in Java:
    Answer:
    1. int length = 10l;
    2. int octalNumber = 0172;
  6. How do you represent an integer in Octal form in Java?
    Answer:
    In Java, you represent an integer in Octal form by placing a zero in front of the number.
    The following will return the value of 122 for the octal number 172:
    int octalNumber = 0172; 
    
  7. Will Java accept capital or lowercase letters for the extra digits in Hex?
    Answer:
    Java will accept capital or lowercase letters for the extra digits in Hex.
    [A - 10, 11 - B, 12- C, 13- 4 , 14- D, 15- F]
    
    int intValue = 0x1c; //hex digits may be upper- or lowercase
  8. What are 1) octal 2) decimal and 3) hexadecimal defined as by default?
    Answer:
    All three integer literals 1) octal 2) decimal 3) hexadecimal are defined as int data type by default.
  9. How are floating point numbers defined in Java?
    Answer:
    Floating - point numbers are defined as 1) number 2) decimal symbol (followed by) 3) additional numbers representing the fraction.
    Java has IEEE 754 single and double precision types supported by keywords:
    1. float f = 0.1f; // 32 bit float, note f suffix
    2. double d = 0.1d; // 64 bit float, suffix optional
  10. How can you remember that double contains more bits than float?
    Answer:
    A double contains 64 bits and a float contains 32 bits. A double contains twice the number of bits as a float.
    Float g = 498.47F