Java MCQ Questions and Answers

21. Which of the following tool is used to generate API documentation in HTML format from doc comments in source code?

  1. javap tool
  2. javaw command
  3. Javadoc tool
  4. javah command

Answer : C
Explanation: The Javadoc is a tool that is used to generate API documentation in HTML format from the Java source files. In other words, it is a program (tool) that reads a collection of source files into an internal form.
The Javadoc command line syntax is,
Javadoc [options] [packagenames] [sourcefiles] [@files]

22. To successfully overload a method in Java, the method names must be

  1. Same
  2. Different
  3. Same or different based on the situation
  4. None

Answer : A
Explanation: To successfully overload a method in Java, the method names must be the same.

23. Which of the following is a garbage collection technique?

  1. Sweep model
  2. Space management model
  3. Mark and sweep model
  4. Cleanup model

Answer : C
Explanation: A mark and sweep garbage collection consists of two phases, the mark phase, and the sweep phase. I mark phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are garbage. In the sweep phase, the heap is traversed to find gaps between live objects, and the gaps are marked free lists used for allocating memory to new objects so option C is correct.

24. When finally block gets executed?

  1. Only when exception occurs in try block code
  2. Always when try block get executed, no matter exception occured or not
  3. Always when a method get executed, no matter exception occured or not
  4. Always when a try block get executed, if exception do not occur

Answer : B
Explanation: Finally block gets executed always when try block get executed, no matter exception occured or not.

25. In which process, a local variable has the same name as one of the instance variables?

  1. Variable Shadowing
  2. Serialization
  3. Abstraction
  4. Multi-threading

Answer : A
Explanation: Variable Shadowing is a process in which a local variable has the same name as one of the instance variables.

26. Which method must be implemented by all threads?

  1. run()
  2. stop()
  3. start()
  4. wait()

Answer : A
Explanation: All threads must implement the run() method.

27. Iterator returned by ConcurrentSkipListSet is

  1. Fail-fast
  2. Fail-safe
  3. Both of the above
  4. None of the above

Answer : B
Explanation: Iterator returned by ConcurrentSkipListSet is Fail-safe.

28. Which statement transfer execution to different parts of your code based on the value of an expression?

  1. if
  2. nested-if
  3. switch
  4. if-else-if

Answer : C
Explanation: switch statement transfer execution to different parts of program’s code based on the value of an expression.

29. Method Overriding is an example of

  1. Static binding
  2. Dynamic binding
  3. Both of the above
  4. None

Answer : B
Explanation: Method Overriding is an example of dynamic binding.

30. Which is a valid declaration of a String?

  1. String s1 = null;
  2. String s2 = ‘null’;
  3. String s3 = (String) ‘abc’;
  4. String s4 = (String) ‘\ufeed’;

Answer : A
Explanation: Option A is correct because it sets the String reference to null. Other options are wrong because null cannot be in single quotes, there are multiple characters between the single quotes (‘abc’), and can’t cast a char (primitive) to a String (object).

This Post Has 2 Comments

Leave a Reply