Thursday, 19 January 2017

Core Java Coding / Programming Questions And Answers

Is the  code compiles ? If yes then what will be the output ?

package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void JavaInterviewHubTest(Object obj) {
              System.out.println("Object");
       }

       public static void JavaInterviewHubTest(String arg) {
              System.out.println("String");
       }

       public static void main(String[] args) {
              JavaInterviewHubTest(null);
       }

}
Answer >>
Compile :  Yes    

Output   :  String 
Next Question >>
package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void JavaInterviewHubTest(Object obj) {
              System.out.println("Object");
       }

       public static void JavaInterviewHubTest(String arg) {
              System.out.println("String");
       }

       public static void JavaInterviewHubTest(Integer a) {
              System.out.println("Integer");
       }

       public static void main(String[] args) {
              JavaInterviewHubTest(null);
// The method JavaInterviewHubTest(Object) is ambiguous for the type JavaInterviewHub
       }

}

Answer >>
Compile :  No   , Error at  line 17

Reason  :   Method is ambiguous 
Next Question >>
package swain.java91.blogspot.in;

public class JavaInterviewHub {

       public static void main(String[] args) {
              if (null == null) {
                     System.out.println("javainterviewhub.blogspot.in");
              }
       }

}


Compile :  Yes
Output  :   javainterviewhub.blogspot.in


Next Question >>

package swain.java91.blogspot.in;

public class Top {
      
       public Top(String arg){
              System.out.println("B");
       }

}

package swain.java91.blogspot.in;

public class Bottom2 extends Top{

       public Bottom2(String arg) {
              System.out.println("D");
       }

       public static void main(String[] args) {
              new Bottom2("c");
              System.out.println("");

       }}
Compile :  Compilation fails
Reason  :   Implicit super constructor Top() is undefined. Must explicitly invoke another constructor
Next Question >>

package swain.java91.blogspot.in;

public class Top {
      
       public Top(){
              System.out.println("B");
       }

}

package swain.java91.blogspot.in;

public class Bottom2 extends Top{

       public Bottom2(String arg) {
              System.out.println("D");
       }

       public static void main(String[] args) {
              new Bottom2("c");
              System.out.println("");

       }}

Compile :  Yes
Output  :   BD
Next Question >>
package swain.java91.blogspot.in;

public class Clidder {
       private final void flipper() {
              System.out.println("Clidder");
       }
}


package swain.java91.blogspot.in;

public class Clidlet extends Clidder {
       public final void flipper() {
              System.out.println("Clidlet");
       }

       public static void main(String[] args) {
              // TODO Auto-generated method stub
              new Clidlet().flipper();
       }

}


Compile :  Yes
Output  :   Clidlet
Next Question >>
package swain.java91.blogspot.in;

public class X {
void do1(){
      
}
}
package swain.java91.blogspot.in;

public class Y extends X{
       void do2(){
             
       }

}

package swain.java91.blogspot.in;

public class Chrome {

       public static void main(String[] args) {
              X x1 = new X();
              X x2 = new Y();
              Y y1 = new Y();
              // insert code here
       }

}

Which, inserted at line 9, will compile? (Choose all that apply.)

A. x2.do2();
 B. (Y)x2.do2();
C. ((Y)x2).do2();
 D. None of the above statements will compile
Next Question >>
1. ClassA has a ClassD
2. Methods in ClassA use public methods in ClassB
3. Methods in ClassC use public methods in ClassA
4. Methods in ClassA use public variables in ClassB
Which is most likely true? (Choose the most likely.)
A. ClassD has low cohesion
B. ClassA has weak encapsulation
C. ClassB has weak encapsulation
D. ClassB has strong encapsulation
E. ClassC is tightly coupled to ClassA
Next Question >>

package swain.java91.blogspot.in;

public class OverLoadedPlusOperator {

       public static void main(String[] args) {
              System.out.println(23 + 'c');//ASCII VALUE OF C IS 99,that why 99+23=122

       }

}

Compile :  Yes
Output  :   122

Next Question >>

Q.How to print “Hello, Sitansu how are you” without change the below program main method.

package swain.java91.blogspot.in;

public class JavaInterviewHub {

       public static void main(String[] args) {
              System.out.println("Sitansu");

       }

}

Answer:

package swain.java91.blogspot.in;

public class JavaInterviewHub {

       static {
              System.out.print("Hello, ");
              String[] args = null;
              main(args);
              System.out.println("how are you");
              System.exit(0);
       }

       public static void main(String[] args) {
              System.out.println("Sitansu");

       }

}

Next Question >>
Q. What happens When System.out.println(null)?.

package swain.java91.blogspot.in;

public class JavaInterviewHub {

       public static void main(String[] args) {
              System.out.println(null);

       }

}

Compile: Fail
Cause: Ambiguous  error

Explanation:
·   Compilation Error -This is because you can pass an Object or a String or char[]. Since null can fit in both, the compiler doesn't know which method to use, leading to compile error.
  • Method Overloading:
        1.public void prinltln(String str) { }
        2.public void prinltln(char[] ch){ }
        3.public void prinltln(Object ch){ 
  •  It seems the call System.out.print(null) is ambiguous to compiler because print(null) here will find the two best specific matches i.e. print(String) and print(char[]) . So compiler is unable to determine which method to call here .
  • Compilation Error:
       System.out.println(null)
  • Compile Fine:
         System.out.println((String)null);//null
         System.out.println((char[])null);
         System.out.println((Object)null);//null
  • It's the compiler type-checking the parameters of the method call.
  • But here we need to know one more thing  System.out.println((char[])null); will compile fine but at run time will throw runtime exception.
Next Question >>
Q.What will be the output of below program.
package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void main(String[] args) {
              Integer i1 = 100;
              System.out.println((longi1);
       }
}
Output:100

Next Question >>

Q.What will be the output of below program.
package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void main(String[] args) {
              String i1 = "Its  ";
              System.out.println(i1 + null);
       }
}

Output:Its null

Next Question >>

Q.What will be the output of below program.
package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void main(String[] args) {
              Integer i1 = 100;
              System.out.println(i1 + null);
       }
}

Compile: No

Next Question >>

Q.What will be the output of below program.
package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void main(String[] args) {
              Object[] obj = new Object[] { new Integer(100), new String("Hello"), new Float(3.40) };
              for (Object temp : obj) {
                     System.out.println(temp);
              }

       }
}

Output:   100
        Hello
        3.4

Next Question >>

Q.What will be the output of below program.
package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void main(String[] args) {
              String x = "xyz";
              x.toUpperCase();
              String y = x.replace('Y''y');
              y = y + "abc";
              System.out.println(y);
       }
}

Output: xyzabc

Next Question >>

Q.What will be the output of below program.
package swain.java91.blogspot.in;

public class JavaInterviewHub {
       public static void main(String[] args) {
              int i = 1, j = 10;
              do {
                     if (i++ > --j) {
                           continue;
                     }
              } while (i < 5);
              System.out.println("i = " + i + " and j = " + j);

       }
}

Output: i = 5 and j = 6

Next Question >>

Q.What will be the output of below program.
package swain.java91.blogspot.in;

Q.What would you invoke the program to cause it to print :Age is 2.

public class JavaInterviewHub {

       public static void main(String[] args) {
              String a = args[1];
              String b = args[2];
              String c = args[3];
              System.out.println("Age is " + c);

       }
}

(a)java JavaInterviewHub 2 2 2
(b)java JavaInterviewHub 1 2 2 3 4
(c)java JavaInterviewHub 1 3 2 2
(d)java JavaInterviewHub 0 1 2 3

Answer:
public class JavaInterviewHub {
       static {
              String[] arg = { "1""3""2""2" };
              main(arg);
       }

       public static void main(String[] args) {
              String a = args[1];
              String b = args[2];
              String c = args[3];
              System.out.println("Age is " + c);

       }
}

Output: Age is 2

Next Question >>

Q.What is the result.

package swain.java91.blogspot.in;

public class JavaInterviewHub {

       public static void main(String[] args) {
              int i = 42;
              String s = (i < 40) ? "life" : (i > 50) ? "universe" : "Everything";
              System.out.println(s);
       }
}

(a) null
(b) Life
(c) universe
(d) Everything
(e) Compiletimeerror
(f) RuntimeError

Answer:
package swain.java91.blogspot.in;

public class JavaInterviewHub {

       public static void main(String[] args) {
              int i = 42;
              String s = (i < 40) ? "life" : (i > 50) ? "universe" : "Everything";
              System.out.println(s);
              System.out.println((i < 40));
              System.out.println((i > 50));
       }
}

Output: Everything
        false
        false
        false

Next Question >>

Q.What will be the output of below program.
(a) Compile Time Exception
(b) Run Time Exception
(c) It is method1
    It is method2
(D) none of the above
package swain.java91.blogspot.in;
public interface Test {
       void method1();

}

package swain.java91.blogspot.in;

public class MyClass implements Test {

       @Override
       public void method1() {
              System.out.println("It is method1");

       }

       public void method2() {
              System.out.println("It is method2");

       }

}

package swain.java91.blogspot.in;

public class JavaInterviewHub extends MyClass {

       public static void main(String[] args) {
              Test t = new MyClass();
              t.method1();
              t.method12();
       }
}

Q.What is output of below program.

package swain.java91.blogspot.in;

import java.util.ArrayList;
import java.util.List;

public class JavaInterviewHub extends MyClass {

 public static void main(String[] args) {
  Employe obj = new Employe();
  obj.setId(1);
  obj.setName("Sitansu");
  List<Employe> list = new ArrayList<Employe>();
  list.add(obj);
  Employe data = list.get(0);
  data.setName("swain");
  System.out.println(list.get(0));
  System.out.println(list.get(0).getName());
 }
}

Output:
swain.javainterviewhub.blogspot.in.Employe@1db9742
swain



Q. What is the output of following program?

package swain.java91.blogspot.in;

public class JavaInterviewHub {
       JavaInterviewHub obj = new JavaInterviewHub();

       public int show() {
              return (true ? null : 0);
       }

       public static void main(String[] args) {
              JavaInterviewHub obj = new JavaInterviewHub();
              System.out.println(obj.show());

       }

}

OutPut:

Exception in thread "main" java.lang.StackOverflowError
       at swain.javainterviewhub.blogspot.in.JavaInterviewHub.<init>(JavaInterviewHub.java:4)
       at swain.javainterviewhub.blogspot.in.JavaInterviewHub.<init>(JavaInterviewHub.java:4)

Exaplanation:
·         Whenever we create the object of any class constructor will be called first and memory allocated for all non static variables
·         Here  JavaInterviewHub obj = new JavaInterviewHub(); variable is object and assigned to new object of same class
·         JavaInterviewHub obj = new JavaInterviewHub(); statement leads to recursive execution of constructor will create infinite objects so at run time an exception will be raised
·         Exception in thread "main" java.lang.StackOverflowError

·         The common cause for a stack overflow exception  is a bad recursive call. Typically this is caused when your recursive functions doesn't have the correct termination condition

Next Question >>
Q. What is the output of following program?

package swain.java91.blogspot.in;

public class JavaInterviewHub {

       public static void show() {
              System.out.println("Static method called");
       }

       public static void main(String[] args) {
              JavaInterviewHub obj = null;
              obj.show();// No NullPointerExpection here

       }

}

OutPut: Static method called

Explanation:

We can call static methods using reference variable which is pointing to null because static methods are class level so we can either call using class name and reference variable which is pointing to null.

Next Question >>

package swain.java91.blogspot.in;

public class JavaInterviewHub {

 static int a = 1111;
 static {

  a = a-- - --a;
 }

 {
  a = a++ + ++a;
 }

 public static void main(String[] args) {

  System.out.println(a);

 }

}

Output: 2

Exaplanation:

The static variable a is initialized to 1111.
Then the static initializer runs. a-- evaluates to 1111 but sets a to 1110. Then --a runs, sets ato 1109 and evaluates to 1109. The subtraction occurs, and a is set to the result of the subtraction,2.
The instance initializer (with the ++ operators) doesn't run, because there is no instance of Test2is printed.

Next Question >>

Q.How large is the Integer cache?
package swain.java91.blogspot.in;

public class JavaInterviewHub {

 public static void main(String[] args) {
        Integer i1 = 128;
        Integer i2 = 128;
        System.out.println(i1 == i2);//false
        Integer i3 = 127;
        Integer i4 = 127;
       System.out.println(i3 == i4);//true

      }

    }
Output: false 
             true

Exaplanation:



 I saw a presentation where was the following sample of Java code:



       Integer a = 1000, b = 1000;  
       System.out.println(a == b); // false  
       Integer c = 100, d = 100;  
       System.out.println(c == d); // true
    Now I'm a little confused. I understand why in first case the result is "false" - it is because Integer       is a reference type and the references of "a" and "b" is different.



    But why in second case the result is "true"?


   I've heard an opinion, that JVM caching objects for int values from -128 to 127 for some               optimisation purposes. In this way, references of "c" and "d" is the same.

Next Question >>

Question: What does the following Java program print?


package swain.java91.blogspot.in;

public class JavaInterviewHub {

       public static void main(String[] args) {
              System.out.println(Math.min(Double.MIN_VALUE, 0.0d));

       }

}

Answer:
This questions is tricky because unlike the Integer, where  MIN_VALUE is negative, both the MAX_VALUE and MIN_VALUE of the Double class are positive numbers. The Double.MIN_VALUE is 2^(-1074), a double constant whose magnitude is the least among all double values. So unlike the obvious answer, this program will print 0.0 because Double.MIN_VALUE is greater than 0.

Next Question >>

Question: Why the output is true in all case?

package swain.java91.blogspot.in;
public class JavaInterviewHub {
 public static void main(String[] args) {
  Integer i1 = new Integer(1);
  Integer i2 = new Integer(1);
  System.out.println(i1 != i2);
  System.out.println(i1 <= i2);
  System.out.println(i1 >= i2);
 }
}

Output: true
             true
             true
Reason:
The concept here is the same as String constant pool 
Java maintains an integer constant pool for integers upto -128 to 127. 

Both are different object not created in the pool but somewhere else in the heap. So they have different addresses and so hashcode generated for them is different due to which the answer will be false. 

For {{System.out.println(i1 <= i2);}}, actual unboxing is happening due to which addresses are not being compared but 1<=1 is being compared. 

Instead of ==, we should use equals() method as it properly matches the contents of the two objects and then makes it decisions

Next Question >>
Q. What  will be the output of above program ?
package swain.java91.blogspot.in;

import java.util.Scanner;

public class JavaInterviewHub {

       public static void main(String[] args) {

              short a = 10;
              int b = 20;
              b = a;
              a = b;
              System.out.println(a + b);
       }

}

Compile: No

Reason: short data type is 2 byte and int data type is  4 byte .So you can not  assign 4 byte memory space  to 2 byte memory space. 

Is the  code compiles ? ?

package swain.javainterviewhub.blogspot.in;

public class Test {

          public int test() {
                   return 5;
          }

          public float test() {
                   return 5;
          }

}

Compile: No




1 comment: