

Programming Questions |
|
Q1)What is the o/p of following program? public class Widening1 { public void f1(Object o1){ System.out.println("Inside f1 with object as argument"); } public void f1(String s){ System.out.println("Inside f1 with String as argument"); } public static void main(String[] args) { new Widening1().f1(null); } } a) Inside f1 with String as argument b) Inside f1 with object as argument c) Compilation error d) Runtime error Answer Answer is a. Since the parameter passed to function is null so a function which has parameter less broad or wide is called.In this case it is String as String extends Object and has less scope. |
|
Q2) What is the o/p of following program? public class Widening1 { public void f1(Object o1){ System.out.println("Inside f1 with object as argument"); } public void f1(String s){ System.out.println("Inside f1 with String as argument"); } public void f1(String s){ System.out.println("Inside f1 with Integer as argument"); } public static void main(String[] args) { new Widening1().f1(null); } } a) Inside f1 with String as argument b) Inside f1 with object as argument c) Compilation error d) Inside f1 with object as argument Answer Answer is c, Since the parameter passed to function is null so a function which has parameter less broad or wide is called.In this case String and Integer is at same level and hence causes ambiguity. |
|
Q3)What is the o/p of following program? public class MyClass{ public static void main(String[] args) { int[] dest = new int[]{0,1,2,3,4,5}; System.out.println(dest[0]+ dest[5]+dest[2]); } } a) 052 b) Compilation Error c) 7 d)152 Answer Answer is c. If you are caught with this, then try running it ... |
|
Q4)What is the o/p of following program? public class Overloading { public void f1(Integer i){ System.out.println("inside 1"); } public void f1(int i){ System.out.println("inside 2"); } public static void main(String args[]){ new Overloading().f1(8); } } a) inside 1 b) inside 2 c) Compilation error d) Rutime error Answer
Answer is b.
|
|
Q5)Wil this code compile fine? public class FinalHashMap { 1 private static final Map map = new HashMap(); 2 public static void main(String[] args) { 3 map.put("param1","value1"); 4 map.put("param2","value2"); 5 map=new HashMap(); } } a) Yes b)No,Compilation Error at line 3 c)No, Runtime Error d)No,Compilation Error at line 5 Answer
Answer is d.Once declared the final variable cannot be initialized again
|
|
Q6)Which of the o/p following program? public class FinalHashMap { 1 private final Map map = new HashMap(); 2 public static void main(String[] args) { 3 map.put("param1","value1"); 4 map.put("param2","value2"); 5 map=new HashMap(); } } a) Yes b)No,Compilation Error at line 3 c)No, Runtime Error d)No,Compilation Error at line 5 Answer
Answer is b. Non static variable cannot be used inside static variable.
|
| Q7) What is o/p of following program
public class Parent { public static void staticMethod(){ System.out.println("Inside Parent static"); } public static void main(String[] args) { Parent p = new Child(); p.staticMethod(); } } class Child extends Parent { public static void staticMethod(){ System.out.println("Inside Child static"); } } a) Inside Parent static b) Inside Child static c) Compilte time error. Answer Answer is a. Static method cannot be overriden. |
| Q8) Is it compulsory to implement print() method in abstract class? public interface Interface1 { void print(String str); } public abstract class AbstractClass implements Interface1 { } Answer
Answer is No.
|
