Table Of ContentJava® Programming
Interviews Exposed
Noel Markham
Java® Programming Interviews Exposed
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2014 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-72286-2
ISBN: 978-1-118-72292-3 (ebk)
ISBN: 978-1-118-72288-6 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or
authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood
Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should
be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030,
(201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or pro-
motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold
with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services.
If professional assistance is required, the services of a competent professional person should be sought. Neither the pub-
lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to
in this work as a citation and/or a potential source of further information does not mean that the author or the publisher
endorses the information the organization or website may provide or recommendations it may make. Further, readers
should be aware that Internet websites listed in this work may have changed or disappeared between when this work was
written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard
print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a
CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport
.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2013958289
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its
affiliates, in the United States and other countries, and may not be used without written permission. Java is a registered
trademark of Oracle America, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons,
Inc. is not associated with any product or vendor mentioned in this book.
For my wife and best friend, Rebecca.
About thE Author
NoEl MArkhAM is a developer with almost 15 years’ experience using Java across financial, tech-
nology, and gaming industries. Most recently, he has been working for startups in social gaming and
digital entertainment. He has hosted interviews for all experienced levels of developer, from gradu-
ates to technical leaders, and has set up assessment centers in the UK and overseas to set up full
development teams.
About thE CoNtrIbutINg Author
grEg MIlEttE is a programmer, author, consultant and entrepreneur who loves implementing great
ideas. He’s been developing Android apps since 2009 when he released a voice controlled recipe app
called Digital Recipe Sidekick. In between chatting with his Android device in the kitchen, Greg
co-authored Professional Android Sensor Programming in 2012. Currently, he is the founder of
Gradison Technologies, Inc, where he continues to develop great apps.
About thE tEChNICA l EdItors
IvAr AbrAhAMsEN is a Software Engineer/Architect with over 15 years of experience working
mostly with Java and more recently with Scala. He has worked for small and very large banks, tele-
coms, and consultancy companies in several countries. Ivar is currently a passionate and opinion-
ated Tech Lead at a startup in Shoreditch, London. For more see flurdy.com.
rhEEsE burgEss is a Technical Lead, working closely with recruitment to fill positions in his cur-
rent workplace. He has worked with a number of languages and technologies, and has experience
working in social, gaming, and financial products, ranging from startups to investment banks.
CrEdIts
ExECutIvE EdItor busINEss MANAgEr
Robert Elliott Amy Knies
ProJECt EdItor vICE PrEsIdENt ANd ExECutIvE grouP
Ed Connor PublIshEr
Richard Swadley
tEChNICAl EdItors
Ivar Abrahamsen AssoCIAtE PublIshEr
Rheese Burgess Jim Minatel
sENIor ProduCtIoN EdItor ProJECt CoordINAtor, CovEr
Kathleen Wisor Todd Klemme
CoPy EdItor CoMPosItor
Kim Cofer Maureen Forys,
Happenstance Type-O-Rama
EdItorIAl MANAgEr
Mary Beth Wakefield ProoFrEAdEr
Nancy Carrasco
FrEElANCEr EdItorIAl MANAgEr
Rosemarie Graham INdExEr
Robert Swanson
AssoCIAtE dIrECtor oF MArkEtINg
David Mayhew CovEr IMAgE
©iStockphoto.com/RapidEye
MArkEtINg MANAgEr
Ashley Zurcher CovEr dEsIgNEr
Wiley
ACkNowlEdgMENts
thANk you to James Summerfield for putting me in touch with Wiley to make this book happen.
The team at Wiley has been so encouraging and supportive all through the writing process: Thank
you to editors Ed Connor and Robert Elliott, and to the many others behind the scenes.
Thank you to Greg Milette for writing the Android chapter.
Thanks to my colleagues Rheese Burgess and Ivar Abrahamsen for taking the time to edit and
review the book.
Thank you to many of my other colleagues at Mind Candy, who have helped in some way: Sean
Parsons, Olivia Goodman, Amanda Cowie, and to my good friend, Luca Cannas.
My family: I thank my parents for their unending help and support with everything I’ve ever done.
Finally, my wife, Rebecca: Thank you for putting up with seeing so little of me for these last few
months, and for bringing me all of those cups of tea and other little treats, always smiling. Quite
simply, it wouldn’t have happened without you.
CoNtENts
IntroductIon xi
PArt I: NAvIgAtINg thE INtErvIEw ProCEss
ChAPtEr 1: dIssECtINg INtErvIEw tyPEs 3
Looking at the Phone Screening Process 4
Reviewing Technical Tests 5
Handling Face-to-Face Interviews 7
Making the Decision 8
Summary 9
ChAPtEr 2: wrItINg A NotICEAblE rEsuME 11
How to Write a Resume and Cover Letter 11
Writing a Cover Letter 14
Summary 15
ChAPtEr 3: tEChNICAl tEst ANd INtErvIEw bAsICs 17
Technical Written Tests 17
At-Computer Tests 18
Face-to-Face Technical Interviews 19
Summary 21
ChAPtEr 4: wrItINg CorE AlgorIthMs 23
Looking at Big O Notation 23
Sorting Lists 24
Searching Lists 32
Summary 33
ChAPtEr 5: dAtA struCturEs 35
Lists 35
The Relationship between Arrays and Lists 36
Trees 39
Maps 45
Sets 48
Summary 49
CONTENTS
ChAPtEr 6: dEsIgN PAttErNs 51
Investigating Example Patterns 51
Commonly Used Patterns 60
Summary 64
ChAPtEr 7: IMPlEMENtINg PoPulAr INtErvIEw AlgorIthMs 65
Implementing FizzBuzz 65
Demonstrating the Fibonacci Sequence 67
Demonstrating Factorials 71
Implementing Library Functionality 72
Using Generics 80
Summary 83
PArt II: CorE JAvA
ChAPtEr 8: JAvA bAsICs 87
The Primitive Types 88
Using Objects 91
Java’s Arrays 98
Working with Strings 98
Understanding Generics 101
Autoboxing and Unboxing 107
Using Annotations 109
Naming Conventions 111
Classes 111
Variables and Methods 111
Constants 111
Handling Exceptions 112
Using the Standard Java Library 115
Looking Forward to Java 8 119
Summary 120
ChAPtEr 9: tEstINg wIth JuNIt 123
The JUnit Test Life Cycle 125
Best Practices for Using JUnit 127
Eliminating Dependencies with Mocks 138
Creating System Tests with Behavior-Driven Development 143
Summary 146
viii