Oracle Certified Java Associate (OCA) Exam Preparation

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
        }
    }
}

chevron_right


Output:

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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");
        }
    }
}

chevron_right


Output:

==
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:

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
    public static void main(String[] args)
    {
        int marks = 90;
        String result = marks > 35 ? "Pass" : "Fail";
        System.out.println(result);
    }
}

chevron_right


Output:

Pass

Example-2.2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


OUTPUT: Compile Time Error
Reason: 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


Output:

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

 

filter_none

edit
close

play_arrow

link
brightness_4
code

// This a java method
void 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 ");

chevron_right


4.2 Lambda Expression Example: Single Parameter

 

filter_none

edit
close

play_arrow

link
brightness_4
code

// This a java method
void 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); 

chevron_right


4.3 Lambda Expression Example:Multiple Parameter

 

filter_none

edit
close

play_arrow

link
brightness_4
code

// This a java method
int 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;

chevron_right


5. Study the difference between &(Bitwise AND) and &&(Logical AND) Operator.
Example-5.1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


Output:

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : atishaysharma640



Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.