OCAJP8 Flashcards
If you invoke
System.out.println(exception);
will you see the stack trace on ths console?
No. Using System.out.println(exception) will NOT include the stack trace. It conly includes the name of the exception and the message.
True or False:
Static initializers can call static methods in their body.
True.
Is this a valid variable assignment?
int x, y, z, a; x = y = z = a = 15;
Yes. Variable assignments can be chained.
What will be the output of the following?
String s = null; s += “Hello”;
System.out.println(s);
The output will be “nullHello”. a String that is initialized as NULL is different from “”.
Given the following:
Integer x = new Integer(5);
x++;
Does the variable “x” still point to the same reference created in the the first line?
No. All the wrapper objects are immutable. So when you do x++, what actually happens is something like
x = new Integer( x.intValue() + 1);
Is Java using pass-by-value or pass-by-reference?
Java uses pass by value semantics in method calls. In case of primitive variables, their values are passed, while in case of Objects, their reference values are passed. Thus, when you assign a different object to reference variable in a method, the original reference variable that was passed from the calling method still points to the same object that it pointed to before the call.
Can the overriding method change the return type to a subclass of the original method?
Yes. Covariant returns are allowed since Java 1.5, which means that an overriding method can change the return type to a subclass of the return type declared in the overriden method.
When accessing static and instance variables, which class is being considered? Is it the reference or the actual instance?
Access to static and instance fields and static methods depends on the class of reference variable and not the actual object to which the variable points to.
When calling instance methods, which class is being considered? Is it the reference or the actual instance?
When calling instance methods, the method of the actual class of the the object is called.
Given the following call:
int x = 5; doOperation(x++);
What value of x will be passed to “doOperation”?
The value 6 will be passed. When there are operands in method parameters, they are first evaluated before calling the method.
What will the following code print?
void crazyLoop(){ int c = 0; JACK: while (c 3) break JILL; else c++; } }
It will not compile. Because break JILL; would be valid only when it is within the block of code under the scope of the label JILL. In this case, the scope of JILL extends only up till System.out.println(c); and break JILL; is out of the scope of the label.
Consider the following code:
class A{ A() { print(); } void print() { System.out.println(“A”); } }
class B extends A{ int i = 4;
public static void main(String[] args){ A a = new B(); a.print(); }
void print() { System.out.println(i); } }
What will be the output when class B is run ?
It will print 0, 4 Note that method print() is overridden in class B. Due to polymorphism, the method to be executed is selected depending on the class of the actual object. Here, when an object of class B is created, first A’s constructor is called, which in turn calls print(). Now, since the class of actual object is B, B’s print() is selected. At this point of time, variable i has not been initialized (because we are still initializing A at this point), so its default value i.e. 0 is printed. This happens because the method print() is non-private, hence polymorphic.
If an Exception is handled in a catch block and is not thrown, the execution continues.
If an Exception is handled in a catch block and is not thrown, the execution continues.
If the array reference expression produces null instead of a reference to an array, then a NullPointerException is thrown at runtime, but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally.
If the array reference expression produces null instead of a reference to an array, then a NullPointerException is thrown at runtime, but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally.
In Lambda Expressions, when is it required to put the method body inside curly braces.
In Lambda Expressions, you need to put the body within curly braces if you want to use the return keyword.
What does the add method of ArrayList return?
The add method of ArrayList returns a boolean. Further, it returns true if the list is altered because of the call to add.
Will an occurence of java.io.IOException be handled by a catch block that catches RuntimeException?
java.io.IOException extends Exception. It cannot be caught by a catch block that catches RuntimeException.
If there is an un-reachable code in a method, will it compile?
Un-reachable codes will cause compilation error except if it is inside an IF block.
Do ambiguous fields or methods cause compile time error?
Having ambiguous fields or methods does not cause any problem by itself but referring to such fields or methods in an ambiguous way will cause a compile time error.
Does a lambda expression create new scope for variables?
A lambda expression does not create a new scope for variables. Therefore, you cannot reuse the variable names that have already been used to define new variables in your argument list.
If an interface extends another interface with a default method, can it redeclare the default method?
An interface can redeclare a default method and also make it abstract.
If an interface extends another interface with a default method, can it redeclare the same default method and provide a different implementation?
An interface can redeclare a default method and provide a different implementation.
Can static methods be abstract?
Static methods can never be abstract (neither in an interface not in a class).
Is an interface allowed to have a static method?
An interface can have a static method but the method must have a body.