Most Commonly Asked 10 Java Interview Questions and Answers

0
174
Java Interview Questions

Java is a high-level programming language that runs on various platforms such as UNIX, Windows, Mac OS, and Linux. A career as a Java Developer can take you a long way and help earn handsome pay. But to ensure that you are able to get your dream job in your dream company as a java professional, it is important that you prepare well for your interview.

Here, I have mentioned the top java interview questions for beginners that interviewers mostly ask.

10 Top Java Interview Questions

  1. How to execute a java program?

Open the command prompt window and then head to the directory where the java program is saved.

(HelloWorld. java). …

Type ‘javac HelloWorld. java’ and press enter to compile your code.

Now, type ‘ HelloWorld ‘ to run your program.

You will be able to see the result printed on the window.

  1. What is the code 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();

}”

  1. What is a thread in java?

The thread allows a program to operate more efficiently by doing multiple things at the same time. They are used to perform complicated tasks in the background without interrupting the main program.

A thread can be created by extending the Thread class and overriding its run() method.

The syntax is:

public class MyClass extends Thread {

public void run() {

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

}

}

  1. How to initialize an array in java?

The syntax to initialize an array in java is:

“int[] arr = new int[5];   // integer array of size 5 you can also change data type

String[] cars = {“”Volvo””, “”BMW””, “”Ford””, “”Mazda””};”

  1. How would you take input from the user in java?

The syntax to take input from a user in java is:

“import java.util.Scanner;

Scanner console = new Scanner(System.in);

int num = console.nextInt();

console.nextLine() // to take in the enter after the nextInt()

String str = console.nextLine();”

  1. What is a class in java?

A class is the basic building block of an object-oriented language. It is a template that explains the data and behaviour associated with instances of a particular class. When a class is instantiated, you create an object that has the look and feel of the other instances in the same class. The data that is associated with a class or object is stored in variables. And, the behaviour that is associated with a class or object is implemented with the methods.

  1. How would you enable Java in Chrome?

The steps to enable Java in Chrome are:

  • Click the security tab in the Java Control Panel
  • Select Enable Java content in the browser
  • Click Apply and then click OK to confirm the changes done
  • Next, restart your browser to enable the changes that you have done
  1. What is a ‘String’ in Java?

A string is a sequence of characters. An example of a string is “Hello”. The length of this string is 5 characters. When it comes to Java, a string is an immutable object that means it is constant and therefore cannot be changed after it is created.

  1. What is an array in Java?

An array is a container object that holds a fixed number of values that are of a single type. When the array is created, its length is established and fixed at the same time. The length of an array cannot be altered after its creation.

In an illustration of an array as 10 boxes which are numbered through 0 to 9, index 0 indicates the first element of the array.

Items in an array are called elements and these elements are accessed by their numerical index.

  1. How would you round off a number in java?

The code to round off a number in java is:

“import java.lang.Math; // Needed to use Math.round()

class Program {

public static void main( String args[] ) {

double num1 = 74.65;

System.out.println(Math.round(num1));

float num2 = 1337.345523f;

System.out.println(Math.round(num2));

}

}”

Conclusion:

This brings us to the end of the blog about SQL Interview Questions for Beginners. I hope you found this helpful and are now better equipped with the foundational knowledge about SQL. Happy Learning!