1. What will happen when you attempt to compile and run the following code?
class MyThread extends Thread{
public void run(){
System.out.println("MyThread: run()");
}
public void start(){
System.out.println("MyThread: start()");
}
}
class MyRunnable implements Runnable{
public void run(){
System.out.println("MyRunnable: run()");
}
public void start(){
System.out.println("MyRunnable: start()");
}
}
public class MyTest {
public static void main(String args[]){
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
myThread.start();
thread.start();
}
}
A.prints: MyThread: start() followed by MyRunnable: run()
B.prints: MyThread: run() followed by MyRunnable: start()
C.prints: MyThread: start() followed by MyRunnable: start()
D.prints: MyThread: run() followed by MyRunnable: run()
E.compile time error
F.None of the above
A is the correct choice. In the above code there is not any compilation error.
Thus choice E is incorrect. Inside main() method, objects of MyThread and
MyRunnable class are created followed by creation of Thread with object of
MyRunnable class.
Note that MyThread class extends Thread class and overrides the start() method of
the Thread class. Thus on execution of "myThread.start()" statement, the start()
method of the MyThread class will be executed and as a result "MyThread:start()"
will be printed. Had the start() method not there in MyThread class, the start()
method of the Thread class would be called which in turn would call the run()
method of the MyThread class.
On execution of "thread.start();", the start() method of the Thread class would be
called which in turn will call the run() method of the class which is passed to
Thread constructor (i.e. MyRunnable class). Thus "MyRunnable:run()" will be printed
out. Thus choice A is correct.
3. What results from the following code?
1.class MyClass
2.{
3.void myMethod(int i) {System.out.println("int version");}
4.void myMethod(String s) {System.out.println("String version");}
5.public static void main(String args[])
6.{
7.MyClass obj = new MyClass();
8.char ch = 'c';
9.obj.myMethod(ch);
10.}
11.}
A.Line 4 will not compile as void method can’t e overridden.
B.An exception at line 9.
C.Line 9 will not compile as there is no version of myMethod which takes a char as
argument.
D.The code compiles and produces output: int version
E.The code compiles and produces output: String version
D is correct. A is incorrect as void methods can be overridden without any problem.
B is incorrect as char ch declaration is valid. C is incorrect as char type in java
is internally stored as integer and there is a method which takes int as an input.
D is correct, on line 9 char ch is widened to an int and passed to int version of
the myMethod(). E is incorrect as int version of myMethod() is called.
4. What will happen when you attempt to compile and run the following code?
class Base{
int i = 99;
public void amethod(){
System.out.println("Base.amethod()");
}
Base(){
amethod();
}
}
public class Derived eore therextends Base{
int i = -1;
public static void main(String argv[]){
Base b = new Derived();
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("Derived.amethod()");
}
}
A. Derived.amethod()
-1
Derived.amethod()
B. Derived.amethod()
99
Derived.amethod()
C. 99
D. 99
Derived.amethod()
E. compile time error.
B is correct. The reason is that this code creates an instance of the Derived class
but assigns it to a reference of a the Base class. In this situation a reference to
any of the fields such as i will refer to the value in the Base class, but a call
to a method will refer to the method in the class type rather than its reference
handle. But note that if the amethod() was not present in the base class then
compilation error would be reported as at compile time, when compiler sees the
statement like b.amethod(), it checks if the method is present in the base class
or not. Only at the run time it decides to call the method from the derived class.
5. Multiple objects of MyClass (given below) are used in a program that uses multiple
Threads to create new integer count. What will happen when other threads use the
following code?
class MyClass{
static private int myCount = 0;
int yourNumber;
private static synchronized int nextCount(){
return ++myCount; //myCount为static
}
public void getYourNumber(){
yourNumber = nextCount();
}
}
A. the code ill give ompilation error
B. the code ill give runtime error
C. each thread will get a unique number
D. the uniqueness of the number different Threads can’t be guaranteed.
C is correct. The use of synchronized ensures that the number generated
will not be duplicated, no matter how many Threads are trying to create
the number. Thus D is incorrect. A and B are incorrect as the above code will
not give any compiletime or runtime error.
8. What will happen when you attempt to compile and run the following code?
public class MyThread extends Thread{
String myName;
MyThread(String name){
myName = name;
}
public void run(){
for(int i=0; i<100;i++){
System.out.println(myName);
}
}
public static void main(String args[]){
try{
MyThread mt1 = new MyThread("mt1");
MyThread mt2 = new MyThread("mt2");
mt1.start();
// XXX
mt2.start();
}catch(InterruptedException ex){}
}
}
A. compile error
B. mt1.join();
C. mt1.sleep(100);
D. mt1.run()
E. nothing need
Choice A and B are correct. In its current condition, the above code will not
compile as "InterruptedException" is never thrown in the try block. The compiler
will give following exception: "Exception java.lang.InterruptedException is never
thrown in the body of the corresponding try statement."
Note that calling start() method on a Thread doesn't start the Thread.
It only makes a Thread ready to be called.
Depending on the operating system and other running threads,
the thread on which start is called will get executed. After making the
above code to compile (by changing the InterruptedException to some other
type like Exception), the output can't be predicted (the order in which mt1
and mt2 will be printed can't be guaranteed). In order to make the MyThread
class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join() can
be placed at //XXX position. The join() method waits for the Thread on which
it is called to die. Thus on calling join() on mt1, it is assured that mt2 will
not be executed before mt1 is completed. Also note that the join() method throws
InterruptedException, which will cause the above program to compile successfully.
Thus choice A and B are correct.