Bin Wang

Ranch Hand
+ Follow
since May 01, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Bin Wang

I passed the exam on June 30.
I was very nervous before the exam, but after taking several questions, I calmed down. The exam was not as difficult as I imagine. In fact, the questions that I had were pretty easy. It took me 80 minutes to go through all the questions, then I went back to finish the two questions that I left blank and double checked the marked questions.
I didn't take a lot of mock exams. I read the complete Java certificate guide twice, write a lot of small codes (about 150), go through most of the questions posted on JavaRanch everyday.
Thank you JavaRanch and good luck everybody!
16 years ago
How many objects are created by the following code?
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBuffer("abc");
Answer given is: 3
Can somebody explain?
public class Test {

public static void main(String[] args) {
Test t=new Test();
t.amethod();
}

int amethod () {
return -1;
}
}
The program compile and run (putput nothing). So it's not necessary to assign t.amethod to an int variable?
can somebody explain this? Thanks.
public boolean add(Object o) in API
...
Vector v=new Vector(10);
v.add("String"); //no compile error
....
Why it's not necessary to assign v.add("String") to a boolean variable?
//program 1
class ThreadTest14 extends Thread {
String name;
public static void main(String[] args) {
new ThreadTest14("first").start();
new ThreadTest14("second").start();
}
ThreadTest14(String s) {
name=s;
}

public void run() {
for (int i=0;i<2;i++) {
System.out.println(name + i);
}
}
}
For program 1, the order of output is uncertain. But no matter what the order is, "first 0" will be the first element, right?
//program 2
class ThreadTest14 extends Thread {
String name;
public static void main(String[] args) {
new ThreadTest14("first").start();
new ThreadTest14("second").start();
}
ThreadTest14(String s) {
name=s;
}

public void run() {
for (int i=0;i<2;i++) {
System.out.println(name + i);
try {
Thread.sleep(1);
}
catch(InterruptedException e) {}
}
}
}
For program 2, I think the uncertainty is avoided by using Thread.sleep()method no matter how short current thread will sleep and the output will always be:
first 0
second 0
first 1
second 1
Am I right? Please help.
For the following two programs, suppose file Hello.txt doesn't exist.
//program 1:
import java.io.*;
public class TryCatchTest2 {
public static void main(String[] args) {
TryCatchTest2 t=new TryCatchTest2();
System.out.println(t.amethod());
}
public int amethod() {
try {
FileInputStream fis=new FileInputStream("Hello.txt");
}
catch (IOException e) {
System.out.println("catch");
return 2;
}
finally {
System.out.println("finally");
}
return 1;
}
}
Putput:
catch
finally
2
//program 2
import java.io.*;
public class TryCatchTest2 {
public static void main(String[] args) {
TryCatchTest2 t=new TryCatchTest2();
System.out.println(t.amethod());
}
public int amethod() {
try {
FileInputStream fis=new FileInputStream("Hello.txt");
}
catch (IOException e) {
System.out.println("catch");
return 2;
}
finally {
System.out.println("finally");
return 1;
}
}
}
output:
catch
finally
1
Can somebody explain why program 1 return 2, not 1? If the checked exception is caught and after finally block, the rest of the method should be executed, right?
Hi Junilu, Sandeep, Sandip, April:
Thanks. So the answer should be True, right?
An Interface can never be private or protected.
True or False?
I still don't know what the purpose of interface within class/interface.
Eric,
The following code is answer D in my book, and it's one of the correct answers.
public class SomeClass {
public void aMethod() {
try {
risky();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

Originally posted by Scott Appleton:
Kevin, I'll stand by my response to your original post unless someone convinces me otherwise (http://www.javaranch.com/ubb/Forum24/HTML/010384.html)
I concur with William's answer to your second question -- you've created 2 separate objects for the 2 threads to act upon, so they are acting completely independently of each other and the synchonization of the run() method is irrelevant in this example. If you point both threads to either hey or that, then you would get the output you were expecting.


Hi Scott,
First of all, I think the sample code and the wording of question1 is not very good. Anyway, it's ok.
Then I think I agree with you. B should be the correct answer. But it's hard to get 'true' output, isn't it? I tried and tried but all outputs are 'false' until I changed the setXY method a little bit (maybe it still outputs 'false' on other people's computers). Anyway, it has the potential to output 'true', right?
class SyncTest {

private int x;
private int y;

private synchronized void setX (int i) { x=i;}

private synchronized void setY (int i) {y=i;}

public void setXY (int i) {
setX(i);
try{
Thread.sleep(1000);
setY(i);
}
catch(InterruptedException ie) {}
}

public synchronized boolean check() {return x != y;}
}
class SyncTest1 {
static SyncTest st=new SyncTest();

static class T1 extends Thread {
public void run() {
st.setXY(1);
}
}

static class T2 extends Thread {
public void run() {
System.out.println(st.check());
}
}
public static void main(String[] args) {
new T1().start();
new T2().start();
}
}
Kevin,
Another special case of String class as far as I know:
System.out.println("STRING".toUpperCase() == "STRING");//true

Originally posted by JUNILU LACAR:
False. Because you can have an interface inside a top-level class and declare it to be private or protected.


Junilu,
Thanks. But what the purpose of "inner interface"? Could you please give me a piece of sample code?
Thanks again.
An Interface can never be private or protected.
True or Faluse? Why?
System.out.println("string" == "string"); //output: true
System.out.println("string".toString() == "string"); //output: true
System.out.println(" string ".trim() == "string"); //output: false
So " string ".trim() returns a new String instance of "string" which is not the literal String instance "string"?
//program1
public class ThreadTest {
public static void main(String []args) {
final StringBuffer s1=new StringBuffer();
final StringBuffer s2=new StringBuffer();
new Thread() { //anynomous class
public void run() {
synchronized(s1) {
s1.append("a");
}
synchronized(s2) {
s2.append("b");
}
System.out.println(s1);
System.out.println(s2);
}
}.start();

new Thread() {//anynomous class
public void run() {
synchronized(s2) {
s2.append("c");
}
synchronized(s1) {
s1.append("d");
}
System.out.println(s2);
System.out.println(s1);
}
}.start();
}
}
For program1, deadlock will never happen, but the output is dependent on the threading model of the sysstem the program is running on.
//program2
public class ThreadTest{
public static void main(String []args) {
final StringBuffer s1=new StringBuffer();
final StringBuffer s2=new StringBuffer();
new Thread() {
public void run() {
synchronized(s1) {
s1.append("a");
synchronized(s2) {
s2.append("b");
}
}
System.out.println(s1);
System.out.println(s2);
}
}.start();

new Thread() {
public void run() {
synchronized(s2) {
s2.append("c");
synchronized(s1) {
s1.append("d");
}
}
System.out.println(s2);
System.out.println(s1);
}
}.start();
}
}
For program2, the output is a not-deterministic point because of a possible deadlock condition although it run well on my machine.
output:
a
b
bc
ad
Scott, am I right?
Integer LITERALS can be assigned to byte, short and char if the interger LITERALS are within the range of byte, short and char.
100+10=110, 110 is an integer literal and within the range of byte (-128 -- 127), so it can be assigned to byte type variable b.