Cyient Java Interview Questions

Cyient Java developer Interview Questions
Download Cyient Java developer Interview Questions PDF

Below are the list of Best Cyient Java developer Interview Questions and Answers

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.

Differences between overloading and overriding

OverloadingOverriding
Overloading is performed at compilation time.Overriding is performed at runtime
Overloading is also called static binding.Overriding is also called dynamic binding.
Overloading is done within the class.Overriding occurs in between two classes that have an IS-A (inheritance) relationship.
It is an example of compile-time polymorphism.It is an example of run time polymorphism.

Overloading Example:

class ExampleClass{  
static int sum(int a,int b){return a+b;}  
static int sum(int a,int b,int c){return a+b+c;}  
}  

Overriding Example:

class Animal{  
	void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
	  void eat(){System.out.println("eating bread...");}  
}   

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

There are basically four types of storage classes in Java:

  • Automatic storage class: When a variable that is used in the coding is defined within a function and that also with the auto specifier then it simply belongs to this storage class.
  • Register storage class: Those variables in the coding which are declared by the register specifier then it belongs to this storage class.
  • Static storage class: It has a function to declare the variable with the help of the static specifier and that is how it belongs to the static storage class.
  • External storage class: The main objective with this is that the variable which is being declared consists of external linkage.

Difference between throw and throws are:

ThrowThrows
It is basically used inside the function. It is used when it needs to insert the exception.It is basically present in the function signature. Here if the functions have the same statements then it can result in certain exceptions.
It is used to basically use the exception. It can throw only a single exception at ones.Here multiple exceptions can be thrown and then whichever matches are thrown automatically.
The exception which is been thrown cannot be checked here.Here for the propagation purpose, the throws can use the throw keyword. Here it can use the specific exception class.

Exception in Java is basically an event that takes place all of a sudden. This is really unexpected and also unwanted. Mostly it happens when the program is ready to execute and during the runtime, it can break the normal and easy flow of the program. These exceptions can be also called errors. At a time when the error occurs the method which is present there sets an object which is then sent to the runtime process.

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.

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

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.