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

Group Discussion on Java Topic

Hi Friends We are planning to conduct a group discussion on a topic every week So interested candidates can join us based on everybody convenience time we will schedule it.

We will conclude the topic at the end.

Our Skype ID : Instanceofjava.

Update: Based on your interest and time we will schedule the session. its free




Java Interview programs on Strings


1.Reverse a String Without using String API?

  1. package com.instaceofjava;
  2.  
  3. public class ReverseString {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="Hello world";
  8. String revstring="";
  9.  
  10. for(int i=str.length()-1;i>=0;--i){
  11. revstring +=str.charAt(i);
  12. }
  13.  
  14. System.out.println(revstring);
  15. }
  16. }


Program:

output: dlrow olleH.

2)Sorting the String without using String API?


  1. package com.Instanceofjava;
  2.  
  3. public class SortString {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.   String original = "edcba";
  8.  int j=0;
  9.    char temp=0;
  10.  
  11.      char[] chars = original.toCharArray();
  12.      for (int i = 0; i <chars.length; i++) {
  13.          for ( j = 0; j < chars.length; j++) {
  14.          if(chars[j]>chars[i]){
  15.              temp=chars[i];
  16.              chars[i]=chars[j];
  17.               chars[j]=temp;
  18.           }
  19.      }
  20.   }
  21.  
  22.     for(int k=0;k<chars.length;k++){
  23.     System.out.println(chars[k]);
  24.    }
  25.  
  26.  }
  27. }

program:

output:abcde.

3.Sort the String with using String API?

program:
  1. package com.instanceofjava;
  2.  
  3.  public class SortString {
  4.  
  5.  public static void main(String[] args) {
  6.    String original = "edcba";
  7.  
  8.    char[] chars = original.toCharArray();
  9.    Arrays.sort(chars);
  10.  
  11.     String sorted = new String(chars);
  12.      System.out.println(sorted);
  13.  
  14. }
  15. }




OutPut:abcde

4.Check String is palindrome or not?

program:

Solution #1:

  1. package com.instaceofjava; 
  2.  
  3. public class PalindromeDemo{ 
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="MADAM";
  8. String revstring="";
  9.  
  10. for(int i=str.length()-1;i>=0;--i){
  11. revstring +=str.charAt(i);
  12. }
  13.  
  14. System.out.println(revstring);
  15.  
  16. if(revstring.equalsIgnoreCase(str)){
  17. System.out.println("The string is Palindrome");
  18. }
  19. else{
  20. System.out.println("Not Palindrome");
  21. }
  22.  
  23. }
  24.  
  25. }



  Output:

The string is Palindrome

Solution #2:

  1. package com.instaceofjava;
  2.  
  3.   import java.util.Scanner;
  4.  
  5.  public class Palindrome {
  6.  
  7. public static void main(String[] args)
  8. {
  9.  
  10. Scanner in = new Scanner(System.in);
  11.  
  12.  System.out.println("Enter a string");
  13.  String str=in.nextLine();
  14.  
  15. StringBuffer strone=new StringBuffer(str);
  16.  StringBuffer strtwo=new StringBuffer(strone);
  17.  
  18.   strone.reverse();
  19.  
  20. System.out.println("Orginal String ="+strtwo);
  21. System.out.println("After Reverse ="+strone);
  22.  
  23. if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
  24.    System.out.println("Result:Palindrome");
  25.    else
  26.     System.out.println("Result:Not Palindrome");
  27.  
  28.    }
  29.  
  30. }




 Output:

Enter a string
MOOM
Orginal String =MOOM
After Reverse =MOOM

Result:Palindrome

5.Program to Check given number is palindrome or not?

Program:

  1.  package com.instaceofjava; 
  2.  
  3. import java.util.Scanner; 
  4.  
  5. public class Palindrome {
  6.  
  7. public static void main(String[] args)
  8. {
  9.  
  10. System.out.println("Please Enter a number : ");
  11.       int givennumber = new Scanner(System.in).nextInt();
  12.       int number=givennumber;
  13.        int reverse=0;
  14.         while (number != 0) {
  15.            int remainder = number % 10;
  16.             reverse = reverse * 10 + remainder;
  17.             number = number / 10;
  18.         }           
  19. if(givennumber == reverse)
  20.    System.out.println("Result:Palindrome");
  21.     else
  22.     System.out.println("Result:Not Palindrome");
  23.     }
  24.  
  25. }

 Output:

Please Enter a number :
535
Result:Palindrome.

 Also Read:


Top 16 Java Inheritance Interview questions for freshers and experienced


1.what is inheritance?
  • inheritance is one of the oops concepts in java.inheritance is concept of  getting properties of one class object to another class object.
  • Inheritance represents the IS-A relationship,also known as parent-child relationship.
2.what are the types of inheritance?

1.Multiple inheritance( java doesn't support multiple inheritance).
2.Multilevel inheritance.

3.How Inheritance can be implemented in java?
  • Inheritance can be implemented in JAVA using below two keywords:
1.extends
2.implements
  • extends is used for developing inheritance between two classes and two interfaces.
  • implements keyword is used to developed inheritance between interface and class.
4.Why we need to use Inheritance?

1.For Code Re usability.
2.For Method Overriding.

5.what is syntax of inheritance?

public class subclass extends superclass{

//all methods and variables declare here
}

6.what is multilevel inheritance?
  • Getting the properties from one class object to another class object level wise with different priorities.


6.what is Multiple inheritance?why Java Doesn't Support multiple Inheritance.
  • The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
  • In multiple inheritance there is every chance of multiple properties of multiple objects with  the same name available to the sub class object with same priorities leads for the ambiguity. also known as diamond problem. one class extending two super classes.
  • Because of multiple inheritance there is chance of the root object getting created more than once.
  • Always the root object i.e object of object class hast to be created only once.
  1. Because of above mentioned reasons multiple inheritance would not be supported by java.
  2. Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.

8.How do you implement multiple inheritance in java?
  • Using interfaces java can support multiple inheritance concept in java. in java can not extend more than one classes, but a class can implement more than one interfaces.
Program:

interface A{

}
interface B{
}
class C implements A,B{
}

9.Can a class extend itself?

  • No,A class can't extend itself.

10.What happens if super class and sub class having same field name?


  • Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.

Static methods in java example

  • A method which has static keyword in its definition is called static method.
  1. static void print(){
  2.  
  3. }

  • JVM will not execute static methods by default. They are executed only if they are called explicitly by developer either from main method or from static variable as its assignment statement or from static block.

Static method called from main method:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  
  5. static void show(){
  6. System.out.println("static method"):
  7. }
  8.  
  9. public static void main(String[] args)
  10. {
  11.  
  12. show();
  13.  
  14. }
  15. }
Output:

  1. static method

Static method called from variable assignment:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int x=show();
  5. static int show(){
  6. System.out.println("static method called"):
  7. return 10;
  8. }
  9.  
  10. public static void main(String[] args)
  11. {
  12.  
  13. System.our.println(x);
  14.  
  15. }
  16. }
Output:

  1. static method called
  2. 10

Static method called from static block:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static{
  5.  show();
  6. }

  7. static void show(){
  8. System.out.println("static method called"):
  9. }
  10.  
  11. }
Output:

  1. static method called

Order of Execution:

  •  Static methods are executed in the order of they are called, not in the order of they are defined.
  • All static methods are executed in java stack area by creating separate stack frame.
  • When a method is called from main method, JVM creates stack frame in main thread for that method execution.
  • The stack frame is destroyed immediately after method execution is completed.

Example program on order of execution of static methods:


  1. package com.instanceofjava;
  2. class StaticMehodDemo
  3. {
  4.  
  5. static void show(){
  6.  
  7. System.out.println("show method called");
  8.  
  9. }
  10.  
  11. static void print(){
  12.  
  13. System.out.println("print method called");
  14.  
  15. }
  16.  
  17. static void display(){
  18.  
  19. System.out.println("display method called");
  20.  
  21. }
  22.  
  23.  
  24. public static void main(String[] args)
  25. {
  26.  
  27. System.our.println("main method called");
  28.  
  29. show();
  30. print();
  31. display();
  32.  
  33.  
  34. }
  35. }


Output:

  1. main method called
  2. print method called
  3. show method called
  4. display method called

Variable initialization with same variable:

  •  We can initialize variable with same variable name. this assignment is valid. In this case the variable value is replaced with same value.
  1. int x=20;
  2. x=x; // valid statement
Example program:



  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int a=10;

  5. public static void main(String[] args){
  6.   
  7. int a=20;
  8. a=a;
  9. System.out.println("a="+a);
  10.  System.out.println("StaticDemo.a="+StaticDemo.a);
  11.  
  12. }
  13.  
  14. }
Output:

  1. 20
  2. 10

 

Local preference with parameters:

  • Parameters are also treated as local variables.
  • Hence if parameter declared with same static variable name and if we want to access static variable in presence of parameter or if we want to initialize static variable with parameter name we must refer variable with class name.

Example program:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  static int x=10;
  5.  
  6. static void m1(int x){ 
  7.  
  8.  System.out.println(x);
  9.  System.out.println(StaticDemo.x);

  10. }

  11. public static void main(String[] args){
  12.   
  13.  m1(37);
  14.  System.out.println(x);
  15.  
  16. }
  17.  
  18. }
Output:

  1. 37
  2. 10

 

You might Like:

 

 

 

 


Static Members in java

  • The class level members which have static keyword in their definition are called static members.

Types of Static Members:

  •  Java supports four types of static members
  1. Static Variables
  2. Static Blocks
  3. Static Methods
  4. Main Method (static method)

  • All static members are identified and get memory location at the time of class loading by default by JVM in Method area.
  • Only static variables get memory location, methods will not have separate memory location like variables.
  • Static Methods are just identified and can be accessed directly without object creation.

1.Static Variable:

  • A class level variable which has static keyword in its creation statement is called static variable.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7. }

  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression

  10. }


  • All static variables are executed by JVM in the order of they defined from top to bottom.
  • JVM provides individual memory location to each static variable in method area only once in a class life time.

Life time and scope:

  •  Static variable get life as soon as class is loaded into JVM and is available till class is removed from JVM or JVM is shutdown.
  • And its scope is class scope means it is accessible throughout the class.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7.  public static void main(String [] args){
  8.  
  9.    System.out.println("a="+a);
  10.    System.out.println("a="+b);
  11.    show();

  12.  }
  13.  
  14. public static void show(){
  15.  
  16.    System.out.println("a="+a);
  17.    System.out.println("a="+b);
  18. }

  19. }

static methods JVM architecture

Duplicate Variables:

  • If multiple variables are created with same name are considered as duplicate variables.
  • In the same scope we can not create multiple variables with same name.
  • If we create it leads to compile time error "variable is already defined".
  • Even if we change data type or modifier or its assigned value we can not create another variable with same name.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  
  5. static int a=10;
  6. int a=30;// Compile time error 
  7.  
  8. }

  • But it is possible to create multiple variables with same name in different scopes.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;
  5. int a=30;// Compile time error
  6.  
  7. public static void main(String [] args){
  8.   
  9. // it is allowed to define "a" in this method.
  10. int a=20;
  11.  
  12. }
  13. }

Shadowing:

  • It is possible to create local variables or parameters with same variable name.
  • The concept is called shadowing . It means local variable is a shadow of class level variable.
  • It means when you access it in side method , you will get local variables value but not  from class level.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.   

  8.  int a=20;
  9.  System.out.println("a="+a); // prints 20

  10. }
  11. }

 Local preference:

  • When we call a variable compiler and JVM will search for its definition in that method first, if its definition is not found in that method then will search for its definition at class level.
  • If its definition not found at class level also then compiler throws error: can not find symbol.
  • This phenomenon is called local preference.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a); // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a); // prints 20 :method level variable found

  11. }
  12. }


  • By using class name we can get class level variables value.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a); // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a); // prints 20 :method level variable found

  11.  System.out.println("a="+StaticDemo.a); // prints 10 : class level variable
  12.  
  13. }
  14. }


Order of execution of static variables and main method:

  • First all static variables are executed in the order they defined from top to bottom then main method is executed.
Example Program:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=m1();
  5.  
  6. static int m1(){
  7.  
  8. System.out.println("variable a is created");
  9. return 10;
  10.  
  11.  
  12. static int b=m2();
  13.  
  14. static int m2(){
  15.  
  16. System.out.println("variable b is created");
  17. return 20;

  18.  
  19. public static void main(String [] args){
  20.   
  21.  System.out.println("in main method");
  22.  System.out.println("a="+a);
  23.  System.out.println("b="+b);


  24.  
  25. }
  26. }

Output:
  1. variable a is created
  2. variable b is created
  3. in main method
  4. 10
  5. 20

  • we can access static fields using objects but static fields should be accessed in a static way because static fields are class level so even if we change field value using particular object that will change the original value 


  1. package com.instanceofjava;
  2.  
  3. public class StaticDemo {
  4.  
  5.  static int x=10;
  6.  
  7. public static void main(String[] args) {
  8.  
  9.  StaticDemo obj=new StaticDemo();
  10.  
  11. System.out.println(x);
  12.  
  13. obj.x=30;
  14.  
  15. System.out.println(x);
  16.  
  17. StaticDemo obj2=new StaticDemo();
  18.  System.out.println(obj2.x); 
  19.  
  20. System.out.println(StaticDemo.x); // Recommended way to access static variables

  21. }
  22. }

Output:

  1. 10
  2. 30
  3. 30
  4. 30

Final Static Variables:

  • Final static variables are constants.
  • Declaration itself we need to assign value to the final static variables otherwise compiler error will come: "The blank final field  may not have been initialized".
  • And we can not change this value if we try then compiler error will come: The final field  cannot be assigned.


  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4.  final static int MAX_VALUE=10;

  5.  public static void main(String [] args){
  6.  
  7.    System.out.println(MAX_VALUE);

  8. }



Select Menu