Saturday, June 7, 2008

JAVA

JAVA



1) Which of the following 2 methods executes faster ?

class Trial {

String _member;

void method1() {
for(int i=0;i<2048;i++) {
_member += "test";
}
}

void method2() {

String temp;

for(int i=0;i<2048;i++) {
temp += "test";
}
_member = temp;
}

}


(a) method1()
(b) method2()
(c) Both method1() and method2() takes same time for execution

ANS: (b)

Accessing method variables requires less overhead than accessing class variables.

2) Integer.parseInt("12a") returns

(a) Exception
(b) 1



(c) 0
(d) -1

ANS: (a)

3) By default, Strings to functions are passed using the method

(a) Call by Value
(b) Call by Reference
(c) Strings cannot be passed to function

ANS: (b)

String is a class defined in java.lang and in java all classes are passed by reference.

4) What is the difference between OVERRIDING and OVERLOADING

5) What is the output of following program ?

class Test {
public static void main(String args[]) {
for(int i=0;i<2;i++) {
System.out.println(i--);
}
}
}

(a) Goes into infinite loop
(b) 0,1
(c) 0,1,2
(d) None


ANS: (a)

6) 't' is the reference to a class that extends THREAD. Then how to suspend the execution of this
thread ?

(a) t.yield()
(b) yield(t)
(c) yield()
(d) yield(100) where 100 is the milli seconds of time

7) What is the functionality of instanceOf() ?

8) How many String objects are created by the following statements

No comments: