Check if a particular value exists in Java TreeSet example

1.Basic Java program to check particular value present in TreeSet or not?

  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetDemo{ 
  3.  
  4. public static void main(String args[]) {
  5. //create TreeSet object
  6.  TreeSet<String> treeSet = new TreeSet<String>();
  7.        
  8.  //add elements to TreeSet
  9.   treeSet.add("1");
  10.   treeSet.add("3");
  11.   treeSet.add("2");
  12.   treeSet.add("5");
  13.   treeSet.add("6");
  14.   treeSet.add("7");
  15.   treeSet.add("8");
  16.   treeSet.add("9");
  17.   treeSet.add("10");
  18.         
  19.  boolean isExists = treeSet.contains("5");
  20.  System.out.println("5 exists in treeSet ?  " + isExists); 

  21. }
  22. }



Output:


  1. 5 exists in treeSet ? :true

1.Basic Java program to add elements ans display all elements



  1. package com.TreeSetExamplePrograms;
  2. import java.util.Scanner;
  3. public Class TreeSetDemo{ 
  4.  
  5. public static void main(String args[]) {
  6. //create TreeSet object
  7.  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  8.        
  9.  //add elements to TreeSet
  10.   treeSet.add(1);
  11.   treeSet.add(3);
  12.   treeSet.add(2);
  13.   treeSet.add(5);
  14.   treeSet.add(6);
  15.   treeSet.add(7);
  16.   treeSet.add(8);
  17.   treeSet.add(9);
  18.   treeSet.add(10);
  19.         

  20.  System.out.println(treeset); 

  21. }
  22. }

Output:


  1. [1, 2, 3, 5, 6, 7, 8, 9, 10]

treeset add elements in java example program

Basic Java Example Program to check even or add

1.Basic Java program to check even or add

  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class EvenOrOdd{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. Scanner in= new Scanner(System.in);
  8. System.out.println("Please enter number to check even or odd");
  9.  
  10.     int n=in.nextInt();
  11.    
  12.          
  13. if(n%2==0){
  14.   System.out.println(+n" is even number");
  15. } else{
  16.  
  17. System.out.println(+n" is even number");
  18.  
  19. }
  20. }
  21. }

Output:


  1. Please enter number to check even or odd
  2. 4
  3. 4 is even number




2.Basic Java program to check even or add from 1 to 10

  1. package com.BasicJavaProgramsExamples;
  2. public Class EvenOrOdd{ 
  3.  
  4. public static void main(String args[]) {
  5.  
  6. for (int a=1; a<=10; a=a++){
  7.  if (a%2==0)
  8.     System.out.println(a+" is even");
  9. else
  10.     System.out.println(a+" is odd");
  11.  
  12. }   
  13. }
  14. }

Output:

  1. 1 is odd
  2. 2 is even
  3. 3 is odd
  4. 4 is even
  5. 5 is odd
  6. 6 is even
  7. 7 is odd
  8. 8 is even
  9. 9 is odd
  10. 10 is even

3.Basic Java program to check even or add from an array

  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class EvenOrOdd{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
  8.  
  9. for (int a=0; a<=numbers .length; a=a++){
  10.  
  11.  if (numbers[i]%2==0)
  12.     System.out.println(numbers[i]+" is even number");
  13. else
  14.     System.out.println(numbers[i]+" is odd number");
  15.  
  16. }    

  17. }
  18. }

Output:


  1. 1 is odd number
  2. 2 is even number
  3. 3 is odd number
  4. 4 is even number
  5. 5 is odd number
  6. 6 is even number
  7. 7 is odd number
  8. 8 is even number
  9. 9 is odd number
  10. 10 is even number
 

Java Example program to calculate area of reactangle

In this tutorial we will see how to calculate Area of Rectangle.

1.Write a Basic java example  program to find area of Rectangle

  •  To calculate the area of rectangle we need to ask user to enter length and width of the rectangle so that we can calculate area of rectangle using formula area of rectangle=length*width



  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class AreaOfRectangle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. Scanner in= new Scanner(System.in);
  8. System.out.println("Please enter length of a rectangle");
  9.  
  10.     double length=in.nextDouble();
  11.      
  12. System.out.println("Please enter width of a rectangle");
  13.  
  14.     double width=in.nextDouble();
  15.  
  16. /*
  17.  *Area of a rectangle is
  18.  *Area= length*width;
  19.  *
  20.  */
  21.          
  22.     double area=length*width;
  23.       
  24.     System.out.println("Area of the Rectangle="+area);
  25.  
  26. }
  27. }

Output:

  1. Please enter length of a rectangle
  2. 4
  3. Please enter width of a rectangle
  4. 8
  5. Area of the circle =32.0




2.Write a Basic java example program to find Area of rectangle without user interaction
 

  1. package com.BasicJavaProgramsExamples;
  2. public Class AreaOfRectangle{ 
  3.  
  4. public static void main(String args[]) {
  5.  
  6.  
  7.   double length=4;
  8.      
  9.   double width=8;
  10.  
  11. /*
  12.  *Area of a rectangle is
  13.  *Area= length*width;
  14.  *
  15.  */
  16.          
  17.     double area=length*width;
  18.       
  19.     System.out.println("Area of the Rectangle="+area);
  20.  
  21. }
  22. }

Output:

  1. Area of the circle =32.0
1.Write a Basic java example  program to find perimeter of Rectangle

  •  To calculate the perimeter of rectangle we need to ask user to enter length and width of the rectangle so that we can calculate perimeter of rectangle using formula perimeter of rectangle=2*length*width



  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class PerimeterOfRectangle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. Scanner in= new Scanner(System.in);
  8. System.out.println("Please enter length of a rectangle");
  9.  
  10.     double length=in.nextDouble();
  11.      
  12. System.out.println("Please enter width of a rectangle");
  13.  
  14.     double width=in.nextDouble();
  15.  
  16. /*
  17.  *perimeter of a rectangle is
  18.  *perimeter= length*width;
  19.  *
  20.  */
  21.          
  22.     double perimeter=length*width;
  23.       
  24.     System.out.println("Perimeter of the Rectangle="+perimeter);
  25.  
  26. }
  27. }

Output:

  1. Please enter length of a rectangle
  2. 4
  3. Please enter width of a rectangle
  4. 8
  5. Perimeter of the circle =64.0

Basic Java program to find area and perimeter of circle

1.Write a Basic java example  program to find area of circle

  •  To calculate the area of circle we need to ask user to enter radius of the circle so that we can calculate area of circle using formula area of circle =PI* radius * radius



  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class AreaOfCirle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. int radius = 0;
  8. Scanner in= new Scanner(System.in);
  9. System.out.println("Please enter radius of a circle");
  10.  
  11.     radius=in.nextInt();
  12.      
  13. /*
  14.  * where r is a radius of a circle then Area of a circle is
  15.  *Area= pi * r * r
  16.  *
  17.  */
  18.          
  19.     double area=Math.PI* radius * radius;
  20.       
  21.     System.out.println("Area of the circle ="+area);
  22.  
  23. }
  24. }

Output:

  1. Please enter radius of a circle
  2. 23
  3. Area of the circle =1661.9025137490005




2.Write a Basic java example program to find perimeter of circle

  •  To calculate the perimeter  of circle we need to ask user to enter radius of the circle so that we can calculate perimeter  of circle using formula area of circle =2* PI* radius
  
  1. package com.BasicJavaProgramsExamples;
  2. import java.util.Scanner;
  3. public Class PerimeterOfCirle{ 
  4.  
  5. public static void main(String args[]) {
  6.  
  7. int radius = 0;
  8. Scanner in= new Scanner(System.in);
  9. System.out.println("Please enter radius of a circle");
  10.  
  11.     radius=in.nextInt();
  12.      
  13. /*
  14.  * where r is a radius of a circle then perimeter of a circle is
  15.  *Area= pi * r * r
  16.  *
  17.  */
  18.          
  19.     double perimeter =2* Math.PI* radius;
  20.  
  21.     System.out.println("Perimeter of the circle ="+perimeter );
  22.  
  23. }
  24. }

Output:

  1. Please enter radius of a circle
  2. 12
  3. Perimeter of the circle =75.39822368615503

Basic java programs

  1. Print prime numbers? 
  2. Interfaces allows constructors? 
  3. Can we create static constructor in java 
  4. Super keyword interview questions java 
  5. Java interview questions on final keyword
  6. Can we create private constructor in java
  7. Java Program Find Second highest number in an integer array 
  8. Java interview programming questions on interfaces 
  9. Top 15 abstract class interview questions  
  10. Java interview Questions on main() method
  11. Java Interview Program to find smallest and second smallest number in an array 
  12. Java Coding Interview programming Questions : Java Test on HashMap  
  13. Explain java data types with example programs 
  14. Constructor chaining in java with example programs 
  15. Swap two numbers without using third variable in java 
  16. Find sum of digits in java 
  17. How to create immutable class in java 
  18. AtomicInteger in java 
  19. Check Even or Odd without using modulus and division  
  20. String Reverse Without using String API 
  21. Find Biggest substring in between specified character
  22. Check string is palindrome or not?
  23. Reverse a number in java?


  24. Fibonacci series with Recursive?
  25. Fibonacci series without using Recursive?
  26. Sort the String using string API?
  27. Sort the String without using String API?
  28. what is the difference between method overloading and method overriding?
  29. How to find largest element in an array with index and value ?
  30. Sort integer array using bubble sort in java?
  31. Object Cloning in java example?
  32. Method Overriding in java?
  33. Program for create Singleton class?
  34. Print numbers in pyramid shape?
  35. Check armstrong number or not?
  36. Producer Consumer Problem?
  37. Remove duplicate elements from an array
  38. Convert Byte Array to String
  39. Print 1 to 10 without using loops
  40. Add 2 Matrices
  41. Multiply 2 Matrices
  42. How to Add elements to hash map and Display
  43. Sort ArrayList in descending order
  44. Sort Object Using Comparator
  45. Count Number of Occurrences of character in a String
  46. Can we Overload static methods in java
  47. Can we Override static methods in java 
  48. Can we call super class static methods from sub class 
  49. Explain return type in java 
  50. Can we call Sub class methods using super class object? 
  51. Can we Override private methods ? 
  52. Basic Programming Questions to Practice : Test your Skill

Top 10 Java interview questions on final keyword

1. What is the use of final keyword in java?   


  • By using final keyword we can make
  • Final class
  • Final method
  • Final variables
  • If we declare any class as final we can not extend that class
  • If we declare any method as final it can not be overridden in sub class
  • If we declare any variable as final its value unchangeable once assigned.

2. What is the main difference between abstract method and final method?

  • Abstract methods must be overridden in sub class where as final methods can not be overridden in sub class
3. What is the actual use of final class in java?

  • If a class needs some security and it should not participate in inheritance in this scenario we need to use final class.
  • We can not extend final class.
4. What will happen if we try to extend final class in java?
  • Compile time error will come.
  1. package com.finalkeywordintweviewprograms;
  2.  public final  Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }


  1. package com.finalkeywordintweviewprograms;
  2.  public Class Sample  extends SuperDemo{  //The type Sample cannot subclass the final class
  3. SuperDemo

  4. }

5.Can we declare interface as final?

  • No We can not declare interface as final because interface should be implemented by some class so its not possible to declare interface as final.
final keyword interface in java




6. Is it possible to declare final variables without initialization?

  • No. Its not possible to declare a final variable without initial value assigned.
  • While declaring itself we need to initialize some value and that value can not be change at any time.

final variable in java
  1. package com.finalkeywordintweviewprograms;
  2. public final  Class Sample{ 
  3.  
  4. final int x=12,y=13;
  5.  
  6. public void Method() {

  7. x=25;// compile time error:The final field Super.x cannot be assigned
  8. y=33;// compile time error: The final field Super.y cannot be assigned
  9.  
  10. }
  11.  
  12. }

7. Can we declare constructor as final?
  • No . Constructors can not be final.

8.What will happen if we try to override final methods in sub classes?

  • Compile time error will come :Cannot override the final method from Super class
9.Can we create object for final class?
  • Yes we can create object for final class. 
10.What is the most common predefined final class object you used in your code? 
  • String (for example)
Select Menu