Posts

Showing posts from July, 2017

Java Constructor 2 !?

What is the expected output? class Plant {       Plant() {       System.out.println("Plant created"); } } class Tree extends Plant {        Tree() {        System.out.println("Tree created");        super(); } } public class Test { public static void main(String args[]) {        Tree tree = new Tree(); } } Please choose only one answer: 1)    Plant created        Tree created 2)    Tree created        Plant created 3)    RuntimeException 4)     Compilation error

Java Constructor !?

What is the expected output? public class Profile {           private Profile(int w) { // line 1           System.out.println(w); }            public static Profile() { // line 5            System.out.println(10); }            public static void main(String args[]) {            Profile obj = new Profile(50); } } Please choose only one answer: Won't compile because of line (1) – constructor can't be private 10            50 50 Won't compile because of line (5) – constructor can't be static

Java Constructor Legal Modifiers.

Q.What are the legal modifiers which the constructor can be declared with? Please choose all the answers that apply: public protected private final static abstract

String and NULL!!!

What is the expected output?       public static void main(String args []) {                     String stmt = null;                     System.out.print(null+stmt);                     System.out.print(stmt+null);         } Please choose only one answer:  RuntimeException is thrown because of the first print statement  RuntimeException is thrown because of the second print statement  nullnullnullnull  nullnull  compilation error Post your doubt in the comment section below.

String and StringBuffer!

Select the common methods, which are defined for both type String and type StringBuffer ?         Please choose all the answers that apply:   toString()   length()  append(String)  trim()  equals(Object)   Put your doubts in the comment section. 

Wrapper Class Vs Primitive types Output 1

public class Tester {  public static void main(String[] args) {     Integer sum1 = 125; //line 1     int sum2 = 125; //line 2     System.out.print(sum1.equals(sum2)); //line 3     System.out.print(sum2.equals(sum1)); //line 4     System.out.print(sum1==sum2); //line 5  } } Output: 1. Compilation error at line 3  2. Compilation error at line 4  3. truetruefalse  4. truetrueture

Array Output 2

public class JavaRecipe{      public static void main(String[] jr){           int[] arr = new int[0];           System.out.println(arr.length); //line 1           arr[0] = 10; //line 2           Ststem.out.println(arr[0]); //line 3                    } } 1. Compilation error at Line 1 : size of array cant be 0 2. Runntime error at Line 2 : ArrayIndexOutOfBoundException 3. Compiles fine and gives output as 0 10 4. Runntime error at line 1 & 3 : NullPointerException

Array Output 1

public class JavaRecipe{      public static void main(String[] jr){           int[] arr = new int[0]; // Line 1           System.out.println(arr.length); // Line 2      } } 1. Compilation error at Line 1 : size of array cant be 0 2. Compiles fine and gives output as 0 3. Runntime error at line 2 : NullPointerException