Skip to main content

java interview questions

 1. What is Java?

Java is a general-purpose programming language that is class-based, object-oriented and is very popular. It’s one of the most popular programming languages in the world.


Hello World in Java:

public class FileName {

  public static void main(String args[]) {

    System.out.println("Hello World!");

  }

2. How to install Java?

Install Java through command prompt so that it can generate necessary log files to troubleshoot the issue.

Go to java.com and click on the Free Java Download button.


Click on the Save button and save Java software on the Desktop


Verify that Java software is saved on the desktop.


Open Windows Command Prompt window.


Windows XP: Click Start -> Run -> Type: cmd


Windows Vista and Windows 7: Click Start -> Type: cmd in the Start Search field.

cd <Java download directory> (for example Downloads or Desktop etc.)


IRun the installer and follow onscreen instructions.


3. How to reverse a string in Java?

"String str = ""Hello"";

String reverse(String str){

  StringBuilder sb = new StringBuilder();

  sb.append(str);

  sb.reverse();

  return sb.toString();

}"

4. What is thread in Java?

Threads allow a program to operate more efficiently by doing multiple things at the same time.


Threads can be used to perform complicated tasks in the background without interrupting the main program.


It can be created by extending the Thread class and overriding its run() method:


Extend Syntax

public class MyClass extends Thread { 

  public void run() { 

    System.out.println("This code is running in a thread"); 

  } 

5. How to take input in Java?

"Scanner in = new Scanner(System.in);

      System.out.print(""Please enter hour 1: "");

      int hour1 = in.nextInt();

      System.out.print(""Please enter hour 2: "");

      int hour2 = in.nextInt();

      System.out.print(""Please enter minute 1: "");

      int min1 = in.nextInt();

      System.out.print(""Please enter minute 2: "");

      int min2 = in.nextInt();"


Comments