Win a copy of Terraform in Action this week in the Cloud forum!

Ram Reddy

Ranch Hand
+ Follow
since Feb 20, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ram Reddy

Thanks for good explanation !!!
My bad.
you are correct John...
I think the answer is 2.
Objects referenced by a1 & b2.

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?

rami

class Main{
String data = "SCJP"; //1
}



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?
Source : Javablackbelt.com

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?

rami
Thanks antonio.
That means , you are serializing an object , then its class constructor which accessed must be public. am i correct?

thanks
rami
Source : Javablackbelt.com


import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

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)



I am sorry if this doen't cover on SCJP.

rami
thanks vishwanth murthi.
That is really nice example
Hi,



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.



My Answer is : b) , d), e)

Correct answers given is b), d)


Can any one explain me these as per answers?

rami
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?

rami
hi mahajan,

"public class TadKebad extends Thread implements Runnable".


We can write like this.But it is not required.

"public class TadKebad extends Thread".



Both are syntactically correct