Java Interview Questions

These are Java Interview Questions explained in the simplest form possible.

OOP Concepts (Object Oriented Programming Concepts in Java)

  • Polymorphism
    • Polymorphism is a state of having the capability to take in many forms. In Java, this is the ability to process multiple types of objects using a uniform interface. Example is method overloading.
      • screen.draw(circle) <– calls method(Circle obj)
      • screen.draw(triangle) <– calls method(Triangle obj)
  • Encapsulation¬†
    • Encapsulation is all about bundling the class (fields and methods).
    • Encapsulation is Data Hiding or Data Wrapping mechanism on the data (fields or methods).
      • Declaring the fields private.
      • Using Setter and Getter methods.
  • Abstraction
    • Hiding the internal workings of the class from the outside view.

Java Class Access Modifiers

  1. private – available to class only (CLASS)
  2. default – no modifier, package-only <– only available to the package the class is in. (PACKAGE)
  3. protected – available to all the classes that implements it. (SUBCLASS)
  4. public – available to all (ALL)

What is the purpose of using Getter and Setter methods?

  • The fields of the class can be made Read only or Write only. Try doing that without getter/setter methods.
  • You can add logic or code that acts on the data on the field.
    • Set the valid values that the field can accept.

AutoBoxing and Unboxing

  • AutoBoxing – wrapping the primitive type to its object counterpart. Ex. int to Integer
  • Unboxing – wrapping the object counterpart to its primitive type.

Interface vs Abstract Class

  • Interface
    • method definitions only
    • is implemented by another class
    • cannot be instantiated
    • multiple interfaces can be implemented by a class
  • Abstract Class
    • have full methods
    • cannot be instantiated compared to a regular class
    • is extended by another class
    • only one class can extend an abstract class

What are the Java Primitive Types

  • int, short, long – no decimal places
  • float, double – with decimal places
  • char – for string information
  • byte – for holding data like file/image
  • boolean – true or false

Final, Finally, Finalized

  • Final used in different types
    • Final Class – prevents inheritance
    • Final Method – cannot be overriden
    • Final Variable – constant
  • Finally
    • used in Try Catch Finally
    • Executes any clean up before exiting the method
    • Trick questions from interviewers: how to skip Finally block?
      • if you invoke System.exit()
      • if you turn off the server
      • if JVM crashes
  • Finalized
    • Called when an object is about to be garbage collected.

Java Data Structures

  • Enumeration – retrieving successive elements
  • BitSet – structure for holding and manipulating bits/boolean values
  • Vector – similar to array except that it can grow.
  • Stack – last in first out LIFO
  • Dictionary – key value
  • Hashtable – key value data structure
  • Properties – key value like hashtable but for system properties.

Spring MVC

  • What is Spring MVC

Spring – Autowired

  • Autowired annotation in Spring tells the application context to inject the instance of the class that has the autowired annotation.
    • No Autowiring – default
    • by Name – name of the bean
    • by Type – by the type of the bean
    • constructor – similar as byType but using the constructor types.

Spring Bean Scopes

  • Singleton – only one instance, most common.
  • Prototype – creates an instance everytime the bean is requested.
  • Request – for web apps, every request
  • Session – new instance for each session.
  • Global – global session beans for portlet applications

… will continue on this.