Vous êtes sur la page 1sur 3

Ten OCPJP 7 Questions

1. Expression (s instanceof java.util.Date) will return false if 's' was declared as a variable of class
java.lang.String.
a) True
b) False
2. Consider the following program:
class SuperClass {
SuperClass() {
foo();
}
public void foo(){
System.out.println("In SuperClass.foo()");
}
}
class SubClass extends SuperClass {
public SubClass() {
member = "HI";
}
public void foo() {
System.out.println("In Derived.foo(): " + member.toLowerCase());
}
private String member;
public static void main(String[] args) {
SuperClass reference = new SubClass();
reference.foo();
}
}
3. Consider the following class:
package com.enthu;
public class Resource {
private String data = "DATA";
String getData(){
return data;
}
void setData(String data){
this.data = data == null ? "" : data;
}
boolean equals(Resource r){
return (r != null && r.getData().equals(this.getData()));
}
}
Identify the options that correctly describe the behavior of the above class.
a) If two distinct Resource objects are considered to be equal, then their 'data' fields must have contained the same string value.
b) Option 1 would be correct only if the provided equals(...) method is made public.
c) Two distinct Resource objects may be considered "not equal" even if their data values are same.
d) The class will not compile because equals method is not correctly overridden.
e) The statement : new Resource().equals(new Resource()); will always return true.
f) The statement new Resource().equals(new Object()); will throw a ClassCastException at runtime.
g) The statement new Resource().equals(new Object()); will not compile.
4. Given
Connection con = DriverManager.getConnection(dbURL);
//code for enabling transactions
String updateString =
"update SALES set T_AMOUNT = 100 where T_NAME = 'BOB'";

Statement stmt = con.createStatement();


stmt.executeUpdate(updateString);
stmt.executeUpdate("update ACCOUNTS set STATUS=1");
con.commit();

//REPLACE THIS LINE OF CODE

//INSERT CODE HERE


Which statement can replace the call to con.commit() and still have the transaction committed?
a) con.setAutoCommit(false);
b) con.setAutoCommit(true);
c) stmt.commit();
d) con.rollback();
e) con.close();
5. What will the following code print when run?
public class RunTest {
public static volatile int counter = 0;
static class RunnerDec implements Runnable{
public void run(){
for(int i=0;i<5000; i++){
counter--;
}
}
}
static class RunnerInc implements Runnable{
public void run(){
for(int i=0;i<5000; i++){
counter++;
}
}
}
public static void main(String[] args) {
RunnerDec rd = new RunnerDec();
RunnerInc ri = new RunnerInc();
Thread t1 = new Thread(rd);
Thread t2 = new Thread(ri);
t1.start();
t2.start();
try{
t1.join();
t2.join();
}catch(Exception e){
e.printStackTrace();
}
System.out.println(counter);
}
}
a) It will always print 0.
b) It may print any number between -5000 to 5000.
c) It may print any number between -5000 to 5000 except 0.
d) The program will keep running for ever.
6. Consider these two interfaces:
interface I1
{
void m1() throws java.io.IOException;
}
interface I2
{
void m1() throws java.io.FileNotFoundException;
}

Which of the following are valid method declarations for a class that says it implements I1 and I2 ?
a) Both, public void m1() throws FileNotFoundException; and public void m1() throws IOException;
b) public void m1() throws FileNotFoundException
c) The class cannot implement both the interfaces as they have conflicting methods.
d) public void m1() throws Exception;
e) None of the above.
7. Consider these two interfaces:
interface I1
{
void m1() throws java.io.IOException;
}
interface I2
{
void m1() throws java.sql.SQLException;
}
What methods have to be implemented by a class that says it implements I1 and I2 ?
a) Both, public void m1() throws SQLException; and public void m1() throws IOException;
b) public void m1() throws Exception
c) The class cannot implement both the interfaces simultaneously as they have conflicting methods.
d) public void m1() throws SQLException, IOException;
e) None of the above.
8. The signature of a method in a class is as follows:
public static <E extends CharSequence> List<? super E> doIt(List<E> nums)

This method is being called in the following code:


result = doIt(in);
Given that String implements CharSequence interface, what should be the reference type of 'in' and 'result' variables?
a) ArrayList<String> in;
List<CharSequence> result;
b) List<String> in;
List<Object> result;
c) ArrayList<String> in;
List result;
d) List<CharSequence> in;
List<CharSequence> result;
e) ArrayList<Object> in;
List<CharSequence> result;

f) None of these