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

Encapsulation refers to the winding of data into a single unit that is known as a class, while Data hiding is the process of hiding data from unauthentic and unauthorized access.

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.

A JAVA program to generate Fibonacci series is as follows:

import java.util.Scanner;
public class Fibonacci 
{
    public static void main(String[] args) 
    {
        int n, a = 0, b = 0, c = 1;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        n = s.nextInt();
        System.out.print("Fibonacci Series: ");
        for(int i = 1; i <= n; i++)
        {
            a = b;
            b = c;
            c = a + b;
            System.out.print(a+" ");
        }
    }
}
Compile: $ javac Fibonacci.java
Run: $ java Fibonacci
Output:
Enter the value of n: 5
Fibonacci Series: 0 1 1 2 3

Garbage collection is the process of automatically managing memory in java. It is helpful to finds the unused objects that are no longer used by the program and deletes or remove them to free up the memory. The internal structure of the garbage collection mechanism uses several GC algorithms. It works with the most popular algorithm such as Mark and Sweep. Garbage collection is controlled by a thread known as Garbage Collector.

In Java, System.gc() and Runtime.gc() are two methods that sends requests to the JVM for garbage collection. It totally depends on the JVM. If the Heap Memory is full, the JVM will not allow creating a new object and shows an error java.lang.OutOfMemoryError. When the garbage collector removes the object from the memory, first, the garbage collector thread calls the finalize() method of that object and then remove it.

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.

C program to Check String is Palindrome without using a loop is as follows:

/*
 * C program to find the length of a string without using the
 * built-in function also check whether it is a palindrome
 */
#include <stdio.h>
#include <conio.h>
 
void main()
{
    char string[25], reverse_string[25] = {'\0'};
    int i, length = 0, flag = 0;
 
    printf("Enter a string: ");
    gets(string);
    /*  keep going through each character of the string till its end */
    for (i = 0; string[i] != '\0'; i++)
    {
        length++;
    }
    printf("The length of the string '%s' = %d\n", string, length);
    for (i = length - 1; i >= 0 ; i--)
    {
        reverse_string[length - i - 1] = string[i];
    }
   /*  Check if the string is a Palindrome */
 
    for (flag = 1, i = 0; i < length ; i++)
    {
        if (reverse_string[i] != string[i])
            flag = 0;
    }
    if (flag == 1)
       printf ("%s is a palindrome \n", string);
    else
       printf("%s is not a palindrome \n", string);
}

Output:
Enter a string: how  are you
The length of the string 'how  are you' = 12
how  are you is not a palindrome
 
Enter a string: madam
The length of the string 'madam' = 5
madam is a palindrome
 
Enter a string: mam
The length of the string 'mam' = 3
mam is a palindrome

The static keyword indicates that the particular member belongs to a type itself, rather than to an instance of that type in the Java programming language.

The differences between JAVA and C are as follows:

S. N.CJava
1C is a Procedural Programming Language.Java is an Object-Oriented language.
2C was developed by Dennis M. Ritchie in 1972.Java language was developed by James Gosling in 1995.
3It is a middle-level language as it is binding the gaps between machine level and high-level languages.It is a high-level language because the translation of code is taken place into machine language, which uses a compiler or interpreter.
4The C declaration variable is declared at the beginning of the block.In Java, you can declare a variable anywhere.
5Free is a variable used for freeing the memory in C.A compiler will free up the memory by calling the garbage collector.
6C generally breaks down to functions.Java breaks down to Objects.
7C support pointers.Java does not support pointers.
8Memory allocation can be done by malloc.Memory allocation can be done by a new keyword.
9The garbage collector needs to manage manually.In Java, it is automatically managed by a garbage collector
10C does not have a feature of overloading functionality.Java supports method overloading.
11C support for call by value and call by reference.Java only supports a call by value.
12C is a platform-dependent language.Java is a platform-independent language.
13C does not support Multithreading.Java supports the concept of Multithreading.
14It is not portable.It is portable.
15Exception handling cannot be directly achieved in C.Exception Handling is supported in Java.

Exception chaining is also known as exception wrapping. It is an object-oriented programming technique for handling exceptions by re-throwing a caught exception after wrapping it inside a new exception. The original exception is saved as property such as the cause of the new exception. In an application, Chained Exception helps to identify a situation in which one exception causes another Exception. For instance, Consider the I/O error: A method that throws an ArithmeticException because of an attempt to divide by zero but that caused the divisor to be zero.

OutOfMemoryError is an exception that is a runtime error thrown in the JAVA programming application when there is insufficient space to allocate an object in the Java heap.

The differences between the final and finally are as follows:

S. N.finalfinally
1A final method can't be overridden.It ensures that the code inside that block is executed no matter what happens in try and catch blocks. 
2The final variable value can't be changed.Only “System.exit()” can stop finally from being executed.
3The final class can't be inherited,finally is an optional block that is used together with try-catch blocks.
4The final keyword is used to apply restrictions on class, method, and variable. Finally is used to place important code, it will be executed whether an exception is handled or not.

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.