Friday, September 11, 2009

Java Programming Questions


Q1)What is the o/p of following program?

public class Test{
static {i=5;}
static int i;

public static void main(String[] args) {
System.out.println("i value is "+i);
}
}

a) 0
b) 5
c) Compilation error
d) 1

Answer
Answer is b
Q2) What is the o/p of following program?

public class Test{
static i=5;

public static void main(String[] args) {
Test test = null;
System.out.println("i value is "+test.i);
}
}

a) 0
b) 5
c) Compilation error
d) NullPointerException

Answer
Answer is b, because static variables are related to class and not instance. Hence te instance value doesnt hold good if a static variable is tried to access.
Q3)What is the o/p of following program?

class Father {

protected Father() {
System.out.println("Created a Father");
}
}
public class Child extends Father {

private Child() {
System.out.println("Inside child");
}
public static void main(String args[]) {
new Child();
}
a) Inside Child
b) Created a Father
c) Compilation error
d) Created a Father Inside Child

Answer
Answer is d. As a rule always the parent default constructor is called and then child constructor.
Q4)What is the o/p of following program?

class Father {

protected Father(String str) {
System.out.println("Created a Father " +str);
}
}
public class Child extends Father {

private Child() {
System.out.println("Inside child");
}
public static void main(String args[]) {
super("Hi");
new Child();
}
a) Inside Child
b) Created a Father
c) Compilation error
d) Created a Father Hi Inside Child

Answer
Answer is c. As a rule always the parent default constructor should be always present if a parameterized constructor is created. In this case create a default constructor for Father class.
Q5)What is a WeakHashMap?

a) A hashtable-based Map implementation with weak keys
b) A list with weak references to objects
c) A hasttable map with duplictated keys
d) A general purpose hashtable-based implementation to better store.

Answer
Answer is a.
Q6)Which of the following is/are true?

a) if("String ".trim() == "String")
b) if(" String ".trim() == "String")
c) if("String".trim() == "String")
d) if("Str ing ".trim() == "String")

Answer
Answer is c. trim() function is only performed if blankspaces are present at the beginning or end. If not present it will return the same string. Hence the function will return true. Bit if the string contains blankspaces, subString() is called to trim the string and return a new String using new operator
Q7) Which of the following lines allow main method to be executed by ClassLoader?

1: public static String main(String args[]);
2: protected static void main(String args[]);
3: final public static void main(String args[]);
4: public static void main();
5: private static void main(String args[]);
6: public void main(String args[]);

Answer
Answer is 3. Only final,public, static ,void is allowed with main method.
Q8) What is o/p of following program?

public class FinalVar {
private int final i;

public static void main(String args[]){
System.out.println(new FinalVar().i);
}
}

a) 1
b) 0
c) RunTimeException occurs
d) Compile time error 'i should be initialized'
Answer
Answer is d.final variable need to be declared always even the default value should be mentioned explicitly.

No comments:

Post a Comment

Happy Reading!

Ganex Improves .. Headline Animator