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:
Java
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:
Java
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:
Java
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:
Java
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 3rd div."; System.out.println(result); } } |
OUTPUT: Compile Time Error Reason: marks < 40 ? “Pass with 3rd div.” is not completed. Correction: marks < 40 ? “Pass with 3rd div.”:”Fail” . 3. Study the rule “String objects are Immutable” . Example-3.1:
Java
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
Java
// 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 "); |
4.2 Lambda Expression Example: Single Parameter
Java
// 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); |
4.3 Lambda Expression Example:Multiple Parameter
Java
// 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; |
5. Study the difference between &(Bitwise AND) and &&(Logical AND) Operator. Example-5.1:
Java
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.