Top 50+ Java Interview MCQS for 2024




1. What is the size of the int data type in Java?
A. 8-bit
B. 16-bit
C. 32-bit
D. 64-bit
Answer: C. 32-bit

2. Which of the following is not a Java keyword?
A. static
B. Boolean
C. void
D. private
Answer: B. Boolean

3. Which method is used to get the length of a string in Java?
A. length()
B. size()
C. getLength()
D. getSize()
Answer: A. length()

4. Which of the following is the correct way to create an object in Java?
A. ClassName obj = new ClassName();
B. ClassName obj = ClassName();
C. new ClassName obj = new ClassName();
D. ClassName obj;
Answer: A. ClassName obj = new ClassName();

5. What is the default value of a local variable in Java?
A. null
B. 0
C. undefined
D. Local variables do not have a default value.
Answer: D. Local variables do not have a default value.

6. Which keyword is used for inheritance in Java?

A. implement
B. extends
C. inherits
D. super
Answer: B. extends

7. Which method must be implemented by all threads in Java?
A. start()
B. run()
C. execute()
D. main()
Answer: B. run()

8. Which of the following is a marker interface?
A. Cloneable
B. Runnable
C. Comparable
D. Serializable
Answer: A. Cloneable

9. Which of these packages contains the classes and interfaces for collections framework?
A. java.util
B. java.lang
C. java.io
D. java.net
Answer: A. java.util

10. What is the base class of all classes in Java?
A. Object
B. Class
C. System
D. Runtime
Answer: A. Object

11. Which exception is thrown when an array is accessed with an illegal index?
A. NullPointerException
B. ArrayIndexOutOfBoundsException
C. ClassCastException
D. IllegalArgumentException
Answer: B. ArrayIndexOutOfBoundsException

12. What does the final keyword mean in Java?
A. The class cannot be inherited.
B. The method cannot be overridden.
C. The variable cannot be changed.
D. All of the above.
Answer: D. All of the above.

13. Which of the following is not a feature of Java?

A. Object-oriented
B. Platform-independent
C. Use of pointers
D. Dynamic and Extensible
Answer: C. Use of pointers

14. Which method is used to compare two strings in Java?

A. compareTo()
B. equals()
C. ==
D. compare()
Answer: B. equals()

15. What does the static keyword mean in Java?
A. The method belongs to an instance of a class.
B. The method belongs to the class itself.
C. The method cannot be overridden.
D. The method cannot be accessed outside its class.
Answer: B. The method belongs to the class itself.

16. Which of these cannot be used for a variable name in Java?

A. identifier
B. keyword
C. variable
D. method
Answer: B. keyword

17. What is the output of the following code?
int a = 5;
int b = 2;
System.out.println(a / b);
A. 2.5
B. 2
C. 3
D. Error
Answer: B. 2

18. Which of the following is not a primitive data type in Java?
A. int
B. float
C. boolean
D. String
Answer: D. String

19. Which operator is used to concatenate two strings in Java?
A. +
B. &
C. *
D. @
Answer: A. +

20. Which of the following is not a valid access modifier in Java?
A. public
B. private
C. protected
D. internal
Answer: D. internal

21. What does JVM stand for?
A. Java Visual Machine
B. Java Virtual Machine
C. Java Variable Machine
D. Java Verified Machine
Answer: B. Java Virtual Machine

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

A. final
B. const
C. static
D. constant
Answer: A. final

23. What is the use of the super keyword in Java?
A. To refer to the current class instance
B. To refer to the immediate parent class instance
C. To create an instance of the superclass
D. To create an instance of the subclass
Answer: B. To refer to the immediate parent class instance

24. Which method is called to start a thread execution in Java?

A. run()
B. start()
C. execute()
D. begin()
Answer: B. start()

25. Which collection class does not allow duplicates?
A. List
B. Set
C. Map
D. ArrayList
Answer: B. Set

26. What is the default value of an instance variable in Java?
A. null
B. 0
C. Depends on the data type
D. undefined
Answer: C. Depends on the data type

27. Which of these statements is correct about the Java switch statement?

A. It must include a default case.
B. It can only evaluate int and char data types.
C. It can evaluate any primitive data type.
D. It can evaluate int, char, byte, short, and enum data types.
Answer: D. It can evaluate int, char, byte, short, and enum data types.

28. Which of the following is used to declare an abstract class?

A. abstract
B. interface
C. extends
D. final
Answer: A. abstract

29. Which of these is a correct method signature for a method that does not return any value?
A. void methodName()
B. void methodName
C. methodName()
D. methodName(void)
Answer: A. void methodName()

30. Which of the following is true about a constructor?
A. It has the same name as the class.
B. It does not have a return type.
C. It is called when an object is created.
D. All of the above.
Answer: D. All of the above.

31. Which of the following is used to create a new package in Java?

A. create package
B. define package
C. new package
D. package
Answer: D. package

32. Which of these keywords is used to handle exceptions?
A. try
B. catch
C. finally
D. All of the above
Answer: D. All of the above

33. Which of these access specifiers can be used for a constructor?

A. public
B. protected
C. private
D. All of the above
Answer: D. All of the above

34. What is the return type of the hashCode() method in Java?

A. int
B. long
C. String
D. Object
Answer: A. int

35. Which method is used to terminate a running thread?
A. stop()
B. terminate()
C. exit()
D. end()
Answer: A. stop()

36. Which of these cannot be used for a method name in Java?

A. start()
B. stop()
C. run()
D. 1start()
Answer: D. 1start()

37. Which of the following is the correct syntax for a for loop in Java?
A. for (int i = 0; i < 10; i++)
B. for (int i = 0; i < 10; i++) {}
C. for (int i = 0, i < 10, i++) {}
D. for (int i = 0; i < 10; i++ {}
Answer: B. for (int i = 0; i < 10; i++) {}

38. What is the output of the following code?
int x = 10;
int y = 20;
x += y;
System.out.println(x);
A. 10
B. 20
C. 30
D. 40
Answer: C. 30

39. Which of the following is not a method of the String class?
A. toUpperCase()
B. toLowerCase()
C. charAt()
D. lengthof()
Answer: D. lengthof()

40. Which of the following is the correct way to declare an array in Java?
A. int arr[] = new int[5];
B. int arr = new int[5];
C. int[] arr = int[5];
D. int arr[] = int[5];
Answer: A. int arr[] = new int[5];

41. What is the output of the following code?
String s = "Hello";
String t = s.toUpperCase();
System.out.println(t);
A. HELLO
B. hello
C. Hello
D. Error
Answer: A. HELLO

42. Which of the following is not a loop structure in Java?
A. for
B. while
C. do-while
D. foreach
Answer: D. foreach

43. Which of the following is true about interfaces in Java?
A. Interfaces can have instance variables.
B. Interfaces can have method implementations.
C. Interfaces can have static methods.
D. Interfaces cannot be extended.
Answer: C. Interfaces can have static methods.

44. What is the purpose of the break statement in a loop?

A. To terminate the loop immediately.
B. To skip the current iteration.
C. To continue to the next iteration.
D. To exit the method.
Answer: A. To terminate the loop immediately.

45. What is the output of the following code?

int a = 10;
int b = 5;
System.out.println(a > b ? a : b);
A. 10
B. 5
C. true
D. false
Answer: A. 10

46. Which of the following is not a valid Java identifier?
A. myVariable
B. 1variable
C. _variable
D. $variable
Answer: B. 1variable

47. What is the use of the this keyword in Java?
A. To refer to the current class instance.
B. To refer to the superclass instance.
C. To refer to a static method.
D. To refer to a static variable.
Answer: A. To refer to the current class instance.

48. Which of these is used to create an anonymous inner class?

A. new
B. class
C. interface
D. abstract
Answer: A. new

49. Which of the following is a valid constructor for the MyClass class?
A. void MyClass() {}
B. MyClass() {}
C. public MyClass() {}
D. static MyClass() {}
Answer: C. public MyClass() {}

50. Which of the following is used to read input from the console in Java?
A. Scanner
B. BufferedReader
C. InputStreamReader
D. All of the above
Answer: D. All of the above

0 Comments:

Post a Comment