Exam 808: Java SE 8
1. Given:
import [Link];
import [Link];
public class JavaSETest {
public static void main(String[] args) {
List<Integer> elements = new ArrayList<>();
[Link](10);
int firstElmnt = [Link](1);
[Link](firstElmnt);
}
}
What is the result?
A) null
B) 10
C) 0
D) An IndexOutOfBoundsException is thrown at runtime.
2. Given the code fragment:
// Line n1
switch (cardVal) {
case 4: case 5: case 6:
case 7: case 8:
[Link]("Hit");
break;
case 9: case 10: case 11:
[Link]("Double");
break;
case 15: case 16:
[Link]("Surrender");
break;
default:
[Link]("Stand");
}
Which two code fragments can be inserted at Line n1, independently, enable to print Stand?
A) int cardVal = 6;
B) int cardVal = 10;
C) int cardVal = 14;
D) int cardVal = 18;
3. Given:
abstract class Writer {
public static void write() {
[Link]("Writing...");
}
}
class Author extends Writer {
public static void write() {
[Link]("Writing book");
}
}
public class Programmer extends Writer {
public static void write() {
[Link]("Writing code");
}
public static void main(String[] args) {
Writer w = new Programmer();
[Link]();
}
}
What is the result?
A) Writing...
B) Writing book
C) Writing code
D) Compilation fails.
4. Given:
class SuperClass {
SuperClass(int x) {
[Link]("Super");
}
}
public class SubClass extends SuperClass {
SubClass() {
// Line n1
[Link]("Sub 2");
}
}
Which statement, when inserted at Line n1, enables the code to compile?
A) this(10);
B) super(10);
C) SuperClass(10);
D) [Link] (10);
5. Given the code fragment:
public class TestClass {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
[Link]("Pen");
[Link]("Pencil");
[Link]("Box");
for (String i : items) {
if ([Link]("P") == 0) {
continue;
} else {
[Link](i+" ");
}
}
}
}
What is the result?
A) Pen Pencil Box
B) Pen Pencil
C) Box
D) Compilation fails.
6. Which access modifier makes a member available only to classes within the same package or
subclasses?
A) private
B) protected
C) public
D) package-private
7. 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;
[Link](ans+ " ");
}
} catch (Exception e1) {
[Link]("E1");
} catch (ArithmeticException e1) {
[Link]("E2");
}
}
}
What is the result?
A) E1
B) E2
C) 5 10 E1
D) Compilation fails.
8. Given the code fragment:
StringBuilder s1 = new StringBuilder("Java");
String s2 = "Love";
[Link](s2);
[Link](4);
int foundAt = [Link](s2);
[Link](foundAt);
What is the result?
A) -1
B) 3
C) 4
D) A StringIndexOutOfBoundsException is thrown at runtime.
Answers:
1) D
2) C and D
3) A
4) B
5) C
6) B
7) D
8) C