Cyient Java Interview Questions

  1. How to write custom exception in Java?
  2. What is the difference between a checked and an unchecked exception?
  3. What are meta-annotations?
  4. Write a program to check for a prime number in Java?
  5. How can you catch multiple exceptions in java?
  6. What is Exception in Java?
  7. What is meant by Data Hiding/Encapsulation?
  8. What is difference between final and finally in Java?
  9. What is OutOfMemoryError in Java?
Cyient Java developer Interview Questions

1. How to write custom exception in Java?

A custom exception is also known as a user-defined exception are derived classes of Java Exception classes. In order to create your Custom exception in Java following point must be taken care of.

  • All exceptions must be a child of Throwable.
  • If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.
  • If you want to write a runtime exception, you need to extend the RuntimeException class.

Custom exception example in Java

class InvalidAgeException extends Exception{  
 InvalidAgeException(String s){  
  super(s);  
 }  
}  

class CustomExceptionTest{  
  
   static void validate(int age)throws InvalidAgeException{  
     if(age<18)  
      throw new InvalidAgeException("not valid");  
     else  
      System.out.println("welcome to vote");  
   }  
     
   public static void main(String args[]){  
      try{  
      validate(13);  
      }catch(Exception m){System.out.println("Exception occured: "+m);}  
  
      System.out.println("rest of the code...");  
  }  
} 

Above is an example of a custom exception in Java that checks the age of voter. If age is less than 18 years than it throws an InvalidAgeException.

2. What is the difference between a checked and an unchecked exception?

Whenever an exception is generated in Java it is either type of a checked or unchecked exception. Below are the few differences between checked and unchecked exceptions in Java.

Checked ExceptionUnchecked Exception
Checked exceptions are checked at compile-time.Unchecked exceptions are not checked at compile time
Interrupted Exception, file not found, Class Not Found are few Checked exceptions.Empty Stack Exception, Arithmetic Exception, Null Pointer Exception, Array Index Out of Bounds Exception are few Unchecked exceptions

Checked Vs Unchecked Exceptions in Java

3. What are meta-annotations?

Meta annotations are annotations that are used to annotate other annotation types.In Java java.lang.annotation package contain four annotation types they are @Documented, @Inherited, @Repeatable, @Target.

4. Write a program to check for a prime number in Java?

A number that is divisible by 1 or itself is a Prime Number. 2, 3, 5, 7, 11, 13, 17 are examples of some Prime numbers.

Please Note: 0 and 1 are not a prime number.

Below is a simple Prime number Program in Java to check a number is Prime or Not.

import java.util.Scanner;
public class PrimeExample {
 public static void main(String args[]) {
  int i, m = 0, flag = 0;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter any number: ");
  // This method reads the number provided using keyboard
  int num = scan.nextInt();
  // Closing Scanner after the use
  scan.close();
  m = num / 2;
  if (num == 0 || num == 1) {
   System.out.println(num + " is not prime number");
  } else {
   for (i = 2; i <= m; i++) {
    if (num % i == 0) {
     System.out.println(num + " is not prime number");
     flag = 1;
     break;
    }
   }
   if (flag == 0) {
    System.out.println(num + " is prime number");
   }
  } //end of else
 }
}

5. How can you catch multiple exceptions in java?

6. What is Exception in Java?

7. What is meant by Data Hiding/Encapsulation?

8. What is difference between final and finally in Java?

9. What is OutOfMemoryError in Java?

10. Explain exception chaining in Java?

11. Why is string buffer better than string ?

12. What is difference between java and c?

13. What is used of static keyword in Java?

14. Write a program to check String is Palindrome without using loop?

15. What is the difference between an interface and an abstract class?

   

16. What is difference overloading and overriding?

        

17. What is difference between throw and throws ?

18. How does the garbage collector works in Java?

19. Differentiate call by value and call by reference ?

Call by value: 
A process in which the values of the actual parameters sent by the calling function are copied to the formal parameters of the called function.

Call by reference: 
A process in which the parameters of a calling function are passed to the parameters of the called function using an address.

20. Write a java program to generate fibonacci series ?

21. List types of storage classes in java?