This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line! See this thread for details.
List<String> test = new ArrayList<String>(); System.out.println(test.contains(42));
Here test is a list reference can only contain the Strings. When what is the use of checking other types ( here 42 Integer) in test by using contains method? why API written for contains method accepting the object of type 'Object'. Can any one calrify me about this?
My doubt is that option (a) is not correct when we initialize instance variables like the above. So option (a) looks vague.then we should avoid it while answering it right?
a)Instance variables are always initiated to defaults b)Local variables are always initiated to defaults c)Array Elements are always initiated to defaults d)Object references that are not initialized explicitly will have their value set to null always
Answer given to this question is a) and c). But a) is correct when instance variables are not explictly initialized an value. can any one clarify this?
public class ExternalizableClass { public static void main(String...strings) throws IOException,ClassNotFoundException { TransientWriter tw = new TransientWriter(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tw.ser")); out.writeObject(tw); ObjectInputStream input = new ObjectInputStream(new FileInputStream("tw.ser")); TransientWriter tw2 = (TransientWriter) input.readObject(); System.out.println(tw2); }
} class TransientWriter implements Externalizable { private String s = "Hope i ever be persisted";
public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException { s = (String) arg0.readObject(); }
public void writeExternal(ObjectOutput arg0) throws IOException { arg0.writeObject(s);
} public String toString() { return s; } }
I am getting Runtime exception
Exception in thread "main" java.io.InvalidClassException: Java.mockTests.javablackbelt.AbilaskKoneri.TransientWriter; no valid constructor at java.io.ObjectStreamClass.<init>(Unknown Source) at java.io.ObjectStreamClass.lookup(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at Java.mockTests.javablackbelt.AbilaskKoneri.ExternalizableClass.main(ExternalizableClass.java:17)
Source : javablackbelf.com 1) If a Runtime Exception is thrown in the finalize method
a)The running application crashes. b)The exception is simply ignored and the object is garbage collected. c)The exception is simply ignored, but the object is not garbage collected. d)The Exception causes the JVM to crash.
My Answer is : d). JVM wont collect the object when the runtime exception occurs. Then it garbage collects it next time wihtout calling the finalize() method.
correct answer given is : b)
2) When does the JVM exit?
a)After the main method returns. b)After all the non-daemon threads created by the application complete. c)After all the daemon threads created by the application complete d)When a thread executes System.exit(); e)When an uncaught exception is thrown in a non-demon thread. f)When an uncaught exception is thrown in a demon thread.
public class GenericClassDefinition<T super Number> { public static void main(String...strings) {
}
}
T extends Number :- Compiles fine T super Number :- Giving error.
Why compiler is not allowed me to declare 'T' Type as T super Number? I searched for similar topic here, not able to find Can anyone provide me link if it is already discussed here? Otherwise can anyone explain me it?