OCA

Description

Core Java APIs
Java Student
Quiz by Java Student, updated more than 1 year ago
Java Student
Created by Java Student about 7 years ago
2200
2

Resource summary

Question 1

Question
What is output by the following code? (Choose all that apply) 1: public class Fish { 2: public static void main(String[] args) { 3: int numFish = 4; 4: String fishType = "tuna"; 5: String anotherFish = numFish + 1; 6: System.out.println(anotherFish + " " + fishType); 7: System.out.println(numFish + " " + 1); 8: } }
Answer
  • A. 4 1
  • B. 41
  • C. 5
  • D. 5 tuna
  • E. 5tuna
  • F. 51tuna
  • G. The code does not compile.

Question 2

Question
Which of the following are output by this code? (Choose all that apply) 3: String s = "Hello"; 4: String t = new String(s); 5: if ("Hello".equals(s)) System.out.println("one"); 6: if (t == s) System.out.println("two"); 7: if (t.equals(s)) System.out.println("three"); 8: if ("Hello" == s) System.out.println("four"); 9: if ("Hello" == t) System.out.println("five");
Answer
  • A. one
  • B. two
  • C. three
  • D. four
  • E. five
  • F. The code does not compile.

Question 3

Question
Which are true statements? (Choose all that apply)
Answer
  • A. An immutable object can be modified.
  • B. An immutable object cannot be modified.
  • C. An immutable object can be garbage collected.
  • D. An immutable object cannot be garbage collected.
  • E. String is immutable.
  • F. StringBuffer is immutable.
  • G. StringBuilder is immutable.

Question 4

Question
Given: import java.util.ArrayList; import java.util.List; public class JavaSETest { public static void main(String[] args) { List<Integer> elements= new ArrayList<>(); elements.add(10); int firstElmnt= elements.get(1); System.out.println(firstElmnt); } } What is the result?
Answer
  • null
  • 10
  • 0
  • An IndexOutOfBoundsException is thrown at runtime.

Question 5

Question
Given the code fragment: // Line n1 switch (cardVal) { case 4: case 5: case 6: case 7: case 8: System.out.println("Hit"); break; case 9: case 10: case 11: System.out.println("Double"); break; case 15: case 16: System.out.println("Surrender"); break; default: System.out.println("Stand"); } Which two code fragments can be inserted at Line n1, independently, enable to print Stand?
Answer
  • int cardVal = 6;
  • int cardVal = 10;
  • int cardVal = 14;
  • int cardVal = 18;

Question 6

Question
Given: abstract class Writer { public static void write() { System.out.println("Writing..."); } } class Author extends Writer { public static void write() { System.out.println("Writing book"); } } public class Programmer extends Writer { public static void write() { System.out.println("Writing code"); } public static void main(String[] args) { Writer w = new Programmer(); w.write(); } } What is the result?
Answer
  • Writing...
  • Writing book
  • Writing code
  • Compilation fails.

Question 7

Question
Given: class SuperClass { SuperClass(int x) { System.out.println("Super"); } } public class SubClass extends SuperClass { SubClass() { //Line n1 System.out.println("Sub 2"); } } Which statement, when inserted at Line n1, enables the code to compile?
Answer
  • this(10);
  • super(10);
  • SuperClass(10);
  • super.SuperClass (10);

Question 8

Question
Given the code fragment: public class TestClass { public static void main(String[] args) { List<String> items = new ArrayList<>(); items.add("Pen"); items.add("Pencil"); items.add("Box"); for (String i : items) { if (i.indexOf("P") == 0) { continue; } else { System.out.print(i+" "); } } } } What is the result?
Answer
  • Pen Pencil Box
  • Pen Pencil
  • Box
  • Compilation fails.

Question 9

Question
Which access modifier makes a member available only to classes within the same package or subclasses?
Answer
  • private
  • protected
  • public
  • package-private

Question 10

Question
Given the code fragment: public class Test { public static void main(String[] args) { int x = 10; int y = 2; try { for (int z = 2; z >= 0; z--) { int ans = x / z; System.out.print(ans+ " "); } } catch (Exception e1) { System.out.println("E1"); } catch (ArithmeticException e1) { System.out.println("E2"); } } } What is the result?
Answer
  • E1
  • E2
  • 5 10 E1
  • Compilation fails.

Question 11

Question
Given the code fragment: StringBuilder s1 = new StringBuilder("Java"); String s2 = "Love"; s1.append(s2); s1.substring(4); int foundAt = s1.indexOf(s2); System.out.println(foundAt); What is the result?
Answer
  • -1
  • 3
  • 4
  • A StringIndexOutOfBoundsException is thrown at runtime.

Question 12

Question
Which of the following are valid Java identifiers? (Choose all that apply)
Answer
  • A. A$B
  • B. _helloWorld
  • C. true
  • D. java.lang
  • E. Public
  • F. 1980_s

Question 13

Question
What is the output of the following program? 1: public class WaterBottle { 2: private String brand; 3: private boolean empty; 4: public static void main(String[] args) { 5: WaterBottle wb = new WaterBottle(); 6: System.out.print("Empty = " + wb.empty); 7: System.out.print(", Brand = " + wb.brand); 8: } }
Answer
  • A. Line 6 generates a compiler error.
  • B. Line 7 generates a compiler error.
  • C. There is no output
  • D. Empty = false, Brand = null
  • E. Empty = false, Brand =
  • F. Empty = null, Brand = null

Question 14

Question
Which of the following are true? (Choose all that apply) 4: short numPets = 5; 5: int numGrains = 5.6; 6: String name = "Scruffy"; 7: numPets.length(); 8: numGrains.length(); 9: name.length();
Answer
  • A. Line 4 generates a compiler error.
  • B. Line 5 generates a compiler error.
  • C. Line 6 generates a compiler error.
  • D. Line 7 generates a compiler error.
  • E. Line 8 generates a compiler error.
  • F. Line 9 generates a compiler error.
  • G. The code compiles as is

Question 15

Question
Which of the following Java operators can be used with boolean variables? (Choose all that apply)
Answer
  • A. ==
  • B. +
  • C. --
  • D. !
  • E. %
  • F. <=

Question 16

Question
What data type (or types) will allow the following code snippet to compile? (Choose all that apply) byte x = 5; byte y = 10; _____ z = x + y;
Answer
  • A. int
  • B. long
  • C. boolean
  • D. double
  • E. short
  • F. byte

Question 17

Question
What is the output of the following application? 1: public class CompareValues { 2: public static void main(String[] args) { 3: int x = 0; 4: while(x++ < 10) {} 5: String message = x > 10 ? "Greater than" : false; 6: System.out.println(message+","+x); 7: } 8: }
Answer
  • A. Greater than,10
  • B. false,10
  • C. Greater than,11
  • D. false,11
  • E. The code will not compile because of line 4.
  • F. The code will not compile because of line 5.

Question 18

Question
What change would allow the following code snippet to compile? (Choose all that apply) 3: long x = 10; 4: int y = 2 * x;
Answer
  • A. No change; it compiles as is.
  • B. Cast x on line 4 to int.
  • C. Change the data type of x on line 3 to short.
  • D. Cast 2 * x on line 4 to int.
  • E. Change the data type of y on line 4 to short.
  • F. Change the data type of y on line 4 to long.

Question 19

Question
What is the output of the following code snippet? 3: java.util.List<Integer> list = new java.util.ArrayList<Integer>(); 4: list.add(10); 5: list.add(14); 6: for(int x : list) { 7: System.out.print(x + ", "); 8: break; 9: }
Answer
  • A. 10, 14,
  • B. 10, 14
  • C. 10,
  • D. The code will not compile because of line 7.
  • E. The code will not compile because of line 8.
  • F. The code contains an infinite loop and does not terminate.

Question 20

Question
What is the output of the following code snippet? 3: int x = 4; 4: long y = x * 4 - x++; 5: if(y<10) System.out.println("Too Low"); 6: else System.out.println("Just right"); 7: else System.out.println("Too High");
Answer
  • A. Too Low
  • B. Just Right
  • C. Too High
  • D. Compiles but throws a NullPointerException.
  • E. The code will not compile because of line 6.
  • F. The code will not compile because of line 7.

Question 21

Question
What is the output of the following code? 1: public class TernaryTester { 2: public static void main(String[] args) { 3: int x = 5; 4: System.out.println(x > 2 ? x < 4 ? 10 : 8 : 7); 5: }}
Answer
  • A. 5
  • B. 4
  • C. 10
  • D. 8
  • E. 7
  • F. The code will not compile because of line 4.

Question 22

Question
What is the output of the following code snippet? 3: boolean x = true, z = true; 4: int y = 20; 5: x = (y != 10) ^ (z=false); 6: System.out.println(x+", "+y+", "+z);
Answer
  • A. true, 10, true
  • B. true, 20, false
  • C. false, 20, true
  • D. false, 20, false
  • E. false, 20, true
  • F. The code will not compile because of line 5.

Question 23

Question
How many times will the following code print "Hello World"? 3: for(int i=0; i<10 ; ) { 4: i = i++; 5: System.out.println("Hello World"); 6: }
Answer
  • A. 9
  • B. 10
  • C. 11
  • D. The code will not compile because of line 3.
  • E. The code will not compile because of line 5.
  • F. The code contains an infinite loop and does not terminate.

Question 24

Question
What is the output of the following code? 3: byte a = 40, b = 50; 4: byte sum = (byte) a + b; 5: System.out.println(sum);
Answer
  • A. 40
  • B. 50
  • C. 90
  • D. The code will not compile because of line 4.
  • E. An undefined value.

Question 25

Question
What is the output of the following code? 1: public class ArithmeticSample { 2: public static void main(String[] args) { 3: int x = 5 * 4 % 3; 4: System.out.println(x); 5: }}
Answer
  • A. 2
  • B. 3
  • C. 5
  • D. 6
  • E. The code will not compile because of line 3.

Question 26

Question
What is the output of the following code snippet? 3: int x = 0; 4: String s = null; 5: if(x == s) System.out.println("Success"); 6: else System.out.println("Failure");
Answer
  • A. Success
  • B. Failure
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 5.

Question 27

Question
What is the output of the following code snippet? 3: int x1 = 50, x2 = 75; 4: boolean b = x1 >= x2; 5: if(b = true) System.out.println("Success"); 6: else System.out.println("Failure");
Answer
  • A. Success
  • B. Failure
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 5.

Question 28

Question
What is the output of the following code snippet? 3: int c = 7; 4: int result = 4; 5: result += ++c; 6: System.out.println(result);
Answer
  • A. 8
  • B. 11
  • C. 12
  • D. 15
  • E. 16
  • F. The code will not compile because of line 5.

Question 29

Question
What is the output of the following code snippet? 3: int x = 1, y = 15; 4: while x < 10 5: y––; 6: x++; 7: System.out.println(x+", "+y);
Answer
  • A. 10, 5
  • B. 10, 6
  • C. 11, 5
  • D. The code will not compile because of line 3.
  • E. The code will not compile because of line 4.
  • F. The code contains an infinite loop and does not terminate.

Question 30

Question
What is the output of the following code snippet? 3: do { 4: int y = 1; 5: System.out.print(y++ + " "); 6: } while(y <= 10);
Answer
  • A. 1 2 3 4 5 6 7 8 9
  • B. 1 2 3 4 5 6 7 8 9 10
  • C. 1 2 3 4 5 6 7 8 9 10 11
  • D. The code will not compile because of line 6.
  • E. The code contains an infinite loop and does not terminate.

Question 31

Question
What is the output of the following code snippet? 3: boolean keepGoing = true; 4: int result = 15, i = 10; 5: do { 6: i--; 7: if(i==8) keepGoing = false; 8: result -= 2; 9: } while(keepGoing); 10: System.out.println(result);
Answer
  • A. 7
  • B. 9
  • C. 10
  • D. 11
  • E. 15
  • F. The code will not compile because of line 8.

Question 32

Question
What is the output of the following code snippet? 3: int count = 0; 4: ROW_LOOP: for(int row = 1; row <=3; row++) 5: for(int col = 1; col <=2 ; col++) { 6: if(row * col % 2 == 0) continue ROW_LOOP; 7: count++; 8: } 9: System.out.println(count);
Answer
  • A. 1
  • B. 2
  • C. 3
  • D. 4
  • E. 6
  • F. The code will not compile because of line 6.

Question 33

Question
What is the result of the following code snippet? 3: int m = 9, n = 1, x = 0; 4: while(m > n) { 5: m--; 6: n += 2; 7: x += m + n; 8: } 9: System.out.println(x);
Answer
  • A. 11
  • B. 13
  • C. 23
  • D. 36
  • E. 50
  • F. The code will not compile because of line 7.

Question 34

Question
What is the result of the following code snippet? 3: final char a = 'A', d = 'D'; 4: char grade = 'B'; 5: switch(grade) { 6: case a: 7: case 'B': System.out.print("great"); 8: case 'C': System.out.print("good"); break; 9: case d: 10: case 'F': System.out.print("not good"); 11: }
Answer
  • A. great
  • B. greatgood
  • C. The code will not compile because of line 3.
  • D. The code will not compile because of line 6.
  • E. The code will not compile because of lines 6 and 9.

Question 35

Question
What is the result of the following code? 7: StringBuilder sb = new StringBuilder(); 8: sb.append("aaa").insert(1, "bb").insert(4, "ccc"); 9: System.out.println(sb);
Answer
  • A. abbaaccc
  • B. abbaccca
  • C. bbaaaccc
  • D. bbaaccca
  • E. An exception is thrown.
  • F. The code does not compile.
  • B. This example uses method chaining. After the call to append(), sb contains "aaa". That result is passed to the first insert() call, which inserts at index 1. At this point sb contains abbbaa. That result is passed to the final insert(), which inserts at index 4, resulting in abbaccca.

Question 36

Question
What is the result of the following code? 2: String s1 = "java"; 3: StringBuilder s2 = new StringBuilder("java"); 4: if (s1 == s2) 5: System.out.print("1"); 6: if (s1.equals(s2)) 7: System.out.print("2");
Answer
  • A. 1
  • B. 2
  • C. 12
  • D. No output is printed.
  • E. An exception is thrown.
  • F. The code does not compile.

Question 37

Question
What is the result of the following code? public class Lion { public void roar(String roar1, StringBuilder roar2) { roar1.concat("!!!"); roar2.append("!!!"); } public static void main(String[] args) { String roar1 = "roar"; StringBuilder roar2 = new StringBuilder("roar"); new Lion().roar(roar1, roar2); System.out.println(roar1 + " " + roar2); } }
Answer
  • A. roar roar
  • B. roar roar!!!
  • C. roar!!! roar
  • D. roar!!! roar!!!
  • E. An exception is thrown.
  • F. The code does not compile.

Question 38

Question
Which are the results of the following code? (Choose all that apply) String letters = "abcdef"; System.out.println(letters.length()); System.out.println(letters.charAt(3)); System.out.println(letters.charAt(6));
Answer
  • A. 5
  • B. 6
  • C. c
  • D. d
  • E. An exception is thrown.
  • F. The code does not compile.

Question 39

Question
Which are the results of the following code? (Choose all that apply) String numbers = "012345678"; System.out.println(numbers.substring(1, 3)); System.out.println(numbers.substring(7, 7)); System.out.println(numbers.substring(7));
Answer
  • A. 12
  • B. 123
  • C. 7
  • D. 78
  • E. A blank line.
  • F. An exception is thrown.
  • G. The code does not compile.

Question 40

Question
What is the result of the following code? 3: String s = "purr"; 4: s.toUpperCase(); 5: s.trim(); 6: s.substring(1, 3); 7: s += " two"; 8: System.out.println(s.length());
Answer
  • A. 2
  • B. 4
  • C. 8
  • D. 10
  • E. An exception is thrown.
  • F. The code does not compile.

Question 41

Question
What is the result of the following code? (Choose all that apply) 13: String a = ""; 14: a += 2; 15: a += 'c'; 16: a += false; 17: if ( a == "2cfalse") System.out.println("=="); 18: if ( a.equals("2cfalse")) System.out.println("equals");
Answer
  • A. Compile error on line 14.
  • B. Compile error on line 15.
  • C. Compile error on line 16.
  • E. ==
  • F. equals
  • G. An exception is thrown.

Question 42

Question
What is the result of the following code? 4: int total = 0; 5: StringBuilder letters = new StringBuilder("abcdefg"); 6: total += letters.substring(1, 2).length(); 7: total += letters.substring(6, 6).length(); 8: total += letters.substring(6, 5).length(); 9: System.out.println(total);
Answer
  • A. 1
  • B. 2
  • C. 3
  • D. 7
  • E. An exception is thrown.
  • F. The code does not compile.

Question 43

Question
What is the result of the following code? (Choose all that apply) StringBuilder numbers = new StringBuilder("0123456789"); numbers.delete(2, 8); numbers.append("-").insert(2, "+"); System.out.println(numbers);
Answer
  • A. 01+89–
  • B. 012+9–
  • C. 012+–9
  • D. 0123456789
  • E. An exception is thrown.
  • F. The code does not compile.

Question 44

Question
What is the result of the following code? StringBuilder b = "rumble"; b.append(4).deleteCharAt(3).delete(3, b.length() - 1); System.out.println(b);
Answer
  • A. rum
  • B. rum4
  • C. rumb4
  • D. rumble4
  • E. An exception is thrown.
  • F. The code does not compile.

Question 45

Question
Which of the following can replace line 4 to print "avaJ"? (Choose all that apply) 3: StringBuilder puzzle = new StringBuilder("Java"); 4: // INSERT CODE HERE 5: System.out.println(puzzle);
Answer
  • A. puzzle.reverse();
  • B. puzzle.append("vaJ$").substring(0, 4);
  • C. puzzle.append("vaJ$").delete(0, 3).deleteCharAt(puzzle.length() - 1);
  • D. puzzle.append("vaJ$").delete(0, 3).deleteCharAt(puzzle.length());
  • E. None of the above.

Question 46

Question
Which of these array declarations is not legal? (Choose all that apply)
Answer
  • A. int[][] scores = new int[5][];
  • B. Object[][][] cubbies = new Object[3][0][5];
  • C. String beans[] = new beans[6];
  • D. java.util.Date[] dates[] = new java.util.Date[2][];
  • E. int[][] types = new int[];
  • F. int[][] java = new int[][];

Question 47

Question
Which of these compile when replacing line 8? (Choose all that apply) 7: char[]c = new char[2]; 8: // INSERT CODE HERE
Answer
  • A. int length = c.capacity;
  • B. int length = c.capacity();
  • C. int length = c.length;
  • D. int length = c.length();
  • E. int length = c.size;
  • F. int length = c.size();
  • G. None of the above.

Question 48

Question
Which of these compile when replacing line 8? (Choose all that apply) 7: ArrayList l = new ArrayList(); 8: // INSERT CODE HERE
Answer
  • A. int length = l.capacity;
  • B. int length = l.capacity();
  • C. int length = l.length;
  • D. int length = l.length();
  • E. int length = l.size;
  • F. int length = l.size();
  • G. None of the above.

Question 49

Question
Which of the following can fill in the blank in this code to make it compile? (Choose all that apply) public class Ant { _____ void method() { } }
Answer
  • A. default
  • B. final
  • C. private
  • D. Public
  • E. String
  • F. zzz:

Question 50

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. An array has a fixed size.
  • B. An ArrayList has a fixed size.
  • C. An array allows multiple dimensions.
  • D. An array is ordered.
  • E. An ArrayList is ordered.
  • F. An array is immutable.
  • G. An ArrayList is immutable.

Question 51

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. Two arrays with the same content are equal.
  • B. Two ArrayLists with the same content are equal.
  • C. If you call remove(0) using an empty ArrayList object, it will compile successfully.
  • D. If you call remove(0) using an empty ArrayList object, it will run successfully.
  • E. None of the above.

Question 52

Question
What is the result of the following statements? 6: List<String> list = new ArrayList<String>(); 7: list.add("one"); 8: list.add("two"); 9: list.add(7); 10: for(String s : list) System.out.print(s);
Answer
  • A. onetwo
  • B. onetwo7
  • C. onetwo followed by an exception
  • D. Compiler error on line 9.
  • E. Compiler error on line 10.

Question 53

Question
Which of the following compile? (Choose all that apply)
Answer
  • A. final static void method4() { }
  • B. public final int void method() { }
  • C. private void int method() { }
  • D. static final void method3() { }
  • E. void final method() {}
  • F. void public method() { }

Question 54

Question
What is the result of the following statements? 3: ArrayList<Integer> values = new ArrayList<>(); 4: values.add(4); 5: values.add(5); 6: values.set(1, 6); 7: values.remove(0); 8: for (Integer v : values) System.out.print(v);
Answer
  • A. 4
  • B. 5
  • C. 6
  • D. 46
  • E. 45
  • F. An exception is thrown.
  • G. The code does not compile.

Question 55

Question
What is the result of the following? int[] random = { 6, -4, 12, 0, -10 }; int x = 12; int y = Arrays.binarySearch(random, x); System.out.println(y);
Answer
  • A. 2
  • B. 4
  • C. 6
  • D. The result is undefined.
  • E. An exception is thrown.
  • F. The code does not compile.

Question 56

Question
What is the result of the following? 4: List<Integer> list = Arrays.asList(10, 4, -1, 5); 5: Collections.sort(list); 6: Integer array[] = list.toArray(new Integer[4]); 7: System.out.println(array[0]);
Answer
  • A. –1
  • B. 10
  • C. Compiler error on line 4.
  • D. Compiler error on line 5.
  • E. Compiler error on line 6.
  • F. An exception is thrown.

Question 57

Question
Which of the following methods compile? (Choose all that apply)
Answer
  • A. public void methodA() { return;}
  • B. public void methodB() { return null;}
  • C. public void methodD() {}
  • D. public int methodD() { return 9;}
  • E. public int methodE() { return 9.0;}
  • F. public int methodF() { return;}
  • G. public int methodG() {return null;}

Question 58

Question
What is the result of the following? 6: String [] names = {"Tom", "Dick", "Harry"}; 7: List<String> list = names.asList(); 8: list.set(0, "Sue"); 9: System.out.println(names[0]);
Answer
  • A. Sue
  • B. Tom
  • C. Compiler error on line 7.
  • D. Compiler error on line 8.
  • E. An exception is thrown.

Question 59

Question
What is the result of the following? List<String> hex = Arrays.asList("30", "8", "3A", "FF"); Collections.sort(hex); int x = Collections.binarySearch(hex, "8"); int y = Collections.binarySearch(hex, "3A"); int z = Collections.binarySearch(hex, "4F"); System.out.println(x + " " + y + " " + z);
Answer
  • A 0 1 –2
  • B. 0 1 –3
  • C. 2 1 –2
  • D. 2 1 –3
  • E. None of the above.
  • F. The code doesn’t compile.

Question 60

Question
Which of the following compile? (Choose all that apply)
Answer
  • A. public void moreA(int... nums) {}
  • B. public void moreB(String values, int... nums) {}
  • C. public void moreC(int... nums, String values) {}
  • D. public void moreD(String... values, int... nums) {}
  • E. public void moreE(String[] values, ...int nums) {}
  • F. public void moreF(String... values, int[] nums) {}
  • G. public void moreG(String[] values, int[] nums) {}

Question 61

Question
Which of the following are true statements about the following code? (Choose all that apply) 4: List<Integer> ages = new ArrayList<>(); 5: ages.add(Integer.parseInt("5")); 6: ages.add(Integer.valueOf("6")); 7: ages.add(7); 8: ages.add(null); 9: for (int age : ages) System.out.print(age);
Answer
  • A. The code compiles.
  • B. The code throws a runtime exception.
  • C. Exactly one of the add statements uses autoboxing.
  • D. Exactly two of the add statements use autoboxing.
  • E. Exactly three of the add statements use autoboxing.

Question 62

Question
What is the result of the following? List<String> one = new ArrayList<String>(); one.add("abc"); List<String> two = new ArrayList<>(); two.add("abc"); if (one == two) System.out.println("A"); else if (one.equals(two)) System.out.println("B"); else System.out.println("C");
Answer
  • A. A
  • B. B
  • C. C
  • D. An exception is thrown.
  • E. The code does not compile.

Question 63

Question
Which of the following can be inserted into the blank to create a date of June 21, 2014? (Choose all that apply) import java.time.*; public class StartOfSummer { public static void main(String[] args) { LocalDate date = ____________________________ } }
Answer
  • A. new LocalDate(2014, 5, 21);
  • B. new LocalDate(2014, 6, 21);
  • C. LocalDate.of(2014, 5, 21);
  • D. LocalDate.of(2014, 6, 21);
  • E. LocalDate.of(2014, Calendar.JUNE, 21);
  • F. LocalDate.of(2014, Month.JUNE, 21);

Question 64

Question
Given the following method, which of the method calls return 2? (Choose all that apply) public int howMany(boolean b, boolean... b2) { return b2.length; }
Answer
  • A. howMany();
  • B. howMany(true);
  • C.howMany(true, true);
  • D. howMany(true, true, true);
  • E. howMany(true, {true});
  • F. howMany(true, {true, true});
  • G. howMany(true, new boolean[2]);

Question 65

Question
What is the output of the following code? LocalDate date = LocalDate.parse("2018-04-30", DateTimeFormatter.ISO_LOCAL_ DATE); date.plusDays(2); date.plusHours(3); System.out.println(date.getYear() + " " + date.getMonth() + " " + date.getDayOfMonth());
Answer
  • A. 2018 APRIL 2
  • B. 2018 APRIL 30
  • C. 2018 MAY 2
  • D. The code does not compile.
  • E. A runtime exception is thrown.

Question 66

Question
What is the output of the following code? LocalDate date = LocalDate.of(2018, Month.APRIL, 40); System.out.println(date.getYear() + " " + date.getMonth() + " " + date.getDayOfMonth());
Answer
  • A. 2018 APRIL 4
  • B. 2018 APRIL 30
  • C. 2018 MAY 10
  • D. Another date.
  • E. The code does not compile.
  • F. A runtime exception is thrown.

Question 67

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. Package private access is more lenient than protected access.
  • B. A public class that has private fields and package private methods is not visible to classes outside the package.
  • C. You can use access modifiers so only some of the classes in a package see a particular package private class.
  • D. You can use access modifiers to allow read access to all methods, but not any instance variables.
  • E. You can use access modifiers to restrict read access to all classes that begin with the word Test

Question 68

Question
What is the output of the following code? LocalDate date = LocalDate.of(2018, Month.APRIL, 30); date.plusDays(2); date.plusYears(3); System.out.println(date.getYear() + " " + date.getMonth() + " " + date.getDayOfMonth());
Answer
  • A. 2018 APRIL 2
  • B. 2018 APRIL 30
  • C. 2018 MAY 2
  • D. 2021 APRIL 2
  • E. 2021 APRIL 30
  • F. 2021 MAY 2
  • G. A runtime exception is thrown.

Question 69

Question
What is the output of the following code? LocalDateTime d = LocalDateTime.of(2015, 5, 10, 11, 22, 33); Period p = Period.of(1, 2, 3); d = d.minus(p); DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT); System.out.println(d.format(f));
Answer
  • A. 3/7/14 11:22 AM
  • B. 5/10/15 11:22 AM
  • C. 3/7/14
  • D. 5/10/15
  • E. 11:22 AM
  • F. The code does not compile.
  • G. A runtime exception is thrown.

Question 70

Question
What is the output of the following code? LocalDateTime d = LocalDateTime.of(2015, 5, 10, 11, 22, 33); Period p = Period.ofDays(1).ofYears(2); d = d.minus(p); DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle .SHORT); System.out.println(f.format(d));
Answer
  • A. 5/9/13 11:22 AM
  • B. 5/10/13 11:22 AM
  • C. 5/9/14
  • D. 5/10/14
  • E. The code does not compile.
  • F. A runtime exception is thrown.

Question 71

Question
Given the following my.school.ClassRoom and my.city.School class definitions, which line numbers in main() generate a compiler error? (Choose all that apply) 1: package my.school; 2: public class Classroom { 3: private int roomNumber; 4: protected String teacherName; 5: static int globalKey = 54321; 6: public int floor = 3; 7: Classroom(int r, String t) { 8: roomNumber = r; 9: teacherName = t; } } 1: package my.city; 2: import my.school.*; 3: public class School { 4: public static void main(String[] args) { 5: System.out.println(Classroom.globalKey); 6: Classroom room = new Classroom(101, ""Mrs. Anderson"); 7: System.out.println(room.roomNumber); 8: System.out.println(room.floor); 9: System.out.println(room.teacherName); } }
Answer
  • A. None, the code compiles fine.
  • B. Line 5
  • C. Line 6
  • D. Line 7
  • E. Line 8
  • F. Line 9

Question 72

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. Encapsulation uses package private instance variables.
  • B. Encapsulation uses private instance variables.
  • C. Encapsulation allows setters.
  • D. Immutability uses package private instance variables.
  • E. Immutability uses private instance variables.
  • F. Immutability allows setters.

Question 73

Question
Which are methods using JavaBeans naming conventions for accessors and mutators? (Choose all that apply)
Answer
  • A. public boolean getCanSwim() {return canSwim;}
  • B. public boolean canSwim() { return numberWings;}
  • C. public int getNumWings() { return numberWings;}
  • D. public int numWings() return numberWings;}
  • E. public void setCanSwim(boolean b) { canSwim = b;}

Question 74

Question
What is the output of the following code? 1: package rope; 2: public class Rope { 3: public static int LENGTH = 5; 4: static { 5: LENGTH = 10; 6: } canSwim = b;} 7: public static void swing() { 8: System.out.print("swing "); 9: } 10:} 1: import rope.*; 2: import static rope.Rope.*; 3: public class Chimp { 4: public static void main(String[] args) { 5: Rope.swing(); 6: new Rope().swing(); 7: System.out.println(LENGTH); 8: } 9:}
Answer
  • A. swing swing 5
  • B. swing swing 10
  • C. Compiler error on line 2 of Chimp.
  • D. Compiler error on line 5 of Chimp.
  • E. Compiler error on line 6 of Chimp.
  • F. Compiler error on line 7 of Chimp.

Question 75

Question
Which are true of the following code? (Choose all that apply) 1: public class Rope { 2: public static void swing() { 3: System.out.print("swing "); 4: } 5: public void climb() { 6: System.out.println("climb "); 7: } 8: public static void play() { 9: swing(); 10: climb(); 11: } 12: public static void main(String[] args) { 13: Rope rope = new Rope(); 14: rope.play(); 15: Rope rope2 = null; 16: rope2.play(); 17: } 18:}
Answer
  • A.The code compiles as is.
  • B. There is exactly one compiler error in the code.
  • C. There are exactly two compiler errors in the code.
  • D. If the lines with compiler errors are removed, the output is climb climb.
  • E. If the lines with compiler errors are removed, the output is swing swing.
  • F. If the lines with compile errors are removed, the code throws a NullPointerException.

Question 76

Question
What is the output of the following code? import rope.*; import static rope.Rope.*; public class RopeSwing { private static Rope rope1 = new Rope(); private static Rope rope2 = new Rope(); { System.out.println(rope1.length); } public static void main(String[] args) { rope1.length = 2; rope2.length = 8; System.out.println(rope1.length); } } package rope; public class Rope { public static int length = 0; }
Answer
  • A. 02
  • B. 08
  • C. 2
  • D. 8
  • E. The code does not compile.
  • F. An exception is thrown.

Question 77

Question
How many compiler errors are in the following code? 1: public class RopeSwing { 2: private static final String leftRope; 3: private static final String rightRope; 4: private static final String bench; 5: private static final String name = "name"; 6: static { 7: leftRope = "left"; 8: rightRope = "right"; 9: } 10: static { 11: name = "name"; 12: rightRope = "right"; 13: } 14: public static void main(String[] args) { 15: bench = "bench"; 16: } 17:}
Answer
  • A. 0
  • B. 1
  • C. 2
  • D. 3
  • E. 4
  • F. 5

Question 78

Question
Which of the following can replace line 2 to make this code compile? (Choose all that apply) 1: import java.util.*; 2: // INSERT CODE HERE 3: public class Imports { 4: public void method(ArrayList<String> list) { 5: sort(list); 6: } 7: }
Answer
  • A. import static java.util.Collections;
  • B. import static java.util.Collections.*;
  • C. import static java.util.Collections.sort(ArrayList<String>);
  • D. static import java.util.Collections;
  • E. static import java.util.Collections.*;
  • F. static import java.util.Collections.sort(ArrayList<String>);

Question 79

Question
What is the result of the following statements? 1: public class Test { 2: public void print(byte x) { 3: System.out.print("byte"); 4: } 5: public void print(int x) { 6: System.out.print("int"); 7: } 8: public void print(float x) { 9: System.out.print("float"); 10: } 11: public void print(Object x) { 12: System.out.print("Object"); 13: } 14: public static void main(String[] args) { 15: Test t = new Test(); 16: short s = 123; 17: t.print(s); 18: t.print(true); 19: t.print(6.789); 20: } 21:}
Answer
  • A. bytefloatObject
  • B. intfloatObject
  • C. byteObjectfloat
  • D. intObjectfloat
  • E. intObjectObject
  • F. byteObjectObject

Question 80

Question
What is the result of the following program? 1: public class Squares { 2: public static long square(int x) { 3: long y = x * (long) x; 4: x = -1; 5: return y; 6: } 7: public static void main(String[] args) { 8: int value = 9; 9: long result = square(value); 10: System.out.println(value); 11: } }
Answer
  • A. -1
  • B. 9
  • C. 81
  • D. Compiler error on line 9.
  • E. Compiler error on a different line.

Question 81

Question
Which of the following are output by the following code? (Choose all that apply) public class StringBuilders { public static StringBuilder work(StringBuilder a, StringBuilder b) { a = new StringBuilder("a"); b.append("b"); return a; } public static void main(String[] args) { StringBuilder s1 = new StringBuilder("s1"); StringBuilder s2 = new StringBuilder("s2"); StringBuilder s3 = work(s1, s2); System.out.println("s1 = " + s1); System.out.println("s2 = " + s2); System.out.println("s3 = " + s3); } }
Answer
  • A. s1 = a
  • B. s1 = s1
  • C. s2 = s2
  • D. s2 = s2b
  • E. s3 = a
  • F. s3 = null
  • G. The code does not compile.

Question 82

Question
Which of the following are true? (Choose 2)
Answer
  • A. this() can be called from anywhere in a constructor.
  • B. this() can be called from any instance method in the class.
  • C. this.variableName can be called from any instance method in the class.
  • D. this.variableName can be called from any static method in the class.
  • E. You must include a default constructor in the code if the compiler does not include one.
  • F. You can call the default constructor written by the compiler using this().
  • G. You can access a private constructor with the main() method.

Question 83

Question
Which of these classes compile and use a default constructor? (Choose all that apply)
Answer
  • A. public class Bird { }
  • B. public class Bird { public bird() {} }
  • C. public class Bird { public bird(String name) {} }
  • D. public class Bird { public Bird() {} }
  • E. public class Bird { Bird(String name) {} }
  • F. public class Bird { private Bird(int age) {} }
  • G. public class Bird { void Bird() { }

Question 84

Question
Which code can be inserted to have the code print 2? public class BirdSeed { private int numberBags; boolean call; public BirdSeed() { // LINE 1 call = false; // LINE 2 } public BirdSeed(int numberBags) { this.numberBags = numberBags; } public static void main(String[] args) { BirdSeed seed = new BirdSeed(); System.out.println(seed.numberBags); } }
Answer
  • A. Replace line 1 with BirdSeed(2);
  • B. Replace line 2 with BirdSeed(2);
  • C. Replace line 1 with new BirdSeed(2);
  • D. Replace line 2 with new BirdSeed(2);
  • E. Replace line 1 with this(2);
  • F. Replace line 2 with this(2);

Question 85

Question
Which of the following complete the constructor so that this code prints out 50? (Choose all that apply) public class Cheetah { int numSpots; public Cheetah(int numSpots) { // INSERT CODE HERE } public static void main(String[] args) { System.out.println(new Cheetah(50).numSpots); } }
Answer
  • A. numSpots = numSpots;
  • B. numSpots = this.numSpots;
  • C. this.numSpots = numSpots;
  • D. numSpots = super.numSpots;
  • E. super.numSpots = numSpots;
  • F. None of the above.

Question 86

Question
What is the result of the following? 1: public class Order { 2: static String result = ""; 3: { result += "c"; } 4: static 5: { result += "u"; } 6: { result += "r"; } 7: } 1: public class OrderDriver { 2: public static void main(String[] args) { 3: System.out.print(Order.result + " "); 4: System.out.print(Order.result + " "); 5: new Order(); 6: new Order(); 7: System.out.print(Order.result + " "); 8: } 9: }
Answer
  • A. curur
  • B. ucrcr
  • C. u ucrcr
  • D. u u curcur
  • E. u u ucrcr
  • F. ur ur urc
  • G. The code does not compile.

Question 87

Question
What is the result of the following? 1: public class Order { 2: String value = "t"; 3: { value += "a"; } 4: { value += "c"; } 5: public Order() { 6: value += "b"; 7: } 8: public Order(String s) { 9: value += s; 10: } 11: public static void main(String[] args) { 12: Order order = new Order("f"); 13: order = new Order(); 14: System.out.println(order.value); 15: } }
Answer
  • A. tacb
  • B. tacf
  • C. tacbf
  • D. tacfb
  • E. tacftacb
  • F. The code does not compile.
  • G. An exception is thrown.

Question 88

Question
Which of the following will compile when inserted in the following code? (Choose all that apply) public class Order3 { final String value1 = "1"; static String value2 = "2"; String value3 = "3"; { // CODE SNIPPET 1 } static { // CODE SNIPPET 2 } }
Answer
  • A. value1 = "d"; instead of // CODE SNIPPET 1
  • B. value2 = "e"; instead of // CODE SNIPPET 1
  • C. value3 = "f"; instead of // CODE SNIPPET 1
  • D. value1 = "g"; instead of // CODE SNIPPET 2
  • E. value2 = "h"; instead of // CODE SNIPPET 2
  • F. value3 = "i"; instead of // CODE SNIPPET 2

Question 89

Question
Which of the following are true about the following code? (Choose all that apply) public class Create { Create() { System.out.print("1 "); } Create(int num) { System.out.print("2 "); } Create(Integer num) { System.out.print("3 "); } Create(Object num) { System.out.print("4 "); } Create(int... nums) { System.out.print("5 "); } public static void main(String[] args) { new Create(100); new Create(1000L); } }
Answer
  • A. The code prints out 2 4.
  • B. The code prints out 3 4.
  • C. The code prints out 4 2.
  • D. The code prints out 4 4.
  • E. The code prints 3 4 if you remove the constructor Create(int num).
  • F. The code prints 4 4 if you remove the constructor Create(int num).
  • G. The code prints 5 4 if you remove the constructor Create(int num).

Question 90

Question
What is the result of the following class? 1: import java.util.function.*; 2: 3: public class Panda { 4: int age; 5: public static void main(String[] args) { 6: Panda p1 = new Panda(); 7: p1.age = 1; 8: check(p1, p -> p.age < 5); 9: } 10: private static void check(Panda panda, Predicate<Panda> pred) { 11: String result = pred.test(panda) ? "match" : "not match"; 12: System.out.print(result); 13: } }
Answer
  • A. match
  • B. not match
  • C. Compiler error on line 8.
  • D. Compiler error on line 10.
  • E. Compiler error on line 11.
  • F. A runtime exception is thrown.

Question 91

Question
What is the result of the following code? 1: interface Climb { 2: boolean isTooHigh(int height, int limit); 3: } 4: 5: public class Climber { 6: public static void main(String[] args) { 7: check((h, l) -> h.append(l).isEmpty(), 5); 8: } 9: private static void check(Climb climb, int height) { 10: if (climb.isTooHigh(height, 10)) 11: System.out.println("too high"); 12: else 13: System.out.println("ok"); 14: } 15:}
Answer
  • A. ok
  • B. too high
  • C. Compiler error on line 7.
  • D. Compiler error on line 10.
  • E. Compiler error on a different line.
  • F. A runtime exception is thrown.

Question 92

Question
Which of the following lambda expressions can fill in the blank? (Choose all that apply) List<String> list = new ArrayList<>(); list.removeIf(___________________);
Answer
  • A. s -> s.isEmpty()
  • B. s -> {s.isEmpty()}
  • C. s -> {s.isEmpty();}
  • D. s -> {return s.isEmpty();}
  • E. String s -> s.isEmpty()
  • F. (String s) -> s.isEmpty()

Question 93

Question
Which lambda can replace the MySecret class to return the same value? (Choose all that apply) interface Secret { String magic(double d); } class MySecret implements Secret { public String magic(double d) { return "Poof"; } }
Answer
  • A. caller((e) -> "Poof");
  • B. caller((e) -> {"Poof"});
  • C. caller((e) -> { String e = ""; "Poof" });
  • D. caller((e) -> { String e = ""; return "Poof"; });
  • E. caller((e) -> { String e = ""; return "Poof" });
  • F. caller((e) -> { String f = ""; return "Poof"; });

Question 94

Question
Given the following class, which of the following is true? (Choose all that apply) 1: public class Snake { 2: 3: public void shed(boolean time) { 4: 5: if (time) { 6: 7: } 8: System.out.println(result); 9: 10: } 11: }
Answer
  • A. If String result = "done"; is inserted on line 2, the code will compile.
  • B. If String result = "done"; is inserted on line 4, the code will compile.
  • C. If String result = "done"; is inserted on line 6, the code will compile.
  • D. If String result = "done"; is inserted on line 9, the code will compile.
  • E. None of the above changes will make the code compile.

Question 95

Question
Given the following classes, which of the following can independently replace INSERT IMPORTS HERE to make the code compile? (Choose all that apply) package aquarium; public class Tank { } package aquarium.jellies; public class Jelly { } package visitor; INSERT IMPORTS HERE public class AquariumVisitor { public void admire(Jelly jelly) { } }
Answer
  • A. import aquarium.*;
  • B. import aquarium.*.Jelly;
  • C. import aquarium.jellies.Jelly;
  • D. import aquarium.jellies.*;
  • E. import aquarium.jellies.Jelly.*;
  • F. None of these can make the code compile.

Question 96

Question
Given the following classes, what is the maximum number of imports that can be removed and have the code still compile? package aquarium; public class Water { } package aquarium; import java.lang.*; import java.lang.System; import aquarium.Water; import aquarium.*; public class Tank { public void print(Water water) { System.out.println(water); } }
Answer
  • A.0
  • B. 1
  • C. 2
  • D. 3
  • E. 4
  • F. Does not compile.

Question 97

Question
Given the following classes, which of the following snippets can be inserted in place of INSERT IMPORTS HERE and have the code compile? (Choose all that apply) package aquarium; public class Water { boolean salty = false; } package aquarium.jellies; public class Water { boolean salty = true; } package employee; INSERT IMPORTS HERE public class WaterFiller { Water water; }
Answer
  • A. import aquarium.*;
  • B. import aquarium.Water; import aquarium.jellies.*;
  • C. import aquarium.*; import aquarium.jellies.Water;
  • D. import aquarium.*; import aquarium.jellies.*;
  • E. import aquarium.Water; import aquarium.jellies.Water;
  • F. None of these imports can make the code compile.

Question 98

Question
Given the following class, which of the following calls print out Blue Jay? (Choose all that apply) public class BirdDisplay { public static void main(String[] name) { System.out.println(name[1]); } }
Answer
  • A. java BirdDisplay Sparrow Blue Jay
  • B. java BirdDisplay Sparrow "Blue Jay"
  • C. java BirdDisplay Blue Jay Sparrow
  • D. java BirdDisplay "Blue Jay" Sparrow
  • E. java BirdDisplay.class Sparrow "Blue Jay"
  • F. java BirdDisplay.class "Blue Jay" Sparrow
  • G. Does not compile.

Question 99

Question
Which of the following legally fill in the blank so you can run the main() method from the command line? (Choose all that apply) public static void main( )
Answer
  • A. String[] _names
  • B. String[] 123
  • C. String abc[]
  • D. String _Names[]
  • E. String... $n
  • F. String names
  • G. None of the above

Question 100

Question
Which of the following are legal entry point methods that can be run from the command line? (Choose all that apply)
Answer
  • A. private static void main(String[] args)
  • B. public static final main(String[] args)
  • C. public void main(String[] args)
  • D. public static void test(String[] args)
  • E. public static void main(String[] args)
  • F. public static main(String[] args)
  • G. None of the above.

Question 101

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. An instance variable of type double defaults to null.
  • B. An instance variable of type int defaults to null.
  • C. An instance variable of type String defaults to null.
  • D. An instance variable of type double defaults to 0.0.
  • E. An instance variable of type int defaults to 0.0.
  • F. An instance variable of type String defaults to 0.0.
  • G. None of the above.

Question 102

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. A local variable of type boolean defaults to null.
  • B. A local variable of type float defaults to 0.
  • C. A local variable of type Object defaults to null.
  • D. A local variable of type boolean defaults to false.
  • E. A local variable of type boolean defaults to true.
  • F. A local variable of type float defaults to 0.0.
  • G. None of the above.

Question 103

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. An instance variable of type boolean defaults to false.
  • B. An instance variable of type boolean defaults to true.
  • C. An instance variable of type boolean defaults to null.
  • D. An instance variable of type int defaults to 0.
  • E. An instance variable of type int defaults to 0.0.
  • F. An instance variable of type int defaults to null.
  • G. None of the above.

Question 104

Question
Given the following class in the file /my/directory/named/A/Bird.java: INSERT CODE HERE public class Bird { } Which of the following replaces INSERT CODE HERE if we compile from /my/directory? (Choose all that apply)
Answer
  • A. package my.directory.named.a;
  • B. package my.directory.named.A;
  • C. package named.a;
  • D. package named.A;
  • E. package a;
  • F. package A;
  • G. Does not compile.

Question 105

Question
Which of the following lines of code compile? (Choose all that apply)
Answer
  • A. int i1 = 1_234;
  • B. double d1 = 1_234_.0;
  • C. double d2 = 1_234._0;
  • D. double d3 = 1_234.0_;
  • E. double d4 = 1_234.0;
  • F. None of the above.

Question 106

Question
Given the following class, which of the following lines of code can replace INSERT CODE HERE to make the code compile? (Choose all that apply) public class Price { public void admission() { INSERT CODE HERE System.out.println(amount); } }
Answer
  • A. int amount = 9L;
  • B. int amount = 0b101;
  • C. int amount = 0xE;
  • D. double amount = 0xE;
  • E. double amount = 1_2_.0_0;
  • F. int amount = 1_2_;
  • G. None of the above

Question 107

Question
Which of the following are true? (Choose all that apply) public class Bunny { public static void main(String[] args) { Bunny bun = new Bunny(); } }
Answer
  • A. Bunny is a class.
  • B. bun is a class.
  • C. main is a class.
  • D. Bunny is a reference to an object.
  • E. bun is a reference to an object.
  • F. main is a reference to an object.
  • G. None of the above.

Question 108

Question
Which represent the order in which the following statements can be assembled into a program that will compile successfully? (Choose all that apply) A: class Rabbit {} B: import java.util.*; C: package animals;
Answer
  • A. A, B, C
  • B. B, C, A
  • C. C, B, A
  • D. B, A
  • E. C, A
  • F. A, C
  • G. A, B

Question 109

Question
Suppose we have a class named Rabbit. Which of the following statements are true? (Choose all that apply) 1: public class Rabbit { 2: public static void main(String[] args) { 3: Rabbit one = new Rabbit(); 4: Rabbit two = new Rabbit(); 5: Rabbit three = one; 6: one = null; 7: Rabbit four = one; 8: three = null; 9: two = null; 10: two = new Rabbit(); 11: System.gc(); 12: } }
Answer
  • A. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 6.
  • B. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 8.
  • C. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 12.
  • D. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 9.
  • E. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 11.
  • F. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 12.

Question 110

Question
What is true about the following code? (Choose all that apply) public class Bear { protected void finalize() { System.out.println("Roar!"); } Review Questions 49 c01.indd 1½ 4/2014 Page 49 public static void main(String[] args) { Bear bear = new Bear(); bear = null; System.gc(); } }
Answer
  • A. finalize() is guaranteed to be called.
  • B. finalize() might or might not be called
  • C. finalize() is guaranteed not to be called.
  • D. Garbage collection is guaranteed to run.
  • E. Garbage collection might or might not run.
  • F. Garbage collection is guaranteed not to run.
  • G. The code does not compile.

Question 111

Question
What does the following code output? 1: public class Salmon { 2: int count; 3: public void Salmon() { 4: count = 4; 5: } 6: public static void main(String[] args) { 7: Salmon s = new Salmon(); 8: System.out.println(s.count); 9: } }
Answer
  • A. 0
  • B. 4
  • C. Compilation fails on line 3.
  • D. Compilation fails on line 4.
  • E. Compilation fails on line 7.
  • F. Compilation fails on line 8.

Question 112

Question
22. Which of the following are true statements? (Choose all that apply)
Answer
  • A. Java allows operator overloading.
  • B. Java code compiled on Windows can run on Linux.
  • C. Java has pointers to specific locations in memory.
  • D. Java is a procedural language.
  • E. Java is an object-oriented language.
  • F. Java is a functional programming language

Question 113

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. javac compiles a .class file into a .java file.
  • B. javac compiles a .java file into a .bytecode file.
  • C. javac compiles a .java file into a .class file.
  • D. Java takes the name of the class as a parameter.
  • E. Java takes the name of the .bytecode file as a parameter.
  • F. Java takes the name of the .class file as a parameter.

Question 114

Question
What modifiers are implicitly applied to all interface methods? (Choose all that apply)
Answer
  • A. protected
  • B. public
  • C. static
  • D. void
  • E. abstract
  • F. default

Question 115

Question
What is the output of the following code? 1: class Mammal { 2: public Mammal(int age) { 3: System.out.print("Mammal"); 4: } 5: } 6: public class Platypus extends Mammal { 7: public Platypus() { 8: System.out.print("Platypus"); 9: } 10: public static void main(String[] args) { 11: new Mammal(5); 12: } 13: }
Answer
  • Platypus
  • Mammal
  • PlatypusMammal
  • MammalPlatypus
  • The code will not compile because of line 8.
  • The code will not compile because of line 11.

Question 116

Question
Which of the following statements can be inserted in the blank line so that the code will compile successfully? (Choose all that apply) public interface CanHop {} public class Frog implements CanHop { public static void main(String[] args) { frog = new TurtleFrog(); } } public class BrazilianHornedFrog extends Frog {} public class TurtleFrog extends Frog {}
Answer
  • A. Frog
  • B. TurtleFrog
  • C. BrazilianHornedFrog
  • D. CanHop
  • E. Object
  • F. Long

Question 117

Question
Which statement(s) are correct about the following code? (Choose all that apply) public class Rodent { protected static Integer chew() throws Exception { System.out.println("Rodent is chewing"); return 1; } } public class Beaver extends Rodent { public Number chew() throws RuntimeException { System.out.println("Beaver is chewing on wood"); return 2; } }
Answer
  • A. It will compile without issue.
  • B. It fails to compile because the type of the exception the method throws is a subclass of the type of exception the parent method throws.
  • C. It fails to compile because the return types are not covariant.
  • D. It fails to compile because the method is protected in the parent class and public in the subclass.
  • E. It fails to compile because of a static modifier mismatch between the two methods.

Question 118

Question
Which of the following may only be hidden and not overridden? (Choose all that apply)
Answer
  • A. private instance methods
  • B. protected instance methods
  • C. public instance methods
  • D. static methods
  • E. public variables
  • F. private variables

Question 119

Question
Choose the correct statement about the following code: 1: interface HasExoskeleton { 2: abstract int getNumberOfSections(); 3: } 4: abstract class Insect implements HasExoskeleton { 5: abstract int getNumberOfLegs(); 6: } 7: public class Beetle extends Insect { 8: int getNumberOfLegs() { return 6; } 9: }
Answer
  • A. It compiles and runs without issue.
  • B. The code will not compile because of line 2.
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 7.
  • E. It compiles but throws an exception at runtime.

Question 120

Question
Which of the following statements about polymorphism are true? (Choose all that apply)
Answer
  • A. A reference to an object may be cast to a subclass of the object without an explicit cast.
  • B. If a method takes a superclass of three objects, then any of those classes may be passed as a parameter to the method.
  • C. A method that takes a parameter with type java.lang.Object will take any reference.
  • D. All cast exceptions can be detected at compile-time.
  • E. By defining a public instance method in the superclass, you guarantee that the specific method will be called in the parent class at runtime.

Question 121

Question
Choose the correct statement about the following code: 1: public interface Herbivore { 2: int amount = 10; 3: public static void eatGrass(); 4: public int chew() { 5: return 13; 6: } 7: }
Answer
  • A. It compiles and runs without issue.
  • B. The code will not compile because of line 2.
  • C. The code will not compile because of line 3.
  • D. The code will not compile because of line 4.
  • E. The code will not compile because of lines 2 and 3.
  • F. The code will not compile because of lines 3 and 4.

Question 122

Question
Choose the correct statement about the following code: 1: public interface CanFly { 2: void fly(); 3: } 4: interface HasWings { 5: public abstract Object getWindSpan(); 6: } 7: abstract class Falcon implements CanFly, HasWings { 8: }
Answer
  • A. It compiles without issue.
  • B. The code will not compile because of line 2.
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 5.
  • E. The code will not compile because of lines 2 and 5.
  • F. The code will not compile because the class Falcon doesn’t implement the interface methods.

Question 123

Question
Which statements are true for both abstract classes and interfaces? (Choose all that apply)
Answer
  • All methods within them are assumed to be abstract.
  • B. Both can contain public static final variables.
  • C. Both can be extended using the extend keyword.
  • D. Both can contain default methods.
  • E. Both can contain static methods.
  • F. Neither can be instantiated directly.
  • G. Both inherit java.lang.Object.

Question 124

Question
What modifiers are assumed for all interface variables? (Choose all that apply)
Answer
  • A. public
  • B. protected
  • C. private
  • D. static
  • E. final
  • F. abstract

Question 125

Question
What is the output of the following code? 1: interface Nocturnal { 2: default boolean isBlind() { return true; } 3: } 4: public class Owl implements Nocturnal { 5: public boolean isBlind() { return false; } 6: public static void main(String[] args) { 7: Nocturnal nocturnal = (Nocturnal)new Owl(); 8: System.out.println(nocturnal.isBlind()); 9: } 10: }
Answer
  • A. true
  • B. false
  • C. The code will not compile because of line 2.
  • D. The code will not compile because of line 5.
  • E. The code will not compile because of line 7.
  • F. The code will not compile because of line 8.

Question 126

Question
What is the output of the following code? 1: class Arthropod 2: public void printName(double input) { System.out .print("Arthropod"); } 3: } 4: public class Spider extends Arthropod { 5: public void printName(int input) { System.out.print("Spider"); } 6: public static void main(String[] args) { 7: Spider spider = new Spider(); 8: spider.printName(4); 9: spider.printName(9.0); 10: } 11: }
Answer
  • A. SpiderArthropod
  • B. ArthropodSpider
  • C. SpiderSpider
  • D. ArthropodArthropod
  • E. The code will not compile because of line 5.
  • F. The code will not compile because of line 9.

Question 127

Question
Which statements are true about the following code? (Choose all that apply) 1: interface HasVocalCords { 2: public abstract void makeSound(); 3: } 4: public interface CanBark extends HasVocalCords { 5: public void bark(); 6: }
Answer
  • The CanBark interface doesn’t compile.
  • B. A class that implements HasVocalCords must override the makeSound() method.
  • C. A class that implements CanBark inherits both the makeSound() and bark() methods.
  • D. A class that implements CanBark only inherits the bark() method.
  • E. An interface cannot extend another interface.

Question 128

Question
Which of the following is true about a concrete subclass? (Choose all that apply)
Answer
  • A. A concrete subclass can be declared as abstract.
  • B. A concrete subclass must implement all inherited abstract methods.
  • C. A concrete subclass must implement all methods defined in an inherited interface.
  • D. A concrete subclass cannot be marked as final.
  • E. Abstract methods cannot be overridden by a concrete subclass.

Question 129

Question
What is the output of the following code? 1: abstract class Reptile { 2: public final void layEggs() { System.out.println("Reptile laying eggs"); } 3: public static void main(String[] args) { 4: Reptile reptile = new Lizard(); 5: reptile.layEggs(); 6: } 7: } 8: public class Lizard extends Reptile { 9: public void layEggs() { System.out.println("Lizard laying eggs"); } 10: }
Answer
  • A. Reptile laying eggs
  • B. Lizard laying eggs
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 5.
  • E. The code will not compile because of line 9.

Question 130

Question
What is the output of the following code? 1: public abstract class Whale { 2: public abstract void dive() {}; 3: public static void main(String[] args) { 4: Whale whale = new Orca(); 5: whale.dive(); 6: } 7: } 8: class Orca extends Whale { 9: public void dive(int depth) { System.out.println("Orca diving"); } 10: }
Answer
  • A. Orca diving
  • B. The code will not compile because of line 2.
  • C. The code will not compile because of line 8.
  • D. The code will not compile because of line 9.
  • E. The output cannot be determined from the code provided.

Question 131

Question
What is the output of the following code? (Choose all that apply) 1: interface Aquatic { 2: public default int getNumberOfGills(int input) { return 2; } 3: } 4: public class ClownFish implements Aquatic { 5: public String getNumberOfGills() { return "4"; } 6: public String getNumberOfGills(int input) { return "6"; } 7: public static void main(String[] args) { 8: System.out.println(new ClownFish().getNumberOfGills(-1)); 9: } 10: }
Answer
  • A. 2
  • B. 4
  • C. 6
  • D. The code will not compile because of line 5.
  • E. The code will not compile because of line 6.
  • F. The code will not compile because of line 8.

Question 132

Question
Which of the following statements can be inserted in the blank so that the code will compile successfully? (Choose all that apply) public class Snake {} public class Cobra extends Snake {} public class GardenSnake {} public class SnakeHandler { private Snake snake; public void setSnake(Snake snake) { this.snake = snake; } public static void main(String[] args) { new SnakeHandler().setSnake( ); } }
Answer
  • A. new Cobra()
  • B. new GardenSnake()
  • C. new Snake()
  • D. new Object()
  • E. new String("Snake")
  • F. null

Question 133

Question
What is the result of the following code? 1: public abstract class Bird { 2: private void fly() { System.out.println("Bird is flying"); } 3: public static void main(String[] args) { 4: Bird bird = new Pelican(); 5: bird.fly(); 6: } 7: } 8: class Pelican extends Bird { 9: protected void fly() { System.out.println("Pelican is flying"); } 10: }
Answer
  • A. Bird is flying
  • B. Pelican is flying
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 5.
  • E. The code will not compile because of line 9.

Question 134

Question
Which of the following statements are true? (Choose all that apply)
Answer
  • A. Runtime exceptions are the same thing as checked exceptions.
  • B. Runtime exceptions are the same thing as unchecked exceptions.
  • C. You can declare only checked exceptions.
  • D. You can declare only unchecked exceptions.
  • E. You can handle only Exception subclasses.

Question 135

Question
Which of the following pairs fill in the blanks to make this code compile? (Choose all that apply) 7: public void ohNo() _____ Exception { 8: _____________ Exception(); 9: }
Answer
  • A. On line 7, fill in throw
  • B. On line 7, fill in throws
  • C. On line 8, fill in throw
  • D. On line 8, fill in throw new
  • E. On line 8, fill in throws
  • F. On line 8, fill in throws new

Question 136

Question
When are you required to use a finally block in a regular try statement (not a try-with-resources)?
Answer
  • A. Never.
  • B. When the program code doesn’t terminate on its own.
  • C. When there are no catch blocks in a try statement.
  • D. When there is exactly one catch block in a try statement.
  • E. When there are two or more catch blocks in a try statement.

Question 137

Question
Which exception will the following throw? Object obj = new Integer(3); String str = (String) obj; System.out.println(str);
Answer
  • A. ArrayIndexOutOfBoundsException
  • B. ClassCastException
  • C. IllegalArgumentException
  • D. NumberFormatException
  • E. None of the above.

Question 138

Question
Which of the following exceptions are thrown by the JVM? (Choose all that apply)
Answer
  • A. ArrayIndexOutOfBoundsException
  • B. ExceptionInInitializerError
  • C. java.io.IOException
  • D. NullPointerException
  • E. NumberFormatException

Question 139

Question
What will happen if you add the statement System.out.println(5 / 0); to a working main() method?
Answer
  • A. It will not compile.
  • B. It will not run.
  • C. It will run and throw an ArithmeticException.
  • D. It will run and throw an IllegalArgumentException.
  • E. None of the above.

Question 140

Question
What is printed besides the stack trace caused by the NullPointerException from line 16? 1: public class DoSomething { 2: public void go() { 3: System.out.print("A"); 4: try { 5: stop(); 6: } catch (ArithmeticException e) { 7: System.out.print("B"); 8: } finally { 9: System.out.print("C"); 10: } 11: System.out.print("D"); 12: } 13: public void stop() { 14: System.out.print("E"); 15: Object x = null; 16: x.toString(); 17: System.out.print("F"); 18: } 19: public static void main(String[] args) { 20: new DoSomething().go(); 21: } 22: }
Answer
  • A. AE
  • B. AEBCD
  • C. AEC
  • D. AECD
  • E. No output appears other than the stack trace.

Question 141

Question
What is the output of the following snippet, assuming a and b are both 0? 3:try { 4: return a / b; 5:} catch (RuntimeException e) { 6: return -1; 7:} catch (ArithmeticException e) { 8: return 0; 9:} finally { 10: System.out.print("done"); 11:}
Answer
  • A. -1
  • B. 0
  • C. done-1
  • D. done0
  • E. The code does not compile.
  • F. An uncaught exception is thrown.

Question 142

Question
What is the output of the following program? 1: public class Laptop { 2: public void start() { 3: try { 4: System.out.print("Starting up "); 5: throw new Exception(); 6: } catch (Exception e) { 7: System.out.print("Problem "); 8: System.exit(0); 9: } finally { 10: System.out.print("Shutting down "); 11: } 12: } 13: public static void main(String[] args) { 14: new Laptop().start(); 15: } }
Answer
  • A. Starting up
  • B. Starting up Problem
  • C. Starting up Problem Shutting down
  • D. Starting up Shutting down
  • E. The code does not compile.
  • F. An uncaught exception is thrown.

Question 143

Question
What is the output of the following program? 1: public class Dog { 2: public String name; 3: public void parseName() { 4: System.out.print("1"); 5: try { 6: System.out.print("2"); 7: int x = Integer.parseInt(name); 8: System.out.print("3"); 9: } catch (NumberFormatException e) { 10: System.out.print("4"); 11: } 12: } 13: public static void main(String[] args) { 14: Dog leroy = new Dog(); 15: leroy.name = "Leroy"; 16: leroy.parseName(); 17: System.out.print("5"); 18: } }
Answer
  • A. 12
  • B. 1234
  • C. 1235
  • D. 124
  • E. 1245
  • F. The code does not compile.
  • G. An uncaught exception is thrown.

Question 144

Question
What is the output of the following program? 1: public class Cat { 2: public String name; 3: public void parseName() { 4: System.out.print("1"); 5: try { 6: System.out.print("2"); 7: int x = Integer.parseInt(name); 8: System.out.print("3"); 9: } catch (NullPointerException e) { 10: System.out.print("4"); 11: } 12: System.out.print("5"); 13: } 14: public static void main(String[] args) { 15: Cat leo = new Cat(); 16: leo.name = "Leo"; 17: leo.parseName(); 18: System.out.print("6"); 19: } 20: }
Answer
  • A. 12, followed by a stack trace for a NumberFormatException
  • B. 124, followed by a stack trace for a NumberFormatException
  • C. 12456
  • D. 12456
  • E. 1256, followed by a stack trace for a NumberFormatException
  • F. The code does not compile.
  • G. An uncaught exception is thrown.

Question 145

Question
What is printed by the following? (Choose all that apply) 1: public class Mouse { 2: public String name; 3: public void run() { 4: System.out.print("1"); 5: try { 6: System.out.print("2"); 7: name.toString(); 8: System.out.print("3"); 9: } catch (NullPointerException e) { 10: System.out.print("4"); 11: throw e; 12: } 13: System.out.print("5"); 14: } 15: public static void main(String[] args) { 16: Mouse jerry = new Mouse(); 17: jerry.run(); 18: System.out.print("6"); 19: } }
Answer
  • A. 1
  • B. 2
  • C. 3
  • D. 4
  • E. 5
  • F. 6
  • G. The stack trace for a NullPointerException

Question 146

Question
Which of the following statements are true? (Choose all that apply)
Answer
  • A. You can declare a method with Exception as the return type.
  • B. You can declare any subclass of Error in the throws part of a method declaration.
  • C. You can declare any subclass of Exception in the throws part of a method declaration.
  • D. You can declare any subclass of Object in the throws part of a method declaration.
  • E. You can declare any subclass of RuntimeException in the throws part of a method declaration.

Question 147

Question
Which of the following can be inserted on line 8 to make this code compile? (Choose all that apply) 7: public void ohNo() throws IOException { 8: // INSERT CODE HERE 9: }
Answer
  • A. System.out.println("it's ok");
  • B. throw new Exception();
  • C. throw new IllegalArgumentException();
  • D. throw new java.io.IOException();
  • E. throw new RuntimeException();

Question 148

Question
Which of the following are unchecked exceptions? (Choose all that apply)
Answer
  • A. ArrayIndexOutOfBoundsException
  • B. IllegalArgumentException
  • C. IOException
  • D. NumberFormatException
  • E. Any exception that extends RuntimeException
  • F. Any exception that extends Exception

Question 149

Question
Which scenario is the best use of an exception?
Answer
  • A. An element is not found when searching a list.
  • B. An unexpected parameter is passed into a method.
  • C. The computer caught fire.
  • D. You want to loop through a list.
  • E. You don’t know how to code a method.

Question 150

Question
Which of the following can be inserted into Lion to make this code compile? (Choose all that apply) class HasSoreThroatException extends Exception {} class TiredException extends RuntimeException {} interface Roar { void roar() throws HasSoreThroatException; } class Lion implements Roar {// INSERT CODE HERE }
Answer
  • A. public void roar(){}
  • B. public void roar() throws Exception{}
  • C. public void roar() throws HasSoreThroatException{}
  • D. public void roar() throws IllegalArgumentException{}
  • E. public void roar() throws TiredException{}

Question 151

Question
Which of the following are true? (Choose all that apply)
Answer
  • A. Checked exceptions are allowed to be handled or declared.
  • B. Checked exceptions are required to be handled or declared.
  • C. Errors are allowed to be handled or declared.
  • D. Errors are required to be handled or declared.
  • E. Runtime exceptions are allowed to be handled or declared.
  • F. Runtime exceptions are required to be handled or declared.

Question 152

Question
Which of the following can be inserted in the blank to make the code compile? (Choose all that apply) public static void main(String[] args) { try { System.out.println("work real hard"); } catch (______________e) { } catch (RuntimeException e) { } }
Answer
  • A. Exception
  • B. IOException
  • C. IllegalArgumentException
  • D. RuntimeException
  • E. StackOverflowError
  • F. None of the above.

Question 153

Question
What does the output of the following contain? (Choose all that apply) 12: public static void main(String[] args) { 13: System.out.print("a"); 14: try { 15: System.out.print("b"); 16: throw new IllegalArgumentException(); 17: } catch (IllegalArgumentException e) { 18: System.out.print("c"); 19: throw new RuntimeException("1"); 20: } catch (RuntimeException e) { 21: System.out.print("d"); 22: throw new RuntimeException("2"); 23: } finally { 24: System.out.print("e"); 25: throw new RuntimeException("3"); 26: } 27: }
Answer
  • A. abce
  • B. abde
  • C. An exception with the message set to "1"
  • D. An exception with the message set to "2"
  • E. An exception with the message set to "3"
  • F. Nothing; the code does not compile.
Show full summary Hide full summary

Similar

Ciencia Abierta en la UNC
Mario Pizzi
ROUND & TRUNC [DATES]
Adrian Mora
An Inspector Calls - Themes
mhancoc3
An Inspector calls Techniques
anya14
Chemical Symbols
Keera
GCSE AQA Citizenship Studies: Theme 1
I Turner
GCSE Chemistry C2 topic notes
imogen.shiels
Development of Cold War Tensions
c7jeremy
GCSE AQA Physics 1 Energy & Efficiency
Lilac Potato
2PR101 1.test - 1. část
Nikola Truong
Creating Mind Maps with GoConqr
Sarah Egan