Saturday, December 19, 2009

Core Java Interview Questions

Q1: What will be the output of following code?
class Test {
Test(int x){
System.out.print("Test("+x+")");
}
}

class Q1{
static Test t1 =new Test(1);
Test t2=new Test(2);
static Test t3 = new Test(3);
public static void main(String[] args) {
Q1 q=new Q1();
}
}

now this question for testing the sequence of fields to initialize.
Output - Test(1)Test(3)Test(2)

Q2: What will be the output of following code?
byte b =0;
while(++b>0)
;
System.out.println("Welcome to Java");
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q3: What will be the output of following code?
if(new Boolean("true")==new Boolean("true"))
System.out.println("TRUE");
else
 System.out.println("FALSE");
FALSE (they are two diffrenent objects)

Q4: What will be the output of following code?
class AA{
static void show(){
System.out.println("A");
}
}
class B extends AA{
static void show(){
System.out.println("B");
}
}
class Test {
public static void main(String[] args) {
AA a=new AA();
a.show();
B b=new B();
b.show();
a=b;
a.show();
b=a;
b.show();

}}
Answer: b=(B)a Compilation Error

Q5: What will be the output of following code?
class AA{
static void show(){
System.out.println("A");
}
}
class B extends AA{
void show(){
System.out.println("B");
}}
Answer: Compilation Error (Instance method cannot override the static method from class AA)

Q6: What will be the output of following code?
class AA{
void show(){
System.out.println("A");
}
}
class B extends AA{
static void show(){
System.out.println("B");
}}
Answer: Compilation Error (static method can not hide the instance method from AA)

Q7: What will be the output of following code?
class Test {
public static void main(String[] args) {
B b= new B();
b.show();
B.show();
}
}

class AA{
static void show(){
System.out.println("A");
}
}
class B extends AA{
}

Output: (You can access static method with subclass reference)
A

A


Q8: What will be the output of following code?
class Test {
static int x=11;
public static void main(String[] args) {

int x =2;
System.out.println(x);
}}
Output: 2

Q9: What will be the output of following code?
ArrayList list = new ArrayList();
list.add("B");
list.add("A");
list.add(new Integer(23));
Collections.sort(list);
Answer: Compile but not run. Throw runtime exception – ClassCastException.Collection.sort(List) is used to sort parameterized list. If you comment out list.add(new Integer(23)); then it will run.

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

Q2: What will be the output of following code?
code code
Welcome to Java (because in byte, when b=127 and then next iteration it will be -1)

No comments:

Post a Comment