Bookmark and Share

Front Back
float f = 1.0f; double d = d + f; System.out.println(d); What prints?
Compiler complains that d might not have been initialized. This is true even if at the member variable level (vs. the method local level).
float f = 1.0; double d = f; System.out.println(d); What prints?
Compiler complains about loss of precision. 1.0 is a double which is bigger than float f.
Instance variables. Stack or Heap?
Heap. Because instance variables live inside an object and that object is on the heap then they too live on the heap!
Objects. Stack or Heap?
Local variables. Stack or Heap?
Stack. Each thread has its own stack while all threads in an application share 1 heap.
When do you use the digit 8 in an octal representation of a number?
Never. An octal number begins with the digit 0 and then is followed by a series of digits each with a range of 0 to 7.
What does a hexidecimal value begin with?
ox or oX.
Is the following a legal? long temp = 0XDeadCafel;
char c = 1248; Compiles? Runs?
Yes and yes. Char is an integral type though not an integer (byte short int long).
When can implict narrowing primitive conversions during assignment occur?
When... 1) The source is a constant expression. 2) The source is of type byte short char or int. 3) The target is if type byte short or char. 4) The value of the source it determined to be in the range of the target type at compile time. ------- Also.. using shortcut assignment operators implicitly do narrowing conversions (i.e. +=)
String str = ""\\\\u0041""; System.out.println(str); What prints out?
\u0041 If there has only been 1 slash in the String then A would have printed out. (unicode \u0041 = capital A)
Char c = \u0041; Will this compile?
No. Literal unicode values being assigned to a char must occur inside single quoutes.
Char c = ""\u0041""; Will this compile?
No. Double quotes will cause compiler to complain that an assignment of incompatible type is being attempted. Single quoutes are required.
What 2 non-primitives have literal represenations?
String Objects... String s = ""hello""; Array Objects... int[] i = {1 2 3};
Does an object reference always refer to an object on the heap?
No. The reference might have the value of null. But all objects do live on the heap.
byte b1 = 1; byte b2 = 1; byte b3; b3 = b1 + b2; int total = b3; Does this compile?
No. The result of integral arithmetic expressions is always type int. The assignment of such an int value to b3 causes compiler to complain that there is a possible loss of precision.
int i = (int)7.9f; What is the value of i?
7. All decimals are dropped when casting from floating points to integral types.
When casting from larger integral types to smaller integral types what get's removed?
The high order bits on the left simply get dropped. No adjustment is done to preserve the sign of signed types.
byte b = 128; Compiler error? byte b = (byte)128; Compiler error? If not what is the value of b?
Yes and No. In the first case a byte can only hold up to +127 so the compiler complains. Adding the explicit cast essentially is a message from the user saying ""I know that that this doesn't quite fit in here but trust me... I know what I'm doing."" In this second case the eigth bit (which was a 1) becomes the high bit which is also the signed bit. This means our number is negative. b = -128
byte b = 1; b += 1; Compiles?
Yes. b += 1 is shorthand for b = (byte)b + 1; These compound operators implicitly add a cast. b = b + 1 would NOT compile;
What are the 4 basic scopes of variables from longest to shortest scope?
Static Instance Local Block
What is the default value of a char member variable?
'\u0000' This is the character equivalent of null.
What's the difference between null and the empty string ("""")?
null says that a reference does not refer to an object on the heap. When the empty string is assigned to a String reference there is a String object on the heap with the value of the empty string and the String reference points to that object.
What are 2 other names for ""automatic"" variables?
1) local variables 2) stack variables
Is a reference variable that isn't pointing to an object always null?
No. When a reference variable is declared locally it does not get initialized by default. Until an explicit initialization it is NOT null it also does NOT point to an object.
What is shadowing?
Shadowing hides variables in higher scopes with declarations of same-named variables in lower scopes. local/block can shadow static/member variables. NOTE: a member variable CANNOT shadow a static variable nor can a local block shadow a local variable. An initializer block (at the member level) can shadow a static or member variable but after the object is constructed that shadowed variable is no longer available. Local variables can shadow static and member variables.
Explain Declare-Construct-Initialize in terms of arrays.
Declare is creation of reference variable that will point to an array object at some point. Construct is the creation of the array object. Initialize is populating the array object with elements.
int[][]i[][]; legal?
Yes. Not recommended but legal. int[][][][] i; (preferred)
When creating an array object (construct phase of array generation) what is the technique used to build the array object without setting a limit on the number of slots it will have?
There is no such technique. You MUST declare directly or indirectly how much space an array object will need when placing it on the heap. You set the size indirectly by initializing it with a number of items whose number also becomes the size of the array.
int[][][]i = new int[2][3][]; Legal?
int[] i = new int[4]; i[4] = 100; Does this compile?
Yes. But it throws a runtime ArrayIndexOutOfBoundException.
int[] i = new int[3]; int z = -2; i[z] = 100; Does this compile? Run? If not what exceptions get thrown?
Yes. Compiles fine but causes a runtime exception of type ArrayIndexOutOfBoundsException.
int[] i = new int[4]; System.out.println(i.length()); What prints out?
Compiler error. Arrays have a member variable called ""length"" not a member method called ""length()"".
What is the syntax for creating an anonymous array?
new TYPE[] {val1 val2 ... valN} Notice that there is no number inside the square brackets. When constructing an array object you EITHER: 1) declare a size inside the square brackets OR 2) you let that size be deduced from the number of elements you place inside curly brackets. 3) you use this annoymous array syntax Only at the time of declaration can you use #2. At all other times if you want to construct a new array for an existing already declared array reference -- you have to use #1 or #3
What are the two syntaxes for custom initializing an array object at the same time you declare the array?
int[] x = {1 2 3}; OR int[] x = new int[]{1 2 3}; NOTE: This second format is actually using the ""anonymous array"" syntax to first create an anonymous array - which is then assigned to the array reference.
short[] s = new short[]; Compile?
No. When creating an array object (construct) you MUST tell compiler how much space will be needed on heap. Thus the second set of square brackets needs a 0 or postive integer inside of it.
short[] s = new short[1]; int[] x = s; Does this compile?
No. Think of int[] not as a primitive but as object of type Integer[]. Integer and Short wrapper classes both descend from Number -- they cannot be assigned to each other. This is exactly how arrays of primitives behave.
Car[] cars; Honda[] hondas = new Honda[5]; cars = hondas; Will this compile?
String[][][] i = new String[3][2][2]; i[1] = new String[7][7]; i[2] = new String[7][7]; Legal?
Yes. While an array reference must honor the dimensions of the array object it points at it can be set to point to an array of different array size than was initially declared.
String[][][] i = new String[3][2][2]; i[1] = new String[3]; Legal?
No. The assignment to i[1] is an attempt to assign an array of different dimentions than what i[1] knows it should hold. Compiler complains about a type mismatch as String[][] is a different type than String[].
When does a static initialization block get called?
Right after a class get's loaded. NOTE: If a class has a static main method the class is fully loaded (including static blocks evaluated) before the main method is called.
When does an instance initialization block get called?
Immediatedly after the call to super(). Any non-default initialization of instance variables and initializers happens at the same time in the order they're encountered
How can an initialization block centralize code?
Code that might occur in many constructors of the same class can be pulled out and written once in a single initialization block.
What are the steps in creating an initial object state?
1. Instance variables are initialized to default values (regardless of whether there exist expressions intended to assign non-default values to these same variables). 2. The constructor is invoked. 3. Local chaining of constructors might take place (this()). 4. At the end of the local chain of constructor invocations before that constructor's execution resumes the superclass constructor is either implciitly or explicitly invoked (super()). 5. A chain of superclass constructor calls might be called. 6. All superclass constructor calls are completed and control returns to the local constructor just after the implicit or explicit call to a form of super(); 7. instance initialization blocks AND instance variable initialization expressions are both evaluated in the order they occur. 8. The local constructor completes and returns as does any local constructor it was chained to.
Integer i = 2; Long l = 2l; System.out.println(i.equals(l)); System.out.println((int)i == (long)l); What prints?
false true
Integer i = 2; Long l = 2l; System.out.println(i == l); What prints?
Compiler error. Incompatible types. Think of this problem as comparing a Cat and a Dog. You might compare a Cat to an Animal but can't compare Cat & Dog
If a serializable object HAS-A another object that is not serializable what happens when the code is compiled?
It compiles just fine. Any attempts though to serialize the objects out to a ObjectOutputStream will cause a runtime error.
If a serializable object IS-A child of a parent that is not serializable what happens when the code is compiled? run?
Compiles fine. Runs fine. When a serialized object is unserialized (""brought back to life"") the first ancestor that isn't marked as serializable will simply have it's constructor called (as will all it's ancestors - even if some of its ancestors are serializable!!!). For all below this cutoff ancestor state is simply restored without any call to constructors.
System.out.printf(""%f"" 123); Compiles? Runs?
Yes (compiles) and No(doesn't run). Print formatting does not automatically promote int to float. This error does not occur until runtime.
System.out.printf(""%f"" 123.0); Compiles? Runs?
Yes and yes. Though the literal is a double print formatting can adequately handle it as a float.
x of y cards Next >|