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



Java Variables and Types of Variables


Variable:

  • Variable is a named memory location used to store data temporarily.
  • During program execution we can modify that data

Limitation of Variable:

  •  It can only store single value at a time.
  • It means , always it returns latest modified value.

How can a variable be created?

  • A variable can be created by using data type.
  • As we have two types of data types  we can create two types of variables.
    1.Primitive Variable
    2.Referenced variable
  • The difference between primitive and referenced variable is "primitive variable stores data directly , where referenced variables stores reference/address of object, not direct values"

  1. package com.instanceofjava;
  2.  
  3. class Example
  4. {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9. }


  1. package instanceofjava;
  2. class Test
  3. {
  4.  
  5. public int get(){
  6. return 10:
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11. //primitive variables
  12. int s=50;
  13. int t=get(); 
  14.  
  15. //reference variables
  16. String str="a";
  17. String str1=new String("a");
  18. Example e= new Example();

  19. }
  20. }



What is an object?

  • Technically object means collection of all non static variables and methods memory locations.
  • Object is created using new keyword

Defining a variable:

  • Variable creation with value is called defining a variable
  • <Accessibility modifier><Modifier><Data type><Variable name>=<Value> ;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int x=10;
  6. public static Example e= new Example();

  7. }
  8. }

Declaring a variable:

  • Variable creation without value is called declaring a variable.
  • <Accessibility modifier><Modifier><Data type><Variable name>;


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int a;
  6. public static Example e;

  7. }
  8. }


Initializing/Assigning a variable:

  • Storing a value in a variable at the time of its creation is called initializing a variable.
  • Storing a value in a variable after its creation is called assigning a value to a variable.
  • <Variable_Name>=<Value>;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. //declaring a variable
  6.  int a;
  7.  int x;
  8.  
  9. //assigning a variable
  10.  a=20;
  11.  x=10;
  12.  
  13. // re initialization/ re assignment
  14.  a=30;
  15.  x=20;

  16. }
  17. }


Calling a variable:

  • Reading a value from a variable is called calling a variable.


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5.  int x=10;
  6.   
  7.  //calling x variable for printing
  8. System.out.println(x);
  9.  
  10. // calling x variable for initializing y
  11.  int y=x;


  12. }
  13. }



Types of Variables:

  • Local
  • Static
  • Non static
  • Final
  • Transient
  • Volatile

1.Local Variables:

  • The variables created inside a method or block are called local variables.

Rules:

  • While working with local variables , we must follow below 3 rules.

Rule #1:

  •  Local variables can not be accessed from another method. Because its scope is restricted only within its method.
  • Also we can not guarantee that variable creation , because that method may or may not be called. It leads to compile time Exception : can not find symbol.
 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5. public void show(){
  6.  System.out.println("a:"+a); Compile time error: can not find symbol a.
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11.  int a=10;
  12.  System.out.println("a:"+a);
  13. }
  14. }


Rule #2:

  • Local variable should not be accessed without initialization.

 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4. public static void main(String [] args){
  5.  
  6.  int a=10;
  7.  int b;
  8.  System.out.println("a:"+a);
  9.  
  10.  System.out.println("b:"+b);//Error
  11.  // Above statement throws Compile time Error: variable b might not have been initialized
  12.  
  13.  b=20;
  14. System.out.println("b:"+b);
  15.  
  16. }
  17. }


Rule #3:

  • Local variable must be accessed only after its creation statement. because method execution is sequential execution from top to bottom. If we access before  its creation statement it leads compile time Error : Can not find symbol.


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5.  static void show(){
  6.  System.out.println("a:"+a);// Compile time Error
  7.  int a;
  8.  System.out.println("a:"+a); // Compile time Error
  9.   
  10.  a=10
  11.  
  12.  System.out.println("a:"+a);
  13.  
  14. }
  15. }

2. Static variables :

  • Static variables gets life when class is loaded.
  • It is destroyed either if class is unloaded from JVM or JVM is destroyed.
  • Its scope is throughout the class it means where class is available there static variable is available provided it is public.

 Example program:

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

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

3. Non Static variables:

  • Non static variable gets life when object is created. It is destroyed when object is destroyed.
  • Object is destroyed when it is unreferenced.
  • Its scope is the scope of the object, object is available only if its referenced variable is available

 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5.  int a=10;

  6. public void show(){
  7.   test t= new test();
  8.  System.out.println("a:"+t.a);
  9. }
  10.  
  11. public static void main(String [] args){
  12.  
  13.   test t= new test();
  14.  System.out.println("a:"+t.a);
  15.  
  16. }
  17. }


4.Final variables:

  • The class level or local variable that has final keyword in its definition is called final variable.

Rule:

  •  Once it is initialized by developer its value can not be changed. If we try to change its value it leads to Compile tile error.


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static final int a=10;
  5. static final int b=20;

  6. public static void main(String [] args){
  7.  
  8.  a=20;//Error : variable might be already have been assigned
  9.  b=30; //Error : variable might be already have been assigned
  10.  
  11. }
  12. }


5. Transient variable:

  • The class level variable that has transient keyword in its definition is called transient variable.

Rule:

  • Local variable can not be declared as transient.
  • It leads to Compile time error: Illegal start expression


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static transient int a=10;
  5. static transient  int b=20;

  6. public static void main(String [] args){
  7.  
  8.  transient int x=10; // compile time error : illegal start expression
  9.  
  10. }
  11. }

  • We declare variable as transient to tell to JVM that we do not want to store variable value in a file in object serialization. Since local variable is not part of object , declaring it as transient is illegal.
  • Refer IOStreams for more details

6.Volatile Variable:

  • The class level variable that has volatile keyword in its definition. 

Rule:

  • Local variable can not be declared as transient.
  • It leads to Compile time error: Illegal start expression


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static volatile int a=10;
  5. volatile int b=20;

  6. public static void main(String [] args){
  7.  
  8.  volatile int x=10; // compile time error : illegal start expression
  9.  
  10. }
  11. }

  • We declare variable as volatile to tell to JVM that we do not want to modify variable value concurrently by multiple threads.
  • If we declare variable as volatile multiple threads are allowed to change its value in sequence one after another one.


You might like:

Top 15 Garbage Collection Interview Questions

Top 50 Java Questions everybody should know

Top 25 Programs asked in interviews

Top 25 Core Java Interview Questions

Top 20 Java Interview Questions on Constructors


1. Define Constructor?

  • Constructor is a special method given in OOP language for creating and initializing object.
  • In java , constructor role is only initializing object , and new keyword role is crating object.

2.What are the Rules in defining a constructor?

  •  Constructor name should be same as class name.
  • It should not contain return type.
  • It should not contain Non Access Modifiers: final ,static, abstract, synchronized
  • In it logic return statement with value is not allowed.
  • It can have all four accessibility modifiers: private , public, protected, default
  • It can have parameters
  • It can have throws clause: we can throw exception from constructor.
  • It can have logic, as part of logic it can have all java legal statement except return statement with value.
  • We can not place return in constructor.

 3. Can we define a method with same name of class?

  • Yes, it is allowed to define a method with same class name. No compile time error and no runtime error is raised, but it is not recommended as per coding standards.

4.If we place return type in constructor prototype will it leads to Error?

  • No, because compiler and JVM considers it as a method.

5. How compiler and JVM can differentiate constructor and method definitions of both have same class name?

  • By using return type , if there is a return type it is considered as a method else it is considered as constructor.

6. How compiler and JVM can differentiate constructor and method invocations of both have same class name?

  • By using new keyword, if new keyword is used in calling then constructor is executed else method is executed.

7.Why return type is not allowed for constructor?

  •  As there is a possibility to define a method with same class name , return type is not allowed to constructor to differentiate constructor block from method block.

8.Why constructor name is same as class name?

  •  Every class object is created using the same new keyword , so it must have information about the class to which it must create object .
  • For this reason constructor name should be same as class name.

9.Can we declare constructor as private?

  •  Yes we can declare constructor as private.
  • All four access modifiers are allowed to
  •  constructor.
  • We should declare constructor as private for not to allow user to create object from outside of our class.
  • Basically we will declare private constructor in Singleton design pattern.
  • Read more at

constructor interview questions in java

10.Is Constructor definition is mandatory in class?

  •  No, it is optional . If we do not define a constructor compiler will define a default constructor.

11. Why compiler given constructor is called as default constructor?

  • Because it obtain all its default properties from its class.
  • They are
    1.Its accessibility modifier is same as its class accessibility modifier
    2.Its name is same as class name.
    3.Its does not have parameters and logic.

12. what is default accessibility modifier of default constructor?

  •  It is assigned from its class.

13.When compiler provides default constructor?

  •  Only if there is no explicit constructor defined by developer.

14.When developer must provide constructor explicitly?

  • If we want do execute some logic at the time of object creation, that logic may be object initialization logic or some other useful logic.

15.If class has explicit constructor , will it has default constructor?

  • No. compiler places default constructor only if there is no explicit constructor.
16.Programming interview questions on constructors





19.Differences between default constructor and no argument constructor

20.What is the order of execution of constructor with Non static blocks 


  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 java programs: Top 50 java interview programs

How Java Technology Change My Life

  • We can not promise you fame, fortune or even a job if you learn the Java Programming
  • Still , It is likely to make your programs better and requires less effort than other languages.
  • We believe that Java technology will help you do the following.

1. Get Started Quickly:

  • Although the java programming is powerful object oriented language, its easy to learn.

2.Write less code

  •  Comparisons of program metrics(class count , method count and so on.) suggest that a program written in java programming language can be four times smaller than the same program written in C++.

3.Write Better code:

  •  Java Programming language encourages good coding practices and automatic garbage collection helps you avoid memory leak.

4.Develop programs more quickly:

  • The Java programming language is simpler than C++  and as such , your development time could be up to twice as fast when writing in it.
  • Your programs also require fewer lines of code.

5.Avoid platform dependencies:

  • You can keep your program portable by avoiding the use of libraries written in other languages.

6.Write once run anywhere:

  • Because applications written in the java programming language are compiled into machine independent bytecode, they run consistently on any java platform.

 7.Distribute software more easily:

  • With Java web start software , users will be able to launch your application with a single click of the mouse .
  • An automatic version check at start up ensures that users are always up to date with the latest version of your software .
  • If an update is available , the java web start software will automatically update their installation.

 What can Java Technology Do?

  • The general purpose , high level java programming language is powerful software platform.
  • Every full implementation of the java platform gives you the following features.

Development Tools:

  • The development tools provide everything you will need for 
  • compiling , 
  • running ,
  • mongering,
  • debugging and documenting your application.  

Application Programming Interface(API):

  • The API provides the core functionality of the java programming language . It offers a wide array of useful classes ready for use in your own applications.

Deployment Technologies:

  • The JDK software provides standard mechanism such as the java web start software  and java plug in software for developing your applications to end users.

User Interface Tool-kits:

  •  The swing and Java 2D tool kits make it possible to create sophisticated Graphical User Interfaces(GUIs).

Integration Libraries:

  • Integration libraries such as Java IDL API, JDBC API, Java Naming and Directory Interface(JNDI)  API, Java RMI and Java remote method invocation over Internet inter -ORB protocol Technology(Java RMI-IIOP technology)  enable database access and manipulation of remote objects.
Select Menu