Top 50 Java Interview Questions with Answers

Java Interview Questions with Answers

1. What is the output of the following code?

int x = 5;
System.out.println(++x);

a) 5
b) 6
c) Compilation error
d) Runtime error

Answer: b) 6

2. Which keyword is used to prevent a member variable from being serialized?

a) transient
b) static
c) final
d) volatile

Answer: a) transient

3. What is the output of the following code?

String str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2);

a) true
b) false
c) Compilation error
d) Runtime error

Answer: a) true

4. What is an abstract class?


a) A class that cannot be instantiated
b) A class that can only be instantiated
c) A class that is final
d) A class with no constructor

Answer: a) A class that cannot be instantiated

5. What is polymorphism?

a) The ability of an object to take on many forms
b) The ability of a method to be defined in multiple classes
c) The ability of a variable to be of multiple types
d) The ability of a class to be inherited by multiple classes

Answer: a) The ability of an object to take on many forms

6. Which keyword is used to create a new instance of a class?

a) new
b) this
c) super
d) instance

Answer: a) new

7. What is the output of the following code?

int[] nums = {1, 2, 3};
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}

a) 1 2 3
b) 1
2
3
c) Compilation error
d) Runtime error

Answer: b) 1 2 3

8. Which of the following is not a primitive data type in Java?

a) int
b) float
c) char
d) String

Answer: d) String

9. What is inheritance?

a) The ability of a class to implement an interface
b) The ability of a class to contain an object of another class
c) The ability of a class to be derived from another class
d) The ability of a class to have multiple constructors

Answer: c) The ability of a class to be derived from another class

10. What is the output of the following code?

String str = "hello";
System.out.println(str.substring(1, 4));

a) h
b) el
c) ell
d) llo

Answer: c) ell

11. Which keyword is used to exit a for loop?

a) exit
b) break
c) continue
d) return

Answer: b) break

12. What is the output of the following code?

String str = "hello";
for (int i = str.length() - 1; i >= 0; i--) {
System.out.print(str.charAt(i));
}

a) hello
b) olleh
c) hlo
d) Compilation error

Answer: b) olleh

13. Which keyword is used to create an interface?

a) interface
b) extends
c) implements
d) abstract

Answer: a) interface

14. What is encapsulation?

a) The ability of a class to be extended by another class
b) The ability of a class to implement an interface
c) The ability of a class to contain an object of another class
d) The ability of a class to hide its implementation details

Answer: d) The ability of a class to hide its implementation details

15. What is the output of the following code?

int x = 6;
if (x < 5) {
System.out.println("Less than 5");
} else if (x < 10) {
System.out.println("Between 5 and 10");
} else {
System.out.println("Greater than or equal to 10");
}

a) Less than 5
b) Between 5 and 10
c) Greater than or equal to 10
d) Compilation error

Answer: b) Between 5 and 10

16. Which keyword is used to access a static method or variable?

a) static
b) private
c) public
d) this

Answer: a) static

17. What is a constructor?

a) A method used to create an object
b) A method used to destroy an object
c) A method used to access an object’s properties
d) A method used to declare an object’s class

Answer: a) A method used to create an object

18. What is the output of the following code?

int x = 2;
switch(x) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Default");
break;
}

a) One
b) Two
c) Default
d) Compilation error

Answer: b) Two

19. Which keyword is used to access a parent class’s method or variable?

a) extends
b) super
c) this
d) implements

Answer: b) super

20. What is the output of the following code?

int x = 2;
while (x < 5) {
System.out.println(x);
x++;
}

a) 2 3 4
b) 2 3 4 5
c) 3 4
d) 3 4 5

Answer: a) 2 3 4

21. Which keyword is used to declare an abstract method?

a) abstract
b) final
c) static
d) private

Answer: a) abstract

22. What is the output of the following code?

String str = "hello";
System.out.println(str.indexOf("l"));

a) 2
b) 3
c) 4
d) -1

Answer: b) 3

23. Which keyword is used to create a constant variable?

a) final
b) static
c) volatile
d) transient

Answer: a) final

24. What is the output of the following code?

int x = 5;
if (x > 3 && x < 7) {
System.out.println("In range");
} else {
System.out.println("Out of range");
}

a) In range
b) Out of range
c) Compilation error
d) Runtime error

Answer: a) In range

25. What is a static method?

a) A method that can be accessed without creating an object of the class
b) A method that can only be accessed by objects of the class
c) A method that is final
d) A method that is abstract

Answer: a) A method that can be accessed without creating an object of the class

26. What is the output of the following code?

int x = 10;
do {
System.out.println(x);
x--;
} while (x > 5);

a) 10 9 8 7 6 5
b) 10 9 8 7 6
c) 9 8 7 6
d) Compilation error

Answer: b) 10 9 8 7 6

27. Which keyword is used to declare a synchronized method?

a) synchronized
b) static
c) final
d) volatile

Answer: a) synchronized

28. What is the output of the following code?

int x = 2;
System.out.println(x++);

a) 2
b) 3
c) Compilation error
d) Runtime error

Answer: a) 2

29. What is the difference between an interface and an abstract class?

a) An interface cannot have any method implementations, while an abstract class can have some method implementations
b) An abstract class cannot have any method implementations, while an interface can have some method implementations
c) An abstract class can be instantiated, while an interface cannot be instantiated
d) An interface cannot be extended, while an abstract class can be extended

Answer: a) An interface cannot have any method implementations, while an abstract class can have some method implementations

30. What is a package in Java?

a) A container for classes and other packages
b) A keyword that tells the compiler to import a class
c) A keyword that indicates the start of a block of code
d) A keyword that indicates the end of a block of code

Answer: a) A container for classes and other packages

31. What is the output of the following code?

int x = 2;
int y = x == 2 ? 5 : 7;
System.out.println(y);

a) 5
b) 7
c) Compilation error
d) Runtime error

Answer: a) 5

32. Which keyword is used to specify a variable as a reference to an object?

a) obj
b) ref
c) this
d) none of the above

Answer: d) none of the above

33. What is the output of the following code?

String str = "hello";
System.out.println(str.toUpperCase());

a) hello
b) HELLO
c) Compilation error
d) Runtime error

Answer: b) HELLO

34. Which keyword is used to declare a variable that cannot be changed?

a) final
b) static
c) volatile
d) transient

Answer: a) final

35. What is a thread?

a) A container for code and data
b) A unit of execution within a process
c) A keyword that indicates the start of a block of code
d) A keyword that indicates the end of a block of code

Answer: b) A unit of execution within a process

36. What is the output of the following code?

for (int i = 0; i < 5; i) {
System.out.println(i);
}

a) Compilation error
b) 0 1 2 3 4
c) Infinite loop
d) Runtime error

Answer: c) Infinite loop

37. Which keyword is used to declare a method that cannot be overridden?

a) final
b) static
c) synchronized
d) abstract

Answer: a) final

38. What is the output of the following code?

int x = 1;
int y = 2;
System.out.println(x + y);

a) 1
b) 2
c) 3
d) Compilation error

Answer: c) 3

39. Which keyword is used to import a class into a Java program?

a) import
b) package
c) class
d) none of the above

Answer: a) import

40. What is the output of the following code?

int x = 1;
int y = 2;
System.out.println(x > y ? x : y);

a) 1
b) 2
c) Compilation error
d) Runtime error

Answer: b) 2

41. Which keyword is used to declare an inner class?

a) class
b) private
c) protected
d) none of the above

Answer: d) none of the above (an inner class is declared within another class)

42. What is the output of the following code?

String str = "hello";
System.out.println(str.length());

a) 5
b) 6
c) Compilation error
d) Runtime error

Answer: a) 5

43. Which keyword is used to create a new instance of an array?

a) new
b) this
c) super
d) instance

Answer: a) new

44. What is the output of the following code?

int x = 5;
if (x == 5) {
int y = 2;
System.out.println(y);
}

a) 2
b) Compilation error
c) Runtime error
d) No output

Answer: a) 2

45. What is the output of the following code?

String str1 = "hello";
String str2 = "world";
System.out.println(str1 + " " + str2);

a) helloworld
b) hello world
c) Compilation error
d) Runtime error

Answer: b) hello world

46. Which keyword is used to declare a variable that can be accessed by any object of the class?

a) private
b) protected
c) public
d) none of the above

Answer: c) public

47. What is the output of the following code?

int[] nums = {1, 2, 3};
System.out.println(nums[1]);

a) 1
b) 2
c) 3
d) Compilation error

Answer: b) 2

48. Which keyword is used to declare that a method can throw an exception?

a) try
b) catch
c) throw
d) throws

Answer: d) throws

49. What is the output of the following code?

int x = 2;
System.out.println(x--);

a) 2
b) 1
c) Compilation error
d) Runtime error

Answer: a) 2

50. What is the difference between a while loop and a do-while loop?

a) A while loop checks the condition before executing the loop body, while a do-while loop checks the condition after executing the loop body
b) A do-while loop checks the condition before executing the loop body, while a while loop checks the condition after executing the loop body
c) A while loop can only be executed once, while a do-while loop can be executed multiple times
d) There is no difference between a while loop and a do-while loop

Answer: a) A while loop checks the condition before executing the loop body, while a do-while loop checks the condition after executing the loop body

Ashwani Kumar
Latest posts by Ashwani Kumar (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x