Core Java Interview Preparation Questions

Interview Questions Categories
Core Java | J2EE | Database | Struts | XML | HTTP | Web Services

I have prepared this list for those of you seeking to appear in Java interview at various companies globally. This post contains a list of core java questions. These are prepared from most frequently asked questions from various software industries. I myself have interviewed over 100 candidates. I have intentionally not provided the solution here. But if you are desperate for one, send me an email via Contact Us link. For other interview questions such as those for J2EE, XML, Database etc and for some sample programming implementations intended to help you to prepare for the interview of tech giants like Google  / Microsoft / Amazon, go to my page here.

The questions below are alphabetically sorted.

  1. Can you instantiate the Math class?
  2. Define Modularity?
  3. Describe synchronization in respect to multithreading. How do you do that Java?
  4. Does it matter in what order catch statements for FileNotFoundException and IOException are written?
  5. Does java support multiple inheritance?
  6. Explain Exception handling in Java.
  7. Explain the difference between overriding and overloading?
  8. Give an example of where you would use Interface not an Abstract Class and vice versa?
  9. How do you do bit manipulation, bitwise operation in Java?
  10. How is polymorphism achieved in java?
  11. How will you get the platform dependent values like line separator, path separator, etc., ?
  12. How will you invoke any external process in Java?
  13. How will you load a specific locale?
  14. In thread, which method resumes the first thread that went into sleep mode?
  15. Is JVM a compiler or an interpreter?
  16. Is Private Method or member variable available to the subclass in the same package?
  17. State the significance of protected and default modifiers.
  18. What are the approaches that you will follow for making a program very efficient?
  19. What are the core OOP’s concepts?
  20. What are the methods in Object?
  21. What does the finalize method do?
  22. What is a base class?
  23. What is a Class?
  24. What is a constructor?
  25. What is a daemon thread?
  26. What is a DatabaseMetaData?
  27. What is a destructor?
  28. What is a final class?
  29. What is a final method?
  30. What is a final variable?
  31. What is a package?
  32. What is a subclass?
  33. What is a superclass?
  34. What is aggregation?
  35. What is an Abstract Class?
  36. What is an Instance?
  37. What is an Interface?
  38. What is an Object?
  39. What is Class.forName() does and how it is useful?
  40. What is collaboration?
  41. What is composition
  42. What is composition?
  43. What is DriverManager?
  44. What is final?
  45. What is Finally? And give an example of how to use it?
  46. What is garbage collection? What is the process that is responsible for doing that in java?
  47. What is inner class?
  48. What is inheritance?
  49. What is JIT and its use?
  50. What is Locale?
  51. What is meant by abstraction?
  52. What is meant by Binding?
  53. What is meant by Dynamic binding?
  54. What is meant by Encapsulation?
  55. What is meant by Inheritance?
  56. What is meant by Object Oriented Programming?
  57. What is meant by Persistence?
  58. What is meant by Polymorphism?
  59. What is meant by static binding?
  60. What is mutable object and immutable object?
  61. What is nested class?
  62. What is pass by reference and pass by value?
  63. What is Polymorphism
  64. What is reflection?
  65. What is singleton?
  66. What is skeleton and stub? what is the purpose of those?
  67. What is static in java?
  68. What is the base class for Error and Exception?
  69. What is the basic difference between String and StringBuffer and StringBuilder object?
  70. What is the byte range?
  71. What is the difference between an Interface and an Abstract class?
  72. What is the final keyword denotes?
  73. What is the impact and usage of Static member variables in Multithreaded programs.
  74. What is the implementation of destroy method in java.. is it native or java code?
  75. What is the major difference between LinkedList and ArrayList?
  76. What is the purpose of assert keyword used in JDK1.4.x?
  77. What is the purpose of garbage collection in Java, and when is it used?
  78. What is the purpose of Void class?
  79. What is the significance of ListIterator?
  80. What kind of thread is the Garbage collector thread?
  81. What operator would you use to compare two String variables?
  82. When you think about optimization, what is the best way to find out the time/memory consuming process?

For other interview preparation questions and materials, go to my page here.

List of Valid Grails Commands

Grails Framework comes with a lot of powerful terminal commands. Here is a list that I populated for the Grails version 2.3.7

  1. grails add-proxy
  2. grails alias
  3. grails bootstrap
  4. grails bug-report
  5. grails clean
  6. grails clean-all
  7. grails clear-proxy
  8. grails compile
  9. grails console
  10. grails create-app
  11. grails create-controller
  12. grails create-domain-class
  13. grails create-filters
  14. grails create-hibernate-cfg-xml
  15. grails create-integration-test
  16. grails create-multi-project-build
  17. grails create-plugin
  18. grails create-pom
  19. grails create-scaffold-controller
  20. grails create-script
  21. grails create-service
  22. grails create-tag-lib
  23. grails create-unit-test
  24. grails create-web-xml-config
  25. grails dbm-changelog-sync
  26. grails dbm-changelog-sync-sql
  27. grails dbm-changelog-to-groovy
  28. grails dbm-clear-checksums
  29. grails dbm-create-changelog
  30. grails dbm-db-doc
  31. grails dbm-diff
  32. grails dbm-drop-all
  33. grails dbm-future-rollback-sql
  34. grails dbm-generate-changelog
  35. grails dbm-generate-gorm-changelog
  36. grails dbm-gorm-diff
  37. grails dbm-list-locks
  38. grails dbm-list-tags
  39. grails dbm-mark-next-changeset-ran
  40. grails dbm-previous-changeset-sql
  41. grails dbm-register-changelog
  42. grails dbm-release-locks
  43. grails dbm-rollback
  44. grails dbm-rollback-count
  45. grails dbm-rollback-count-sql
  46. grails dbm-rollback-sql
  47. grails dbm-rollback-to-date
  48. grails dbm-rollback-to-date-sql
  49. grails dbm-status
  50. grails dbm-tag
  51. grails dbm-update
  52. grails dbm-update-count
  53. grails dbm-update-count-sql
  54. grails dbm-update-sql
  55. grails dbm-validate
  56. grails dependency-report
  57. grails doc
  58. grails generate-all
  59. grails generate-async-controller
  60. grails generate-controller
  61. grails generate-restful-controller
  62. grails generate-views
  63. grails help
  64. grails init
  65. grails install-app-templates
  66. grails install-dependency
  67. grails install-jq-uery
  68. grails install-plugin
  69. grails install-templates
  70. grails integrate-with
  71. grails interactive
  72. grails list-plugin-updates
  73. grails list-plugins
  74. grails migrate-docs
  75. grails package
  76. grails package-plugin
  77. grails plugin-info
  78. grails refresh-dependencies
  79. grails remove-proxy
  80. grails run-app
  81. grails run-script
  82. grails run-war
  83. grails schema-export
  84. grails set-proxy
  85. grails set-version
  86. grails shell
  87. grails stats
  88. grails stop-app
  89. grails test-app
  90. grails tomcat
  91. grails uninstall-plugin
  92. grails upgrade
  93. grails url-mappings-report
  94. grails war
  95. grails wrapper

How to find the Lowest Common Ancestor (LCA) of two nodes in a Binary Tree

If you are given a binary tree and two nodes and you have to find the lowest common ancestor, you can use the following approach.

  1. If the binary tree itself is null, you don’t have any common ancestor. This is an error condition.
  2. If one of the two nodes is the root, then the root itself is the common ancestor.
  3. Otherwise, recursively start finding the ancestor on the left node and right node.
    • If Node a and Node b lie in the left, their Lowest Common Ancestor is in the left.
    • If Node a and Node b lie in the right,their Lowest Common Ancestor is in the right.
    • Otherwise, root is the Lowest common ancestor.

Java code to find the common ancestor of Binary Nodes

package com.icodejava.blog;

/**
 * @author Kushal Paudyal
 * www.icodejava.com
 * Created On -  Mar 13, 2014
 * Last Modified On - Mar 13, 2014
 */
public class LowestCommonAncestorBinaryTree {
	
	public static void main(String args[]) {
		/**
		 * Create a sample Binary Tree. A Binary tree does not have to maintain
		 * left <root < right relationship.
		 */
		Node root = new Node(1);
		root.left = new Node(2);
		root.right = new Node(4);
		root.left.left = new Node(6);
		root.left.right = new Node(5);

		root.right.left = new Node(9);
		root.right.right = new Node(11);
		root.right.right.left = new Node(7);
		root.right.right.right = new Node(3);

		System.out.println("Lowest Common Ancestor of Node 3 and 9 is: "
				+ findLowestCommonAncestor(root, root.right.right.right, root.right.left).value);
		
		System.out.println("Lowest Common Ancestor of Node 3 and null is: "
				+ findLowestCommonAncestor(root, root.right.right.right, null).value);
		
		System.out.println("Lowest Common Ancestor of Node 11 and null is: "
				+ findLowestCommonAncestor(root, root.right.right, null).value);

	}
	
	/**
	 * Recursive approach to find the Lowest Common Ancestor
	 * @param root
	 * @param a - first Node
	 * @param b - second Node
	 * @return Node that is lowest common ancestor of both a and b
	 */
	public static Node findLowestCommonAncestor(Node root, Node a, Node b) {

		Node left = null;
		Node right = null;

		if (root == null) {
			return null;
		}

		/**
		 * If Node a or Node b is also the root, then the root itself is lowest common ancestor
		 */
		if (root == a || root == b) {
			return root;
		}

		left = findLowestCommonAncestor(root.left, a, b);
		right = findLowestCommonAncestor(root.right, a, b);

		/**
		 * If Node a and Node b lie in the left, their Lowest Common Ancestor is in the left.
		 * If Node a and Node b lie in the right,their Lowest Common Ancestor is in the right.
		 * 
		 * Otherwise, root is the Lowest common ancestor.
		 */
		if (left != null && right != null) {
			return root;
		}

		return (left != null) ? left : right;
	}
}
/**
 * Binary Tree representation.
 */
class Node {
	Node left;
	Node right;
	int value;
	
	public Node(int value) {
		left = null;
		right = null;
		this.value = value;
	}
}

The output of running above program is:

Lowest Common Ancestor of Node 3 and 9 is: 4
Lowest Common Ancestor of Node 3 and null is: 3
Lowest Common Ancestor of Node 11 and null is: 11