Java Questions 41 - 50  «Prev  Next»

Java Questions 45

  1. Remember that a byte is signed with the leftmost bit representing the sign.
    Answer:
  2. What are the casting capabilities of the compound assignment operator +=?
    Answer:
    The compound assignment operator += lets you add it to the value of b without putting in an explicit cast.
  3. What do the following compound assignment operators result in?
    += 	-=	*=	/= 
    

    Answer:
    package com.java.operators;
    
    public class CompoundOperators {
    	double myDouble2 = 10.2;
    	int a = 10;
    	int b = a;
    	float float1 = 10.2F;
    	float float2 = float1;
    
    	public void math() {
    		b += a;
    		System.out.println(b);
    		a = b = 10;
    		System.out.println(a);
    		b /= a;
    		System.out.println(b);
    	}
    
    	public static void main(String[] args) {
    		CompoundOperators c1 = new CompoundOperators();
    		c1.math();
    	}
    }
    
    Output:
    20
    10
    1
    
  4. What is the rule regarding inheritance for the assignment of types?
    Answer:
    The rule is that you can assign a subclass of the declared type, but not a superclass of the declared type.
  5. What is the java.lang.ClassCastException?
    Answer
    Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
    Object x = new Integer(0);
    System.out.println((String)x);
    
  6. What are the 4 basic scopes?
    Answer:
    1. Static variables
    2. Instance variables
    3. Local Variables
    4. Block variables
  7. What is The most common reason for scoping errors?
    Answer:
    The most common reason for scoping errors is when you attempt to access a variable that is not in scope.
  8. Can Unicode be assigned to the following wrapper objects?
    1. Integer
    2. Long
    3. Float
    4. Double

    Answer:
    Only if you use a cast.
    package com.java.assignments;
    
    public class UnicodeAssign {
    	char a = 'a';
    	char letterN = '\u004E';
    
    	public static void main(String[] args) {
    
    		Integer I1 = (int) '\u004E';
    		Long L1 = (long) '\u004E';
    		Float f1 = (float) '\u004E';
    		Double d1 = (double) '\u004E';
    	}
    }
    
  9. What are local variables sometimes called ?
    Answer:
    Local variables are sometimes called
    1. stack
    2. temporary
    3. automatic
    4. method variables
    The rules for these variables are the same regardless of what you call them.
  10. What is the scope of instance variables?
    Answer:
    Instance variables (also called member variables) are variables defined at the class level.