OCAJP 7 Free Mock Exam

Question 1 of 15 [Java Basics]
Which of the following statements are TRUE about a java source file?
Choice 1Package statement must be the first line.
Choice 2Package statement should be after the import statements.
Choice 3Comments can be placed before package statements.
Choice 4Package statements are not allowed in java.
Question 2 of 15 [Java Basics]
Which of the following import statements are illegal?
Choice 1import java.io;
Choice 2static import java.lang.Math;
Choice 3import java.lang.Math;
Choice 4import static java.lang.Math.*;
Choice 5static import java.lang.Math.*;
Question 3 of 15 [Working With Java Data Types]
Which of the following expressions are legal?
Choice 1 String str="10";
int a=new Integer(10);
str+=a;
Choice 2 String str="10";
int a=new Integer(10);
boolean b=str==a;
Choice 3 String str="10";
Integer a=10;
boolean b=str==a.toString();
Choice 4 String str="10";
Integer a=10;
boolean b=str==a+"";
Question 4 of 15 [Working With Java Data Types]
Which of the following Java SE 7 declarations are valid?
Choice 1int x1 = _99;
Choice 2long accountNumber = 2323_4545_2323_34343L;
Choice 3long hexBytes = 0xFF_EC_DE_5E;
Choice 4byte nybbles = 0b0010_0101;
Choice 5float pi1 = 9_.1415F;
Question 5 of 15 [Using Operators and Decision Constructs]
A developer wants to implement switch statement.
Which of the following type Java SE 7 switch can accept?
Choice 1String class
Choice 2Character
Choice 3Byte
Choice 4Short
Choice 5int
Choice 6All the above
Question 6 of 15 [Using Operators and Decision Constructs]
Code:
package com.epractizelabs.cart;
public class ProductNameFinder {
public static String getProductName(String productId) {
String productName = null;
if (productId == null) {
return "ERROR";
}
switch (productId) {
case "15898-30": productName = "SCEA 5 Part 1 Training Lab";
break;
case "15898-31": productName = "SCEA 5 Part 2 and Part 3 Training Lab";
break;
case "15898-62": productName = "OCP Java SE 7 Training Lab";
break;
default: productName = "ERROR";
break;
}
return productName;
}
public static void main(String[] args) {
String productId = "15898-62";
String productName = ProductNameFinder.getProductName(productId);
System.out.println(productName);
}
}
What is the output of the above code?
Choice 1Compilation error (string cannot be used in switch)
Choice 2Runtime error
Choice 3OCP Java SE 7 Training Lab
Question 7 of 15 [Using Operators and Decision Constructs]
Which of the following are valid String declarations?
Choice 1String name=null;
Choice 2String name="Brian";
Choice 3String name=new String("Brian");
Choice 4String name='Brian';
Choice 5String name=Brian;
Question 8 of 15 [Creating and Using Arrays]
Which of the following are valid array declarations?
Choice 1float itemCost[] = {8.4, 9.3, 5.8, 5.11};
Choice 2int productCounts= new array[20];
Choice 3String[] customerNames= String(?Brian christopher?);
Choice 4double[] itemPrice= new double[10];
Question 9 of 15 [Using Loop Constructs]
Which of the following code fragment will compile without error?
Choice 1while(true){ }
Choice 2while(true&&(7==7)){ }
Choice 3while(7==7){ }
Choice 4while(7){ }
Question 10 of 15 [Working with Methods and Encapsulation]
Which of the following statements are TRUE about well-encapsulated classes?
Choice 1Implementation details of the class can be changed without affecting client code.
Choice 2Data is private and methods are public.
Choice 3Data is public and methods also public.
Question 11 of 15 [Working with Methods and Encapsulation]
Code : Test.java
1. public class Test {
2. public void aMethod(int x, String y){}
3. }
Which of the following methods can be declared in Test class?
Choice 1public void AMethod(int x, String y) {?}
Choice 2public int aMethod(int x, String y) {?}
Choice 3public void aMethod(int x, String yy) {?}
Choice 4public void aMethod(String y, int x) {?}
Choice 5public String aMethod(int x, String y) {?}
Question 12 of 15 [Working with Inheritance]
Code :
class Account{}
class Customer{Account ac;}
Which of the following statements are TRUE about the above code?
Choice 1Customer has a HAS-A relationship with Account.
Choice 2Customer has a IS-A relationship with Account.
Choice 3Account has a IS-A relationship with Customer.
Choice 4None
Question 13 of 15 [Working with Inheritance]
Code :
class Account{}
class CreditCardCustomer {
Account ac;
}
Select the OO relationship between the classes.
Choice 1HAS-A
Choice 2IS-A
Choice 3None of these
Question 14 of 15 [Handling Exceptions]
Which of the following Java SE 7 exception handling codes are valid?
Choice 1 try {
//Logic
}
catch(ParseException | IOException exception) {
// Exception handling code
}
Choice 2 try {
//Logic?
}
catch(ParseException or IOException exception) {
// Exception handling code
}
Choice 3 try {
//Logic
}
catch(ParseException & IOException exception) {
// Exception handling code
}
Choice 4 try {
//Logic
}
catch(ParseException || IOException exception) {
// Exception handling code
}
Question 15 of 15 [Handling Exceptions]
Code:
BufferedReader reader = null;
try {
URL url = new URL("http://www.epractizelabs.com/serverdate.html");
reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line = reader.readLine();
SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");
Date date = format.parse(line);
}

catch (MalformedURLException exception) {
// handle wrong URL
} catch (IOException exception) {
// handle I/O errors
} catch (ParseException exception) {
// handle date parse problems.
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Which of the following Java SE 7 code replaces above code?
Choice 1 try
{
BufferedReader reader = new BufferedReader( new InputStreamReader( new URL("http://www.epractizelabs.com/serverdate.html").openStream()));
String line = reader.readLine();
SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");
Date date = format.parse(line);
} catch (ParseException | IOException exception) {
// handle I/O problems.
}
Choice 2 try (BufferedReader reader = new BufferedReader( new InputStreamReader( new URL("http://www.epractizelabs.com/serverdate.html").openStream())))
{
String line = reader.readLine();
SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");
Date date = format.parse(line);
} catch (ParseException | IOException exception) {
// handle I/O problems.
}
Choice 3 try (@BufferedReader reader)
{
reader = new BufferedReader( new InputStreamReader( new URL("http://www.epractizelabs.com/serverdate.html").openStream()));
String line = reader.readLine();
SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");
Date date = format.parse(line);
} catch (ParseException | IOException exception) {
// handle I/O problems.
}