Home / Computer Science / Interview Stuff / Top 20 Java Programming Interview Questions and Logical Programs
"Discover frequently asked Java programming interview questions and logical programs, such as reversing a number. Explore essential Java features, the Singleton design pattern, and the Java Virtual Machine (JVM).

Top 20 Java Programming Interview Questions and Logical Programs

Java programming interview questions

Java programming interview questions often test your knowledge of core programming concepts. One common task is reversing a number, which can be accomplished using different approaches. Let’s dive into some frequently asked Java interview questions and explore program examples for reversing numbers.

  1. Java programming interview questions include about key language features, such as immutability in strings and the usage of StringBuffer and StringBuilder. It’s essential to understand the differences between these concepts to excel in Java interviews.
  2. One of the essential design patterns in Java is the Singleton pattern, which ensures that a class has only one instance. Understanding how to implement this pattern can showcase your proficiency in Java development.
  3. The Java Virtual Machine (JVM) is the heart of Java’s platform independence. Being familiar with its role and how it executes Java bytecode can help you tackle questions about Java’s cross-platform compatibility.
  4. Logical programming challenges, like reversing a number, are also common in Java interviews. The provided code examples demonstrate different approaches to solving such problems, offering valuable insights into Java’s syntax and control structures.

1. Reversing Numbers with a While Loop: You can reverse a number using a while loop by repeatedly extracting the last digit and appending it to the reverse. This method is efficient and straightforward.

2. Reversing Numbers with a For Loop: A for loop can also be used to reverse a number by iterating through its digits and constructing the reverse number in the process. It offers a concise way to achieve the desired result.

3. Reversing Numbers with Recursion: Recursion is a powerful technique to reverse numbers in Java. By breaking down the problem into smaller parts and leveraging the recursive call stack, you can efficiently reverse numbers of any length.

These Java programming interview questions not only test your coding skills but also your problem-solving ability. Whether you’re a beginner or an experienced Java developer, mastering these techniques is essential for acing Java interviews and building robust Java programs.

Java programming interview questions

Top 20 Java programming interview questions

1) String is mutable or immutable in java?

Ans: In java all string are immutable. ( a mutable string can be changed but immutable string cannot be changed).

2) StringBuffer is thread safe or not?

Ans: A StringBuffer is thread safe because it is synchronized. StringBuffer object is mutable.

3) What is StringTokenizer?

Ans: StringTokenizer is a class in java.util package this allows the string into tokens (pieces).

4) What is StringBuffer?

Ans: The StringBuffer is a class provided by java which is used to represent characters that can be modified.

5) What is StringBuilder?

Ans: StringBuilder is class in java introduced in java5. It allows you to expand the number of characters that it encapsulates it you can specify a value for the maximum number of characters that it can hold. if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.

6) What is String in java?

Ans: String is a class in Java and available in java.lang package.String is immutable and final in Java and JVM uses String Constant Pool to store all the String objects.

7) What is the operator is used to compare two strings?

Ans: equals() or equalsIgnoreCase() method should be used to compare two string objects in Java.

8) How to create String objects in java?

Ans: Creating string objects in java is of two types.

1.String s1 = new String(“java”);

2. String s1=”java”;

9). What does intern() method do in Java?

Ans: String object created by new() operator is by default not added in String pool as opposed to String literal the intern() method allows putting string object into pool.

10) What is the difference when String is gets created using literal or new() operator ?

Ans: When we create string with new() its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.

11) Can we write multiple main() methods in java program?

Ans: Yes, we can write by override the main() method but there should be only one main() method with the standard signature like public static void main(String args[ ]).

12) What is transient in Java?

Ans: A transient is a key word which is used to a variable that may not be serialize.

13) Explain about main() method in java?

Ans: Public: This is the first method called by java environment when the program starts its execution. So the access specifier is public.

Static: Java environment should call this method without creating any instance, hence static is used.

Void: main() method does not return any thing so return type should be void.

14) Can we make abstract class as final?

Ans: Abstract class may not be declared as final.

15)Which package is import always by default in java program?

Ans: By default java.lang package is always imported.

16) What are the four main pillars of Object Oriented Programming?

Ans: Inheritance, Abstraction,Encapsulation and Polymorphism.

17) What is JVM?

Ans: In expansion of JVM is Java Virtual Machine. JVM is an abstract computing machine that enables a computer to run a Java program. There are three notions of the JVM specification, implementation and instance. The specification is a document that formally describes what is required of a JVM implementation.

18) What is a Singleton property in Java?

Ans: Singleton is good property, java has this property. Singleton is to control the object creation by creating constructor in private. This singleton is useful to keep only one instance per class at any time.

19) What is WORA?

Ans: Write Once Run Anywhere (WORA) This is a slogan for java. Means java can be developed on any device and compiled into a standard byte code and expected to run any where or any device / platform equipped with JVM.

20) List Java features?

Ans: Object Oriented, Robust,Secure,Simple,Platform Independent, Architectural neutral, Portable and Dynamic.

Java programming interview questions – Logical Programs Useful at Interview

1. Write program to print the reverse of a number?

Finding of the reverse can be done in different ways, see the below.

Reverse of a number by using While loop:

import java.util.Scanner;

public class ReverseofNumberByWhile {
public int ReverseByWhile() {

int number = 0;
int reverse = 0;
System.out.println(“Enter a number to find its reverse: “);
Scanner in = new Scanner(System.in);
// To get the runtime input
number = in.nextInt();
while (number != 0) {
reverse = reverse * 10;
reverse = reverse + number % 10;
number = number / 10;
}

System.out.println(“Reverse of input “+number+” is “+reverse);
return reverse;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ReverseofNumberByWhile rev = new ReverseofNumberByWhile();
rev.ReverseByWhile();
}
}

Output is :

Enter a number to find its reverse:
123456
Reverse of input 123456 is 654321

Reverse of Number By using For Loop:

import java.util.Scanner;

public class ReverseofNumber {
int number;
int reverse = 0;
Scanner sr = new Scanner(System.in);

public int reverseByforloop() {
System.out.println(“Enter an intger value”);
number = sr.nextInt();

for (; number != 0; number /= 10) { // Initialization not required
int reminder = number % 10;
reverse = reverse * 10 + reminder;
}
System.out.println(“Reverse of by using for loop ” + number + ” “+ reverse);
return reverse;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ReverseofNumber rn = new ReverseofNumber();
rn.reverseByforloop();
}

}

Output:

Enter an integer value : 5678
Reverse of by using for loop : 8765

Reverse of Number By using For Recursion:

import java.util.Scanner;

public class ReverseByRecursion {
public static void reverseOfNumber(int input_number) {
if (input_number < 10) {
System.out.println(input_number);
return;
}
else {
System.out.print(input_number % 10);
//Method is calling itself: recursion
reverseOfNumber(input_number/10);
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number=0;
System.out.println(“Enter a number to find its reverse: “);
Scanner sr = new Scanner(System.in);
number = sr.nextInt();
System.out.print(“Reverse of input”+ number+” is:”);
reverseOfNumber(number);
System.out.println();

}

}

Output is:

Enter a number to find its reverse: 4567
Reverse of input4567 is:7654

Official documentation on Java Programming from Oracle.

Java and Other articles on Java related blogs are available under Java Programming in this website.

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.

Check Also

Dax Interview Questions

DAX Interview Questions Part – 2

These are mostly asked DAX Interview Questions in Power BI developer or Data analyst interview’s. …