Core Java Interview Questions - Part 1

Had an interview the other day for a java opening and just compiled a list of frequently asked Java Interview questions for different specializations. Thought of sharing the lists for the benefit of all those who are attending or planning for a Java Interview.

This post focuses on Interview questions for the Java Fundamentals i.e the core functionality. If you feel that some important questions are missed or you want suggest a different answer to any Interview question, do comment and I will be glad to update on it.
1. What are Class, Constructor and Primitive data types?
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform.
Constructor is a special kind of method that determines how an object is initialized when created.
Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.
2. What is an Object and describe what happens when an object is created in Java?
Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it.
Several things happen in a particular order to ensure the object is constructed properly:
  1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implementation-specific data includes pointers to class and method data.
  2. The instance variables of the objects are initialized to their default values.
  3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its uppercase. This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java.
  4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.
3. What is the difference between constructor and method?
Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.
4. What are methods and how are they defined?
Methods are functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods and can call methods in other classes.
Method definition has four parts. They are :
  • name of the method
  • type of object or primitive type the method returns
  • a list of parameters
  • body of the method.
A method’s signature is a combination of the first three parts mentioned above.
5. How many ways can an argument be passed to a subroutine and explain them?
An argument can be passed in two ways. They are passing by value and passing by reference.
Passing by value: This method copies the value of an argument into the formal parameter of the subroutine.
Passing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter.
6. What is the difference between this() and super()?
this() can be used to invoke a constructor of the same class. Use this() to call a constructor from another constructor.
super() can be used to invoke a super class constructor. Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.
7. Explain the usage of Java packages.
This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.
8. If a class is located in a package, what do you need to change in the OS environment to be able to use it?
You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable.
Let's say a class Employee belongs to a package; and is located in the file c:\dev\com\xyz\hr\ In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:
9. What's the difference between constructors and normal methods?
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.
10. What is the difference between superclass and subclass?
A super class is a class that is inherited whereas sub class is a class that does the inheriting.
11. What does the "abstract" keyword mean in front of a method? A class?
If a method has a abstract keyword in front of it,it is called abstract method. Abstract method has no body.It has only arguments and return type.Abstract methods act as placeholder methods that are implemented in the subclasses.
Abstract classes can’t be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract.
12. How to define an Abstract class?
A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class:
abstract class testAbstractClass { 
    protected String myString; 
    public String getMyString() { 
    return myString; 
public abstract string anyAbstractFunction();
13. How to define an Interface?
In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Example of Interface:
public interface sampleInterface {
    public void functionOne();
    public long CONSTANT_ONE = 1000;
14. What are the advantages of interface?
Interfaces are useful for:
  • Declaring methods that one or more classes are expected to implement
  • Capturing similarities between unrelated classes without forcing a class relationship.
  • Determining an object’s programming interface without revealing the actual body of the class.
15. What's the difference between an interface and an abstract class? Also discuss the similarities. (Very Important)
Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods

Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.

  • Interfaces provide a form of multiple inheritance. A class can extend only one other class.
  • Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
  • A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
  • Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
  • Neither Abstract classes or Interface can be instantiated.

You can find the next set of Core Java Interview Questions from below :


Post a Comment

Related Posts Plugin for WordPress, Blogger...

Copyright © 2012 | ScriptSplash | Powered by Blogger Templates