JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

OCAJP – Switch Statement Practice Questions

October 24, 2016 by krishna Leave a Comment

In my previous article, I have published how to prepare ArrayList topic for OCAJP exam. I am writing a series of articles on how to prepare for OCAJP certification exam. Also, read about the sample questions on Lambda Expressions.

Are you looking for mock exam questions to prepare for OCAJP 8 exam, please try these free mock exam practice questions for OCAJP exam?

OCAJP – Switch statement practice questions

1. Given

[code lang=”java”]
switch (a) {
case 1:
System.out.println("1");
break;
case 4:
System.out.println("4");
break;
case 2:
System.out.println("2");
break;
case 5:
System.out.println("5");
break;

}
[/code]

Which of the following statements can be inserted in place of //insert code here So that the above code compiles and runs?

  • A. int a = 1;
  • B. long a = 1;
  • C. Byte a = 1;
  • D. float a =1;
  • E. Integer a = new Integer(“1”);
  • F. compile time error.

2. Which of the following are valid statements about switch ?

  • A. switch expression of type int and case label value of type char are correct combination.
  • B. switch expression of type byte and case label value of type float are correct combination.
  • C. switch expression of type char and case label value of type byte are correct combination.
  • D. All combinations are wrong.

3. What is the output?

[code lang=”java”]
public class SwitchDemo {
public static void main(String args[]) {
boolean b = true;
switch (b) {
case true:
System.out.println("true");
break;
case false:
System.out.println("false");
default:
System.out.println("true false");

}

}
}
[/code]

  • A. true
  • B. false
  • C. true false
  • D. Compile time error
  • E. false

4. What data type of “value” is allowed so that it compiles and runs?

[code lang=”java”]
XXXX value = 50 ;
switch (value) {
case 32:
System.out.println("32");
break;
case 50:
System.out.println("50");
break;
case 150:
System.out.println("50");
break;
case 250:
System.out.println("50");

}
[/code]

  • A. int value = 50;
  • B. long value = 50;
  • C. char value = 50;
  • D. byte value = 50;
  • E. short value = 50;

5. What changes can be done to make this program compile and run successfully ?

[code lang=”java”]
public class SwitchDemo {

public static void main(String args[]) {

boolean value = true; //line 1

switch (value) {

case true: // line 2
System.out.println("true");
break;

default:
System.out.println("default");

}

}
}
[/code]

  • A. No change required.
  • B. Replace line 1 with String value = “true”;
  • C. Replace line 2 with the case “true” :
  • D. Replace both line 1 with String value = “true”; and line 2 with the case “true” :

6. What is the output?

[code lang=”java”]
public class SwitchDemo {
public static void main(String args[]) {
final int a = 5;
final int b = 6;
final int c;
c = 8;
switch (a) {
case a:
System.out.println("a");
break;
case b:
System.out.println("b");
break;
case c:
System.out.println("c");
break;
default:
System.out.println("none");

}

}
}
[/code]

  • A.
  • B. b
  • C. c
  • D. none
  • E. Compile time error.

7. What is the output?

[code lang=”java”]
String day = "Sun";
switch (day) {
case "Mon":
System.out.println("Monday");

case "Tue":
System.out.println("Tuesday");
break;
case "Sun":
System.out.println("Sunday");

default:
System.out.println("Friday");

}
[/code]

  • A. Monday
  • B. Tuesday
  • C. Sunday
  • D. Friday
  • E. Compile time error because String is not allowed.

Answers

1. Correct options : A,C,E. A, C, E are correct because int, byte, short, char and their wrapper classes are allowed as a data type of switch parameter data type. B, D are wrong options because long, float, double, boolean are not allowed by a switch statement.

2. Correct option: A. To answer this question you need to remember one rule about the switch.
Switch variable must able hold to all the case constants. For example, byte ranges from -128 to 127. So all case values must be within that range. A is correct because char values are in the range of it.

3. Correct option: D. D is correct because boolean data type is not allowed by the switch.

4. Correct options: A,C,E. A, C, E are correct because int, char, short can hold all case label values. B is wrong because long is not allowed. D is also wrong because byte range is -128 to 127.

5. Correct option: D. boolean data type is not allowed by the switch. So you need to change at line 1 and line 2.

6. Correct option: E. Because c is a final variable but it is initialized in the next line. So it is evaluated at runtime. so the whole expression evaluated at runtime. So it is not compiled time constant. It gives compile time error.

7. Correct option: D. Once case constant is matched, JVM will execute the associated case branch and subsequent code blocks if there is no break statement after first matched case.

References

If you are preparing for the OCAJP certification exam, then the following resources will be useful to you.

  • JavaBeat’s OCAJP Guide
  • OCAJP Mock Exams (Java Basics)
  • Lambda Expressions Practice Questions
  • How to prepare for OCAJP Certification Exam?
  • What are the good books for OCAJP Exam?

 

Filed Under: Certifications Tagged With: Lambda Expressions., OCAJP Certifications, OCAJP exam, Switch statement practice questions

OCAJP – Lambda Practice Questions for Preparing OCAJP 8 Certification

October 6, 2016 by krishna Leave a Comment

Lambda Practice Questions
Photo credit to AC4 Consultoria

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?

Lambda Expressions in Java
Photo credit to YouTube

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

[code]
interface Test {
public void print( );
}
[/code]

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)

[code]
interface Sample {
String change(int d);
}
class MyTest implements Sample {
public String change(int s) {
return "Hello";
}
}
[/code]

  • 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 ?

[code]
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: } }
[/code]

  • 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?

[code]
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: }
[/code]

  • 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?

[code]
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);
}
}
[/code]

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

[code]
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);
[/code]

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

[code]
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
[/code]

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.

Filed Under: Certifications Tagged With: Lambda and Predicate expressions, Lambda Expression, OCAJP Certifications, OCAJP Practice Exam Simulators

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved