1. Which of the following statements can be used to determine if cat can be found in the list?
ArrayList<String> list = new ArrayList<>();
list.add("dog");
list.add("cat");
list.add("frog");
A. list.contains("cat")
B. list.hasObject("cat")
C. list.indexOf("cat")
D. list.indexOf(1)
a) A, C b) A, B c) A, B, C d) All of them
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) A, C
Correct! A, C
2. What is the output?
StringBuilder s1 = new StringBuilder("Java");
String s2 = "Love";
s1.append(s2);
s1.substring(4);
int foundAt = s1.indexOf(s2);
System.out.println(foundAt);
a) -1 b) 3 c) 4 d) A StringIndexOutOfBoundsException is thrown at runtime
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) 4
Correct! 4
3. What is the output?
public class Test {
public static void main(String[] args) {
int x = 10;
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");
}
}
}
a) 5 10 E1
b) 5 10 E2
c) Exception thrown
d) Compile error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Compile error ArithmeticException should be caught first
Correct! Compile error ArithmeticException should be caught first
4. Which access modifier makes a member available only to classes within the same package or subclasses?
a) private b) public c) package-private d) protected
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) protected
Correct! protected
5. What is the output?
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+" ");
}
}
}
}
a) Pen Pencil Box b) Pen Pencil c) Box d) Compilation fails
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Box
Correct! Box
6. What can be written at XX?
class SuperClass {
SuperClass(int x) {
System.out.println("Super");
}
}
public class SubClass extends SuperClass {
SubClass() {
XX
System.out.println("Sub");
}
SubClass(int x) {
super(x);
}
}
A) this(10);
B) super(10);
C) SuperClass(10);
D) super.SuperClass (10);
a) only B b) A or B c) only A d) false true
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) A or B : SuperClass does not have default constructor. The default super() should be replaced with this(x) or super(10)
Correct! A or B : SuperClass does not have default constructor. The default super() should be replaced with this(x) or super(10)
7. What is the output?
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();
}
}
a) Writing code b) Writing... c) Writing book d) Compilation fails