Automation Using Selenium Webdriver

Sunday, 27 November 2016

Java Example Programs Interview

3 different ways to print exception message in java

In Java there are three ways to find the details of the exception .
They are
Using an object of java.lang.Exception
Using public void printStackTrace() method
Using public String getMessage() method.

1.Using an object of java.lang.Exception

 An object of Exception class prints the name of the exception and nature of the exception.
Write a Java program get detailed message details using exception class object

package exceptions;
public class ExceptionDetails {

/**
 *
 **/
 public static void main(String[] args) {

try {

int x=1/0;
         
} catch (Exception e) {
            System.out.println(e);
}

}

}
OUTPUT:
java.lang.ArithmeticException: / by zero
 2.Using  public void printStackTrace() method


This is the method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error class and java.lang.Exception class
This method will display the name of the exception and nature of the message and line number where exception has occurred.

Write a simple java example program to print exception message to console using printStacktrace() method
package exceptions;
public class ExceptionDetails {

    /**
     *
     */
public static void main(String[] args) {


 try {
          int a[]= new int[1];
            a[1]=12
         
} catch (Exception e) {
   e.printStackTrace();
         
 }
}

}

 Output:


java.lang.ArrayIndexOutOfBoundsException: 1
    at exceptions.ExceptionDetails.main(ExceptionDetails.java:13)
  3.Using public String getMessage() method

 This is also a method which is defined in java.lang.Throwable class and it is inherited in to both java.lanf.Error and java.lang.Exception classes.
This method will display the only exception message


Write a Java program print exception message using getMessage() method.
package exceptions;
public class ExceptionDetails {

    /**
     *
     */
    public static void main(String[] args) {


        try {
            int x=1/0;
         
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

 Output:


 / by zero



How can we count upper case letter in String  java ?
How to find uppercase letters in String  java?
How to find capital letters in string in java?
How u discover the capital letter given sentence?
Yes We can find uppercase or capital letters in a String using isUpperCase() method of Character class 
in java
In order to find number of uppercase letters in a string of all capital letters in a string we need to 
iterate all characters of a string in a loop and check individual characters are uppercase letters or 
not using Character class provided isUpperCase() method.
Program #1: Java example program to find all capital letters / Uppercase letters in a String 


package findUppercaseletters.String;
public class FinfUppercaseLetters {
    /**
     * 
     */
 public static void main(String[] args) {
        
        
  String str= "How to Print Uppercase Letters in Java";
    for (int i = 0; i < str.length(); i++) {
    
            if(Character.isUpperCase(str.charAt(i))){    
            System.out.println(str.charAt(i));
            }
            
 }
}
}
OUTPUT::
H
P
U
L
J

Java program to remove vowels from string java


java program to remove vowels from a string
To remove vowels from a string we can use predefined method of string  replaceAll()
By passing all vowels to the method replaceAll() with empty it will replaces all vowels with empty. 
Check below topic for more programs on string 
Java Experience interview programs on strings

 Program #1: Java example program to remove all vowels from a String



package inheritanceInterviewPrograms;
public class RemoveVowels {
    /**
     * 
     * @String interview programs asked in interviews
     * @Remove vowels from a string in java
     */
 public static void main(String[] args) {
        String str = "RemoveVowels";
        String resustr = str.replaceAll("[aeiouAEIOU]", "");
        System.out.println(resustr);
    }
}

 Output:


RmvVwls


Java example program to reverse vowels in a string.
One more java string interview question for freshers and experienced 
Check below topic for more programs on string 
Java Experience interview programs on strings

Program : Java example program to Reverse Vowels  in a String

package inheritanceInterviewPrograms;
/*
 */
public class ReverseVowels {
    public static String reverseVowels(String string) {
        String vowelsStr = "aeiouAEIOU";
        int lo = 0;
        int hi = string.length() - 1;
        char[] ch = string.toCharArray();
 while (lo < hi) {
     if (!vowelsStr.contains(String.valueOf(string.charAt(lo)))) {
                lo++;
                continue;
       }
    if (!vowelsStr.contains(String.valueOf(string.charAt(hi)))) {
                hi--;
                continue;
       }
    // swaping variables
     swap(ch, lo, hi);
            lo++;
            hi--;
      }
        return String.valueOf(ch);
    }
private static void swap(char[] ch, int lo, int hi) {
        char temparray = ch[lo];
        ch[lo] = ch[hi];
        ch[hi] = temparray;
 }
    
public static void main (String args[]) {
        
         
 System.out.println("After reversing vowels in a string="reverseVowels("InstanceOfjava"));
        
         
}
}
Output:


After reversing vowels in a string=anstancOefjavI
Swap two numbers without using third variable

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

package com.instaceofjava;
public class SwapTwoNumbers {
public static void main(String[] args) {
int number1=20;
int number2=30;
System.out.println("Before Swapping");
System.out.println("Value of number1 is :" + number1);
System.out.println("Value of number2 is :" +number2); 
number1=number1+number2;
number2=number1-number2;
number1=number1-number2;
System.out.println("After Swapping");
System.out.println("Value of number1 is :" + number1);
System.out.println("Value of number2 is :" +number2);
}
}
Output:

Before Swapping
Value of number1 is :20
Value of number2 is :30
After Swapping
Value of number1 is :30
Value of number2 is :20
2. Java Program to Swap two numbers by using division and multiplication.


package com.instaceofjava;
public class SwapTwoNumbers {
public static void main(String[] args) {
int number1=20;
int number2=30;
System.out.println("Before Swapping");
System.out.println("Value of number1 is :" + number1);
System.out.println("Value of number2 is :" +number2); 
number1=number1*number2;
number2=number1/number2;
number1=number1/number2;
System.out.println("After Swapping");
System.out.println("Value of number1 is :" + number1);
System.out.println("Value of number2 is :" +number2);
}
}
Output:

Before Swapping
Value of number1 is :20
Value of number2 is :30
After Swapping
Value of number1 is :30
Value of number2 is :20


3. Java Program to Swap two integers by using bit wise operators


package com.instaceofjava;
public class SwapTwoNumbers {
public static void main(String[] args) {
int number1=2;
int number2=4;
System.out.println("Before Swapping");
System.out.println("Value of number1 is :" + number1);
System.out.println("Value of number2 is :" +number2); 
number1=number1^number2;
number2=number1^number2;
number1=number1^number2;
System.out.println("After Swapping");
System.out.println("Value of number1 is :" + number1);
System.out.println("Value of number2 is :" +number2);
}
}
Output:

Before Swapping
Value of number1 is :2
Value of number2 is :4
After Swapping
Value of number1 is :4
Value of number2 is :2