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




Please Respond If you know about any Opportunity


 Hi Friends.


Are you currently working in some company ? Any openings in your company?
Please let us know if you have any openings in your company or your friends company  so that we can help lot of people who are really talented and waiting for an opportunity.

Send an email to us by mentioning your requirement details at Instanceofjava@gmail.com.

If you are fresher ? or looking for a job change ? 
Send your updated profile to us at Instanceofjava@gmail.com 
please mention your preferred location in subject.

Follow this naming convention for Resume Name: Yourname_yearofpassout_preferredlocation 

Regards
InstanceOfJava team

Skype id : instanceofjava

Fb Group:InstanceofJava
 

Select Menu