
- •Contents at a Glance
- •Contents
- •About the Authors
- •About the Technical Reviewer
- •Acknowledgments
- •Introduction
- •Oracle Java Certifications: Overview
- •FAQ 1. What are the different levels of Oracle Java certification exams?
- •FAQ 4. Is OCPJP 7 prerequisite for other Oracle certification exams?
- •FAQ 5. Should I take the OCPJP 7 or OCPJP 6 exam?
- •The OCPJP 7 Exam
- •FAQ 7. How many questions are there in the OCPJP 7 exam?
- •FAQ 8. What is the duration of the OCPJP 7 exam?
- •FAQ 9. What is the cost of the OCPJP 7 exam?
- •FAQ 10. What are the passing scores for the OCPJP 7 exam?
- •FAQ 11. What kinds of questions are asked in the OCPJP 7 exam?
- •FAQ 12. What does the OCPJP 7 exam test for?
- •FAQ 13. I’ve been a Java programmer for last five years. Do I have to prepare for the OCPJP 7 exam?
- •FAQ 14. How do I prepare for the OCPJP 7 exam?
- •FAQ 15. How do I know when I’m ready to take the OCPJP 7 exam?
- •Taking the OCPJP 7 Exam
- •FAQ 16. What are my options to register for the exam?
- •FAQ 17. How do I register for the exam, schedule a day and time for taking the exam, and appear for the exam?
- •The OCPJP 7 Exam: Pretest
- •Answers with Explanations
- •Post-Pretest Evaluation
- •Essentials of OOP
- •FunPaint Application: An Example
- •Foundations of OOP
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Class Fundamentals
- •Object Creation
- •Constructors
- •Access Modifiers
- •Public Access Modifier
- •Private Access Modifier
- •Protected and Default Access Modifier
- •Overloading
- •Method Overloading
- •Constructor Overloading
- •Overload resolution
- •Points to Remember
- •Inheritance
- •Runtime Polymorphism
- •An Example
- •Overriding Issues
- •Overriding: Deeper Dive
- •Invoking Superclass Methods
- •Type Conversions
- •Upcasts and Downcasts
- •Casting Between Inconvertible Types
- •Using “instanceof” for Safe Downcasts
- •Java Packages
- •Working with Packages
- •Static Import
- •Summary
- •Abstract Classes
- •Points to Remember
- •Using the “final” Keyword
- •Final Classes
- •Final Methods and Variables
- •Points to Remember
- •Using the “static” Keyword
- •Static Block
- •Points to Remember
- •Flavors of Nested Classes
- •Static Nested Classes (or Interfaces)
- •Points to Remember
- •Inner Classes
- •Points to Remember
- •Local Inner Classes
- •Points to Remember
- •Anonymous Inner Classes
- •Points to Remember
- •Enum Data Types
- •Points to Remember
- •Summary
- •Interfaces
- •Declaring and Using Interfaces
- •Points to Remember
- •Abstract Classes vs. Interfaces
- •Choosing Between an Abstract Class and an Interface
- •Object Composition
- •Composition vs. Inheritance
- •Points to Remember
- •Design Patterns
- •The Singleton Design Pattern
- •Ensuring That Your Singleton Is Indeed a Singleton
- •The Factory Design Pattern
- •Differences Between Factory and Abstract Factory Design Patterns
- •The Data Access Object (DAO) Design Pattern
- •Points to Remember
- •Summary
- •Generics
- •Using Object Type and Type Safety
- •Using the Object Class vs. Generics
- •Container Implementation Using the Object Class
- •Container Implementation Using Generics
- •Creating Generic Classes
- •Diamond Syntax
- •Interoperability of Raw Types and Generic Types
- •Generic Methods
- •Generics and Subtyping
- •Wildcard Parameters
- •Limitations of Wildcards
- •Bounded Wildcards
- •Wildcards in the Collections Class
- •Points to Remember
- •The Collections Framework
- •Why Reusable Classes?
- •Basic Components of the Collections Framework
- •Abstract Classes and Interfaces
- •Concrete Classes
- •List Classes
- •ArrayList Class
- •The ListIterator Interface
- •The LinkedList Class
- •The Set Interface
- •The HashSet Class
- •The TreeSet Class
- •The Map Interface
- •The HashMap Class
- •Overriding the hashCode() Method
- •The NavigableMap Interface
- •The Queue Interface
- •The Deque Interface
- •Comparable and Comparator Interfaces
- •Algorithms (Collections Class)
- •The Arrays Class
- •Methods in the Arrays Class
- •Array as a List
- •Points to Remember
- •Summary
- •Generics
- •Collections Framework
- •Processing Strings
- •String Searching
- •The IndexOf() Method
- •The regionMatches() Method
- •String Parsing
- •String Conversions
- •The Split() Method
- •Regular Expressions
- •Understanding regex Symbols
- •Regex Support in Java
- •Searching and Parsing with regex
- •Replacing Strings with regex
- •String Formatting
- •Format Specifiers
- •Points to Remember
- •Summary
- •Reading and Writing from Console
- •Understanding the Console Class
- •Formatted I/O with the Console Class
- •Special Character Handling in the Console Class
- •Using Streams to Read and Write Files
- •Character Streams and Byte Streams
- •Character Streams
- •Reading Text Files
- •Reading and Writing Text Files
- •“Tokenizing” Text
- •Byte Streams
- •Reading a Byte Stream
- •Data Streams
- •Writing to and Reading from Object Streams: Serialization
- •Serialization: Some More Details
- •Points to Remember
- •Summary
- •A Quick History of I/O APIs
- •Using the Path Interface
- •Getting Path Information
- •Comparing Two Paths
- •Using the Files Class
- •Checking File Properties and Metadata
- •Copying a File
- •Moving a File
- •Deleting a File
- •Walking a File Tree
- •Revisiting File Copy
- •Finding a File
- •Watching a Directory for Changes
- •Points to Remember
- •Summary
- •Introduction to JDBC
- •The Architecture of JDBC
- •Two-Tier and Three-Tier JDBC Architecture
- •Types of JDBC Drivers
- •Setting Up the Database
- •Connecting to a Database Using a JDBC Driver
- •The Connection Interface
- •Connecting to the Database
- •Statement
- •ResultSet
- •Querying the Database
- •Updating the Database
- •Getting the Database Metadata
- •Points to Remember
- •Querying and Updating the Database
- •Performing Transactions
- •Rolling Back Database Operations
- •The RowSet Interface
- •Points to Remember
- •Summary
- •Define the Layout of the JDBC API
- •Connect to a Database by Using a JDBC driver
- •Update and Query a Database
- •Customize the Transaction Behavior of JDBC and Commit Transactions
- •Use the JDBC 4.1 RowSetProvider, RowSetFactory, and RowSet Interfaces
- •Introduction to Exception Handling
- •Throwing Exceptions
- •Unhandled Exceptions
- •Try and Catch Statements
- •Programmatically Accessing the Stack Trace
- •Multiple Catch Blocks
- •Multi-Catch Blocks
- •General Catch Handlers
- •Finally Blocks
- •Points to Remember
- •Try-with-Resources
- •Closing Multiple Resources
- •Points to Remember
- •Exception Types
- •The Exception Class
- •The RuntimeException Class
- •The Error Class
- •The Throws Clause
- •Method Overriding and the Throws Clause
- •Points to Remember
- •Custom Exceptions
- •Assertions
- •Assert Statement
- •How Not to Use Asserts
- •Summary
- •Introduction
- •Locales
- •The Locale Class
- •Getting Locale Details
- •Resource Bundles
- •Using PropertyResourceBundle
- •Using ListResourceBundle
- •Loading a Resource Bundle
- •Naming Convention for Resource Bundles
- •Formatting for Local Culture
- •The NumberFormat Class
- •The Currency Class
- •The DateFormat Class
- •The SimpleDateFormat Class
- •Points to Remember
- •Summary
- •Introduction to Concurrent Programming
- •Important Threading-Related Methods
- •Creating Threads
- •Extending the Thread Class
- •Implementing the Runnable Interface
- •The Start( ) and Run( ) Methods
- •Thread Name, Priority, and Group
- •Using the Thread.sleep() Method
- •Using Thread’s Join Method
- •Asynchronous Execution
- •The States of a Thread
- •Two States in “Runnable” State
- •Concurrent Access Problems
- •Data Races
- •Thread Synchronization
- •Synchronized Blocks
- •Synchronized Methods
- •Synchronized Blocks vs. Synchronized Methods
- •Deadlocks
- •Other Threading Problems
- •Livelocks
- •Lock Starvation
- •The Wait/Notify Mechanism
- •Let’s Solve a Problem
- •More Thread States
- •timed_waiting and blocked States
- •waiting State
- •Using Thread.State enum
- •Understanding IllegalThreadStateException
- •Summary
- •Using java.util.concurrent Collections
- •Semaphore
- •CountDownLatch
- •Exchanger
- •CyclicBarrier
- •Phaser
- •Concurrent Collections
- •Apply Atomic Variables and Locks
- •Atomic Variables
- •Locks
- •Conditions
- •Multiple Conditions on a Lock
- •Use Executors and ThreadPools
- •Executor
- •Callable, Executors, ExecutorService, ThreadPool, and Future
- •ThreadFactory
- •The ThreadLocalRandom Class
- •TimeUnit Enumeration
- •Use the Parallel Fork/Join Framework
- •Useful Classes of the Fork/Join Framework
- •Using the Fork/Join Framework
- •Points to Remember
- •Summary
- •Using java.util.concurrent Collections
- •Applying Atomic Variables and Locks
- •Using Executors and ThreadPools
- •Using the Parallel Fork/Join Framework
- •Chapter 3: Java Class Design
- •Chapter 4: Advanced Class Design
- •Chapter 5: Object-Oriented Design Principles
- •Chapter 6: Generics and Collections
- •Chapter 7: String Processing
- •Chapter 8: Java I/O Fundamentals
- •Chapter 9: Java File I/O (NIO.2)
- •Chapter 10: Building Database Applications with JDBC
- •Chapter 11: Exceptions and Assertions
- •Chapter 12: Localization
- •Chapter 13: Threads
- •Chapter 14: Concurrency
- •OCPJP7 Exam (1Z0-804 a.k.a. Java SE 7 Programmer II) Topics
- •OCPJP 7 Exam (1Z0-805, a.k.a. Upgrade to Java SE 7 Programmer) Topics
- •Answers and Explanations
- •Answer Sheet
- •Answers and Explanations
- •Index
Chapter 1 ■The OCPJP 7 Exam: FAQ
•Oracle’s free online Java tutorials: Access the Java tutorial at http://docs.oracle.com/javase/tutorial/ and the OCPJP 7 tutorial at http://docs. oracle.com/javase/tutorial/extra/certification/javase-7-programmer2.html.
•Java documentation: The Java API documentation is a mine of information. This documentation is available online (see http://docs.oracle.com/javase/7/docs/api/) and is shipped as part of the Java SDK. If you don’t have immediate Internet access, you may find javac’s -Xprint option handy. To print the textual representation of String class, type the fully qualified name, as in
javac -Xprint java.lang.String
This will print the list of members in String class in console.
•Read, code, read, code! Cycle back and forth between your reading and coding so that your book knowledge and its practical application are mutually reinforcing. This way, you’ll not just know a concept, but you’ll also understand it.
•Focus most on the topics you’re least comfortable with. Grade yourself on each of the topics in OCPJP 7 exam on an ascending scale from 1 to 10. Do remedial preparation in all topics for which you rate yourself 8 or less.
FAQ 15. How do I know when I’m ready to take the OCPJP 7 exam?
Take the two full-length OCPJP 7 sample exams given in Appendix B under actual exam conditions: stick to the 2.5-hour time limit; don’t take any breaks; and don’t refer any books or web sites. If you score 75% or above (the actual exam pass scores for 1Z0-804 and 1Z0-805 are 65% and 60%, respectively), you’ll probably pass the actual exam.
Taking the OCPJP 7 Exam
FAQ 16. What are my options to register for the exam?
You have three registration options for the OCPJP 7 exam:
•Register and pay at the Pearson VUE web site.
•Buy an exam voucher from Oracle and then register yourself in Pearson VUE web site.
•Register and pay at the Oracle Testing Center (OTC), if you have one in your region.
FAQ 17. How do I register for the exam, schedule a day and time for taking the exam, and appear for the exam?
Option 1: Register and pay on the Pearson VUE web site by the following steps:
Step 1. Go to www.pearsonvue.com/oracle/ (you will be directed here if you click the first option from Oracle Certification page). Click on “Schedule online” in “Schedule an exam” section.
Step 2. Select “Sign In.” Click on “proctored” in the “what type of exam you are planning to take” section. Select this exam as "Information Technology (IT)" "Oracle" "Proctored." Then you’ll be asked to sign in.
8
Chapter 1 ■The OCPJP 7 Exam: FAQ
Step 3. Log in to your web account on the Pearson site. If you don’t have one, create one; you will get the user name and password by the e-mail you provide. When you log in first time, you need to change your password and set security questions and their answers. When you are done with this, you’re ready to schedule your exam.
Step 4. Once logged in, you’ll get the list of Oracle exams to select from. Select one of the following:
•1Z0-803, Java SE 7 Programmer I (aka OCAJP 7 exam)
•1Z0-804, Java SE 7 Programmer II (aka OCPJP 7 exam)
•1Z0-805, Upgrade to Java SE 7 Programmer (aka OCPJP 7 exam)
These exams are in English. (You can choose another language if you wish and if it is available in the list). This page will also show you the cost of the exam. Select
relevant exam from the list, say, “1Z0-804 (Java SE 7 Programmer II),” and click Next.
Step 5. Now you need to select your test location. Choose Country City State/ Province, and you’ll be shown test locations close to your place. Each center will have an icon for information: click it for address and directions. Select up to four centers near to your location and click Next.
Step 6. Select a test center and select date and time for appointments. The page will indicate the available dates and time slots; choose the one most convenient for you. If you have an exam voucher or Oracle University coupon or Oracle promotion code, enter it here.
Step 7. Select from the available payment options (the usual way is to pay using your credit card) and pay your exam fees. Make sure that you have selected the right exam, appropriate test center, and date/time before paying the fees.
Step 8. Done! You will get an appointment confirmation payment receipt by e-mail.
Option 2: Buy an exam voucher from Oracle and register on the Pearson VUE web site.
You can buy a generic exam voucher from Oracle and use it at Pearson site. It costs US$300 if you are living in US and is denominated in an appropriate currency if you live elsewhere. To buy the voucher from Oracle, select “OU Java, Solaris, and other Sun Technology Exam eVoucher.” You will be asked to create an Oracle account if you do not have one. Once the account is created, confirm customer type, customer contact information, and pay. Once you pay the fees, you can use the eVoucher at the Pearson VUE site.
Option 3: Register and pay online to take the exam in person at an Oracle Testing Center (OTC).
You can choose this option if a physical exam session is scheduled in your vicinity. It costs US$300 or the local equivalent.
FAQ 18. What are the key things I need to remember before taking the exam and on the day of exam?
Before the exam day:
•You’ll get an e-mail from Pearson confirming your appointment and payment. Check the details on what you should bring when you go to the exam center. Note that you’ll need at least two photo IDs.
•Before the exam, you’ll get a call from the Pearson exam center where you’ve booked your appointment (albeit, it depends on the exam center).
9
Chapter 1 ■The OCPJP 7 Exam: FAQ
On the exam day:
•Go to the exam center at least 30 minutes before the exam starts. Your exam center will have lockers for storing your belongings.
•Show your exam schedule information and IDs and then complete the exam formalities, such as signing the documents.
•You’ll be taken to a computer in the exam room and will log in to the exam-taking software.
Taking the exam:
•You will see the following on the exam-taking software screen:
•A timer ticking in one corner showing the time left
•The current question number you are attempting
•A check box to select if you want to review the question later
•The button (labeled “Review”) for going to a review screen where you can revisit the questions before completing the exam.
•Once you start, you’ll get questions displayed one by one. You can choose the answers by selecting them in the check box. If you are unsure of an answer, select the Review button so that you can revisit it at any point during the exam.
•You may not consult any person or print or electronic materials or programs during the exam.
After the exam:
•Once you’re done with the exam, you will get an email after half-an-hour or so containing the details for accessing your score.
•Irrespective of passing or failing the exam, topics from questions you’ve answered incorrectly will be supplied with your score. You will not, however, be shown the correct answers to the questions you missed.
•If you’ve passed the OCPJP 7 exam and you’ve also satisfied the applicable prerequisites for certification (e.g., OCAJP certification as the prerequisite of OCPJP 7 certification via the 1Z0-804 exam), a printable certificate can be downloaded from Oracle’s CertView web site (https://education.oracle.com/certview.html).
•If you failed the exam, you may register and pay again to retake it after a 14-day waiting period.
10

Chapter 2
Pretest
The prospect of taking the OCPJP 7 exam raises many questions in the candidate’s mind.
•“What types of questions are asked in the exam?”
•“What topics do the exams cover?”
•“How hard are the questions?”
•“How do I know if I’m ready to take the exam?”
•“Which topics will I need to focus my preparations on in order to pass the exam?”
This chapter presents a pretest designed to answer all of your preliminary questions concretely and measurably. Use this pretest as a mental dip-stick to gauge how likely you would be to pass the OCPJP 7 exam if you were to take it today. The questions in this pretest closely mimic the actual questions you will encounter on the OCPJP 7 exam. And they are distributed among the 12 topics in the 1Z0-804 exam syllabus. The post-pretest answer key and evaluation tool at the end of this chapter will enable you to identify precisely those topics on which you will need to focus your preparations to ensure success when you take the actual exam.
The only significant difference between the following pretest and the OCPJP 7 exam is the number of questions and duration. This pretest is configured exactly like an OCPJP 7 exam, only half as long: you will answer 45 questions in 1 hour 15 minutes (rather than 90 questions in 2 hrs 30 minutes).
So let’s get started. Simulate real test conditions. Find a quiet place where you can take this pretest without interruption or distraction. Mark your start and finish times. Observe closed-book rules: do not consult the answer key or any other any print, human, or web resources before or during this pretest.
11