Java Interview Guide
How to Build Confidence With a Solid Understanding of Core Java Principles
By Anthony DePalma
Copyright 2015 by Anthony DePalma
All rights reserved.
Editors: Ayesha Khan; Jon Hall
eBook formatting/conversion: www.fromprinttoebook.com
Table of Contents
Introduction
The Purpose of this Guide
This guide will prepare you for an interview to an entry-level or a senior-level position as a Java software developer. Its intended to be a high-level resource and its assumed that you are already familiar with the Java language and know how to write basic applications.
The book is divided into chapters of related concepts that are presented in interview-sized chunks. At the end of each chapter is a series of potential interview questions. Read through each chapter and research any topics that are unfamiliar until you are able to answer every question confidently. All of the questions are listed again at the end of the book.
In order to get the most out of this guide, make sure that you are able to answer every question in your own words. From the perspective of an interviewer, its always better to hear a candidate explain something uniquely, because it proves that you understand the underlying concepts. It will also prepare you for variations of the same questions. The stronger you understand the concepts, the more confident youll appear in your interview. Are you ready?
The Java Programming Language
The History of Java
Java is an object-oriented programming language that was released by Sun Microsystems in 1995. Unlike C++, it allowed developers to write code that could run on multiple platforms. This principle, called Write Once, Run Anywhere (WORA), prevented the need for developers to write different applications for different operating systems. Java borrowed much of its syntax from C++, but it streamlined development by providing features such as automatic memory management and by eliminating pitfalls such as multiple inheritance.
The Java Virtual Machine
The WORA principle is possible because of the Java Virtual Machine (JVM). A virtual machine is software that emulates a physical machine. In traditional programming languages, code is compiled into machine language for a specific platform. In Java, code is compiled into a virtual machine language called bytecode. The JVM acts as an intermediary between bytecode and the underlying physical machine.
Every platform that supports Java has its own implementation of the JVM. Java applications are portable because every JVM adheres to a standard interface. The distribution package of the JVM and standard libraries is called the Java Runtime Environment (JRE). The distribution package of the JRE and development tools, such as the compiler and debugger, is called the Java Development Kit (JDK).
Procedural Programming vs Object-Oriented Programming
Procedural programming is a style of writing code that executes a series of linear procedures to produce a result. Object-oriented programming is a style of writing code that uses objects to encapsulate state and behavior. Procedural code is easier to use in small projects or in multithreaded environments due to its stateless nature, but object-oriented code is far more flexible and easier to maintain.
Questions
What is the WORA principle? Why is it beneficial?
How can Java applications run on multiple platforms?
What is the difference between the JRE and the JDK?
What is the difference between procedural programming and object-oriented programming?
Object-Oriented Concepts
Abstraction
Abstraction is the act of perceiving an entity from a narrow perspective. For example, in the context of education a person can be reduced to a student, and in the context of employment a person can be reduced to an employee. Each abstraction reduces the attributes of a person to a subset of relevant information. The goal of abstraction is to reduce complexity in software systems.
Encapsulation
Encapsulation is a technique that encourages abstraction by purposefully hiding information. For example, the mechanical details of a car engine are encapsulated behind a steering wheel and floor pedals. Anyone familiar with this interface could drive a car without knowing what type of engine was under the hood. Java encourages encapsulation through the use of interfaces and by providing access modifiers that limit the visibility of classes, fields, and methods.
Polymorphism
Polymorphism is a technique that encourages abstraction by allowing an entity to assume multiple forms. For example, a smartphone is polymorphic because it can assume the role of a camera, web browser, music player, or digital clock. Each application exposes a relevant interface to the user. In Java, an object can take on the form of any parent in its hierarchy or any interface in its hierarchy.
Mutability
Mutability refers to the ability of an entity to change its state. An iPod is an example of a mutable entity because its contents frequently change. A vinyl record is an example of an immutable entity because its contents are permanently engraved. Immutable objects provide numerous benefits in software applications, such as stability and thread safety. Strings and all of the primitive wrapper objects are examples of immutable objects. In order to make an object immutable, the class must be final , all fields must be final , and it cannot expose any mutable fields or methods that modify mutable fields.
Coupling
Coupling refers to the level of dependency that exists between two entities. For example, a cell phone battery that is soldered to a motherboard is tightly coupled because neither the battery nor the motherboard can be replaced without affecting the other component. A battery that snaps into the back of a phone is loosely coupled because both entities could be replaced independently. In software applications, decoupled components are more flexible and easier to maintain.
Cohesion
Cohesion refers to an entitys level of focus. For example, a Swiss Army knife is a low cohesion entity because it can do multiple things, but it cant do them very well. A multi-tool could never match the productivity of a toolbox filled with highly cohesive tools. In software applications, cohesive components are more robust and easier to test.
Questions
What is the purpose of abstraction in software development?
What is encapsulation? How does Java support it?
What is polymorphism? How does Java support it?
What is the difference between a mutable object and an immutable object?
How can you design an object to be immutable?
What is the difference between coupling and cohesion?
What is the preferred relationship between software components and why?
Object-Oriented Programming (Part I)
Classes & Objects
Java objects are built from a blueprint called a class. A class defines the name of an object, what type of state the object has in the form of fields, and what kind of behavior the object has in the form of methods.
When an object is instantiated with the new keyword, the class file and all of its superclasses are loaded into the JVM by the system class loader. After the one-time initialization of the class, the object is initialized by invoking a special method called a constructor. A constructor performs initialization logic after recursively invoking all of the constructors of its superclasses. Superclasses must be initialized before subclasses in case a child object relies on functionality from a parent.
Next page