www.it-ebooks.info www.it-ebooks.info ® Java Programming Interviews Exposed Noel Markham www.it-ebooks.info 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 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 promotional 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 publisher 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 www.it-ebooks.info For my wife and best friend, Rebecca www.it-ebooks.info 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 graduates 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 Technical 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, telecoms, and consultancy companies in several countries Ivar is currently a passionate and opinionated 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 current 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 www.it-ebooks.info Credits Executive Editor Business Manager Robert Elliott Amy Knies Project Editor Vice President and Executive Group Publisher Ed Connor Richard Swadley Technical Editors Ivar Abrahamsen Rheese Burgess Associate Publisher Senior Production Editor Project Coordinator, Cover Kathleen Wisor Todd Klemme Copy Editor Compositor Kim Cofer Maureen Forys, Happenstance Type-O-Rama Jim Minatel 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 www.it-ebooks.info 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 www.it-ebooks.info Contents Introduction xi Part I: Navigating the Interview Process Chapter 1: Dissecting Interview Types Looking at the Phone Screening Process Reviewing Technical Tests Handling Face-to-Face Interviews Making the Decision 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 www.it-ebooks.info 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 The Primitive Types Using Objects Java’s Arrays Working with Strings Understanding Generics Autoboxing and Unboxing Using Annotations Naming Conventions 87 88 91 98 98 101 107 109 111 Classes 111 Variables and Methods 111 Constants 111 Handling Exceptions 112 Using the Standard Java Library 115 Looking Forward to Java 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 www.it-ebooks.info ...www.it-ebooks.info ® Java Programming Interviews Exposed Noel Markham www.it-ebooks.info Java Programming Interviews Exposed Published by John Wiley & Sons, Inc 10475... preparation for finding your next job as a Java developer It has been written to help you overcome any sense of fear or worry around Java programming interviews by giving you enough material to... experienced candidate for a Java developer position Chapter 8: Java Basics This chapter covers many of the language features of Java, and can be treated as a recap for any experienced Java developer Chapter