Download Review Copy of OCAJP 8 Associate Java Programmer Certification Fundamentals book written by Hanumant Deshmukh. OCAJP Java 8 Programmer. Java Certification question Hi, I was looking at Hanumant Deshmukh book s exercises sections and noticed the there are no answer sections. This week, we're delighted to have Hanumant Deshmukh helping to answer questions about the new book OCA Java Programmer 8.
|Language:||English, Spanish, Hindi|
|Genre:||Fiction & Literature|
|ePub File Size:||30.39 MB|
|PDF File Size:||17.22 MB|
|Distribution:||Free* [*Sign up for free]|
About Hanumant Deshmukh. Hanumant Deshmukh has over 15 years of professional Java experience as an architect, Books By Hanumant Deshmukh . Hanumant Deshmukh is the author of OCAJP Oracle Certified Associate Java SE 7 Programmer Practice Exams ( avg rating, Hanumant Deshmukh's books SCWCD Exam Study Kit: Java Web Component Developer Certification by. Looking for books by Hanumant Deshmukh? See all books authored by Hanumant Deshmukh, including SCWCD Exam Study Kit: Java Web Component .
Goodreads helps you keep track of books you want to read. Want to Read saving…. Want to Read Currently Reading Read. Other editions. Enlarge cover.
For 1. As per the posts in this site, it will be available in the market during the last week of August. Book Review Team. Bartender Posts: It's easy to recognize, there's no CD bundled with it.
Hanumant Deshmukh is a senior technical architect at Planet Pro, Inc. Limited time offer while we load Click here - for a direct dowlnoad! Like us while we load stuff for you! Hanumant Deshmukh, Jignesh Malavia, Find all the books, read about the author, and more. This is called overriding and not overloading. A reference of type I can be cast to any class at compile time. Since B is-a A, it can be assigned to a.
Therefore, it will not compile. But it will fail at runtime because a does not point to an object of class C. So it should be: However, Integer cannot be passed to a method that expects a List. Therefore, this option is not valid. Had the method bean saveObject Object obj , it would have been valid because an Integer is an Object.
This is valid because every object in Java is an Object. Since Runnable is an interface, it cannot be instantiated like this. But you can do: A constructor or any other method can access static members. So are instance fields. The code will compile and run without any problem.
All the instance or static variables are given a default values if not explicitly initialized. All numeric variable are given a value of zero or equivalent to zero i.
Make 'id' private. This will not allow others to read or write. Make 'id' private and provide a public method getId which will return its value. This is a standard way of providing read only access to internal variables.
Integer 63 will be unboxed and added to Therefore, the final value will be int However, due to auto-unboxing, it will generate an int value of Division by 0 throws a java. ArithmeticException, which is a RuntimeException. This is caught by the first catch clause because it is the first block that can handle ArithmeticException.
This prints 1. Theoretically, java supports Pass by Value for everything i. Primitives are always passed by value. Object "references" are passed by value. So it looks like the object is passed by reference but actually it is the value of the reference that is passed. An example: Now, when you call someMethod o1 ; the value is passed to the method. Inside the method someMethod: Therefore, when you call a method on localVar, it will be executed on the same object. But the original variable o1 still contains so it still points to the same object.
This is what happens in the this question. In the method changeIt If you need even more detailed explanation, please check http: It can be chained. The code will not compile as 'j' is being used before getting initialized. The code will compile correctly and will display '9' when run. Every expression has a value, in this case the value of the expression is the value that is assigned to the Right Hand Side of the equation. Another implication of this is: The above code is valid and will print TRUE.
Therefore, explicit cast is not required here. It is will still work fine with the explicit cast though. Although o refers to an object which is Runnable but the compiler doesn't know about it.
You have to do: When the program is run without any arguments, args gets assigned a string array of size 0. Thus, the first call becomes: Follow through the code like this: Method m1 uses pre-increment operation.
Therefore, first i is incremented and then the new value of i is returned. Thus, k gets the value of 1. But the value of Right Hand Side has not yet been assigned to k. Therefore, the final value of k is 6.
Operates only on integral types. Look at it like this: Note that if and else do not cascade. They are like opening an closing brackets. So it will also throw a NullPointerException.
The concept is: Let's break this down in two cases: Had it been instead of , the second part would not have been evaluated and no exception would have been thrown. No exception is thrown. Note that second part will still be evaluated although by looking at the first part itself we can tell that the whole expression will return true. No exception is thrown as str. It would be nice if you try to run the following program to understand the concept: Uncomment only one of the commented lines one by one.
Code 3 is invalid because a switch statement must have a body. The body may even be empty as shown in Code 4. All are valid. So private is fine.
No access modifier. You are creating an array of length Since array numbering starts with 0, the last element would be array. The exception is thus thrown out of the main method and is handled by the JVM's uncaught exception handling mechanism, which prints the stack trace.
Error in thread "main" java. RuntimeException, which in turn extends java. Exception from doAnotherTest. Note that there are questions in the exam that test your knowledge about how exception messages are printed.
When you use System.
Just the name of the exception class and the message is printed. When you use exception. In case of overriding, the return type of the overriding method must match exactly to the return type of the overridden method if the return type is a primitive. In case of objects, the return type of the overriding method may be a subclass of the return type of the overridden method.
When you create an array of Objects here, Strings all the elements are initialized to null. So in the line 3, null is assigned to myStr. Note that. By the way, System. This will make the object eligible for GC because there are no other references to it. Although not mentioned explicitly in the objectives, the exam has a few basic questions on garbage collection. All you need to know is: An object can be made eligible for garbage collection by making sure there are no references pointing to that object.
You cannot directly invoke the garbage collector. You can suggest the JVM to perform garbage collection by calling System. You can at most make an object eligible for GC by making sure that there are no references to it. The line 1 will be allowed during compilation, since assignment is done from a subclass reference to a superclass reference.
The cast in line 2 is needed because a superclass reference is assigned to a subclass reference variable. And this works at runtime because the object referenced to by a is actually of an array of B. Now, the cast at line 3 tells the compiler not to worry, that I'm a good programmer and I know what I am doing and the object referenced by the super class reference a1 will actually be of class B at run time.
So there is no compile time error. But at run time, this fails because the actual object is not an array of B but is an array of A.
A do-while loop is always executed at least once. So in the first iteration, x is decremented and becomes 9. Now the while condition is tested, which returns true because 9 is less than In the loop, x is decremented to 8 and the condition is tested again, which again returns true because 8 is less than A class, in which all the members are declared private, cannot be declared public.
There is no such rule. The extends clause is used to specify that a class extends another class and thereby inherits all non-private instance members of that class. A subclass can be declared abstract regardless of whether the superclass was declared abstract. A class cannot be declared abstract and final at the same time.
This restriction makes sense because abstract classes need to be subclassed to be useful and final forbids subclasses. The visibility of the class is not limited by the visibility of its members.
A class with all the members declared private can still be declared public or a class having all public members may be declared private.
You can only import one package i. Since the given class does not have any package declaration, it belongs to the default package and therefore it must import com. Helper and com. Stock classes. The code will not compile because RuntimeExceptions cannot be given in throws clause.
Any Exception can be specified in the throws clause. There is no problem with the code. Overriding method only needs to specify a subset of the list of exception classes the overridden method can throw.
A set of no classes is a valid subset of that list. Compiler will complain that the method intValue is not available in Object. This is because the.
So you have to write it like this: This means the resulting value is truncated and not rounded. Therefore, the above statement will print 11 and not The given expression will not be able to distinguish between an object of class A and an object of class B.
It will return true in both the cases. Also, The last part! This will return true only if obj points to an object of class B and not of A, C, or D. Note that it extends java. Error and Errors are always thrown by the JVM. A programmer should never throw an Error explicitly.
These are usually thrown implicitly. A programmer should not throw these exceptions explicitly. Exception and its subclasses except RuntimeException should be used by the programmer to reflect known exceptional situations, while RuntimeExceptions are used to reflect unforseen or unrecoverable exceptional.
There is no hard and fast rule that says RuntimeExceptions such as the ones mentioned in this questions must not be thrown explicitly.
It is ok to throw these exceptions explicitly in certain situations. But for the purpose of the exam, it is a good way to determine if a given application should be thrown expliclity by the programmer or not.
The following types can be used as a switch variable: All the case constants should be assignable to the switch variable type. Because the range of a byte is from to and so is not assignable to 'x'. Although it is not required for the exam to know the integral values of characters, it is good to know that all English letters upper case as well as lower case as well as are below and so are assignable to byte.
So Circle implements shape is wrong. But it is not wrong to do so. Since MyComparable class specifies that it implements the Comparable interface that has been typed to MyClass, it must implement compareTo method that takes a MyClass. Had it not declared a typed Comparable in its implements clause, compareTo Object. To answer this type of questions, you need to know the following rules: The compiler always tries to choose the most specific method available with least number of modifications to the arguments.
Java designers have decided that old code should work exactly as it used to work before boxing-unboxing functionality became available.
Thus, 1. It is never bound to probe Long because Integer and Long are different object types and there is no IS-A relation between them. This holds true for any two wrapper classes. It is never bound to probe Long because int is not compatible with Long.
We advise you to run this program and try out various combinations. The exam has questions on this pattern but they are not this tough. If you have a basic understanding, you should be ok. Hence, the compiler will not complain. Although if a does not point to an object of class AA at run time, a ClassCastException will be thrown. Of course, if a does not point to an object of class AA at runtime, a ClassCastException will be thrown.
You can have multiple catch blocks to catch different kinds of exceptions, including exceptions that are subclasses of other exceptions. However, the catch clause for more specific exceptions i. Failure to do so results in a compiler error as the more specific exception is unreachable.
In this case, catch for MyException3 cannot follow catch for MyException because if MyException3 is thrown, it will be caught by the catch clause for MyException. And so, there is no way the catch clause for MyException3 can ever execute.
And so it becomes an "unreachable" statement. Private methods cannot be overridden in subclasses. Only methods that are inherited can be overridden and private methods are not inherited. A subclass can override any method in a non-final superclass. Only the methods that are not declared to be final can be overridden. Further, private methods are not inherited so they cannot be overridden either. An overriding method the method that is trying to override the base classs method must have the same parameters.
No exception i. A method can be overridden by defining a method with the same signature i. The return type can also be a subclass of the orginal method's return type.
Only methods that are accessible can be overridden. A private method cannot, therefore, be overridden in subclasses, but the subclasses are allowed to define a new method with exactly the same signature.
A final method cannot be overridden. An overriding method cannot exhibit behavior that contradicts the declaration of the original method. An overriding method therefore cannot return a different type except a subtype or throw a wider spectrum of exceptions than the original method in the superclass. A subclass may have a static method with the same signature as a static method in the base class but it is not called overriding.
It is called shadowing because the concept of polymorphism doesn't apply to static members. Therefore, the loop will only execute twice, printing 2 and 1.
In this case, the loop would execute thrice, printing 2, 1, and 0. Package import does not use static keyword. Had it been just System. Syntax for importing static fields is: They create a new String object. That's because it is final, not because it is immutable. You can have a final class whose objects are mutable. String class implements Comparable interface. Subclasses must define all the abstract methods that the superclass defines.
Not if the subclass is also defined abstract! A class implementing an interface must define all the methods of that interface. Not if the class is defined abstract. A class cannot override the super class's constructor. Because constructors are not inherited.
It is possible for two classes to be the superclass of each other. An interface can implement multiple interfaces. Interface cannot "implement" anything. It can extend multiple interfaces. The following is a valid declaration: It will throw NullPointerBoundsException. There is no such exception. So it will print null. String String is a class in java. The package with no name.
If there is no package statement in the source file, the class is assumed to be created in a default package that has no name. Not even void. But not static, final, synchronized, native and abstract. It is not a constructor, but compiler considers it as a valid method!
Unlike a while loop, where the condition is checked before the execution of the block. It allows this construct to support optimizations through the conditional compilation.
For example,. Here, the DEBUG variable can be set to false in the code while generating the production version of the class file, which will allow the compiler to optimize the code by removing the whole if statement entirely from the class file.
But it becomes a valid method if you give a return type. The class has an implicit default constructor since the class doesn't have any constructor defined. A compilation error will be encountered at 3. Because 1 is a method and not a constructor. So there is no constructor that take a parameter. The program will compile correctly. If 1 was a valid constructor 'int' would be promoted to long at the time of passing.
The declaration at 1 declares a method, not a constructor because it has a return value. The method happens to have the same name as the class, but that is ok.
The class has an implicit default constructor since the class contains no constructor declarations. This allows the instantiation at 2 to work. Observe that play in Game declares Exception in its throws clause. Further, class Soccer overrides the play method without any throws clause. This is valid because a list of no exception is a valid subset of a list of exceptions thrown by the superclass method. Now, even though the actual object referred to by 'g' is of class Soccer, the class of the variable g is of class Game.
Therefore, at compile time, compiler assumes that g. The while condition uses post increment operator, which means count is first compared with 11 and based on this comparison a decision is made whether to execute the loop again or not and then incremented. When count is completely divisible by 3, i. Thus, the result is the summation of: Encapsulation makes sure that messages are sent to the right object at run time.
This is dynamic binding, an outcome of polymorphism. Encapsulation helps you inherit the properties of another class. Encapsulation is the technique used to package the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.
In simple terms, encapsulation generally means making the data variables private and providing public accessors. Note that any class name can be used as a variable name. It will compile but will throw an exception at runtime. It will print 3. The loop body is executed twice and the program will print 3. It will go in an infinite loop. At the beginning, flag is true. Thus, flag becomes false and the condition becomes false therefore 1 is not printed.
In the first 'else if', again since flag is false, 2 is not printed. In second 'else if',! Finally, since an else-if condition has been satisfied, the last else is not executed. Since A doesn't have any no-args constructor, the above code will fail to compile. Notice that class A does not define a no-argument constructor. Also note that the class B does not define a constructor. Thus, class B relies on the default constructor B. Class B's default constructor looks like this: So, class B's default constructor actually looks like this: Now, since class A does not define a no-argument constructor the above code will not compile.
However, class B would be correct if changed to: You could also add a no-argument constructor to class A and leave class B as is. An Exception is thrown in method m1 so println "A" will not be executed. As there is no catch block the exception will not be handled and the main method will propagate the exception.
So println "C" ; will also not be executed. Here, m1 is overloading for three different argument types. So when you call ot. So, c is not incremented at all. Hence c remains 'a'.
What is wrong with the following code? What is the result of executing the following fragment of code: You do not have to worry about the size of the ArrayList while inserting elements. Given the following code: Consider the following code in TestClass. Any class that extends java. Exception except any class that extends java. Which of the following lines can be added independently to the above class so that it will run without any errors or exceptions?
The code will fail to compile because the syntax of the if statement is not correct. What will be the output of the following code snippet? Given the following class, which of the given blocks can be inserted at line 1 without errors? An abstract class can be extended by an abstract or a concrete class. You want to invoke the overridden method the method in the base class from the overriding method the method in the derived class named m.
Which of the following constructs which will let you do that? At least one of the constructors of each class is called as a result of constructing an object of class C. Consider the following lines of code: Which line s in the following code will cause a compilation error? Consider that you are writing a set of classes related to a new Data Transmission Protocol and have created your own exception hierarchy derived from java.
Exception as follows: DataFloodingException, enthu. You have a TransSocket class that has the following method: Now, you also want to write another "AdvancedTransSocket" class, derived from "TransSocket" which overrides the above mentioned method. Which of the following are valid declaration of the overriding method?
Less restrictive than protected from within a package, and more restrictive than protected from outside a package. Which of the changes given in options can be done independent of each other to let the following code compile and run without errors? Which of the following correctly defines a method named stringProcessor that can be called by other programmers as follows: Assume that a, b, and c refer to instances of primitive wrapper classes.
Which of the following statements are correct? Although primitive wrapper classes are not explicitly mentioned in the exam objectives, we have seen some candidates get questions on this aspect of wrapper classes. It is provided by the compiler only if the class and any of its super classes does not define any constructor. Consider the following program: Consider the following class hierarchy shown in the image.
So, objectOfC1 means a reference variable that is pointing to an object of class C1. The following code snippet will print 'true'. What letters will be printed by this program? What will the following program print when run using the command line: In the following code, after which statement earliest , the object originally held in s, may be garbage collected? What will the following program print when run? After some time, the requirements changed and the programmer now wants to make sure that radiusB is always - radiusA instead of - radiusA without breaking existing code that other people have written.
Which of the following will accomplish his goal? What will happen when the following program is compiled and run? What will it print? Assume that Math. What will be result of attempting to compile this class? The class will fail to compile, since the class OtherClass is used before it is defined.
The class will fail to compile, since the class OtherClass must be defined in a file called OtherClass. If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException. The following code snippet will not compile: Consider the following hierarchy of Exception classes: The method calling this method will either have to catch these 2 exceptions or declare them in its throws clause.
What will be written to the standard output when the following program is run? What will be the output of the following program excluding the quotes?
A java source file contains the following code: Given the following code, which method declarations can be inserted at line 1 without any problems? Consider the following code snippet Consider the following two classes in the same package but defined in different source files: Add a calculateArea method: Change setSide method to: The code will fail to compile, since the instance initializer tries to assign a value to a static member.
The code will fail to compile, since the member variable x will be uninitialized when it is used. What happens when you try to compile and run the following program? The compiler will refuse to compile it because i and b are of different types cannot be assigned to each other. The compiler will refuse to compile it because is outside the legal range of values for a byte. If you have already attempted Objective wise questions, taking Standard Tests is of no use.
You would have seen all the questions and your score will not be a real measure of your preparation. Last Day Test, however, is an exception.
We have made this test completely unique. Questions in this test are not included in Objective wise sets and so even if you have attempted all the objective wise questions, you may still attempt this test. Taking the mock exams You should start with the first Standard Test. Your score on this test will give you a fair idea of how well you are prepared for the exam. We have included a lot of reading material with the questions and you should go through the detailed explanation for each question Your objective should be to improve your score on the topics on which you scored less in this test.
If you fail in a standard test, you should not move on to the next test. Instead, first read up on the topics in which you failed from any book, write some sample programs to reinforce the concepts, and then attempt the next test. So it is a good idea to check the current passing percentage at the time of your exam.
At the end of your preparation, you should attempt the "Last Day Test". If you pass this test, you are ready for the real exam. That is all there is to this book. Happy Learning! If it hasn't been discussed before, feel free to post a message and we will try our best to help. This will motivate us to create ebooks for other certifications as well. Exam Objectives The following are the exam objectives as of this writing. Java Basics Define the scope of variables Define the structure of a Java class Create executable Java applications with a main method Import other Java packages to make them accessible in your code 2.
Working With Java Data Types Declare and initialize variables Differentiate between object reference variables and primitive variables Read or write to object fields Explain an object's lifecycle Manipulate data using the StringBuilder class and its methods Create and manipulate strings 3. Creating and Using Arrays Declare, instantiate, initialize and use a one-dimensional array Declare, instantiate, initialize and use multi-dimensional array Declare and use an ArrayList 5.
Working with Methods and Encapsulation Create methods with arguments and return values Apply the static keyword to methods and fields Create an overloaded method Differentiate between default and user-defined constructors Create and overload constructors Apply encapsulation principles to a class Determine the effect upon object references and primitive values when they are passed into methods that change the values 7. Working with Inheritance Develop code that demonstrates the use of polymorphism Differentiate between the type of a reference and the type of an object Determine when casting is necessary Use super and this to access objects and constructors Use abstract classes and interfaces 8.
Handling Exceptions Differentiate among checked exceptions, RuntimeExceptions and Errors Create a try-catch block and determine how exceptions alter normal program flow Describe what exceptions are used for in Java Invoke a method that throws an exception Recognize common exception classes and categories Taking the Actual Exam The exam is conducted by Pearson VUE.
You may pay for and schedule the exam online through their website. Type of Questions All the questions in the exam are multiple choice questions and every question tells you how many option you have to select. There are no drag and drop or fill in the blanks type questions. Testing Software The testing application is fairly straight forward.