Java Interview Questions and Answers
Java interviews often include output-based programming questions to check how well a candidate understands core Java behavior. This quiz covers operators, strings, arrays, loops, inheritance, constructors, method overloading, method overriding, regular expressions, and static methods.
Below are 27 Java interview questions with multiple-choice options, code examples, correct answers, and concise explanations. These questions can be useful for freshers as well as candidates preparing for technical Java interviews.
Table of Contents

YT:- DecodeIT
1. What is the output of the following code?
int x = 5;
int y = 10;
System.out.println(x + y + "Hello");
- a) 15Hello
- b) Hello15
- c) 510Hello
- d) 5Hello10
Answer: a) 15Hello
Explanation: Java first evaluates x + y as numerical addition. The result is 15, which is then joined with the String Hello.
2. What is the output?
public class Main {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1 == str2);
}
}
- a) true
- b) false
- c) Compilation error
- d) Runtime exception
Answer: b) false
Explanation: The == operator compares object references. The String literal and the object created with new are different references.
3. What is the output?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr.length);
- a) 1
- b) 2
- c) 3
- d) 5
Answer: d) 5
Explanation: The length property of an array returns the total number of elements it contains.
4. What is the output?
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 10;
if (x < y)
System.out.println("x is less than y");
else if (x > y)
System.out.println("x is greater than y");
else
System.out.println("x is equal to y");
}
}
- a) x is less than y
- b) x is greater than y
- c) x is equal to y
- d) Syntax error
Answer: a) x is less than y
Explanation: The condition x < y is true because 5 is smaller than 10.
5. What is the output?
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 5;
int z = (x > y) ? x : y;
System.out.println(z);
}
}
Answer: a) 10
Explanation: The ternary operator selects x because the condition x > y evaluates to true.
6. What does the following loop print?
public class Main {
public static void main(String[] args) {
int x = 10;
while (x > 0) {
System.out.print(x + " ");
x--;
}
}
}
Answer: a) 10 9 8 7 6 5 4 3 2 1
Explanation: Each value is printed first and then decreased by one. The loop stops when x reaches zero.
7. What is the output?
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
if (i == 2)
break;
}
}
}
Answer: a) 0 1 2
Explanation: When i becomes 2, the break statement immediately terminates the loop.
8. What is the output?
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 2)
continue;
System.out.print(i + " ");
}
}
}
Answer: b) 0 1 3 4
Explanation: The continue statement skips the iteration where i equals 2.
9. What is the output?
public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.print(i + " ");
i++;
} while (i < 5);
}
}
Answer: a) 0 1 2 3 4
Explanation: A do-while loop executes its body before checking the condition. The values from 0 through 4 are therefore printed.
10. What is the output?
public class Main {
public static void main(String[] args) {
int x = 10;
int y = x++;
System.out.println(y);
}
}
Answer: a) 10
Explanation: With postfix increment, the existing value of x is assigned to y before x is increased.
11. What will be printed?
class Base {
public Base() {
System.out.println("Base");
}
}
class Derived extends Base {
public Derived() {
System.out.println("Derived");
}
}
class DeriDerived extends Derived {
public DeriDerived() {
System.out.println("DeriDerived");
}
}
public class Test {
public static void main(String[] args) {
Derived b = new DeriDerived();
}
}
Answer:
Base
Derived
DeriDerived
Explanation: When an object of the lowest subclass is created, constructors execute upward through the inheritance hierarchy before the subclass constructor runs.
12. Which overloaded method is selected?
public class Test {
public void print(Integer i) {
System.out.println("Integer");
}
public void print(int i) {
System.out.println("int");
}
public void print(long i) {
System.out.println("long");
}
public static void main(String[] args) {
Test test = new Test();
test.print(10);
}
}
Answer: d) int
Explanation: The literal 10 is an int value, so the overload accepting an int is selected directly.
13. What is the output?
public class StrEqual {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
String s3 = "hello";
if (s1 == s2) {
System.out.println("s1 and s2 equal");
} else {
System.out.println("s1 and s2 not equal");
}
if (s1 == s3) {
System.out.println("s1 and s3 equal");
} else {
System.out.println("s1 and s3 not equal");
}
}
}
Answer:
s1 and s2 not equal
s1 and s3 equal
Explanation: s2 refers to a separately created String object. The literals assigned to s1 and s3 can refer to the same pooled String object.
14. What is the output?
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
Answer: b) true
Explanation: The intern() method returns the corresponding pooled String reference. After assigning it to s2, both references point to the same pooled object.
15. What is the output?
public class Main {
public static void main(String[] args) {
String str = "Java";
str.concat(" Programming");
System.out.println(str);
}
}
Answer: a) Java
Explanation: String objects are immutable. Since the returned String from concat() is not stored, the original variable remains Java.
16. What does the regular expression match?
import java.util.regex.*;
public class RegexQuiz {
public static void main(String[] args) {
String regex = "\\d+";
String input = "1234";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.print(matcher.group() + " ");
}
}
}
Answer: b) 1234
Explanation: The pattern \d+ represents one or more digits, so the complete input value is matched.
17. What is the output?
import java.util.regex.*;
public class RegexQuiz {
public static void main(String[] args) {
String regex = "[a-c]";
String input = "abcABC";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.print(matcher.group() + " ");
}
}
}
Answer: a) a b c
Explanation: The range [a-c] matches lowercase characters from a through c. Regex matching is case-sensitive here.
18. What is the output?
class One {
public static void print() {
System.out.println("1");
}
}
class Two extends One {
public static void print() {
System.out.println("2");
}
}
public class Test {
public static void main(String[] args) {
One one = new Two();
one.print();
}
}
Answer: b) 1
Explanation: Static methods belong to the class and are resolved using the reference type. The reference is declared as One.
19. What is the output?
class One {
public void print() {
System.out.println("1");
}
}
class Two extends One {
public void print() {
System.out.println("2");
}
}
public class Test {
public static void main(String[] args) {
One one = new Two();
one.print();
}
}
Answer: a) 2
Explanation: This is an instance method, so Java uses runtime polymorphism. The actual object is of type Two, and its overridden method executes.
20. Which constructor runs?
class One {
public One(int x) {
System.out.print("int constructor");
}
public One(long l) {
System.out.print("long constructor");
}
}
public class Test {
public static void main(String[] args) {
long l = 20L;
One one = new One(l);
}
}
Answer: b) long constructor
Explanation: The argument is explicitly declared as a long, so the constructor accepting a long value is selected.
21. What happens in this code?
class Parent {
public void className() {
System.out.println("Parent");
}
}
class Child extends Parent {
void className() {
System.out.println("Child");
}
}
public class Test {
public static void main(String[] args) {
Parent parent = new Child();
parent.className();
}
}
Answer: c) Compile-time error
Explanation: The parent method is public, but the child method has package-private visibility. An overriding method cannot reduce the visibility of the inherited method.
22. What is the output?
class Demo {
public Demo(int i) {
System.out.println("int");
}
public void Demo(short s) {
System.out.println("short");
}
}
public class Test {
public static void main(String[] args) {
short s = 10;
Demo demo = new Demo(s);
}
}
Answer: a) int
Explanation: The Demo constructor accepts an int. The short argument can be promoted to int during constructor invocation.
23. What will be printed?
class Demo {
void Demo() {
System.out.println("Demo");
}
}
public class Test {
public static void main(String[] args) {
Demo demo = new Demo();
}
}
Answer: b) No Output
Explanation: A constructor cannot have a return type. Because void Demo() contains a return type, it is a method, not a constructor, and it is not automatically called during object creation.
24. What is the constructor order?
class One {
public One() {
System.out.print("One,");
}
}
class Two extends One {
public Two() {
System.out.print("Two,");
}
}
class Three extends Two {
public Three() {
System.out.print("Three");
}
}
public class Test {
public static void main(String[] args) {
Three three = new Three();
}
}
Answer: c) One,Two,Three
Explanation: Creating a Three object first invokes the superclass constructors. Therefore, One executes first, followed by Two and then Three.
25. What is the output?
class Hello {
public Hello() {
System.out.println("Hello");
}
}
public class Main {
Hello hello = new Hello();
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Answer: b) Hello World!
Explanation: Execution begins from the static main() method. The instance field is not initialized merely by entering main, so only Hello World! is printed.
26. What is the output?
public class Main {
static String name = "Ramesh";
public Main() {
name = "Prabhas";
}
public static void main(String[] args) {
System.out.println("The name is " + name);
}
}
Answer: b) The name is Ramesh
Explanation: The constructor changes the static variable only when an object is created. No object is created in main(), so the original value remains Ramesh.
27. What happens when a static method is called through null?
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
class First {
static void staticMethod() {
System.out.println("Static Method");
}
}
public class MainClass {
public static void main(String[] args) {
First first = null;
first.staticMethod();
}
}
Answer: a) Static Method
Explanation: A static method is associated with the class rather than an object instance. Therefore, this call does not require a valid object reference and prints Static Method.
Frequently Asked Questions
Are these Java interview questions suitable for freshers?
Yes. The quiz focuses on core Java behavior and covers many fundamental concepts commonly used in programming interviews.
Which Java concepts are covered in Quiz 3?
The questions cover operators, strings, arrays, loops, break and continue, constructors, inheritance, polymorphism, method overloading, method overriding, regular expressions, static methods, and String pooling.
Why are Java output questions important?
Output-based questions require you to understand what the Java compiler and runtime will actually do instead of relying only on definitions.
What is the difference between static and instance methods?
Static methods belong to the class, while instance methods operate through objects and can participate in runtime method overriding.
Why are constructor questions common in Java interviews?
Constructor problems test understanding of object creation, inheritance, constructor chaining, overloaded constructors, and the difference between methods and constructors.
Keywords: Java interview questions and answers PDF, Java interview questions, Java Quiz 3, Core Java interview questions, Java output questions, Java coding interview questions, Java interview questions for freshers, Java Interview Questions and Answers,Java programming questions, Java interview preparation,Java Interview Questions Java Interview Questions and Answers Java Interview Questions Quiz 3Java Coding Interview Questions Java MCQ Questions Java MCQ Quiz Java Programming Questions Core Java Interview Questions Java Interview Preparation Java Coding Questions Java Interview Questions for Freshers Java Interview Questions PDF Java Questions and Answers Java Programming MCQs Java Quiz Questions Java Practice Questions Java Interview Preparation for Freshers Java Coding Quiz Java Technical Interview Questions Java Interview Test,Java Interview Questions and Answers