20. What is the result when you compile and run the following code?
public class Test{
public void method(){
for(int i = 0; i < 3; i++) {
System.out.print(i);
}
System.out.print(i);
}
}
A. 0122
B. 0123
C. compile error
D. none of these
C is correct. The code on compilation will give compile time error because the scope of variable i is only within "for" loop.
21. What will happen when you attempt to compile and run the following code?
int Output = 10;
boolean b1 = false;
if((b1 == true) && ((Output += 10) == 20)){
System.out.println("We are equal " + Output);
}else{
System.out.println("Not equal! " + Output);
}
A.compile error
B.compile and output of “we are equal 10”
C.compile and output of “not equal!20”
D.compile and output of “not equal!10”
D is correct, according to short logic operator rule
22. What will be the result of executing the following code?
Given that Test1 is a class.
1. Test1[] t1 = new Test1[10];
2. Test1[][] t2 = new Test1[5][];
3. if (t1[0] == null)
4. {
5.t2[0] = new Test1[10] ;
6.t2[1] = new Test1[10];
7.t2[2] = new Test1[10];
8.t2[3] = new Test1[10];
9.t2[4] = new Test1[10];
10. }
11. System.out.println(t1[0]);
12. System.out.println(t2[1][0]);
A. The code will not compile because the array t2 is not initialized in an unconditional statement before use.
B. The code will compile but a runtime exception will be thrown at line 12.
C. The code will compile but a runtime exception will be thrown at line 11.
D. None of these
D is correct. Though we cannot use local variables without initializing them (compilation error), there is an exception to it. In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size). In the question above the array t2 is initialized before use, therefore there will be no problem at runtime also and the lines 11 and 12 will both print null.
23. 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 extends 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.
24. What will be the output on compiling/running the following code?
public class MyThread implements Runnable {
String myString = "Yes ";
public void run() {
this.myString = "No ";
}
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t).start();
for (int i=0; i < 10; i++)
System.out.print(t.myString);
}
}
A. compile error
B. prints: yes yes yes yes yes yes and so on
C. prints: no no no no no no no no and so on
D. prints: yes no yes no ye no ye no and so on
E. the output cannot be determinated
E is correct. Please note that there will not be any compilation error when the above code is compiled. Also 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 operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed. Thus the output cannot be determined.
25. 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 compilation 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.