10 Interesting Core Java Interview Coding Questions and Answers

1. What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class B{
  4.  
  5.   B b= new B();
  6.  
  7.  public int show(){
  8.       return (true ? null : 0);
  9.  }
  10.  
  11.  public static void main(String[] args)  {
  12.  
  13.         B b= new B();
  14.         b.show();
  15.     }
  16.  
  17. }




Exaplanation:

  • Whenever we create the object of any class constructor will be called first and memory allocated for all non static variables
  • Here  B b= new B(); variable is object and assigned to new object of same class
  • B b= new B(); 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

2. What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.  
  5.  public static void show(){
  6.  
  7.         System.out.println("Static method called");
  8.  }
  9.  
  10.  public static void main(String[] args)  {
  11.  
  12.         A obj=null;
  13.         obj.show();
  14.  
  15.     }
  16.  
  17. }



Exaplanation:

  • 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.


3. What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.  
  5.  static int a = 1111;
  6.  static
  7.  {
  8.         a = a-- - --a;
  9.  }
  10.     
  11. {
  12.         a = a++ + ++a;
  13.  }
  14.  
  15.  public static void main(String[] args)  {
  16.  
  17.        System.out.println(a);
  18.  
  19.     }
  20.  
  21. }




Explanation:

Top 10 Increment and decrement  operators interview Questions

4. What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.  
  5.  int GetValue()
  6.  {
  7.         return (true ? null : 0);
  8.  }
  9.  
  10.  public static void main(String[] args)  {
  11.  
  12.    A obj= new A();

  13.       obj.GetValue();
  14.  
  15.     }
  16.  
  17. }




5. What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.   
  5.  public static void main(String[] args)  {
  6.  
  7.    Integer i1 = 128;
  8.  
  9.    Integer i2 = 128;
  10.  
  11.      System.out.println(i1 == i2);
  12.  
  13.    Integer i3 = 127;
  14.    Integer i4 = 127;
  15.  
  16.       System.out.println(i3 == i4);
  17.  
  18.     }
  19.  
  20. }




6. What is the output of following program?


  1. package com.instanceofjava;
  2. class A
  3. {
  4.     
  5. void method(int i)
  6. {
  7.  
  8.  }
  9.  
  10.     }
  11.  
  12.  class B extends A
  13.  {
  14.  
  15. @Override
  16. void method(Integer i)
  17.  {
  18.  
  19.  }
  20.           


  21. }



7. Which line will throw compile time error? 8 or 9?


  1. package com.instanceofjava;
  2. class A
  3. {
  4.     
  5. public static void main(String [] args)
  6. {
  7.  
  8.   Integer i = new Integer(null);
  9.   String s = new String(null);
  10.  
  11.  }
  12.  
  13. }



8.What is the output of following program?


  1. package com.instanceofjava;
  2. class A
  3. {
  4.     
  5. public static void main(String [] args)
  6. {
  7.  
  8.   String s = "ONE"+3+2+"TWO"+"THREE"+5+4+"FOUR"+"FIVE"+5;
  9.   System.out.println(s);
  10.  
  11.  }
  12.  
  13. }



9.What is the output of following program?


  1. package com.instanceofjava;
  2. class A
  3. {
  4.  
  5. static int method1(int i)
  6. {
  7. return method2(i *= 11);
  8. }
  9.  
  10.  static int method2(int i)
  11. {
  12.   return method3(i /= 11);
  13. }
  14.  
  15.  static int method3(int i)
  16. {
  17.  return method4(i -= 11);
  18. }
  19.  
  20.  static int method4(int i)
  21. {
  22.   return i += 11;
  23. }
        
  24. public static void main(String [] args)
  25. {
  26.  
  27.   System.out.println(method1(11));
  28.  
  29.  }
  30.  
  31. }





10.What is the output of following program?


  1. package com.instanceofjava;
  2. class A
  3. {
  4.     
  5. public static void main(String [] args)
  6. {
  7.  
  8.   System.out.println(null);

  9.  }
  10.  
  11. }



Explanation:

What happens When System.out.println(null)?


  1. Print prime numbers? 
  2. Java Program Find Second highest number in an integer array 
  3. Java Interview Program to find smallest and second smallest number in an array 
  4. Java Coding Interview programming Questions : Java Test on HashMap 
  5. Constructor chaining in java with example programs 
  6. Swap two numbers without using third variable in java 
  7. Find sum of digits in java 
  8. How to create immutable class in java 
  9. AtomicInteger in java 
  10. Check Even or Odd without using modulus and division  
  11. String Reverse Without using String API 
  12. Find Biggest substring in between specified character
  13. Check string is palindrome or not?
  14. Reverse a number in java? 
For more interview programs : Java Programs asked in interviews

Programing Inrerview Questions on try catch

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("instance of java");    
  10.  
  11.     }
  12.  
  13. }
  14. }





2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("try block");   
  10.  
  11.     }
  12.    finally{
  13.  
  14.     System.out.println("finally block");    
  15.  
  16.   }
  17.  
  18. }
  19. }







3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.             
  8.         try {
  9.  
  10.             int a = 0;
  11.             int b = 10;
  12.             int c = b / a;
  13.  
  14.             System.out.print("try block");
  15.  
  16.         }
  17.         catch(Exception e) {
  18.  
  19.               System.out.print("catch block");
  20.  
  21.         }    
  22.  
  23. }
  24. }




String programing interview questions

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.charAt(3));
  9. }
  10. }



2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.length());
  9. }
  10. }








3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str1="abc";
  8.       String str2="abc";
  9.  
  10.        System.out.println(str1.equals(str2));
  11. }
  12. }






4. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.         String str1 = "abc";
  8.         String str2 = "abc";
  9.         String str3= new String("abc");
  10.  
  11.         System.out.println("str1 == str2 ? "+(str1==str2));
  12.         System.out.println("str1 == str3 ? "+(str1==str3));
  13.         System.out.println("str1 equals str3 ? "+(str1.equals(str3)));
  14.  
  15.  
  16. }
  17. }






Inheritance programming interview questions

1. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("super class method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.   System.out.println("sub class method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  SubDemo subobj=new SubDemo();
  10.  subobj.show();
  11. }







2. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.  super.x=y+2;
  9.   System.out.println("x="+super.x+"y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }




3. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo {

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2.  
  3. public class SubDemo extends SuperDemo{
  4.  
  5.  int y;
  6.  
  7. public void show(){
  8.  
  9.  super.x=y+2;
  10.   System.out.println("x="+super.x+"y="+y);
  11.  
  12. }
  13.  
  14.  public static void main(String args[]){
  15.  
  16.  SubDemo subobj=new SubDemo();
  17.  subobj.x=2;
  18.  subobj.y=2;
  19.  subobj.show();
  20.  
  21. }




4. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4.  int x;
  5. SuperDemo(){
  6.  
  7.  x=24;

  8. }
  9. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.   System.out.println("x="+super.x);
  9.   System.out.println("y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }





Programming Questions on Static

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StaticDemo{
  4.  
  5.  
  6.    static {
  7.           i=10;
  8.     }
  9.  
  10.     static   int i;  
  11.  
  12.       public static void main(String[] args) {
  13.  
  14.        System.out.println("i= "+i);
  15. }
  16. }





2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StaticDemo{
  4.  
  5.    static {
  6.           i=10;
  7.     }
  8.  
  9.     static   int i;  
  10.  
  11.       public static void main(String[] args) {
  12.         StaticDemo obj= new StaticDemo();
  13.           obj.i=20;
  14.  
  15.        System.out.println("i= "+StaticDemo.i);
  16. }
  17. }






Programming Interview Questions on loops

Think Output:

Program #1:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Loops {
  4.  
  5. public static void main(String args[]){
  6.  
  7.             int i=0;
  8.  
  9.             for( i=0;i<5;i++){               
  10.                 System.out.println(i);
  11.              }
  12.  
  13.              System.out.println("value of i after completion of loop: "+i);
  14. }
  15. }





Program #2:

 

  1. package com.instanceofjavaforus;
  2.  
  3. public class LoopsDemo2 {
  4.  
  5. public static void main(String args[]){
  6.  
  7.           int a=0,b=0;
  8.  
  9.             for(int i=0;i<5;i++){
  10.                 if(++a>2||++b>2){
  11.                    a++;
  12.                   }
  13.           }
  14.  
  15.           System.out.println("a= "+a+" b="+b);
  16.         
  17. }
  18. }









Program #3:

  1. package com.instanceofjavaforus;
  2.  
  3. public class LoopsDemo3 {
  4.  
  5. public static void main(String args[]){        
  6. int i=5;
  7. System.out.println(i++);
  8. System.out.println(i--);
  9. }
  10. }




Select Menu