Constructor chaining in java using this and super Keywords

Constructor chaining example program in same class using this keyword.


  1. package instanceofjava;
  2. class ConstructorChaining{
  3. int a,b 
  4. ConstructorChaining(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8.  
  9. ConstructorChaining(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. ConstructorChaining(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20.  
  21. public static void main(String[] args){
  22.  
  23.  ConstructorChaining obj=new ConstructorChaining();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }


Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2

Constructor chaining example program in same class using this and super keywords.

  1. package instanceofjava;
  2. class SuperClass{
  3.  
  4. SuperClass(){
  5. this(1);
  6.  System.out.println("Super Class no-argument constructor");
  7.  
  8.  
  9. SuperClass(int x ){
  10.  
  11. this(1,"constructor chaining"); 
  12. System.out.println("Super class one -argument constructor(int)");
  13.  
  14. }
  15.  
  16. SuperClass(int x , String str){
  17.  System.out.println("Super class two-argument constructor(int, String)");
  18.  
  19. }




  1. package instanceofjava;
  2. class SubClass extends SuperClass{

  3. SubClass(){
  4. this(1);
  5.  System.out.println("Sub Class no-argument constructor");
  6.  
  7.  
  8. SubClass(int x ){
  9.  
  10. this("Constructor chaining"); 
  11.  System.out.println("Sub class integer argument constructor");
  12.  
  13. }
  14.  
  15. ConstructorChaining(String str){
  16. //here by default super() call will be there so it will call super class constructor
  17.   System.out.println("Sub class String argument constructor");
  18.  
  19. public static void main(String[] args){
  20.  
  21.  SubClass obj=new SubClass();
  22.  
  23.  
  24. }
  25. }

Output:

  1. Super class two-argument constructor(int, String)
  2. Super class one -argument constructor(int)
  3. Super Class no-argument constructor
  4. Sub class String argument constructor
  5. Sub class String argument constructor
  6. Sub class integer argument constructor




Java Coding Interview programming Questions : Java Test on HashMap

1.What will be the Output of this program.


  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.     System.out.println(hm.size()); 
  11.  
  12. for (Integer name: hm.keySet()){
  13.  
  14.    System.out.println(name + " " + hm.get(name)); 
  15.   
  16. }
  17.  
  18. }
  19. }





2.Basics of java programming : Hashmap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.  
  12.     System.out.println(hm.size()); 
  13.  
  14. for (Integer name: hm.keySet()){
  15.  
  16.    System.out.println(name + " " + hm.get(name)); 
  17.   
  18. }
  19.  
  20. }
  21. }









3.Java programming examples: Hashmap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.      hm.put(null,null);
  12.  
  13.     System.out.println(hm.size()); 
  14.  
  15. for (Integer name: hm.keySet()){
  16.  
  17.    System.out.println(name + " " + hm.get(name)); 
  18.   
  19. }
  20.  
  21. }
  22. }






4.Java programming for beginners : HashMap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.      hm.put(null,null);
  12.  
  13.      Integer i=null;
  14.      String str="java programming basics Interview questions";
  15.  
  16.      hm.put(i, str);
  17.  
  18.     System.out.println(hm.size()); 
  19.  
  20. for (Integer name: hm.keySet()){
  21.  
  22.    System.out.println(name + " " + hm.get(name)); 
  23.   
  24. }
  25.  
  26. }
  27. }





Find Second smallest number in java without sorting

Java interview Program to find second smallest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. int[] x ={10,11,12,13,14,6,3,-1};
  5.  
  6.         int small=x[0];
  7.  
  8.  for(int i=0;i<x.length;i++)
  9.  {
  10.         if(x[i]<small)
  11.         {
  12.         small=x[i];
  13.         }
  14.  }
  15.  
  16.    int sec_Small=x[0];
  17.  
  18. for(int i=0;i<x.length;i++)
  19.  {
  20.         if(x[i]<sec_Small && x[i]!=small)
  21.         {
  22.         sec_Small=x[i];
  23.         }
  24.   }
  25.  
  26.         System.out.println("Second Smallest Number: "sec_Small);
  27.         }
  28. }



Output:
 
  1. Second Smallest Number:3

Java interview  Program to find second Smallest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Smallest Number: "+numbers[0]);
  11.   System.out.println("Second Smallest Number: "+numbers[1]);

  12.  }
  13.  
  14. }




Output:
 
  1. Smallest Number: 3
  2. Second Smallest Number: 5



Find second highest number in an integer Array

Java Interview Program to find second highest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7. int highest = 0;
  8.  int second_highest = 0;
  9.  
  10. for(int n:numbers){
  11.  
  12. if(highest < n){
  13.  
  14.       second_highest = highest;
  15.       highest =n;
  16.  
  17.  } else if(second_highest < n){
  18.  
  19.                 second_highest = n;
  20.  
  21. }
  22.  
  23. }
  24.         System.out.println("First Max Number: "+highest);
  25.         System.out.println("Second Max Number: "+second_highest);

  26.  
  27.  }
  28.  
  29. }



Output:
 
  1. First Max Number: 64
  2. Second Max Number: 46

Java Interview Program to find second highest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Largest Number: "+numbers[numbers.length-1]);
  11.   System.out.println("Second Largest Number: "+numbers[numbers.length-2]);
  12.  }
  13.  
  14. }




Output:
 
  1. Largest Number: 64
  2. Second Largest Number: 46



How to find Biggest Substring in between specified character or String

1. Java Program to find biggest substring in between specified character or string

String: i am rajesh kumar ravi

in between: 'a'

 

  1. package com.instaceofjava;
  2.  
  3. public class BiggestSubString{
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="i am rajesh kumar ravi";
  8.  
  9.  String longest="";
  10.  int maxlength=0;
  11.  
  12.  String arr[]=str.split("a");
  13.  
  14. for (int i = 1; i < arr.length-1; i++) {
  15.  
  16.    System.out.println(arr[i]); // printing substrings
  17.  
  18.           if(arr[i].length() > maxlength){
  19.                maxlength = arr[i].length();
  20.                 longest = arr[i];
  21.             }   
  22.  
  23.    }      
  24.   
  25. System.out.println("Longest substring is: "+longest);
  26.  
  27. }
  28. }


Output:

  1. m r
  2. jesh kum
  3. r r
  4. Longest substring is: jesh kum


Java Basic Interview Programming Questions on Constructors

  • Below programs are the basic programs frequently asking in java quizzes  try to test your java programming skills by answering the questions. if you are having any doubts feel free to ask us.
  • Java Coding Questions on constructors.


1.What will be the Output of this program.

  1. public class Test1{
  2.  
  3. Test1(int i){
  4.  
  5. System.out.println("Test1 Constructor "+i);
  6.  
  7.  } 
  8. }

  1. public class Test2{
  2.  
  3. Test1 t1= new Test1(10);
  4.  
  5. Test2(int i){
  6.  
  7.  System.out.println("Test2 Constructor "+i);
  8.  

  9. public static void main(String[] args) {
  10.  
  11.         Test2 t2= new Test2(5);
  12.  
  13.     }
  14. }







2.What will be the Output of this program.

  1. public class A{
  2.  
  3. A(){
  4.  
  5.   System.out.println("A Class Constructor ");
  6.  
  7. }
  8.  
  9. }

  1. public class B extends A{
  2.  
  3. B(){
  4.  
  5.   System.out.println("B Class Constructor ");
  6.  
  7. }
  8. public static void main(String[] args) {
  9.  
  10.         B ob= new B();
  11.  
  12.     }
  13. }







3.What will be the Output of this program.

  1. public class  A{
  2.  
  3. A(){

  4. this(0);
  5. System.out.println("Hi ");
  6.  
  7.  
  8. A(int x){

  9. this(0,0);
  10. System.out.println("Hello");
  11.  
  12. }
  13.   
  14. A(int x, int y){
  15.  
  16. System.out.println("How are you");
  17.  
  18. }
  19. public static void main(String[] args) {
  20.  
  21.         A ob= new A();
  22.  
  23.     }
  24. }




Select Menu