• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

valiveru exam errata - please confirm REAL answers, thanks!

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 18.
Given the code below
public class ThreadDemo extends Thread[
public void run(){
System.out.println("Thread started");
try{
Thread.sleep(20000); //20 seconds sleep
System.out.println("I am Back from my sleep");
}
catch(InterruptedException e){
System.out.println("I got Interrupted");
}
}
}
Which of the following statements are true?

A.Exactly 20 seconds after the start method
is called "I am Back from my sleep" be printed.
B.We can be sure that atleast after 20 seconds elapsed,
"I am Back from sleep" will be printed.
C.The delay between output "Thread started" and "I am Back
from sleep" is less or more than 20 seconds.
D.None of the above.
//I CHOSE D, SINCE THERE IS NO start() method invocation, how can run() be invoked ??
Given the following code segments, select the ones that are legal.
A.boolean b = true;
int i = 10;
String s = b + i;
B.char c = 10;
int i = 20;
c += i;
C.String s = "Hello" ;
int i = 10;
i += s;
D.boolean b = null ;
if( b == null ){};
E.String s = "Hello" ;
int i = 10;
s += i;
F.Integer ii = new Integer(10);
int i = 10;
ii += i;
//I CHOSE E BUT ANSWER LIKES B AS WELL, HOW ??
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your first question I think the answer B is correct.
Here you are creating your own thread extending Thread class. You are going to create an object on this class(ThreadDemo) and call start() method.
Regarding my answer, the thread goes to sleep for atleast the specified interval. When it comes back there is a chance that a higher priority thread is running and this thread is put into the ready queue. So we can be sure that the thread sleeps for atleast the specified interval.
-----------

For your second question, the extended assignment operators have the cast built-in. so,
1. char c = 10;
2. int i = 20;
3. c += i;
is valid. At line 3, the += operator will do a implicit conversion from int to char and assigns the value to c.
Hope this helps.
regards
vadiraj
------------
 
sarim raza
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot, that was so helpful !
appreciate it!
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the second case
the type casting is done bcos char is unsigned so the range of char is lies within int since it is signed so implicit type casting is done.
thanks,
sunil.s
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic