This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question -JQ++

 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question ID :1002727540585
Consider the following code:
class MyRunnable implements Runnable
{
public static void main(String[] args)
{
new Thread( new MyRunnable(2) ).start();
}
public void run(int n)
{
for(int i=0; i<n; i++)
{
System.out.println("Hello World");
}
}
}
What will be the output when this program is compiled and run from the command line?
1. will print Hello World once.
2. will print Hello World twice.
3. It will not compile.
4. It will compile but throw runtime exception.
5. It will compile without any output.
answer is 3, will not compile with explanation below. I answered as 2 and was trapped
When a non-abstract class implements Runnable interface, it must implement public void run() method. (run With no parameters). In this case, this class has a run method but it takes an int as a parameter.
Moreover, there is no constructor for this class that take an int. So new MyRunnable(2) is invalid.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun
What is your question?
 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave ,
Do we get questions like these , JQ++ has categorised this question as easy. But it is demoralising to see these easy questions being answered wrongly. MyRunnable does implement the run() method but on carefull checking again you will be surprised to have overlooked the method signature. MyRunnable should provide a default constructor MyRunnable() but unable to handle MyRunnable(int).
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,
Yes, we may get some questions like this on exam. Don't worry if you get it wrong this time, you will be careful next time. You will get used to it.
You can over Maha Anna's traps to be aware in the exam, it is very helpful.
Vanitha
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes!
You can definitely expect questions of this level of difficulty in the real exam. In fact I had few tougher ones on threads as well.
HTH,
- Manish
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A programming language is more precise than a natural language, hence it demands more attention. The devil is truly in the details.
In real-life code, some of the deadliest bugs are the ones that hide in plain sight.
-anthony
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic