Oracle Certified Java Associate (OCA) Exam Preparation

12 Nov, 2018
Friends! I recently appeared for OCA exam and scored 95%. Here i am sharing few techniques and exam question patterns which must be helping you while appearing for OCA test. This exam guarantee to ask question on the below topics or we can say statements.Exam code: 1Z0-808
1. Must practice the differences between str1 == str2 and str1.equals(str2).Example-1.1:
class Test {    public static void main(String[] args)    {        String s = new String("hello");        String s2 = "hello";          if (s == s2) {            System.out.println("==");        }          if (s.equals(s2)) {            System.out.println("equals");        }    }}
equals

Reason: Because String class equals method compare objects, but == operator only compares references. If both the references are pointing to the same object then only == operator returns true.
Example-1.2:
class Test {    public static void main(String[] args)    {        String s = new String("hello");        String s2 = s;          if (s == s2) {            System.out.println("==");        }          if (s.equals(s2)) {            System.out.println("equals");        }    }}
==
equals

Reason: Because both the references are pointing to the same object so “==” printed and If both the reference are pointing to the same object so by default they the equal so “equals” printed.
2. Study ternary operator and its compile time errors.
Example-2.1:
class Test {    public static void main(String[] args)    {        int marks = 90;        String result = marks > 35 ? "Pass" : "Fail";        System.out.println(result);    }}
Pass

Example-2.2:
class Test {    public static void main(String[] args)    {        int marks = 90;        String result = marks > 60 ? "Pass with 1st div."                        : marks < 50 ? "Pass with 2nd div." :                           marks < 40 ? "Pass with 3nd div.";        System.out.println(result);    }}
OUTPUT: Compile Time ErrorReason: marks < 40 ? "Pass with 3nd div." is not completed.Correction: marks < 40 ? "Pass with 3nd div.":”Fail” .
3. Study the rule “String objects are Immutable” .Example-3.1:
class Test {    public static void main(String[] args)    {        String ta = "A ";        ta = ta.concat("B ");        String tb = "C ";        ta = ta.concat(tb);        ta.replace('C', 'D');        ta = ta.concat(tb);        System.out.println(ta);    }}
A B C C

4. Lambda expression and its simplified forms.
Java Lambda Expression Syntax:(argument-list) -> {body}4.1 Lambda Expression Example: No Parameter
// This a java methodvoid printHello(){    System.out.println("Hello World ");}  Or  // As lambda the above method can be written as below() -> { System.out.println("Hello World "); }; Or  // {} is optional for single line statement() -> System.out.println("Hello World ");
4.2 Lambda Expression Example: Single Parameter
// This a java methodvoid sayHello(String name){    System.out.println("Hello " + name);}  Or  (name) -> { System.out.println("Hello " + name); };  Or  // {} optional(name) -> System.out.println("Hello " + name);  Or// () optional for single input parameter.name -> System.out.println("Hello " + name); 
4.3 Lambda Expression Example:Multiple Parameter
// This a java methodint add(int num1, int num2){    return num1 + num2;}  Or  (int num1, int num2) -> { return num1 + num2; };  Or  (int num1, int num2) -> num1 + num2;  Or   // () mandatory for more than one input parameter.(num1, num2) -> num1 + num2;
5. Study the difference between &(Bitwise AND) and &&(Logical AND) Operator.Example-5.1:
class Test {    public static void main(String[] args)    {        int a = 10;        int b = 20;          if (++a <= 10 && --b < 20) {}          System.out.println("Output of && operator: "                           + "a = " + a + " b = " + b);        System.out.println("-------------");          a = 10;        b = 20;        if (++a <= 10 & --b < 20) {}          System.out.println("Output of & operator: "                          + "a = " + a + " b = " + b);    }}
Output of && operator: a = 11 b = 20
-------------
Output of & operator: a = 11 b = 19

Reason: Because ‘&&’ operator doesn’t check second operand if value for the first operand is ‘false’. But ‘&’ must check both the operands.
Note: These concept definitely covers 10 – 12 questions in OCA Exam.
atishaysharma640
Java-Output
java-puzzle
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Stream In Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Stack Class in Java