Explanation: These are the six facts: 1. Literal strings within the same class in the same package represent references to the same String object.
2. Literal strings within different classes in the same package represent references tothe same String object. (So line 1 prints true)
3. Literal strings within different classes in different packages represent references to the same String object. (So line 2 prints true)
4. Strings computed by constantexpressions are computed at compile time and then treated as if they were literals. (So line 3 prints true)
5. Strings computed at run time are newly created and therefore are distinct. (So line 4 prints false)
6. The resultof explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. (So line 5 prints true) (credit: www.jdiscuss.com)
* * * *
[252] 2
Explanation: A class or interface may be unloaded if and only if its class loader is unreachable (the definition of unreachable is given in JLS 12.6.1). Classes loaded by the bootstrap loader are not unloaded. (credit: www.jdiscuss.com)
* * * *
[253] 2
Explanation: It will print "Forget It" because before the division could take place doIt() throws an exception.
Java guarantees that every operand of an operator (except the conditional operators &&, ||, and ? :) appears to be fully evaluatedbefore any part of the operation itself is performed.
If the binary operator is an integer division / or integer remainder % , then its execution may raise an ArithmeticException, but this exception is thrown only afterboth operands of the binary operator have been evaluated and only if these evaluations completed normally. (credit: www.jdiscuss.com)
* * * *
[254] 3
Explanation: Remember : variables are shadowed and methods are overridden.
Which variable will be used depends on the class that the variable is declared of. Which method will be used depends on the actual class of the object that is referenced by thevariable. So, in line o1.m1(), the actual class of the object is C, so C's m1() will be used. So it retruns 30. In line o2.i, o2 is declared to be of class B, so B's i is used. So it returns 20. (credit: www.jdiscuss.com)
* * * *
[255] 2
Explanation: First the value of str1 is evaluated (ie. one). Now, before the method is called the operands are evaluated, so str1 becomes "two". so "one".equals("two") is false.
* * * *
[256] 1,2,4
Explanation: As an anonymous class has no name another class would have nothing to put after the extends keyword. A constructor is a method with the same name as the class and no return type. If the class has no name there is no name to give a constructormethod. (credit: www.examulator.com)
* * * *
[257] 4
Explanation: The method print() if OutputStream takes an Object and prints out a string that is returned by calling toString() on that object. Note that as toString() is defined in Object class, all objects in java have this method. So it prints 2 first.
The second object's toString() returns null, so it prints "null". There is no NullPointerException because no method is called on null.
Now, the other feature of print/println methods is that if they get null as input parameter, they print "null". They do not try to call toString() on null. So, if you have, Object o = null; System.out.println(o); will print null and will not throw NullPointerException.
* * * *
[258] 5
Explanation: NaN is unordered, so a numeric comparison operation involving one or two NaNs always returns false and any != comparison involving NaN returns true, including x != x when x is NaN.
* * * *
[259] 1
Explanation: Note that, boolean operators have more precedence than =. (In fact, = has least precedenace) so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegalbecause false is a value and not a variable!
Had it been something like (b2 = b1 != b2) then its valid because it will boil down to : b2 = false. Because all an if() needs is a boolean, now b1 != b2 returns false which is a boolean andas b2 = false is an expression and every expression has a return value (which is actually the LHS of the erpression). Here it returns true which is again a boolean.
Note, return value of expression (i is int) : i = 10 , is 10 (anint).
* * * *
[260] 4,5
Explanation: public boolean retainAll(Collection c) retains only the elts in this collection that are contained in the specified collection. In other words, removes from this collection all of its elts that are not contained in the specified collection
public boolean removeAll(Collection c) removes all this collection's elts that are also contained in the specified collection. After this call returns, this collection will contain no elts in common with the specifiedcollection
public boolean containsAll(Collection c)
returns true if this collection contains all of the elts in the specified collection
public boolean addAll(Collection c) adds all the elts in the specified collectionto this collection. The behavior of this opern is undefined if the specified collection is modified while the opern is in progress(ie., the behavior of this call is undefined if the specified collection is this collection, and is nonempty)
* * * *
[261] 4
Explanation: 1.0/0.0 results in Double.POSITIVE_INFINITY. Double.POSITIVE_INFINITY is converted to Integer.MAX_VALUE ('0' followed by 31 '1's). Integer.MAX_VALUE is then cast to byte value, which simply takes the last 8 bits(11111111) and is -1.
* * * *
[262] 2
Explanation: PREVIOUSLY: If a class declares a class method classFinalize that takes no arguments and returns no result: static void classFinalize() throws Throwable { . . . } then this method will be invoked before the class is unloaded . Like thefinalize method for objects, this method will be automatically invoked only once. This method may optionally be declared private, protected, or public.
NOW: Class finalization has been removed from the Java language. Thefunctionality of JLS 12.7 is subsumed by instance finalization (JLS 12.6).Here is a rationale for this decision. http://java.sun.com/docs/books/jls/class-finalization-rationale.html Similar thing has happend toclass unloading: A class or interface may be unloaded if and only if its class loader is unreachable (the definition of unreachable is given in JLS 12.6.1). Classes loaded by the bootstrap loader may not be unloaded.
* * * *
[263] 2
Explanation: (1) This may result in a deadlock (3) The is not necessary. Option 2 works just fine
* * * *
[264] 2,3,5
Explanation: Inner class means a NON STATIC class defined inside a class. Top level nested class means a STATIC class defined inside a class.
One can define a class as a static member of any top-level class. Classes which are static class members andclasses which are package members are both called top-level classes. They differ from inner classes in a sense that a top-level class can make direct use only of its own instance variables. The ability to nest classes in this way allows anytop-level class to provide a package-like organization for a logically related group of secondary top-level classes, all of which share full access to private members.(credit: www.jdiscuss.com)
* * * *
[265] 2,4
Explanation: public > protected > package (ie. no modifier) > private
where public is least restrictive
Remember:
protected is less restrictive than package access. So a method(or field) declared as protected will be accessible from asubclass even if the subclass is not in the same package.
The same is not true for package access.
A class can only have either public or no access modifier but a method or field can have all the four. Note that static, final, nativeand synchronized are not considered as access modifiers
* * * *
[266] 5
Explanation: Frequently it is necessary to represent a value of primitive type as if it were an object. There are following wrapper classes for this purpose: Boolean, Byte, Character, Short, Integer, Long, Float, and Double.
Note that Byte,Short, Integer, Long, Float and Double extend from Number which is an abstract class. An object of type Double, for example, contains a field whose type is double, representing that value in such a way that a reference to it can be storedin a variable of reference type. These classes also provide a number of methods for converting among primitive values, as well as supporting such standard methods as equals and hashCode
* * * *
[267] 1,5
Explanation: A method can be overriden by defining a method with the same signature(i.e. name and parameter list) and return type as the method in a superclass. Only methods that are accessible can be overriden. A private method cannot therefore beoverriden in subclasses, but the subclasses are allowed to define a new method with exactly the same signature. A final method cannot be overriden. An overriding method cannot exhibit behaviour that contradicts the declaration of theoriginal method. An overriding method therefore cannot return a different type or throw a wider spectrum of exceptions than the original method in the superclass