Sunday 1 January 2012

Java Objective Questions -Packages and Access Controls

1 )
// Class A is declared in a file named A.java.
package com.dan.chisholm;
public class A {
  public void m1() {System.out.print("A.m1, ");}
  private void m2() {System.out.print("A.m2, ");}
  }
// Class D is declared in a file named D.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class D {
  public static void main(String[] args) {
    A a = new A();
    a.m1();  // 1
    a.m2();  // 2
    }}
What is the result of attempting to compile and run the program? 
    • Prints: A.m1, A.m2,
    • Compile-time error at 1
    • Compile-time error at 2
    • None of the above
      Answer : Compile-time error at 2.
      Private data members and member functions can be accessed only with in the class.So class D can't access the private member function,m2() of class A.
      2 )
      // Class A is declared in a file named A.java.
      package com.dan.chisholm;
      public class A {
        public void m1() {System.out.print("A.m1, ");}
        void m2() {System.out.print("A.m2, ");}
      }
      // Class D is declared in a file named D.java.
      package com.dan.chisholm.other;
      import com.dan.chisholm.A;
      public class D {
        public static void main(String[] args) {
          A a = new A();
          a.m1();  // 1
          a.m2();  // 2
         }}
      What is the result of attempting to compile and run the program? 
      • Prints: A.m1, A.m2,
      • Compile-time error at 1
      • Compile-time error at 2
      • None of the above 
      Answer : Compile-time error at 2.
      The data members and member functions with the 'default' access can be used only within the same package.So class D can't access the function m2() with default access declared in class A.
      3 ) 
      // Class A is declared in a file named A.java.
      package com.dan.chisholm;
      public class A {
        public void m1() {System.out.print("A.m1, ");}
        protected void m2() {System.out.print("A.m2, ");}
        }
      // Class D is declared in a file named D.java.
      package com.dan.chisholm.other;
      import com.dan.chisholm.A;
      public class D {
        public static void main(String[] args) {
          A a = new A();
          a.m1();  // 1
          a.m2();  // 2
         }}
      What is the result of attempting to compile and run the program?  
      • Prints: A.m1, A.m2, 
      • Compile-time error at 1
      • Compile-time error at 2
      • None of the above   
      Answer : Compile-time error at 2.
      Since class D does not extend class A, class D does not have access to the protected method, m2,of class A.Class D can access protected method of class A only through inheritance.
      4 )
      package testpkg.p1;
      public class ParentUtil 
      {public int x = 420;protected int doStuff() { return x; }}
      
      package testpkg.p2;
      import testpkg.p1.ParentUtil;
      public class ChildUtil extends ParentUtil 
      {  public static void main(String [] args){new ChildUtil().callStuff();}
          void callStuff() 
          {   S.o.p("this " + this.doStuff() ); /* Line 18 */
              ParentUtil p = new ParentUtil();
              S.o.p(" parent " + p.doStuff() ); /* Line 20 */
          }}
      which statement is true?
      • The code compiles and runs, with output this 420 parent 420
      • If line 18 is removed, the code will compile and run.
      • If line 20 is removed, the code will compile and run.
      • An exception is thrown at runtime.
      Answer :  If line 20 is removed, the code will compile and run.
      The ParentUtil instance p cannot be used to access the doStuff() method. Because doStuff() has protected access, and the ChildUtil class is not in the same package as the ParentUtil class, doStuff() can be accessed only by instances of the ChildUtil class (a subclass of ParentUtil).
      5 )
      // Class A is declared in a file named A.java.
      package com.dan.chisholm;
      public class A {
      A(){ }
        public void m1() {System.out.print("A.m1, ");}
         }
      // Class D is declared in a file named D.java.
      package com.dan.chisholm.other;
      import com.dan.chisholm.A;
      public class D {
        public static void main(String[] args) {
          A a = new A();  //1
          a.m1();  // 2
            }}
      What is the result of attempting to compile and run the program?  
      Prints: A.m1
      Compile-time error at 1
      Compile-time error at 2
      None of the above   
      Answer : Compile-time error at 1.
      The constructor A() is not visible at class D because A() has the default access specifier.It should be "public" ,if you want to create object of the class outside the current package.


       
       

      No comments:

      Post a Comment