HomeJava

Java – Interview Questions and Answers – Part 2

Java – Interview Questions and Answers – Part 2

1) What is the output of System.out.println(10+20+”Hello”+”World”);?

Answer: The below is the answer for this question:

public class Demo{
       
        public static void main(String[] args) {
       
              System.out.println(10+20+"Hello"+"World");
                            
       }

}

Output: 30HelloWorld

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


2) What is the output of System.out.println(“Hello”+”World”+10+20);?

Answer: The below is the answer for this question:

public class Demo{
       
        public static void main(String[] args) {
       
              System.out.println("Hello"+"World"+10+20);        
       }
}

Output: HelloWorld1020

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


3) What is the output of System.out.println(“Hello”+”World”+(10+20));?

Answer: The below is the answer for this question:

public class Demo{
       
        public static void main(String[] args) {
       
              System.out.println("Hello"+"World"+(10+20));
                            
       }

}

Output: HelloWorld30

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


4) Is Java case sensitive?

Answer: The below is the answer for this question:

Yes, Java is case sensitive.

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


5) What is the Java program to find the greatest of given three numbers?

Answer: The below is the answer for this question:

public class Demo{
       
    public static void main(String[] args) {
       
       int a = 255, b = 99, c = 957;
       
       if(a>b & a>c) {
              
              System. out.println("a is the greatest");
              
       } else if (b >c) {
              
              System.out.println("b is the greatest");
              
       } else {
              
              System.out.println("c is the greatest");
              
       }
       
    }

}

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


6) What is the difference between = and == operators?

Answer: The below is the answer for this question:

= is an assignment operator used for storing the supported value/literal into the required variable.

Where as == is equal to operator used for comparing the values or variables.

The below is a sample program to demonstrate the difference:

public class Demo{
       
    public static void main(String[] args) {
       
       int a = 5;
       
       if(a==5) {
              
              System.out.println("a is equal to 5");
              
       }
       
    }

}

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


7) What is the difference between i++ and ++i?

Answer: The below is the answer for this question:

++ operator when used after the variable, is known as post increment operator.

++ operator when used before the variable, is known as pre increment operator.

The below are the programs for demonstrating ++i and i++

Program demonstrating i++

public class Demo{
       
    public static void main(String[] args) {
       
       int i = 5,j;
                     
        j = i++;
        
        System.out.println(i);
        System.out.println(j);
       
    }

}

output:

6

5

Program demonstrating ++i

public class Demo{
       
    public static void main(String[] args) {
       
       int i = 5,j;
                     
        j = ++i;
        
        System. out.println(i );
        System. out.println(j );
       
    }

}

output:

6

6

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


8) Write a Java program to print the numbers from 1 to 10?

Answer: The below is the answer for this question:

public class Demo{
       
    public static void main(String[] args) {
       
       for(int i=1;i<=10;i++) {
              System. out.println(i );
       }
       
    }

}

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


9) What is the disadvantage of while loop?

Answer: The below is the answer for this question:

while loop results in infinite loop, when we forget to add the logic for coming out of the loop. And the possibility of forgeting adding the logic for exiting the loop is high.

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


10) Write a Java program to print the numbers from 10 to 1?

Answer: The below is the answer for this question:

public class Demo{
       
    public static void main(String[] args) {
       
        for(int i =10;i >=1;i --) {
              
              System. out.println(i );
              
        }
        
    }

}

For detailed answer with practical examples and explanation of this answer, you can check the below youtube video:


Please leave your questions/comments/feedback below.

Happy Learning 🙂

Arun Motoori ( www.QAFox.com )

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *