HomeJava

Java – Interview Questions and Answers – Part 1

Java – Interview Questions and Answers – Part 1

1) Is String a data type?

Answer: The below is the answer for this question:

String is a predefined Class in Java, and hence it is not a data type.

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


2) What is the difference between Print and Println?

Answer: The below is the answer for this question:

print statement displays the given text in the output console and stays in the same line after printing.

Whereas println statement displays the given text in the output console, but moves to the next line after printing.

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


3) Write a simple Java Program?

Answer: The below is the answer for this question:

public class Demo {

        public static void main(String[] args) {
              
          System. out.println("Hello World" );

       }

}

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


4) Write a Java Program to print an Integer value?

Answer: The below is the answer for this question:

public class Demo{
    
    public static void main(String[] args) {
    
        System. out.println(5);
        			
    }
    
    
}

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


5) Write a Java Program to print the command line arguments?

Answer: The below is the answer for this question:

public class Demo{
    
    public static void main(String[] args) {
    
        if(args.length > 0) {
            
              System.out.println("Printing the command line arguments below:");
                             
              for(int i=0;i < args.length ;i++) {
                                   
            	  System.out.println(args[i]);
                                          
              }
                             
            } else {
                             
               System. out.println("There are no command line arguments provided");
                             
            }

        			
    }
    
    
}

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


6) Write a Java program to print the input from Scanner?

Answer: The below is the answer for this question:

import java.util.Scanner;

public class Demo{
       
        public static void main(String[] args) {
              
              Scanner s = new Scanner(System.in);
              
              System. out.println("Enter name:" );
              String name = s.next();
              System. out.println("Enter age:" );
               int age = s .nextInt();
              System. out.println("Enter gender M/F:" );
               char gender = s .next().charAt(0);
              System. out.println("Enter whether Apple is red color true/false:" );
               boolean answer = s .nextBoolean();
              
              System. out.println("Your name is " +name );
              System. out.println("Your age is " +age );
              System. out.println("Your gender is " +gender );
              System. out.println("Your answer is " +answer );   
              
               s.close();                       
       }  
}

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


7) Write a Java Program to convert from Fahrenheit to Celsius?

Answer: The below is the answer for this question:

public class Demo{
       
        public static void main(String[] args) {
              
               double fahrenheit = 98;
              
               double celsius = ((fahrenheit - 32)*5)/9;
              
              System. out.println(celsius );
                            
       }
       
       
}

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


8) Write a Java program to swap two numbers?

Answer: The below is the answer for this question:

public class Demo{
       
        public static void main(String[] args) {
              
           int a = 2, b = 3;
          
           int c ;
          
           c = a;
           a = b;
           b = c;
          
           System. out.println(a );
           System. out.println(b );
                         
       }
}

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


9) Write a Java program to swap two numbers without using third variable?

Answer: The below is the answer for this question:

public class Demo{
       
        public static void main(String[] args) {
              
           int a = 2, b = 3;
          
           a = a + b;
           b = a - b;
           a = a - b;
          
           System. out.println(a );
           System. out.println(b );
                                     
       }
       
}

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


10) Write a Java Program to find the largest number?

Answer: The below is the answer for this question:

public class Demo{
       
        public static void main(String[] args) {
              
               int[] a = {5,1,9,3,6,8};
              
               int max = 0;
              
               for(int i =0;i <a .length ;i ++) {
                     
                      if(max < a [i ]) {
                           
                            max = a[ i];
                           
                     }
              }
              
              System. out.println(max );                    
       } 
}

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 *