aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes unable to find error in the code? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "unable to find error in the code?" Watch "unable to find error in the code?" New topic
Author

unable to find error in the code?

Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Hi everybody,
What should be the output of the following code? The answer given is wrong in my opinion. Please help.
Q. If you compile the following code what will be the result?
public class MyClass {
public static void main(String args[]) {
String str1 = "Test One";
String str2 = new String("Test One");
if ( str1.equals( str2) ) {
System.out.println("Both are equal");
}
boolean b = true;
boolean b1 = false;
if ( b.equals(b1) ) {
System.out.println("true");
}
}
}
[a] Compile time error
[b] Runtime error.
[c] No output
[d] "Both are equal" followed by "true"

Thanx
Jyotsna
Haining Mu
Ranch Hand

Joined: Jun 01, 2001
Posts: 51
Originally posted by Jyotsna Umesh:
Q. If you compile the following code what will be the result?
public class MyClass {
public static void main(String args[]) {
String str1 = "Test One";
String str2 = new String("Test One");
if ( str1.equals( str2) ) {
System.out.println("Both are equal");
}
boolean b = true;
boolean b1 = false;
if ( b.equals(b1) ) {
System.out.println("true");
}
}
}
[a] Compile time error
[b] Runtime error.
[c] No output
[d] "Both are equal" followed by "true"

Compile error, equals() method can not be used for primitive. though first part will be "equal"
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Originally posted by Haining Mu:
Compile error, equals() method can not be used for primitive. though first part will be "equal"


Thanks Haining, i just missed it.
Jyotsna
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12271
    
    1
Another case where writing a test case and trying to compile it would have answered your question faster than posting to JavaRanch.
The best way to learn Java is to write LOTS of test cases.
Bill


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to find error in the code?
 
Similar Threads
String question
Referencing to another object
String handling.
Equality Comparison Operators
comparing Strings