Debug School

Akanksha
Akanksha

Posted on

Top 30 java Interview Questions with Answers

1. What is Java?

a. A programming language
b. An operating system
c. A database management system
d. A web browser
Answer: a. A programming language

2. What is the main advantage of using Java?

a. Platform independence
b. Low memory usage
c. Faster execution
d. Strongly typed language
Answer: a. Platform independence

3. What is the difference between JDK, JRE, and JVM?

a. JDK is for running Java applications, JRE is for development, and JVM is for compilation.
b. JDK is for development, JRE is for running Java applications, and JVM is for executing Java bytecode.
c. JDK is for running Java applications, JRE is for compilation, and JVM is for development.
d. JDK is for execution, JRE is for development, and JVM is for running Java applications.
Answer: b. JDK is for development, JRE is for running Java applications, and JVM is for executing Java bytecode.

4. Which keyword is used to create a copy of an object in Java?

a. copy
b. clone
c. replicate
d. duplicate
Answer: b. clone

5. Which of the following is not a valid data type in Java?

a. float
b. long
c. decimal
d. double
Answer: c. decimal

6. What is the default value of an instance variable in Java?

a. 0
b. null
c. undefined
d. 1
Answer: b. null

7. What is the purpose of the 'break' statement in Java?

a. To terminate a loop or switch statement
b. To skip an iteration in a loop
c. To resume the next iteration in a loop
d. To exit the program
Answer: a. To terminate a loop or switch statement

8. What is the correct syntax for a multi-line comment in Java?

a. // This is a comment
b. /* This is a comment */
c. # This is a comment
d. ' This is a comment
Answer: b. /* This is a comment */

9. Which of the following is a subclass of Throwable in Java?

a. Error
b. Exception
c. RuntimeException
d. Both a and b
Answer: d. Both a and b

10. Which keyword is used to define a constant in Java?

a. static
b. final
c. constant
d. const
Answer: b. final

11. Which collection class allows duplicate elements in Java?

a. HashSet
b. TreeSet
c. LinkedList
d. HashMap
Answer: a. HashSet

12. What is the result of the expression: 10 + 5 / 2 in Java?

a. 10
b. 12.5
c. 7
d. 15
Answer: c. 7

13. Which access modifier restricts access the least in Java?

a. private
b. protected
c. public
d. default
Answer: c. public

14. What is the purpose of the 'super' keyword in Java?

a. To call the parent class constructor
b. To access the superclass's methods and variables
c. To define the superclass of a class
d. To override a method
Answer: b. To access the superclass's methods and variables

15. What is the correct way to handle exceptions in Java?

a. try-catch-finally
b. try-catch
c. try-finally
d. catch-finally
Answer: a. try-catch-finally

16. What is the purpose of the 'interface' keyword in Java?

a. To define a class
b. To declare an interface
c. To extend a class
d. To implement a class
Answer: b. To declare an interface

17. Which design pattern is used to create an object without exposing its creation logic?

a. Singleton
b. Factory
c. Observer
d. Adapter
Answer: b. Factory

18. Which Java feature allows multiple methods to have the same name but different parameters?

a. Polymorphism
b. Encapsulation
c. Inheritance
d. Abstraction
Answer: a. Polymorphism

19. What is the result of the expression: 5 << 2 in Java?

a. 10
b. 20
c. 15
d. 25
Answer: b. 20

20. Which type of inheritance is not supported in Java?

a. Single inheritance
b. Multiple inheritance
c. Multilevel inheritance
d. Hierarchical inheritance
Answer: b. Multiple inheritance

21. Which method is used to convert a string to uppercase in Java?

a. toUpperCase()
b. upperCase()
c. toUpper()
d. convertUpperCase()
Answer: a. toUpperCase()

22. What is the result of the expression: 10 % 3 in Java?

a. 1
b. 2
c. 3
d. 0
Answer: a. 1

23. Which statement is used to exit a loop prematurely in Java?

a. exit
b. continue
c. break
d. return
Answer: c. break

24. Which keyword is used to invoke the superclass constructor in Java?

a. this
b. super
c. extends
d. invoke
Answer: b. super

25. What is the result of the expression: Math.pow(2, 3) in Java?

a. 6
b. 8
c. 16
d. 64
Answer: b. 8

26. Which keyword is used to define a package in Java?

a. import
b. include
c. package
d. define
Answer: c. package

27. What is the purpose of the 'volatile' keyword in Java?

a. To indicate that a variable cannot be modified
b. To prevent multiple threads from accessing a variable simultaneously
c. To indicate that a variable's value may be modified by another thread
d. To specify that a variable is constant
Answer: c. To indicate that a variable's value may be modified by another thread

28. What is the correct way to create a thread in Java?

a. By extending the Thread class
b. By implementing the Runnable interface
c. By creating an object of the Thread class
d. By calling the start() method
Answer: a. By extending the Thread class

29. Which data structure in Java provides a way to access elements based on their index?

a. Set
b. List
c. Map
d. Queue
Answer: b. List

30. What is the result of the expression: 5 + "hello" in Java?

a. 5hello
b. 5
c. hello5
d. Error
Answer: a. 5hello

Top comments (0)