
In this post, I am going to write down few OCAJP 8 mock exam question examples with good explanations. We have earlier explained about the Lambda and Predicate expressions with few sample questions. If you are preparing for the OCAJP 8 exam, then it is most likely that you have to practice more for the Java 8 specific topics since they are relatively new topics in the exam. They might test you with a different combination of syntax and ask you to choose the correct answers.
Here are the 9 questions for OCAJP 8 exam that will be useful for your OCAJP Java Certification Preparation. If you have any questions, please write it in the comments section. If you are interested in practicing more questions, please consider buying any of the popular OCAJP Practice Exam Simulators available in the market. They offer questions that are very much relevant to the real exams.
OCAJP 8 Exam Objective
Here is the exam objective for preparing the OCAJP exam:
- OCAJP 8 Exam expects you to recognize valid, invalid lambda expressions. It doesn’t ask you to write the lambda expressions.
What is Lambda Expression?

Here is the overview or definition of Lambda expression in Java 8 if you are not aware this concepts. before you read the mock questions, please understand the Lambda expression in Java 8.
A lambda expression is an anonymous method with more compact syntax that also allows the omission of modifiers, return type, and in some cases parameter types as well. Before lambda expressions, the anonymous methods are written inside the anonymous classes which are many lines of compare to the single line lambda expression.
1) Which are true about the functional interface?
- A. It has exactly one method and it must be abstract.
- B. It has exactly one method and it may or may not be abstract.
- C. It must have exactly one abstract method and may have any number of default or static methods.
- D. It must have exactly one default method and may have any number of abstract or static methods.
- E. It must have exactly one static method and may have any number of default or abstract methods.
2) Given
interface Test { public void print( ); }
Which are valid lambda expressions (select 2 options)?
- A. ->System.out.println(“Hello world”);
- B. void -> System.out.println(“Hello world”);
- C. ( ) -> System.out.println(“Hello world”);
- D. ( ) ->{ System.out.println(“Hello world”); return; }
- E. (void ) -> System.out.println(“Hello world”);
3) Which lambda can replace the MyTest class to return the same value? (Choose all that apply)
interface Sample { String change(int d); } class MyTest implements Sample { public String change(int s) { return "Hello"; } }
- A. change((e) -> “Hello” )
- B. change((e) -> {“Hello” })
- C. change((e) -> { String e = “”; “Hello” });
- D. change((e) -> { String e = “”; return “Hello”; });
- E. change((e) -> { String e = “”; return “Hello” });
- F. change((e) -> { String f = “”; return “Hello”; });
4) What is the result ?
1: import java.util.function.*; 2: 3: public class Student { 4: int age; 5: public static void main(String[] args) { 6: student p1 = new Student(); 7: p1.age = 1; 8: check(p1, p -> p.age < 5); 9: } 10: private static void check(Student s, Predicate<Student> pred) { 11: String result = pred.test(s) ? "match" : "not match"; 12: System.out.print(result); 13: } }
- A. match
- B. not match
- C. Compiler error on line 8.
- D. Compiler error on line 10.
- E. Compiler error on line 11.
- F. A runtime exception is thrown.
5) What is the output?
1: interface Jump { 2: boolean isToLong(int length, int limit); 3: } 4: 5: public class Climber { 6: public static void main(String[] args) { 7: check((h, l) -> h.append(l).isEmpty(), 5); 8: } 9: private static void check(Jump j, int length) { 10: if (j.isTooLong(length, 10)) 11: System.out.println("too high"); 12: else 13: System.out.println("ok"); 14: } 15: }
- A. ok
- B. too high
- C. Compiler error on line 7.
- D. Compiler error on line 10.
- E. Compiler error on a different line.
- F. A runtime exception is thrown.
6) What can be inserted in the code below so that it will true when run?
class Test { public static boolean check( List l , Predicate<List> p ) { return p.test(l) ; } Public static void main(String[] args) { boolean b = // write code here ; System.out.println(b); } }
Select 2 options
- A. check(new ArrayList( ), al -> al.isEmpty( ) );
- B. check(new ArrayList( ), ArrayList al -> al.isEmpty( ) );
- C. check(new ArrayList( ), al -> return al.size( ) == 0 );
- D. check(new ArrayList( ), al -> al.add(“hello”));
7. Given
class Test { int a ; Test( int a ) { This.a = a; } } And the following code fragment public void filter (ArrayList<Test> al,Predicate<Test> p) { iterator<Test> i = al.iterator( ); while(i.hasNext( ) ) { if(p.test(i.next( ) ) { i.remove( ); } } ---- ArrayList<Test> l = new ArrayList<Test>( ); Test t = new Test(5); l.add(t); t= new Test(6); l.add(t); t=new Test(7); l.add(t); //Insert method call here System.out.println(l);
Which of the following options print [5 7] ?
- A. filter(al,t->t.a%2==0 ) ;
- B. filter(al, (Test y)->y.a%2==0);
- C. filter(al, (Test y)->y.a%2);
- D. filter(al, y-> return y.a%2==0);
8. Which are true about java.util.function.Predicate ?
- A. It is an interface that has one method with a declaration like-
public void test(T t) - B. It is an interface that has one method with a declaration like-
public boolean test(T t) - C. It is an interface that has one method with a declaration like-
public boolean test(T t) - D. It is an abstract class that has one method with a declaration like-
public abstract boolean test(T t) - E. It is an abstract class that has one method with a declaration like-
public abstract void test(T t)
9. Given
class Test { int a ; Test( int a ) { This.a = a; } } And the following code fragment public void filter (ArrayList<Test> al,Predicate<Test> p) { for(Test t : al) { if(p.test(t)) System.out.println(t.a) } } --- ArrayList<Test> l = new ArrayList<Test>( ); Test t = new Test(5); l.add(t); t= new Test(6); l.add(t); t=new Test(7); l.add(t); //Insert method call here
Which of the following options print 7?
- A. filter(al, (Test y) -> { return y.a>6 ; });
- B. filter(al, (Test y) -> { return y.a>6 });
- C. filter(al, ( d) -> return d.a>6) ;
- D. filter(al, d -> d.a>6) ;
Answers
1) Correct option : C
Functional interface must have exactly one abstract method and may have any number of default or static methods.
2) Correct options : C,D
Method doesn’t take any parameters , lambda expression should contain parenthesis in the parameter list of lambda expression. Method doesn’t return anything, so body part should not return anything.
3) Correct options : A, F.
Option B is incorrect because it does not use the return keyword. Options C, D and E are incorrect because the variable e is already in use from the lambda and cannot be redefined. Additionally, option C is missing the return keyword and option E is missing the semicolon.
4) Correct option: A.
This code is correct. Line 8 creates a lambda expression that checks if the age is less than 5. Since there is only one parameter and it does not specify a type, the parentheses around the type parameter are optional. Line 10 uses the Predicate interface, which declares a test() method
5) Correct option: C.
The interface takes two int parameters. The code on line 7 attempts to use them as if one is a StringBuilder. It is tricky to use types in a lambda when they are implicitly specified. Remember to check the interface for the real type.
6) Correct options : A,D
B is incorrect because parenthesis is missing for parameter and ArrayList is incorrect data type for the parameter. C is incorrect because curly braces are mandatory to return keyword.
7) Correct option: B
Option A is syntactically correct but it gives compile time error because t variable within the same scope and can’t be declared two times. C is incorrect because it returns the int, but Predicate method will return boolean. D is incorrect because curly braces are mandatory when the return is being used in the lambda expression.
8) Correct option: B
To answer this question you need to remember Predicate method declaration
Follow this link to know more about Predicate: https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
9) Correct options : A,D
B is incorrect because the semicolon is missing after the return statement. C is incorrect curly braces are mandatory when the return is being used.
I hope this questions would be useful for preparing OCAJP 8 exam. If you have any questions in preparing for OCAJP exam, please write it in the comments section. We are happy to help you in passing the exam.