Java Interview

Theory of Computation
System.out.println("Printing message");
Javac  Demo.java
Java Demo
type variable1 = (type) variable2;
Conditional expression ? expression1 : expression2;
type methodname(argument list) {  statements}
public class AddTwoNumbers {

   public static void main(String[] args) {
        
      int num1 = 85, num2 = 35, sum;
      sum = num1 + num2;

      System.out.println("Sum of given numbers: "+sum);
   }
}
Sum of given numbers: 120
public class SumOfPrimeNumbers  
{  
  public static void main(String args[])   
  {  
    int sum = 0, num = 1;  
 
    while(num <= 150)  
    {  
      int i = 2, counter = 0;  
      while(i <= num/2 )  
      {  
        if(num % i == 0)  
        {  
          counter++;  
          break;  
        }  
        //increments the variable i by 1  
        i++;  
      } 
      // calculates the sum of prime numbers 
      if(counter == 0 && num != 1 )            
      {  
        sum = sum + num;  
      }
      num++;  
    }  
    //prints the sum  
    System.out.println("The Sum of Prime Numbers from 1 to 150 : " + sum);  
  } 
}   
The Sum of Prime Numbers from 1 to 150 : 2276
public class SumOfDigits
{  
  // defining function 
  // to calculate the sum of digits of a number  
  static int findSum(int num)  
  {  
    return num == 0 ? 0 : num % 10 +findSum(num/10) ;  
  }  
  //driver code  
  public static void main(String args[])  
  {  
    int number = 345237;   
    System.out.println("The sum of digits: "+findSum(number));  
  }   
}  
The sum of digits: 24
import java.util.*;  
public class Swap_Numbers {  
    public static void main(String[] args) {  
       int a, b, temp;
       
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter the value of a and b: ");  
       a = sc.nextInt();  
       b = sc.nextInt();  
       System.out.println("Before swapping numbers: "+a +"  "+ b); 
       
       // Value of first is assigned to temporary 
       temp = a;  
       
       // Value of second is assigned to first
       a = b;  
       
       // Value of temporary is assigned to second 
       b = temp;  
       System.out.println("After swapping numbers: "+a +"   " + b);  
       System.out.println( );  
    }    
} 
Enter the value of a and b: 23 53
Before swapping numbers: 23  53
After swapping numbers: 53   23
// Java remove whitespaces
// using built in method  

public class RemoveSpace {
    public static void main(String[] args)
    {
        String str = "  Welcome to etutorialspoint .   ";
        System.out.println("Original String: " + str);
        // Calling the replaceAll() method
        str = str.replaceAll("\\s", "");
        System.out.println("New String: " + str);
    }
} 
Original String:   Welcome to etutorialspoint .   
New String: Welcometoetutorialspoint.
import java.util.Arrays;  
public class SecondLargestInArray{  
    public static int getSecondLargest(int[] a, int len){  
        Arrays.sort(a);  
        return a[len-2];  
    }  
    public static void main(String args[]){  
        int x[]={32,84,48,91};  
        int y[]={2,4,5,7,1,6,9};  
        System.out.println("Second Largest Number: "+getSecondLargest(x,4));  
        System.out.println("Second Largest Number: "+getSecondLargest(y,7));  
    }
    
}
Second Largest Number: 84
Second Largest Number: 7
public class FindGCD   
{  
    public static void main(String[] args)   
    {  
    int x=32, y=28;  
    while(x!=y)   
    {  
        if(x>y)  
        x=x-y;  
        else  
        y=y-x;  
    }  
    System.out.printf("GCD of x and y is: " +y);  
    }   
}  
GCD of x and y is: 4
import java.util.Scanner;

public class AreaOfCircle 
{
  public static void main(String args[]) 
  {   
      Scanner s= new Scanner(System.in);
      System.out.println("Please enter the radius : ");
      double r = s.nextDouble();      
      double a = circleArea(r);
      System.out.println("Area of Circle : " + a);      
  }
  static double circleArea(double r)
  {
	  return (22*r*r)/7;
  }
}
Please enter the radius : 22
Area of Circle : 1521.1428
// Java program for counting vowels
import java.util.Scanner;

public class CountingVowels {
  
   public static void main(String args[]){
      int count = 0;
      System.out.println("Please enter a string :");
      Scanner sc = new Scanner(System.in);
      String string = sc.nextLine();

      for (int i=0 ; i
Please enter a string : elephant is a big animal
Total number of vowels in the given sentence : 9
public class FibonacciSeries{  
    
public static void main(String args[])  
{    
 int f1=0,f2=1,n,i,count = 15;
 
 //printing 0 and 1  
 System.out.print(f1+" "+f2);  
    
 // start the loop from 2    
 for(i=2;i
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377