Wednesday, 1 November 2017

#14 to #20. SCJP/OCJP Exams Questions & Answers-


14.Public interface A{
Public void doSomething(String thing)
}
Public class AImpl implements A{
Public void doSomething(String msg){}
}
Public class B{
Public A doit(){
//More code here
}
 Public String execute(){
//More code here
}
 }
Public class C{
Public AImpl doit(){
//More code here
}
 Public Object execute(){
//More code here
}
}
A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line 2.
C. Compilation of class C will fail because of an error in line 6.
D. Compilation of class AImpl will fail because of an error in line 2.
Correct Answer: C

15. Given:
11. public class Rainbow {
12. public enum MyColor {
13. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);
14. private final int rgb;
15. MyColor(int rgb) { this.rgb = rgb; }
16. public int getRGB() { return rgb; }
17. };
18. public static void main(String[] args) {
19. // insert code here
20. }
21. }
Which code fragment, inserted at line 19, allows the Rainbow class to compile?
A. MyColor skyColor = BLUE;
B. MyColor treeColor = MyColor.GREEN;
C. if(RED.getRGB() < BLUE.getRGB()) { }
D. Compilation fails due to other error(s) in the code.
E. MyColor purple = new MyColor(0xff00ff);
F. MyColor purple = MyColor.BLUE + MyColor.RED;

Correct Answer: B

16. What is the result?
11.Class Person{
12.String name=”No name”;
13.Public Person(String nm){
      Name=nm;
14.}
15.}
16.Class Employee extends person{
17.String empID=”0000”;
18.Public Employee(String id){empID=id;
19.}
20.}
21.Public class EmployeeTest{
22.Public static void main(String[] args){
23.Employee e=new Employee(“4321”);
24.System.out.println(e.empID);
25. }
26.}
A. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.

Correct Answer: D


17. Given:
11. class Mud {
12. // insert code here
13. System.out.println("hi");
 }
 }
And the following five fragments:
public static void main(String...a) {
public static void main(String.* a) {
public static void main(String... a) {
public static void main(String[]... a) {
public static void main(String...[] a) {
How many of the code fragments, inserted independently at line 12, compile?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5

Correct Answer: D

18.Given:
5. class Atom {
6. Atom() { System.out.print("atom "); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super("granite ");
14. new Rock("granite ");
15. }
16. public static void main(String[] a) { new Mountain(); }
17. }
What is the result?
A. Compilation fails.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom granite atom granite

Correct Answer: F

19. Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return "test"; }
6. });
7. }
8. }
What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.

Correct Answer: A

20. Given:
11. public static void parse(String str) {
12. try {
13. float f = Float.parseFloat(str);
14. } catch (NumberFormatException nfe) {
15. f = 0;
16. } finally {
17. System.out.println(f);
18. }
19. }
20. public static void main(String[] args) {
21. parse("invalid");
22. }
What is the result?
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.

Correct  Answer: B

No comments:

Post a Comment

#45 to #51 SCJP/OCJP Exams Questions & Answers-

45. Given: 1. public class A { 2. public void doit() { 3. } 4. public String doit() { 5. return "a"; 6. } 7. ...