Java Coding Interview


Java Coding Interview

Below is the list of questions in the Java Coding Interview

Most frequently asked java coding questions in the Java Coding Interview.


1. What is an Infinite Loop? How can we declare an infinite loop?

An infinite loop runs without any condition and runs infinitely. An infinite loop can be broken by defining any breaking logic in the body of the statement blocks.

For example, An infinite loop is declared as

for (;;)
    // Statements to execute
    // Add any loop breaking logic


2. Explain the difference between continue and break statement?

This is one of the most common question in Java coding round.

Break and continue are two critical keywords used in Loops. When a break keyword is used in a loop, the loop is broken instantly while when continue keyword is used, the current iteration is broken and the loop continues with the next iteration.

 Let’s consider the example, Loop is broken when the counter reaches 5.

for (counter = 0; counter & lt; 10; counter++)
if (counter == 5) {
} }

When the counter reaches 5, the loop jumps to the next iteration, and any statements after the continue keyword are skipped for the current iteration.

for (counter = 0; counter < 10; counter++)
if (counter == 4) {
system.out.println("This will not get printed when counter is 4");


3. What is the default switch case?

The default case is executed when no other switch condition matches. The default case is an optional case. It can be declared only once all other switch cases have been coded.

In the below example, when the score is not 2 or 3, the default case is used.

public class switchExample {
    int score = 15;
    public static void main(String args[]) {
        switch (score) {
            case 1:
                system.out.println("Score is 2");
            case 2:
                system.out.println("Score is 3");
                system.out.println("Default Case");


4. Explain can we have two methods in a class with the same name?

It is the most common question of the Java coding round.

Yes, we can define two methods in a class with the same name but with different number/type of parameters. Which method is to get invoked will depend upon the parameters passed.


5. Can we restrict inheritance for a class so that no class can be inherited from it?

Yes, by using the final keyword. If we want a class not to be extended further by any class, we can use the keyword Final with the class name.


6. Differentiate, comparison done by equals method and == operator.

In Java, equals() method is used to compare the contents of two string objects and returns true if the two have the same value while == operator compares the references of two string objects.

if (str1.equals(str2))
        { // this condition is true
            System.out.println("str1 and str2 are equal in terms of values");
        if (str1 == str2) {
            //This condition is true
            System.out.println("Both strings are referencing same object");


7. Is it possible to call a constructor from another constructor’s body?

The Interviewer frequently asks this question in Java coding round.

Yes, if a class has multiple constructors, it is possible to call one constructor from the body of another one using this().


8. Write a program to return true if the array contains three increasing adjacent numbers.

For Example,

Input [18, 15, 4]

Output false

Input [16,17,18]

Output true

public class Example {
public static void main(String[] args)
int arr[]= {18, 15, 4};
boolean flag=false;
for(int i=0;i<arr.length-2;i++)
if(arr[i+1]-arr[i]==1 && arr[i+2]-arr[i+1]==1)


9. For a given array of integers, return true if the array contains two 7’s next to each other, or there are two 7’s separated by one element.

For Example,

 [1, 7, 7] → true
 [1, 7, 1, 7] → true
[1, 7, 1, 1, 7] → false
 [7, 7, 1, 1, 7] → true
 [9, 0, 5, 1, 7] → false
 [7, 7, 7, 7, 7] → true

public static void main(String[] args) {
int[] array = {1,7,1,7};
System.out.println(""Array cannot contain null values"");
}else {
System.out.println(""Is given array consist of consecutive 7's: "" + checkArray(array));

static boolean checkArray(int[] array) {
for(int i=0;i
if(array[i]==7 && array[i+1]==7)
return true;
return false;


10. Given an array of integers, return true if every element is a 1 or a 4.

For Example,

([1, 4, 1, 4]) → true
([1, 4, 2, 4]) → false
([1, 1]) → true

public class Example2 {

public static void main(String[] args) {
Integer a[] = {1,1,1,1,4,4,3};
System.out.println(“”Given array should not contain any null element””);
}else {
System.out.println( -> num==1 || num== 4));

Check more Java Interview Questions

Wikipedia Java

Leave a Comment

Your email address will not be published.