Question1: Which two statements are true about Java byte code? (Choose two.)
A. It can be serialized across network.
B. It can run on any platform that has a Java compiler.
C. It can run on any platform.
D. It has “.java” extension.
E. It can run on any platform that has the Java Runtime Environment.
Answer: A,E
Question2: Given below:
public interface Readable { public void readBook(); public void setBookMark(); } public abstract class Book implements Readable { // Line N1 public void readBook() { } //Line N2 } public class EBook extends Book { //Line N3 public void readBook() { System.out.println("ebook"); } //Line N4 public static void main(String[] args) { Book book1 = new EBook(); book1.readBook(); } }
Which Option enables the code to compile:
A. Replace the code at line N1 with class Book implements Runnable{
B. At Line N2, Insert public abstract void setBookMark()
C. Replace the code at Line N3 with abstract class EBook extends Book{
D. At Line N4, insert public void setBookMark()
Answer: D
Question3: Given below code:
public static void main(String[] args) { Short s1 = 200; Integer s2 = 400; Long s3 = (long)s1+s2; // Line N1 String s4 = (String)(s3*s2); // Line N2 System.out.println("Sum is"+s4); }
What is the result?
A. Sum is 600
B. Compilation fails at line n1.
C. Compilation fails at line n2.
D. A ClassCastException is thrown at line n1.
E. A ClassCastException is thrown at line n2.
Answer: C . It will give compilation error that “can not cast from long to String”
Question4: Given below code: Which two class definitions fail to compile? (Choose two.)
abstract class A3 { private static int i; public void doStuff() {} public A3() { } } final class A1 { public A1() { } } private class A2 { private static int i; private A2() {} } class A4 { protected static final int i = 10; private A4() { } } final abstract class A5 { protected static int i; void doStuff() {}; abstract void doIt(); }
A. A1
B. A2
C. A3
D. A4
E. A5
Answer: B , E
Explanation: Class A2 and A5 are failed to compile
A1 Class : Illegal modifier for the class A2; only public, abstract & final are permitted
A5 Class: The class A5 can be either abstract or final, not both
Question5: Give the below code:
public class A { public void test() { System.out.println("A"); } } public class B extends A{ public void test() { System.out.println("B"); } } public class C extends A { public void test() { System.out.println("A"); } public static void main(String[] args) { A b1 = new A(); A b2 = new C(); b1 = (A)b2; // Line N1 A b3 = (B) b2; // Line N2 b1.test(); b3.test(); } }
What is the result?
A. AB
B. AC
C. CC
D. A ClassCastException is thrown only at line n1.
E. A ClassCastException is thrown only at line n2.
Answer: E
Exception in thread “main” java.lang.ClassCastException: class inheritanceeg.C cannot be cast to class inheritanceeg.B (inheritanceeg.C and inheritanceeg.B are in module TestJavaSE of loader ‘app’)
at TestJavaSE/inheritanceeg.C.main(C.java:11)
Question6: Given below code:
public static void main(String[] args) { ArrayList<Integer> points = new ArrayList<Integer>(); points.add(1); points.add(2); points.add(3); points.add(4); points.add(null); points.remove(1); points.remove(null); System.out.println(points); }
What is the result?
A. A NullPointerException is thrown at runtim
B. [1, 2, 4]
C. [1, 2, 4, null]
D. [1, 3, 4, null]
E. [1, 3, 4]
F. Compilation fails.
Answer: E
- remove(1) -> removes at index1 which is element 2
- remove(null) -> This removes object from list so it will remove null object