1z0-809 Practice Test Questions

128 Questions


Given the code fragment:
public void recDelete (String dirName) throws IOException {
File [ ] listOfFiles = new File (dirName) .listFiles();
if (listOfFiles ! = null && listOfFiles.length >0) {
for (File aFile : listOfFiles) {
if (aFile.isDirectory ()) {
recDelete (aFile.getAbsolutePath ());
} else {
if (aFile.getName ().endsWith (“.class”))
aFile.delete ();
}
}
}
}
Assume that Projects contains subdirectories that contain .class files and is passed as an
argument to the recDelete () method when it is invoked.
What is the result?


A.

The method deletes all the .class files in the Projects directory and its subdirectories.


B.

The method deletes the .class files of the Projects directory only.


C.

The method executes and does not make any changes to the Projects directory.


D.

The method throws an IOException.





B.
  

The method deletes the .class files of the Projects directory only.



Given the code fragment:
class CallerThread implements Callable<String> {
String str;
public CallerThread(String s) {this.str=s;}
public String call() throws Exception {
return str.concat(“Call”);
}
}
and
public static void main (String[] args) throws InterruptedException, ExecutionException
{
ExecutorService es = Executors.newFixedThreadPool(4); //line n1
Future f1 = es.submit (newCallerThread(“Call”));
String str = f1.get().toString();
System.out.println(str);
Which statement is true?


A.

The program prints Call Call and terminates


B.

The program prints Call Call and does not terminate.


C.

A compilation error occurs at line n1.


D.

An ExecutionException is thrown at run time.





B.
  

The program prints Call Call and does not terminate.



Given the for loop construct:
for ( expr1 ; expr2 ; expr3 ) {
statement;
}
Which two statements are true?


A.

This is not the only valid for loop construct; there exits another form of for loop
constructor.


B.

The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop
begin.


C.

When expr2 evaluates to false, the loop terminates. It is evaluated only after each
iteration through the loop.


D.

The expression expr3 must be present. It is evaluated after each iteration through the
loop.





B.
  

The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop
begin.



C.
  

When expr2 evaluates to false, the loop terminates. It is evaluated only after each
iteration through the loop.



The for statement have this forms:
for (init-stmt; condition; next-stmt) {
body
}
There are three clauses in the for statement.
The init-stmt statement is done before the loop is started, usually to initialize an iteration
variable.
The condition expression is tested before each time the loop is done. The loop isn't
executed if the boolean expression is false (the same as the while loop).
The next-stmt statement is done after the body is executed. It typically increments an
iteration variable.

Given:

What is the result?


A.

100
210


B.

Compilation fails due to an error in line n1


C.

Compilation fails due to an error at line n2


D.

Compilation fails due to an error at line n3





C.
  

Compilation fails due to an error at line n2



Given:
class FuelNotAvailException extends Exception { }
class Vehicle {
void ride() throws FuelNotAvailException {//line n1
System.out.println(“Happy Journey!”);
}
}
class SolarVehicle extends Vehicle {
public void ride () throws Exception {//line n2
super ride ();
}
}
and the code fragment:
public static void main (String[] args) throws FuelNotAvailException, Exception {
Vehicle v = new SolarVehicle ();
v.ride();
}
Which modification enables the code fragment to print Happy Journey!?


A.

Replace line n1 with public void ride() throws FuelNotAvailException {


B.

Replace line n1 with protected void ride() throws Exception {


C.

Replace line n2 with void ride() throws Exception {


D.

Replace line n2 with private void ride() throws FuelNotAvailException {





B.
  

Replace line n1 with protected void ride() throws Exception {



Given:
public class ScopeTest {
int j, int k;
public static void main(String[] args) {
ew ScopeTest().doStuff(); }
void doStuff() {
nt x = 5;
oStuff2();
System.out.println("x");
}
void doStuff2() {
nt y = 7;
ystem.out.println("y");
or (int z = 0; z < 5; z++) {
ystem.out.println("z");
ystem.out.println("y");
}
Which two items are fields?


A.

j


B.

k


C.

x


D.

y


E.

z





A.
  

j



B.
  

k



Given the structure of the STUDENT table:
Student (id INTEGER, name VARCHAR)
Given:
public class Test {
static Connection newConnection =null;
public static Connection get DBConnection () throws SQLException {
try (Connection con = DriveManager.getConnection(URL, username, password)) {
newConnection = con;
}
return newConnection;
}
public static void main (String [] args) throws SQLException {
get DBConnection ();
Statement st = newConnection.createStatement();
st.executeUpdate(“INSERT INTO student VALUES (102, ‘Kelvin’)”);
}
}
Assume that:
The required database driver is configured in the classpath.
The appropriate database is accessible with the URL, userName, and passWord exists.
The SQL query is valid.
What is the result?


A.

The program executes successfully and the STUDENT table is updated with one record.


B.

The program executes successfully and the STUDENT table is NOT updated with any
record.


C.

A SQLException is thrown as runtime.


D.

A NullPointerException is thrown as runtime.





D.
  

A NullPointerException is thrown as runtime.



public class StringReplace {
public static void main(String[] args) {
String message = "Hi everyone!";
System.out.println("message = " + message.replace("e", "X")); }
}
What is the result?


A.

message = Hi everyone!


B.

message = Hi XvXryonX!


C.

A compile time error is produced.


D.

A runtime error is produced.


E.

message =


F.

message = Hi Xveryone!





B.
  

message = Hi XvXryonX!



Given the code fragment:
Path p1 = Paths.get(“/Pics/MyPic.jpeg”);
System.out.println (p1.getNameCount() +
“:” + p1.getName(1) +
“:” + p1.getFileName());
Assume that the Pics directory does NOT exist.
What is the result?


A.

An exception is thrown at run time.


B.

2:MyPic.jpeg: MyPic.jpeg


C.

1:Pics:/Pics/ MyPic.jpeg


D.

2:Pics: MyPic.jpeg





C.
  

1:Pics:/Pics/ MyPic.jpeg



Given the definition of the Country class:
public class country {
public enum Continent {ASIA, EUROPE}
String name;
Continent region;
public Country (String na, Continent reg) {
name = na, region = reg;
}
public String getName () {return name;}
public Continent getRegion () {return region;}
}
and the code fragment:
List<Country> couList = Arrays.asList (
new Country (“Japan”, Country.Continent.ASIA),
new Country (“Italy”, Country.Continent.EUROPE),
new Country (“Germany”, Country.Continent.EUROPE));
Map<Country.Continent, List<String>> regionNames = couList.stream ()
collect(Collectors.groupingBy (Country ::getRegion,
Collectors.mapping(Country::getName, Collectors.toList()))));
System.out.println(regionNames);
What is the output?


A.

{EUROPE = [Italy, Germany], ASIA = [Japan]}


B.

{ASIA = [Japan], EUROPE = [Italy, Germany]}


C.

 {EUROPE = [Germany, Italy], ASIA = [Japan]}


D.

{EUROPE = [Germany], EUROPE = [Italy], ASIA = [Japan]}





A.
  

{EUROPE = [Italy, Germany], ASIA = [Japan]}



Given the code fragment:
List<String> str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”);
Predicate<String> test = s -> {
int i = 0;
boolean result = s.contains (“pen”);
System.out.print(i++) + “:”);
return result;
};
str.stream()
.filter(test)
.findFirst()
.ifPresent(System.out ::print);
What is the result?


A.

0 : 0 : pen


B.

0 : 1 : pen


C.

0 : 0 : 0 : 0 : 0 : pen


D.

0 : 1 : 2 : 3 : 4 :


E.

A compilation error occurs.





E.
  

A compilation error occurs.



The protected modifier on a Field declaration within a public class means that the field
______________.


A.

Cannot be modified


B.

Can be read but not written from outside the class


C.

Can be read and written from this class and its subclasses only within the same
package


D.

Can be read and written from this class and its subclasses defined in any package





D.
  

Can be read and written from this class and its subclasses defined in any package



http://beginnersbook.com/2013/05/java-access-modifiers/


Page 3 out of 11 Pages
Previous