Null will printed as the value of the variable name

  • No School
  • AA 1
  • rinothemaster
  • 157

This preview shows page 142 - 147 out of 157 pages.

null will printed as the value of the variable name, and at line 8 false will be printed. Hence Option C is correct. As explained above options A, B and D are incorrect. Code compiles fine so option E is incorrect. Question No : 215 Given: A. X XX B. X Y X C. Y Y X D. Y Y Y Answer: D public class X implements Z { public String toString() { return "X "; } public static void main(String[] args) { Y myY = new Y(); X myX = myY; Z myZ = myX; System.out.print(myX); System.out.print((Y)myX); System.out.print(myZ); } } class Y extends X { public String toString(){ return "Y "; } } interface Z {}
Image of page 142
Question No : 216 Which of the following can fill in the blank in this code to make it compile? A. abstract B. final C. private D. default E. int Answer: B Explanation: public 或者 protected 都可以。 Question No : 217 Given the code fragment: What is the result? A. 100 B. 101 C. 102 D. 103 E. Compilation fails public class Exam { void method() {} } public class OCAJP extends Exam { ______ void method() {} } public class Test { public static void main(String[] args) { int x = 100; int a = x++; int b = ++x; int c = x++; int d = (a < b) ? (a < c) ? a: (b <c ) ? b: c; System.out.println(d); } }
Image of page 143
Answer: E int d = (a < b) ? (a < c) ? a: (b <c ) ? b: c; 改为 int d = (a < b) ? (a < c) ? a: (b <c ) ? b: c : c; 则选 A Question No : 218 Given: Which change will enable the code to compile? A. Adding the public modifier to the declaration of method1 at line n1 B. Removing the public modifier from the definition of method1 at line n2 C. Changing the private modifier on the declaration of method 2 public at line n3 D. Changing the line n4 DoClass doi = new DoClass ( ); Answer: C Explanation: Private members (both fields and methods) are only accessible inside the class they are declared or inside inner classes. private keyword is one of four access modifier provided by Java and its a most restrictive among all four e.g. public, default(package), protected and private. package p1; public interface DoInterface { void method1(int n1); // line n1 void method2(int n2); } package p3; import p1.DoInterface; public class DoClass implements DoInterface { public DoClass(int p1) { } public void method1(int p1) { } // line n2 private void method2(int p1) { } // line n3 } import p1.DoInterface; import p3.DoClass; public class Test { public static void main(String[] args) { DoInterface doi= new DoClass(100); // line n4 doi.method1(100); doi.method2(100); } }
Image of page 144
Question No : 219 Given the code fragment: Which statement is true? A. After line 8, three objects are eligible for garbage collection B. After line 8, two objects are eligible for garbage collection C. After line 8, one object is eligible for garbage collection D. After line 8, none of the objects are eligible for garbage collection Answer: C Question No : 220 Given: class Student { String name; int age; } And, 1. public class Test { 2. public static void main(String[] args) { 3. Student s1 = new Student(); 4. Student s2 = new Student(); 5. Student s3 = new Student(); 6. s1 = s3; 7. s3 = s2; 8. s2 = null; 9. } 10. }
Image of page 145
What is the result? A. 0 unknown 0 unknown B. 1200 Strawberry 1200 Strawberry C. 1200 Strawberry 1230 Chocolate D. Compilation fails Answer: C Question No : 221 Which of the following exception will be thrown due to the statement given here?
Image of page 146
Image of page 147

You've reached the end of your free preview.

Want to read all 157 pages?

  • Fall '19
  • Madrid Metro, public class Test, public Circle

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture