OCPJP 7 Free Mock Exam

Question 1 of 26 [Java Class Design]
Code :
Parent.java
class Parent
{
void aMethod(String args){
}
}
class Child extends Parent {
}
Which of the following methods are valid at Child class?
Choice 1void aMethod(int number) {}
Choice 2String aMethod(String args) {}
Choice 3private void aMethod(String args) {}
Choice 4protected void aMethod(String args) throws IndexOutOfBoundsException {}
Question 2 of 26 [Java Class Design]
Code : Customer.java class Customer {
static String accountNumber = "";
public Customer() { }
static String getAccountNumber() {
return accountNumber; }
}
class CreditCardCustomer extends Customer {
public String getAccountNumber() throws RuntimeException {
return accountNumber;
}
}
What will happen when you compile and run the Customer class?
Choice 1Compilation error
Choice 2Runtime error
Choice 3Compiles and runs without any error
Choice 4getAccountNumber method of CreditCardCustomer objects throws RuntimeException
Question 3 of 26 [Java Class Design]
Code : Test.java 1. class Test {
2. public static void main(String [] args) {
3. new Test().testMethod();
4. }
5.
6. void testMethod() {
7. try {
8. System.out.println("Java");
9. return;
10. }
11. finally {
12. System.exit(0);
13. System.out.println(" Tiger");
14. }
15. }
16. }
Which of the following statement about the given code is TRUE?
Choice 1Compiler error at line 13.
Choice 2Compile time error. Catch block is missing.
Choice 3Prints "Java"
Choice 4Prints "Java Tiger "
Choice 5Prints "Tiger"
Question 4 of 26 [Java Class Design]
Code : Bank.java
package bank;
public class Bank{
String getBankBranchName() {
return "Head Office";
}
}

BankBranch.java
package bankBranch;
import bank.*;
public class BankBranch extends Bank {
String bankBranchName=null;
String getBankBranchName(){
super.getBankBranchName();
}
}
What will happen when you compile and run the Bank and BankBranch classes?
Choice 1Bank class will compile and BankBranch will not
Choice 2Both classes will not compile
Choice 3BankBranch class will compile and Bank will not
Choice 4Both compiles without any error
Question 5 of 26 [Java Class Design]
Code : CreditCardCustomer.java
class Customer {
String customerID;
private String getCustomerID() {
return customerID;
}
}
class CreditCardCustomer extends Customer {
public static void main(String[] argv) {
Customer brad = new CreditCardCustomer();
brad.customerID="12122";
System.out.print(brad.getCustomerID());
}
String getCustomerID() {
return customerID;
}
}
What will happen when you attempt to compile and run the above code?
Choice 1Compiler error
Choice 212122
Choice 3Runtime error
Choice 4null
Question 6 of 26 [Java Class Design]
Code : BankBranch.java
class Bank {
String bankName;
public String getBranchName() {
return bankName;
}
}
public class BankBranch extends Bank {
public static void main(String[] argv)
{ }
public String getBranchName() throws Exception {
return bankName;
}
}
What will happen when you attempt to compile and run the above code?
Choice 1Compiles and runs without any error
Choice 2Runtime error
Choice 3Compile time error
Choice 4null
Question 7 of 26 [Advanced Class Design]
Code : Inner.java
class Inner {
public static void main(String[] args) {
Inner inner = new Inner();
inner.new Outer().innerMethod();
}
final int num = 2399;
class Outer {
int num=9932;
void innerMethod() {
System.out.println( -------------);
}
}
}
Which of the following code snippets can be inserted on the-------------to print 2399?
Choice 1Inner.this.num
Choice 2super.num
Choice 3num
Choice 4Outer.Inner.num
Choice 5Inner.Outer.num
Choice 6None of the above
Question 8 of 26 [Object-Oriented Design Principles]
You are assigned to develop a print spooler Java class. This class must be accessed by disparate objects throughout your application components.
Which pattern can you use for implementing print spooler class?
Choice 1Facade
Choice 2Singleton
Choice 3Factory
Question 9 of 26 [Object-Oriented Design Principles]
You are assigned to complete data access code for a small desktop application. The application currently uses flat text files to store data and planned to use XML for RDBMS in the near future.
Which design pattern can be used to serve data access object based on database type (text file/ XML / RDBMS)?
Choice 1Singleton
Choice 2Builder
Choice 3Factory
Question 10 of 26 [Generics and Collections]
Which of the following statements are TRUE with respect to Set and Collection?
Choice 1A Set cannot contain repeated elements.
Choice 2The equals() method cannot be used to test whether two objects should be considered the same.
Choice 3The equals() method is used to test whether two objects should be considered the same.
Choice 4A Set can contain repeated elements.
Question 11 of 26 [String Processing]
You are assigned to develop email validation logic for EPractize Labs Email Marketing Software Express.
Select the method to achieve this goal.
Choice 1 public boolean isValidEmail(String emailAddress) {
static final String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(emailAddress);
flag = m.matches();
return flag;
}
Choice 2 public boolean isValidEmail(String emailAddress) {
static final String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern p = Pattern.compile(regex);
flag = p.matches(emailAddress);
return flag;
}
Choice 3 public boolean isValidEmail(String emailAddress) {
static final String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern p = Pattern.compile(regex);
Matcher m = p.contains(emailAddress);
flag = m.matches();
return flag;
}
Question 12 of 26 [Exceptions and Assertions]
Which of the following statements are TRUE about checked exception?
Choice 1Represents the defects in the program.
Choice 2Are subclasses of exception.
Choice 3Are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException.
Choice 4Represent invalid conditions in areas outside the immediate control of the program
Question 13 of 26 [Java I/O Fundamentals]
Code: Test.java
import java.io.*;
class Test{
public static void main(String[] args) throws IOException {
Writer w=new BufferedWriter(new FileWriter("FILE.TXT"));
}
}
What will happen when you try to compile and run the following code without FILE.TXT?
Choice 1Compiler error
Choice 2FileNotFoundException will be thrown
Choice 3IOException will be thrown
Choice 4The code compiles and runs without any error
Question 14 of 26 [Java File I/O (NIO.2)]
Code:
Locale locale=null;
ResourceBundle eplResources=null;
You want to load property file named ERACTIZE_RES.properties.
Which of the following code can achieve this goal. Choice 1eplResources=ResourceBundle.getBundle("ERACTIZE_RES ",Locale.getDefault()); Choice 2eplResources=ResourceBundle.getBundle("ERACTIZE_RES. properties ",Locale.getDefault()); Choice 3eplResources=ResourceBundle.getBundle("ERACTIZE_RES ");
Question 15 of 26 [Java File I/O (NIO.2)]
Which class is a programmatic representation of a path in the file system?
Choice 1Files
Choice 2Path
Choice 3Folder
Question 16 of 26 [Java File I/O (NIO.2)]
Which of the following method returns PathMatcher object?
Choice 1FileSystem.getPathMatcher(String syntaxAndPattern)
Choice 2File.getPathMatcher(String syntaxAndPattern)
Choice 3Path.getPathMatcher(String syntaxAndPattern)
Question 17 of 26 [Building Database Applications with JDBC]
Which class can obtain a RowSetFactory?
Choice 1RowFactory
Choice 2RowSetProvider
Choice 3CachedRowSet
Question 18 of 26 [Building Database Applications with JDBC]
Which of the following code is valid?
Choice 1 RowSetFactory aFactory = RowSetProvider.newFactory();
CachedRowSet crs = aFactory.createCachedRowSet();
Choice 2 RowSetFactory aFactory = RowSetProvider.getFactory();
CachedRowSet crs = aFactory.createCachedRowSet();
Choice 3 RowSetFactory aFactory = RowSetProvider.newFactory();
CachedRowSet crs = aFactory.getCachedRowSet();
Question 19 of 26 [Building Database Applications with JDBC]
Which of the following can be used to execute precompiled SQL statement?
Choice 1PreparedStatement
Choice 2Statement
Choice 3PrepareStatement
Question 20 of 26 [Threads]
Which of the following statements are TRUE?
Choice 1Interrupted method stops the current thread temporarily.
Choice 2Interrupted method always interrupts the thread and returns true.
Choice 3Interrupted method returns true if the current thread has been interrupted; false otherwise.
Choice 4Interrupted method clears the interrupted status of the thread.
Question 21 of 26 [Threads]
Which of the following method by invoking on an object causes all threads waiting for that object's lock to wake up?
Choice 1notify()
Choice 2run()
Choice 3create()
Choice 4notifyAll()
Question 22 of 26 [Concurrency]
Which interface is used instead of explicitly creating threads?
Choice 1Executor
Choice 2Runnable
Choice 3Thread
Question 23 of 26 [Concurrency]
Select the Executor method.
Choice 1void run(Runnable command)
Choice 2void execute(Runnable command)
Choice 3void execute(Thread command)
Question 24 of 26 [Localization]
Locale.getAvailableLocales method must return an array that contains at least a Locale instance equal to Locale.US.
Choice 1True
Choice 2False
Question 25 of 26 [Localization]
Locale locale=null;
ResourceBundle eplResources=null;
You want to load property file named ERACTIZE_RES.properties.
Which of the following code can achieve this goal.
Choice 1eplResources=ResourceBundle.getBundle("ERACTIZE_RES ",Locale.getDefault());
Choice 2eplResources=ResourceBundle.getBundle("ERACTIZE_RES. properties ",Locale.getDefault());
Choice 3eplResources=ResourceBundle.getBundle("ERACTIZE_RES ");
Question 26 of 26 [Localization]
Which method is used to get the list of all installed locales?
Choice 1Locale.getAllLocales();
Choice 2Locale.getAvailableLocales();
Choice 3Locale.getAllDefaultLocales();
Choice 4Locale.getDefaultLocales ();
Choice 5Locale.getInstalledLocales();