• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

I THROW QUESTIONS WHO CAN CATCH , CATCH IT.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends
I DON'T HAVE ANY DOUBT'S.
I DON'T HAVE ANY DOUBT'S.
I DON'T HAVE ANY DOUBT'S.
I DON'T HAVE ANY DOUBT'S.
I DON'T HAVE ANY DOUBT'S.
I DON'T HAVE ANY DOUBT'S.
I DON'T HAVE ANY DOUBT'S.
i saw some discussions yesterday going on in this forum. For helping those people who are involved in those disussion's i put some question on this page and request to all of the person who were involved in yesterdays discussion please try to Attempt these questions.
AND PLEASE BE HONEST WITH YOURSELF AND DON'T TRY TO FIND OUT THE ANS BY COMPILING AND RUNNING THE QUESTIONS. FIRST ATTEMPT THE QUESTION WITH YOUR KNOWLEDGE UP TO THIS INSTANT . THE QUESTIONS ARE VERY BASIC. I ALSO WANT COMMENT ON THESE QUESTIONS. BECAUSE I AM HAVING A LOT OF QUESTIONS OF THESE TYPE .PREVIOUSLY I DECIDED TO DESIGN A MOCK EXAM APPLET BUT NOW I THINK FOR FULL FILL OF MY GOLL I HAVE TO PUT MY QUESTIONARE HERE AND TRY TO GRAB THE OPENION OF ALL OF YOU ABOUT THESE. EVERY OF THE ABOVE QUESTION HAD A SOLID TECHNICAL REASON BEHIND. WHICH I WILL POST TOMMOROW AFTER GETTING YOUR'S COMMENTS.
Q.1) Read the following class definition carefully
public class Test extends Thread {
public synchronized void run() {
print();
}
public synchronized void print() {
System.out.print("JAVARANCH");
try {
Thread.sleep(5000);
}
catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
public static void main(String arg[]) {
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t2.start();
}
}
what will happened when you try to compile and run the class. (choose one)
1)it display on the sccreen JAVARANCH followed by another JAVARANCH without any delay.
2)display two times word JAVARANCH but a delay of 5 seconds is involed between two output
3)when runs initially make 5 second delay then print JAVARANCH and again after making a delay of 5 second again output JAVARANCH appeared
4)compile time error
Q.2 ) Read the following class definition carefully
public class test {
public static vod main(String arg[]) {
Integer i1 = new Integer(5);
Integer i2 = new Integer(6);
String S = new String("JAVARANCH");
System.out.println(i1 + i2 + S )
}
}
what will happened when you try to compile and run the class. (choose one)
1) it displays on screen 11JAVARANCH .
2) it displays on screen 56JAVARANCH .
3) compile time error.
4) it displays on screen JAVARANCH only.
Q.3) Read the following class definition carefully
public class test {
public static void main(String arg[]) {
String a = "JAVARANCH";
String b = new String(a);
StringBuffer sb = new StringBuffer("JAVARANCH");
// insert a line here
}
}
which of the following line if inserted at the commented line in the above class
definition will print "true" on the screen.( choose one )
1) System.out.println(b.toString()==a)
2) System.out.println(sb.toString()==b.toString())
3) System.out.println(b==a)
4) System.out.println(b.toString()==b)
5) None
Q.4) Read the following class definition carefully
public class test {
public static void main(String arg[]) {
int i = 0;
// insert a line here
}
}
which of the following line if inserted at the commented line in the above class
definition will print "true" on the screen.( choose one )
1) System.out.println(i++ == i)
2) System.out.println(++i == i)
3) System.out.println(i++ > i)
4) System.out.println(i < i++ )<br /> 5) System.out.println(i > i++ )
6) Any of the above
7) None.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Billesh,
All of those are really good and tricky...
Do post more..
Lakshmi
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Billesh
I really appreciate you sharing some interesting questions. I'm sure others too like them.
I just thought of reminding you some general guidelines while posting messages -

  • Please post one question per post. Keep it concise and focussed.
  • Try to use more meaningful topics. "Threads and static variables" makes more sense than "Cool question-check it out!!"
  • Capitalized text, more than what is necessary can be sometimes annoying. Checkout how to add accents to your test using UBB Codes.


  • Again, thank you for your contributions and I keep 'em coming!!
    Ajith
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Billesh,
Thx for the tricky Questions.....
Waiting eagerly for more......
Aruna
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Billesh, your questions are great.
I am rethrowing Billesh's questions with a modification:
public class Test{
public static void main(String arg[]) {
String a = "JAVARANCH";
String b = new String(a);
StringBuffer sb = new StringBuffer("JAVARANCH");
// inserted lines here
System.out.println("1) " + b.toString()==a);
System.out.println("2) " + sb.toString()==b.toString());
System.out.println("3) " + b==a);
System.out.println("4) " + b.toString()==b);
}
}
What is the output and can someone explain why?

Thanks.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I can give it a try...

line 3: a String-literal named "JAVARANCH" is created and putted in the String-pool
line 4: a new String-Object is created, with a reference to a String with "JAVARANCH" as value. NOTE: different string then in line 3
line 5: a StringBuffer-Object is created with "JAVARANCH" as input.
so the first comparison (line 8) compares b.toString() which is b with a String literal a. As i said before, these are two different strings, cause one was created with new and the other is a literal. So FALSE is returned
next one compares a new string-object (created from sb.toString()) with another string-object, hence FALSE.
third comparison: same as in line 8 also FALSE.
fourth comparison: compares b.toString() which is b (as defined in the API) with b. This one is the only one which returns TRUE.
Hope that helps, as always correct me if i'm wrong
cheers
Oliver
[This message has been edited by Oliver Grass (edited November 13, 2000).]
[This message has been edited by Oliver Grass (edited November 13, 2000).]
[This message has been edited by Oliver Grass (edited November 13, 2000).]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Billesh,
Can you tell me where I can more questions similiar to the ones you have posted. I think that they are really good.
 
Ramesh Kadur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
All of the comparisons result is false value. My question was not that. I guess, I was not clear in my question. If you look closely in the print statement, you will see that a number and a bracket is also included. I expect the number with a bracket also to be printed out. Like:

1) false
2) false
3) false
4) false
To my surprise the numbers and brackets don't print. It just prints like this:
false
false
false
false
I wanted to know the reason behind skipping the number and a bracket.
Thanks.
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramesh,
The result is due to "operator precedence." Hope I need not elaborate more on that....
Aruna
 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramesh,
Thats because in your program the comparison == is done against
"1)" + b.toString() and not b.toString(), due to the precedence of '+' over '=='.
Note that you are getting a false for the last one, where as the comparison b.toString()==b must return a true.


Hi,
All of the comparisons result is false value. My question was not that. I guess, I was not clear in my question. If you look closely in the print statement, you will see that a number and a bracket is also included. I expect the number with a bracket also to be printed out. Like:
1) false
2) false
3) false
4) false
To my surprise the numbers and brackets don't print. It just prints like this:
false
false
false
false
I wanted to know the reason behind skipping the number and a bracket.
Thanks.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't find question no. 1 and 4 challenging. For question 2 and 3, I think the key is that toString() function returns a new String object. Am I right?
 
Ramesh Kadur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lakshmi
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Cowpokes,
I predicted the results oy Q1 - Q3 accurately, but I messed up Q4. The results are got from compiling and running Q4 were:
false
true
false
false
false
I thought the first would be true and the second false...Otherwise I understood. Can someone explain why the first two are the way they are?

Matt
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt,

Hope this explains ur result better.
Aruna
[This message has been edited by Aru Ven (edited November 13, 2000).]
 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but is the second line actually 2==2 instead of 1==1? If not, I'm still perplexed about the second one.
Matt
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt:
Your confusion arises because I think you are assuming that the first and second statement both appear in the code. (I am only taking these two statements just to clear your confusion). Thats why you think it should be 2==2 instead of 1==1. However, if you look at the original question, it says which of the following line when inserted here.... So, assuming that only one of the statements exist at a time, the second statement would be 1==1..
HTH
[This message has been edited by Viji Bharat (edited November 13, 2000).]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Billesh,


Q.1) Read the following class definition carefully
public class Test extends Thread {
public synchronized void run() {
print();
}
public synchronized void print() {
System.out.print("JAVARANCH");
try {
Thread.sleep(5000);
}
catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
public static void main(String arg[]) {
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t2.start();
}
}
what will happened when you try to compile and run the class. (choose one)
1)it display on the sccreen JAVARANCH followed by another JAVARANCH without any delay.
2)display two times word JAVARANCH but a delay of 5 seconds is involed between two output
3)when runs initially make 5 second delay then print JAVARANCH and again after making a delay of 5 second again output JAVARANCH appeared
4)compile time error


I think the question is bit ambigous. The output should be dependent on the type of operating system. On windows platform, it would display on the sccreen JAVARANCH followed by another JAVARANCH without any delay. But I have a feeling that on Solaris the correct output would be display two times word JAVARANCH but a delay of 5 seconds or little more between two outputs.


Q.2 ) Read the following class definition carefully
public class test {
public static vod main(String arg[]) {
Integer i1 = new Integer(5);
Integer i2 = new Integer(6);
String S = new String("JAVARANCH");
System.out.println(i1 + i2 + S )
}
}
what will happened when you try to compile and run the class. (choose one)
1) it displays on screen 11JAVARANCH .
2) it displays on screen 56JAVARANCH .
3) compile time error.
4) it displays on screen JAVARANCH only.


Great one!


Q.3) Read the following class definition carefully
public class test {
public static void main(String arg[]) {
String a = "JAVARANCH";
String b = new String(a);
StringBuffer sb = new StringBuffer("JAVARANCH");
// insert a line here
}
}
which of the following line if inserted at the commented line in the above class definition will print "true" on the screen.( choose one )
1) System.out.println(b.toString()==a)
2) System.out.println(sb.toString()==b.toString())
3) System.out.println(b==a)
4) System.out.println(b.toString()==b)
5) None


Another great one!


Q.4) Read the following class definition carefully
public class test {
public static void main(String arg[]) {
int i = 0;
// insert a line here
}
}
which of the following line if inserted at the commented line in the above class definition will print "true" on the screen.
(choose one )
1) System.out.println(i++ == i)
2) System.out.println(++i == i)
3) System.out.println(i++ > i)
4) System.out.println(i < i++ )<br /> 5) System.out.println(i > i++ )
6) Any of the above
7) None.


Eye opener if your concepts are not clear!

I think all of us would love to see more questions like this. Please keep them coming.
Shubhangi
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone
Can any one run Q no.1 on different platform than windows NT and post the result pls.
I got doubt about sleep() method. Does it release the lock on the object when it is called by the thread.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can Anyone tell me
public class Test{
public static void main(String arg[]) {
int i = 0;
System.out.println(i+++i*10);
}
}
As far as i know the answer should be 11 but iam getting 10 why..?
bye
Sada
 
Chris Kovach
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this one out:
int[] a = new int[10];
int i = 5;
a[i++]=i+++i++;
System.out.println(a[5] + "," + a[6] + "," + i);
 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No disrespect, but I disagree with gautam...I believe if you are going to add whitespace, it should look like this
public class Test{
public static void main(String arg[])
{
int i = 0;
System.out.println(i++ +i*10); //very slight diff but matters
//in comprehension
}
}
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matt DeLacey:
No disrespect, but I disagree with gautam...I believe if you are going to add whitespace, it should look like this


Actually it doesn't matter where you place the space, the result is the same
public class Test{
public static void main(String arg[])
{
int i = 0;
System.out.println(i++ +i*10); //(1) below
i=0; // to reset it
System.out.println(i+ ++i*10); //(2) below
}
(1) i is incremented to 1 but uses original value 0 then i (now one) is mutiplied by 10 so it looks like this: 0 + 1 * 10
(2) i is 0, it is then incremented to 1 and 1 is used so the formula again looks like 0 + 1 * 10
to get 11 you have to write it this way.
System.out.println(++i+i*10);
Here i is incremented to 1 and 1 is used it is then added to i * 10 and it looks like this 1+1*10
I love this thread.... it really has made the increment essentials really stick!
Lisa
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lisa,
The result may be same. But the compiler calculates it as Matt
showed. Under a different situation or theoretically (i++ +i*10)
is right.

Originally posted by Lisa Yanchunis:

Originally posted by Matt DeLacey:
[b]No disrespect, but I disagree with gautam...I believe if you are going to add whitespace, it should look like this


Actually it doesn't matter where you place the space, the result is the same
public class Test{
public static void main(String arg[])
{
int i = 0;
System.out.println(i++ +i*10); //(1) below
i=0; // to reset it
System.out.println(i+ ++i*10); //(2) below
}
(1) i is incremented to 1 but uses original value 0 then i (now one) is mutiplied by 10 so it looks like this: 0 + 1 * 10
(2) i is 0, it is then incremented to 1 and 1 is used so the formula again looks like 0 + 1 * 10
to get 11 you have to write it this way.
System.out.println(++i+i*10);
Here i is incremented to 1 and 1 is used it is then added to i * 10 and it looks like this 1+1*10
I love this thread.... it really has made the increment essentials really stick!
Lisa[/B]


 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one should give you 10.
i++ --> evaluates to be 0, but i is 1 now
i*10 --> evaluates to be 10
+ --> 0 + 10 = 10
Hope this help.

Originally posted by Sadashiv Borkar:
Hi All,
Can Anyone tell me
public class Test{
public static void main(String arg[]) {
int i = 0;
System.out.println(i+++i*10);
}
}
As far as i know the answer should be 11 but iam getting 10 why..?
bye
Sada


 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bilesh,
Your questions are really amazing... and tricky.
could you please send me more such question to my id.
sakthima@rediffmail.com
Thanks and Regards,
Sakthivel.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sadashiv,
U have already received ur answer, I hope. If u want to clear more fundas, check out Amit Roy's query ' i+++i++ '

Thanx
Rajib
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic