IS5103 WEEK 1 PreAssesment

1) Which of the following can fill in the blanks in order to make this code compile? __________ a = __________.getConnection(url, userName, password);__________ b =  a.prepareStatement(sql);__________ c = b.executeQuery();if (c.next())  
System.out.println(c.getString(1)); 

Connection, DataSource, PreparedStatement, ResultSet

2) What is the output of this code?
Predicate empty = String::isEmpty;Predicate notEmpty = empty.negate();var result = Stream.generate(() -> “”).filter(notEmpty).collect(Collectors.groupingBy(k -> k)).entrySet().stream().map(Entry::getValue).flatMap(Collection::stream).collect(Collectors.p artitioningBy(notEmpty));System.out.println(result);

The code does not terminate.

3) Which statement about the following method is true?
public static void main(String… unused) {System.out.print(“a”);try (StringBuilder reader =
new StringBuilder()) {System.out.print(“b”);throw new IllegalArgumentException();} catch
(Exception e || RuntimeException e) {System.out.print(“c”);throw new
FileNotFoundException();} finally {System.out.print(“d”);} }

Three lines contain a compiler error.

4) What is the result of the following program?
public class MathFunctions {public static void addToInt(int x, int amountToAdd) {x = x +
amountToAdd;}public static void main(String[] args) {var a = 15;var b = 10;MathFunctions.addToInt(a,
b);System.out.println(a); } }

15

5) What interface is used to run SQL defined in the code with bind variables?

PreparedStatement

6) What exception is thrown when there is an issue connecting to the database?

SQLException

7) Which method must be called to properly terminate an OutputStream?

close()

8) Which of the following features are configurable in a for statement that are not configurable in a
for-each statement? Each correct answer represents a complete solution. Choose all that apply

The step statement
The test condition

9) Which of the following expressions compile without error? Each correct answer represents a
complete solution. Choose all that apply.

short thursday = (short)Integer.MAX_VALUE;
double tuesday = 5_6L;

10) Which of the following lines can fill in the blank to print true?
public static void main(String[] args) {System.out.println(test(____________________________));}private static boolean test(Function b) {return b.apply(5);} Each correct answer represents a complete solution. Choose all that apply.

(i) -> i == 5
(i) -> {return i == 5;}

11) What is the name of the class used to delete a directory?

java.io.File

12) Which of the following statements can fill in the blank to make the code compile successfully? Set mySet = new _________ (); Each correct answer represents a complete solution. Choose all that apply.

TreeSet<<RuntimeException>
TreeSet<NullPointerException>

13) Which NIO.2 Path method allows you to join one Path to another Path?

resolve()

14) Which changes, when made independently, guarantee the following code snippet prints 100 at
runtime?

Wrap the lambda body with a synchronized block.
Remove parallel() in the stream operation.
Change forEach() to forEachOrdered() in the stream operation.

15) Which method can be used to create a task that repeats 10 seconds after the end of a previous
task?

scheduleWithFixedDelay()

16. What is the result of the following code?
Copyvar treeMap = new TreeMap<Character, Integer>();treeMap.put(‘k’, 1);treeMap.put(‘k’,  2);treeMap.put(‘m’, 3);treeMap.put(‘M’, 4);treeMap.replaceAll((k, v) -> v +  
v);treeMap.keySet().forEach(k -> System.out.print(treeMap.get(k))); 

846 

17. What interface is used to call a stored procedure? 

CallableStatement 

18. Which of the following types can be inserted into the blank to allow the program to compile  successfully?
import java.util.*;final class Amphibian {}abstract class Tadpole extends Amphibian {}public class  FindAllTadpoles {public static void main(String… args) {var tadpoles = new ArrayList<Tadpole>();for  (var amphibian : tadpoles) {___________ tadpole = amphibian;} } } 

None of these 

19. How many times is the word true printed? 
var s1 = “Java”;var s2 = “Java”;var s3 = s1.indent(1).strip();var s4 = s3.intern();var sb1 = new  StringBuilder();sb1.append(“Ja”).append(“va”);System.out.println(s1 ==  
s2);System.out.println(s1.equals(s2));System.out.println(s1 == s3);System.out.println(s1 ==  s4);System.out.println(sb1.toString() == s1);System.out.println(sb1.toString().equals(s1)); 

Four times

20. What is the result of compiling and executing the following program? 
public class FeedingSchedule {public static void main(String[] args) {var x = 5;var j = 0;OUTER: for (var  i = 0; i < 3;)INNER: do {i++;x++;if (x> 10) break INNER;x += 4;j++;} while (j <= 2);System.out.println(x);}  } 

12

21. Which of the following statements are true if the table is empty before the code execution?
var sql = “INSERT INTO people VALUES(?, ?, ?)”; 
conn.setAutoCommit(false); 
try (var ps = conn.prepareStatemen
ResultSet.CONCUR_UPDATABLE)) { 
ps.setInt(1, 1); 
ps.setString(2, “Joslyn”); 
ps.setString(3, “NY”); 
ps.executeUpdate(); 
Savepoint sp = conn.setSavepoint(); 
ps.setInt(1, 2); 
ps.setString(2, “Kara”); 
ps.executeUpdate(); 
conn._________________; 

Each correct answer represents a complete solution. Choose all that apply.

If the blank line contains rollback(sp), there is one row in the table. 
If the blank line contains rollback(), there are no rows in the table. 

22. Which of the following defines an infinite loop execution?
Each correct answer represents a complete solution. Choose all that apply.

do {} while (true); 
for( ; ; ) {} 

23. A(n) _________________ module always contains a module-info.java file, while a(n)  _________________ module always exports all its packages to other modules. 

named, automatic 

24. What is the output of the following program?
class Deer {public Deer() {System.out.print(“Deer”);}public Deer(int age)  
{System.out.print(“DeerAge”);}protected boolean hasHorns() { return false; }}public class Reindeer  extends Deer {public Reindeer(int age) {System.out.print(“Reindeer”);}public boolean hasHorns() {  return true; }public static void main(String[] args) {Deer deer = new  
Reindeer(5);System.out.println(“,” + deer.hasHorns());} }

DeerReindeer,true 

25. Suppose that we have the following property files and code. What values are printed on lines 15  and 16, respectively? 
Penguin.propertiesname=Billyage=1Penguin_de.propertiesname=Chillyage=4Penguin_en.properties name=WillyLocale fr = new Locale(“fr”);Locale.setDefault(new Locale(“en”, “US”));var b =  ResourceBundle.getBundle(“Penguin”,  
fr);System.out.println(b.getString(“name”));System.out.println(b.getString(“age”))

Willy and 1

26. Which is not a SQL keyword? 

REMOVE

27. A property file resource bundle can only contain String values. 

True

28. When creating a Locale, the country comes before the language. 

False

29. Which of the following are true? 
private static void magic(Stream<Integer> s) {Optional o = s.filter(x -> x < 5).limit(3).max((x, y) -> x y);System.out.println(o.get());} 
Each correct answer represents a complete solution. Choose all that apply. 

magic(Stream.of(5, 10)); throws an exception. 
magic(Stream.empty()); throws an exception.

30. When printed, which String gives the same value as this text block? 
var pooh = “”””Oh, bother.” -Pooh”””.indent(1);System.out.print(pooh);

” \”Oh, bother.\” -Pooh\n” 

31. Which lines in Tadpole.java give a compiler error? 
Copy// Frog.javapackage animal;public class Frog {protected void ribbit() { }void jump() { }}//  Tadpole.javapackage other;import animal.*;public class Tadpole extends Frog {public static void  main(String[] args) {Tadpole t = new Tadpole();t.ribbit();t.jump();Frog f = new  Tadpole();f.ribbit();f.jump();} }
Each correct answer represents a complete solution. Choose all that apply.

Line 18  
Line 17
Line 15 

32. In order for all loops (with no break/continue/return statements) to run at least once but always  terminate, what must be true for the looping statement? 

The test condition must evaluate to false 

33. When creating a Locale, the country code is in uppercase. 

True

34. Suppose you have a module named com.vet. Where could you place the following module info.java file to create a valid module? 
public module com.vet {exports com.vet;}

None of these

35. Which java.io class would be best to use to write text data to a user? 

PrintWriter

36. What is the result of executing the following application? 
final var cb = new CyclicBarrier(3,() -> System.out.println(“Clean!”)); // u1ExecutorService service =  Executors.newSingleThreadExecutor();try {IntStream.generate(() -> 1).limit(12).parallel().forEach(i ->  service.submit(() -> cb.await())); // u2} finally { service.shutdown(); } 

It compiles but waits forever at runtime. 

37. What is the output of the following program?
interface HasTail { private int getTailLength(); }abstract class Puma implements HasTail {String  getTailLength() { return “4”; }}public class Cougar implements HasTail {public static void main(String[]  args) {var puma = new Puma() {};System.out.println(puma.getTailLength());}public int  getTailLength(int length) { return 2; }}

The code will not compile because of line 1. 

38. What is the result of executing the following code snippet? 
final int score1 = 8, score2 = 3;char myScore = 7;var goal = switch (myScore) {default -> {if(10>score1)  yield “unknown”;}case score1 -> “great”;case 2, 4, 6 -> “good”;case score2, 0 ->  {“bad”;}};System.out.println(goal); 

Exactly two lines need to be changed for the code to compile.

39. Assume that birds.dat exists, is accessible, and contains data for a Bird object. What are the  results of executing the following code? 
import java.io.*;public class Bird {private String name;private transient Integer age;// Getters/setters  omittedpublic static void main(String[] args) {try(var is = new ObjectInputStream(new  BufferedInputStream(new FileInputStream(“birds.dat”)))) {Bird b = 
is.readObject();System.out.println(b.age);} } }
Each correct answer represents a complete solution. Choose all that apply. 

The code will not compile because of line 12. 
The code will not compile because of lines 9–11.

40. Identify the correct statement. 

A do/while loop runs at least once 

41. If all four of these files exist, which would be chosen when asking for English US?

File_en.java 

42. What is the output of the following code snippet? 
int moon = 9, star = 2 + 2 * 3;float sun = star>10 ? 1 : 3;double jupiter = (sun + moon) – 1.0f;int mars =  –moon <= 8 ? 2 : 3;System.out.println(sun+”, “+jupiter+”, “+mars); 

3.0, 11.0, 2

43. Which functional interfaces complete the following code, presuming variable r exists? ______ x = r.negate();______ y = () -> System.out.println();______ z = (a, b) -> a – b; Each correct answer represents a part of the solution. Choose all that apply. 

Comparator<Integer> 
Predicate<Integer>
Runnable 

44. Which method defined in the Lock interface will return immediately if a lock cannot be obtained?

tryLock()

45. The body of a for loop must run at least _____ times. 

Zero

46. Which of the following statements can be used to exit a loop early?Each correct answer  represents a complete solution. Choose all that apply. 

Return 
Continue 
Break

47. Which of the following is a valid instance member of a class?

void var() {} 

48. Which is true if the contents of path1 start with the text Howdy? 
System.out.println(Files.mismatch(path1,path2)); 
Each correct answer represents a complete solution. Choose all that apply.

If the contents of path2 start with Hello, the code prints 1. 
If path2 doesn’t exist, the code throws an exception.

49. What is guaranteed to be printed by the following code? 
int[] array = {6,9,8};System.out.println(“B” + Arrays.binarySearch(array,9));System.out.println(“C” +  Arrays.compare(array,new int[] {6, 9, 8}));System.out.println(“M” + Arrays.mismatch(array,new int[]  {6, 9, 8}));
Each correct answer represents a part of the solution. Choose all that apply. 

C0 
M-1 

50. Assuming the following declarations are top-level types declared in the same file, which of these  will successfully compile?
record Music() {final int score = 10;}record Song(String lyrics) {Song {this.lyrics = lyrics + “Hello  World”;}}sealed class Dance {}record March() {@Override String toString() { return null; }}class Ballet  extends Dance {}

Dance

Other Links:

Statistics Quiz

Networking Quiz

See other websites for quiz:

Check on QUIZLET

Check on CHEGG

Leave a Reply

Your email address will not be published. Required fields are marked *