Pregunta 1
Pregunta
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: } }
Pregunta 2
Pregunta
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");
Pregunta 3
Pregunta
Which are true statements? (Choose all that apply)
Respuesta
-
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.
Pregunta 4
Pregunta
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?
Pregunta 5
Pregunta
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?
Respuesta
-
int cardVal = 6;
-
int cardVal = 10;
-
int cardVal = 14;
-
int cardVal = 18;
Pregunta 6
Pregunta
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?
Respuesta
-
Writing...
-
Writing book
-
Writing code
-
Compilation fails.
Pregunta 7
Pregunta
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?
Respuesta
-
this(10);
-
super(10);
-
SuperClass(10);
-
super.SuperClass (10);
Pregunta 8
Pregunta
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?
Respuesta
-
Pen Pencil Box
-
Pen Pencil
-
Box
-
Compilation fails.
Pregunta 9
Pregunta
Which access modifier makes a member available only to classes within the same package or subclasses?
Respuesta
-
private
-
protected
-
public
-
package-private
Pregunta 10
Pregunta
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?
Respuesta
-
E1
-
E2
-
5 10 E1
-
Compilation fails.
Pregunta 11
Pregunta
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?
Pregunta 12
Pregunta
Which of the following are valid Java identifiers? (Choose all that apply)
Respuesta
-
A. A$B
-
B. _helloWorld
-
C. true
-
D. java.lang
-
E. Public
-
F. 1980_s
Pregunta 13
Pregunta
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: } }
Respuesta
-
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
Pregunta 14
Pregunta
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();
Respuesta
-
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
Pregunta 15
Pregunta
Which of the following Java operators can be used with boolean variables? (Choose all that
apply)
Respuesta
-
A. ==
-
B. +
-
C. --
-
D. !
-
E. %
-
F. <=
Pregunta 16
Pregunta
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;
Respuesta
-
A. int
-
B. long
-
C. boolean
-
D. double
-
E. short
-
F. byte
Pregunta 17
Pregunta
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: }
Pregunta 18
Pregunta
What change would allow the following code snippet to compile? (Choose all that apply)
3: long x = 10;
4: int y = 2 * x;
Respuesta
-
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.
Pregunta 19
Pregunta
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: }
Respuesta
-
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.
Pregunta 20
Pregunta
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");
Respuesta
-
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.
Pregunta 21
Pregunta
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: }}
Pregunta 22
Pregunta
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);
Pregunta 23
Pregunta
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: }
Respuesta
-
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.
Pregunta 24
Pregunta
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);
Pregunta 25
Pregunta
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: }}
Pregunta 26
Pregunta
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");
Pregunta 27
Pregunta
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");
Pregunta 28
Pregunta
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);
Pregunta 29
Pregunta
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);
Respuesta
-
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.
Pregunta 30
Pregunta
What is the output of the following code snippet?
3: do {
4: int y = 1;
5: System.out.print(y++ + " ");
6: } while(y <= 10);
Respuesta
-
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.
Pregunta 31
Pregunta
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);
Pregunta 32
Pregunta
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);
Pregunta 33
Pregunta
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);
Pregunta 34
Pregunta
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: }
Respuesta
-
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.
Pregunta 35
Pregunta
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);
Respuesta
-
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.
Pregunta 36
Pregunta
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");
Pregunta 37
Pregunta
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);
} }
Pregunta 38
Pregunta
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));
Pregunta 39
Pregunta
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));
Pregunta 40
Pregunta
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());
Pregunta 41
Pregunta
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");
Respuesta
-
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.
Pregunta 42
Pregunta
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);
Pregunta 43
Pregunta
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);
Pregunta 44
Pregunta
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);
Pregunta 45
Pregunta
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);
Respuesta
-
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.
Pregunta 46
Pregunta
Which of these array declarations is not legal? (Choose all that apply)
Respuesta
-
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[][];
Pregunta 47
Pregunta
Which of these compile when replacing line 8? (Choose all that apply)
7: char[]c = new char[2];
8: // INSERT CODE HERE
Respuesta
-
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.
Pregunta 48
Pregunta
Which of these compile when replacing line 8? (Choose all that apply)
7: ArrayList l = new ArrayList();
8: // INSERT CODE HERE
Respuesta
-
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.
Pregunta 49
Pregunta
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() { }
}
Respuesta
-
A. default
-
B. final
-
C. private
-
D. Public
-
E. String
-
F. zzz:
Pregunta 50
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 51
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 52
Pregunta
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);
Respuesta
-
A. onetwo
-
B. onetwo7
-
C. onetwo followed by an exception
-
D. Compiler error on line 9.
-
E. Compiler error on line 10.
Pregunta 53
Pregunta
Which of the following compile? (Choose all that apply)
Respuesta
-
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() { }
Pregunta 54
Pregunta
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);
Pregunta 55
Pregunta
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);
Respuesta
-
A. 2
-
B. 4
-
C. 6
-
D. The result is undefined.
-
E. An exception is thrown.
-
F. The code does not compile.
Pregunta 56
Pregunta
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]);
Respuesta
-
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.
Pregunta 57
Pregunta
Which of the following methods compile? (Choose all that apply)
Respuesta
-
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;}
Pregunta 58
Pregunta
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]);
Respuesta
-
A. Sue
-
B. Tom
-
C. Compiler error on line 7.
-
D. Compiler error on line 8.
-
E. An exception is thrown.
Pregunta 59
Pregunta
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);
Pregunta 60
Pregunta
Which of the following compile? (Choose all that apply)
Respuesta
-
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) {}
Pregunta 61
Pregunta
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);
Respuesta
-
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.
Pregunta 62
Pregunta
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");
Pregunta 63
Pregunta
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 = ____________________________
}
}
Respuesta
-
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);
Pregunta 64
Pregunta
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;
}
Respuesta
-
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]);
Pregunta 65
Pregunta
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());
Pregunta 66
Pregunta
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());
Pregunta 67
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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
Pregunta 68
Pregunta
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());
Pregunta 69
Pregunta
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));
Pregunta 70
Pregunta
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));
Pregunta 71
Pregunta
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); } }
Pregunta 72
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 73
Pregunta
Which are methods using JavaBeans naming conventions for accessors and mutators? (Choose all that apply)
Respuesta
-
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;}
Pregunta 74
Pregunta
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:}
Respuesta
-
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.
Pregunta 75
Pregunta
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:}
Respuesta
-
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.
Pregunta 76
Pregunta
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;
}
Pregunta 77
Pregunta
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:}
Respuesta
-
A. 0
-
B. 1
-
C. 2
-
D. 3
-
E. 4
-
F. 5
Pregunta 78
Pregunta
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: }
Respuesta
-
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>);
Pregunta 79
Pregunta
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:}
Respuesta
-
A. bytefloatObject
-
B. intfloatObject
-
C. byteObjectfloat
-
D. intObjectfloat
-
E. intObjectObject
-
F. byteObjectObject
Pregunta 80
Pregunta
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: } }
Pregunta 81
Pregunta
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);
}
}
Pregunta 82
Pregunta
Which of the following are true? (Choose 2)
Respuesta
-
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.
Pregunta 83
Pregunta
Which of these classes compile and use a default constructor? (Choose all that apply)
Respuesta
-
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() { }
Pregunta 84
Pregunta
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);
} }
Respuesta
-
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);
Pregunta 85
Pregunta
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);
}
}
Respuesta
-
A. numSpots = numSpots;
-
B. numSpots = this.numSpots;
-
C. this.numSpots = numSpots;
-
D. numSpots = super.numSpots;
-
E. super.numSpots = numSpots;
-
F. None of the above.
Pregunta 86
Pregunta
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: }
Pregunta 87
Pregunta
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: } }
Pregunta 88
Pregunta
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
}
}
Respuesta
-
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
Pregunta 89
Pregunta
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);
}
}
Respuesta
-
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).
Pregunta 90
Pregunta
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: } }
Respuesta
-
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.
Pregunta 91
Pregunta
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:}
Respuesta
-
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.
Pregunta 92
Pregunta
Which of the following lambda expressions can fill in the blank? (Choose all that apply)
List<String> list = new ArrayList<>();
list.removeIf(___________________);
Respuesta
-
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()
Pregunta 93
Pregunta
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";
}
}
Respuesta
-
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"; });
Pregunta 94
Pregunta
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: }
Respuesta
-
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.
Pregunta 95
Pregunta
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) { } }
Respuesta
-
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.
Pregunta 96
Pregunta
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); } }
Respuesta
-
A.0
-
B. 1
-
C. 2
-
D. 3
-
E. 4
-
F. Does not compile.
Pregunta 97
Pregunta
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;
}
Respuesta
-
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.
Pregunta 98
Pregunta
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]);
} }
Respuesta
-
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.
Pregunta 99
Pregunta
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( )
Respuesta
-
A. String[] _names
-
B. String[] 123
-
C. String abc[]
-
D. String _Names[]
-
E. String... $n
-
F. String names
-
G. None of the above
Pregunta 100
Pregunta
Which of the following are legal entry point methods that can be run from the command
line? (Choose all that apply)
Respuesta
-
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.
Pregunta 101
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 102
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 103
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 104
Pregunta
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)
Pregunta 105
Pregunta
Which of the following lines of code compile? (Choose all that apply)
Respuesta
-
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.
Pregunta 106
Pregunta
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);
} }
Pregunta 107
Pregunta
Which of the following are true? (Choose all that apply)
public class Bunny {
public static void main(String[] args) {
Bunny bun = new Bunny();
} }
Respuesta
-
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.
Pregunta 108
Pregunta
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;
Respuesta
-
A. A, B, C
-
B. B, C, A
-
C. C, B, A
-
D. B, A
-
E. C, A
-
F. A, C
-
G. A, B
Pregunta 109
Pregunta
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: } }
Respuesta
-
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.
Pregunta 110
Pregunta
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();
} }
Respuesta
-
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.
Pregunta 111
Pregunta
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: } }
Respuesta
-
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.
Pregunta 112
Pregunta
22. Which of the following are true statements? (Choose all that apply)
Respuesta
-
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
Pregunta 113
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 114
Pregunta
What modifiers are implicitly applied to all interface methods? (Choose all that apply)
Respuesta
-
A. protected
-
B. public
-
C. static
-
D. void
-
E. abstract
-
F. default
Pregunta 115
Pregunta
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: }
Pregunta 116
Pregunta
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 {}
Respuesta
-
A. Frog
-
B. TurtleFrog
-
C. BrazilianHornedFrog
-
D. CanHop
-
E. Object
-
F. Long
Pregunta 117
Pregunta
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;
}
}
Respuesta
-
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.
Pregunta 118
Pregunta
Which of the following may only be hidden and not overridden? (Choose all that apply)
Respuesta
-
A. private instance methods
-
B. protected instance methods
-
C. public instance methods
-
D. static methods
-
E. public variables
-
F. private variables
Pregunta 119
Pregunta
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: }
Respuesta
-
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.
Pregunta 120
Pregunta
Which of the following statements about polymorphism are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 121
Pregunta
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: }
Respuesta
-
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.
Pregunta 122
Pregunta
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: }
Respuesta
-
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.
Pregunta 123
Pregunta
Which statements are true for both abstract classes and interfaces? (Choose all that apply)
Respuesta
-
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.
Pregunta 124
Pregunta
What modifiers are assumed for all interface variables? (Choose all that apply)
Respuesta
-
A. public
-
B. protected
-
C. private
-
D. static
-
E. final
-
F. abstract
Pregunta 125
Pregunta
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: }
Respuesta
-
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.
Pregunta 126
Pregunta
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: }
Pregunta 127
Pregunta
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: }
Respuesta
-
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.
Pregunta 128
Pregunta
Which of the following is true about a concrete subclass? (Choose all that apply)
Respuesta
-
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.
Pregunta 129
Pregunta
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: }
Respuesta
-
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.
Pregunta 130
Pregunta
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: }
Respuesta
-
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.
Pregunta 131
Pregunta
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: }
Respuesta
-
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.
Pregunta 132
Pregunta
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( );
}
}
Respuesta
-
A. new Cobra()
-
B. new GardenSnake()
-
C. new Snake()
-
D. new Object()
-
E. new String("Snake")
-
F. null
Pregunta 133
Pregunta
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: }
Respuesta
-
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.
Pregunta 134
Pregunta
Which of the following statements are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 135
Pregunta
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: }
Respuesta
-
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
Pregunta 136
Pregunta
When are you required to use a finally block in a regular try statement (not a try-with-resources)?
Respuesta
-
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.
Pregunta 137
Pregunta
Which exception will the following throw?
Object obj = new Integer(3);
String str = (String) obj;
System.out.println(str);
Pregunta 138
Pregunta
Which of the following exceptions are thrown by the JVM? (Choose all that apply)
Pregunta 139
Pregunta
What will happen if you add the statement System.out.println(5 / 0); to a working main() method?
Pregunta 140
Pregunta
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: }
Pregunta 141
Pregunta
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:}
Pregunta 142
Pregunta
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: } }
Respuesta
-
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.
Pregunta 143
Pregunta
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: } }
Pregunta 144
Pregunta
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: }
Respuesta
-
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.
Pregunta 145
Pregunta
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: } }
Pregunta 146
Pregunta
Which of the following statements are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 147
Pregunta
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: }
Respuesta
-
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();
Pregunta 148
Pregunta
Which of the following are unchecked exceptions? (Choose all that apply)
Respuesta
-
A. ArrayIndexOutOfBoundsException
-
B. IllegalArgumentException
-
C. IOException
-
D. NumberFormatException
-
E. Any exception that extends RuntimeException
-
F. Any exception that extends Exception
Pregunta 149
Pregunta
Which scenario is the best use of an exception?
Respuesta
-
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.
Pregunta 150
Pregunta
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
}
Respuesta
-
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{}
Pregunta 151
Pregunta
Which of the following are true? (Choose all that apply)
Respuesta
-
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.
Pregunta 152
Pregunta
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) {
}
}
Pregunta 153
Pregunta
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: }
Respuesta
-
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.