Wednesday, 23 November 2016

Java Interview Program to Swap two numbers without using third variable in java

  1. public class SwapTwoNumbers {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. int number1=20;
  6. int number2=30;
  7.  
  8. System.out.println("Before Swapping");
  9. System.out.println("Value of number1 is :" + number1);
  10. System.out.println("Value of number2 is :" +number2); 
  11.  
  12. number1=number1+number2;
  13. number2=number1-number2;
  14. number1=number1-number2;
  15.  
  16. System.out.println("After Swapping");
  17. System.out.println("Value of number1 is :" + number1);
  18. System.out.println("Value of number2 is :" +number2);
  19.  
  20. }
  21. }



  1. public class SwapTwoNumbers {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. int number1=2;
  6. int number2=4;
  7.  
  8. System.out.println("Before Swapping");
  9. System.out.println("Value of number1 is :" + number1);
  10. System.out.println("Value of number2 is :" +number2); 
  11.  
  12. number1=number1^number2;
  13. number2=number1^number2;
  14. number1=number1^number2;
  15.  
  16. System.out.println("After Swapping");
  17. System.out.println("Value of number1 is :" + number1);
  18. System.out.println("Value of number2 is :" +number2);
  19.  
  20. }
  21. }

Java programming examples: Hashmap

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


 


  1. 4
  2. null null
  3. 1 ONE
  4. 2 two
  5. 3 three

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

class SecondSmallestNumber{

public static void main(String args[])
{

int numbers[] = {6,3,37,12,46,5,64,21};

  Arrays.sort(numbers);

  System.out.println("Smallest Number: "+numbers[0]);
  System.out.println("Second Smallest Number: "+numbers[1]);

 }

}

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

int[] x ={10,11,12,13,14,6,3,-1};

        int small=x[0];

 for(int i=0;i<x.length;i++)
 {
        if(x[i]<small)
        {
        small=x[i];
        }
 }

   int sec_Small=x[0];

for(int i=0;i<x.length;i++)
 {
        if(x[i]<sec_Small && x[i]!=small)
        {
        sec_Small=x[i];
        }
  }

        System.out.println("Second Smallest Number: "sec_Small);
        }
}

Program to print prime numbers in java


public class primenumbers {

public static void main(String[] args) {

int num=50;
int count=0;

for(int i=2;i<=num;i++){

count=0;

for(int j=2;j<=i/2;j++){

if(i%j==0){
count++;
break;
}

}

if(count==0){

System.out.println(i);

}

}

}

}

What is the output of following program?

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

 

  1. Exception in thread "main" java.lang.StackOverflowError
  2. at com.instanceofjava.B.<init>(B.java:3)
  3. at com.instanceofjava.B.<init>(B.java:5)
  4. at com.instanceofjava.B.<init>(B.java:5)

What is the output of following program?

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

 

  1. Static method called

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.

What is the output of following program?

What is the output of following program?

a++ or a-- is postfix operation, meaning that the value of a will get changed after the evaluation of expression.

++a or --a is prefix operation, meaning that the value of a will get changed before the evaluation of expression.

lets assume this;

a = 4;

b = a++; // first b will be 4, and after this a will be 5

// now a value is 5
c = ++a; // first a will be 6, then 6 will be assigned to c
  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. }

  1. 2