Java - Programming MCQ Questions and Answers

Test your knowledge of Java Programming Language with these interactive multiple-choice questions.

« First 1 2 3 4 Last »
« Previous Page Next Page »

41. What is the output of: System.out.println(1 + 2 + "3" + 4 + 5);

  • a) 3345
  • b) 12345
  • c) 3345
  • d) 1239
Answer: A - First 1+2=3, then string concatenation: "3"+"3"+"4"+"5" = "3345".

42. Which interface provides the capability to traverse a collection in both forward and backward directions?

  • a) Iterator
  • b) ListIterator
  • c) Enumeration
  • d) Traverser
Answer: B - ListIterator extends Iterator to allow bidirectional traversal.

43. What is the default value of a local variable in Java?

  • a) 0
  • b) null
  • c) false
  • d) Local variables have no default value
Answer: D - Local variables must be initialized before use; they have no default value.

44. Which of these is not a valid Java comment?

  • a) /* comment */
  • b) // comment
  • c) /** documentation */
  • d) %% comment
Answer: D - %% is not a valid comment syntax in Java.

45. What is the purpose of the 'enum' keyword?

  • a) To create enumerated types
  • b) To enumerate collection elements
  • c) To enable multiple inheritance
  • d) To create anonymous classes
Answer: A - enum is used to define a fixed set of constants.

46. Which method is used to start a thread's execution?

  • a) init()
  • b) begin()
  • c) start()
  • d) run()
Answer: C - The start() method causes the thread to begin execution.

47. What is the output of: System.out.println(9 + 9 + "=" + 9 + 9);

  • a) 18=18
  • b) 99=99
  • c) 18=99
  • d) 99=18
Answer: C - First 9+9=18, then string concatenation: "18=" + "9" + "9" = "18=99".

48. Which collection does not allow duplicate elements?

  • a) ArrayList
  • b) LinkedList
  • c) HashSet
  • d) Vector
Answer: C - HashSet implements the Set interface which prohibits duplicates.

49. What is the purpose of the 'this' keyword?

  • a) To refer to the current class instance
  • b) To create a new object
  • c) To call static methods
  • d) To access superclass members
Answer: A - 'this' refers to the current object instance.

50. Which method is used to convert a String to an integer?

  • a) parseInt()
  • b) valueOf()
  • c) getInteger()
  • d) Both A and B
Answer: D - Both Integer.parseInt() and Integer.valueOf() can convert String to int.

51. What is the purpose of the 'finally' block?

  • a) To handle exceptions
  • b) To declare resources
  • c) To execute code regardless of exceptions
  • d) To catch specific exceptions
Answer: C - finally block executes whether an exception occurs or not.

52. Which of these is not a valid Java access modifier?

  • a) public
  • b) private
  • c) protected
  • d) internal
Answer: D - Java doesn't have an 'internal' access modifier.

53. What is the output of: System.out.println(1 > 2 ? "Yes" : "No");

  • a) Yes
  • b) No
  • c) true
  • d) false
Answer: B - The ternary operator evaluates to "No" since 1 > 2 is false.

54. Which class is used to create immutable objects?

  • a) String
  • b) StringBuilder
  • c) StringBuffer
  • d) All of the above
Answer: A - String objects are immutable by design.

55. What is the purpose of the 'instanceof' operator?

  • a) To check object type
  • b) To create new instances
  • c) To compare object values
  • d) To check array bounds
Answer: A - instanceof checks if an object is an instance of a specific class or interface.

56. Which method is called when a thread is interrupted?

  • a) stop()
  • b) interrupt()
  • c) terminate()
  • d) destroy()
Answer: B - interrupt() is used to interrupt a thread's execution.

57. What is the purpose of the 'assert' keyword?

  • a) To handle exceptions
  • b) To test assumptions in code
  • c) To create assertions in JUnit tests
  • d) To validate method parameters
Answer: B - assert is used for testing assumptions during development.

58. Which collection maintains elements in sorted order?

  • a) HashSet
  • b) TreeSet
  • c) LinkedHashSet
  • d) ArrayList
Answer: B - TreeSet maintains elements in their natural ordering.

59. What is the purpose of the 'native' keyword?

  • a) To indicate platform-specific implementation
  • b) To create natural ordering
  • c) To optimize method execution
  • d) To indicate primitive types
Answer: A - native indicates a method is implemented in platform-dependent code.

60. Which method is used to schedule a task for repeated execution?

  • a) Timer.schedule()
  • b) Thread.run()
  • c) Executor.execute()
  • d) Runnable.start()
Answer: A - Timer.schedule() can be used for repeated task execution.
« First 1 2 3 4 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.