Followers

Sunday, 5 August 2012

Package programming Test



Question 1

package com.dan.chisholm;
public class A {
  public void m1() {System.out.print("A.m1, ");}
  protected void m2() {System.out.print("A.m2, ");}
  private void m3() {System.out.print("A.m3, ");}
  void m4() {System.out.print("A.m4, ");}
class B {
  public static void main(String[] args) {
    A a = new A();
    a.m1();  // 1
    a.m2();  // 2
    a.m3();  // 3
    a.m4();  // 4
}}
Assume that the code appears in a single file named A.java. What is the result of attempting to compile and run the program?
a.   Prints: A.m1, A.m2, A.m3, A.m4,
b.  Compile-time error at 1.
c.   Compile-time error at 2.
d.  Compile-time error at 3.
e.   Compile-time error at 4.
f.   None of the above

Question 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, ");}
  protected void m2() {System.out.print("A.m2, ");}
  private void m3() {System.out.print("A.m3, ");}
  void m4() {System.out.print("A.m4, ");}
}
// 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
    a.m3();  // 3
    a.m4();  // 4
}}
What is the result of attempting to compile and run the program?
a.   Prints: A.m1, A.m2, A.m3, A.m4,
b.  Compile-time error at 1.
c.   Compile-time error at 2.
d.  Compile-time error at 3.
e.   Compile-time error at 4.

Question 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, ");}
   private void m3() {System.out.print("A.m3, ");}
   void m4() {System.out.print("A.m4, ");}
}
// Class C is declared in a file named C.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class C extends A {
  public static void main(String[] args) {
    C c = new C();
    c.m1();  // 1
    c.m2();  // 2
    c.m3();  // 3
    c.m4();  // 4
}}
What is the result of attempting to compile and run the program?
a.   Prints: A.m1, A.m2, A.m3, A.m4,
b.  Compile-time error at 1.
c.   Compile-time error at 2.
d.  Compile-time error at 3.
e.   Compile-time error at 4.

Question 4

interface A {
  void m1();            // 1
  public void m2();     // 2
  protected void m3();  // 3
  private void m4();    // 4
}
Compile-time errors are generated at which lines?
a.   1
b.  2
c.   3
d.  4

Question 5

Which of the following statements is not true?
a.   An interface that is declared within the body of a class or interface is known as a         nested interface.
b.  A constant can be a member of an interface.
c.   A class declaration can be a member of an interface.
d.  If an interface is named in the implements clause of a class, then the class must             implement all of the methods declared within the interface.
e.   None of the above.

Question 6

Which of the following statements are true?
a.   An interface declaration can be a member of an interface.
b.  A method declared within an interface must have a body represented by empty curly   braces.
c.   An interface can implement another interface.
d.  An abstract class that implements an interface must implement all abstract methods      declared within the interface.
e.   An abstract method declaration can be a member of an interface.

 

 

Question 7

Which of the following are modifiers that can be applied to an interface that is a member of a directly enclosing interface?
a.   abstract
b.  implements
c.   final
d.  private
e.   protected
f.   public

Question 8

Which of the following are modifiers that can be applied to an interface that is a member of a directly enclosing class?

a.   abstract
b.  extends
c.   final
d.  private
e.   protected
f.   public

Question 9

Which of the following is a modifier that can be applied to an interface that is a member of a directly enclosing class or interface?
a.   static
b.  synchronized
c.   transient
d.  volatile
e.   implements
f.   None of the above.

 

Question 10

Suppose that an interface, I1, is not a member of an enclosing class or interface. Which of the following modifiers can be applied to interface I1?
a.   abstract
b.  final
c.   private
d.  protected
e.   public

Question 11

Suppose that an interface, I1, is not a member of an enclosing class or interface. Which of the following modifiers can be applied to interface I1?
a.   abstract
b.  public
c.   static
d.  synchronized
e.   transient
f.   volatile

Question 12

Which of the following are modifiers that can be applied to a field declaration within an interface?
a.   abstract
b.  const
c.   final
d.  private
e.   protected
f.   public

Question 13

Which of the following is a modifier that can be applied to a field declaration within an interface?
a.   static
b.  synchronized
c.   transient
d.  volatile
e.   None of the above.

Question 14

Which of the following modifiers can be applied to a class that is declared within an enclosing interface?
a.   public
b.  protected
c.   private
d.  abstract
e.   static
f.   final

Question 15

interface A {
  int a = 1;                        // 1
  public int b = 2;                 // 2
  public static int c = 3;          // 3
  public static final int d = 4;    // 4
}
Which field declaration results in a compile-time error?
a.   1
b.  2
c.   3
d.  4
e.   None of the above




 Answers



No.
Answer
Remark
1
Compile-time error at 3. 
Both class A and B are declared in the same package, so class B has access to the public, protected, and package access methods of class A.  
2
c  d  e 
Compile-time error at 2.  Compile-time error at 3.  Compile-time error at 4. 
Classes A and D are not declared in the same package, so class D does not have access to package access method, m4. Since class D does not extend class A, class D does not have access to the protected method, m2, of class A.  
3
d  e 
Compile-time error at 3.  Compile-time error at 4. 
Class A and C are not declared in the same package; therefore, class C does not have access to package access method, m4. Since class C extends class A, class C does have access to the protected method, m2, of class A.  
4
c  d 
3  4 
Methods declared within an interface are implicitly public. If no access modifier is included in the method declaration; then, the declaration is implicitly public. An attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error.  
5
If an interface is named in the implements clause of a class, then the class must implement all of the methods declared within the interface. 
This question asks which answer option is not true. Some true statements are as follows. An interface can be declared within an enclosing class or interface. The members of an interface can be constants, abstract method declarations, class declarations or interface declarations. If an interface is named in the implements clause of a class, then the class must implement all of the methods declared within the interface or the class must be declared abstract. The untrue answer option did not mention that an abstract class is not required to implement any of the methods declared in an interface that is named in the implements clause of the class declaration.  
6
a  e 
An interface declaration can be a member of an interface.  An abstract method declaration can be a member of an interface. 
An interface can be declared within an enclosing class or interface. The members of an interface can be constants, abstract method declarations, class declarations, or interface declarations. The body of a method declared within an interface is a semicolon. An interface can extend another interface, but can not implement an interface. An abstract class that has an interface, I1, in its implements clause is not required to implement any of the methods declared within I1.  
7
a  f 
abstract  public 
All interfaces are implicitly abstract. The explicit application of the abstract modifier to an interface declaration is redundant and is strongly discouraged. The declaration of an interface within the body of an enclosing class or interface is called a member type declaration. Every member type declaration appearing within the body of a directly enclosing interface is implicitly static and public. Use of the access modifiers, private or protected, is contradictory and results in a compile-time error. In contrast, the modifiers, private and protected, are applicable to a member type declaration appearing within the body of a directly enclosing class. The modifier, final, is never applicable to an interface. The keyword, implements, is not a modifier.  
8
a  d  e  f 
abstract  private  protected  public 
All interfaces are implicitly abstract. The explicit application of the modifier, abstract, to an interface is redundant and is strongly discouraged. The declaration of an interface within the body of an enclosing class or interface is called a member type declaration. The private, protected and static modifiers are applicable to a member type declaration that appears in the body of a directly enclosing class. In contrast, the modifiers, private and protected, are not applicable to a member type declaration appearing within the body of a directly enclosing interface. The modifier, final, is never applicable to an interface. The keyword, extends, is not a modifier.  
9
static 
A member interface is always implicitly static. The modifier, static, can not be applied to an interface that is not a member interface. The modifier, synchronized, is applicable to a concrete implementation of a method, but is not applicable to any interface. The modifiers, volatile and transient, are only applicable to variables that are members of a class. The keyword, implements, is not a modifier.  
10
a  e 
abstract  public 
The modifier, abstract, is applicable to an interface declaration, but its use is strongly discouraged; because every interface is implicitly abstract. An interface can not be final. The modifiers, private and protected, are applicable only to an interface declaration that is a member of a directly enclosing class declaration. If an interface is not a member of a directly enclosing class, or if the interface is a member of a directly enclosing interface; then, the modifiers, private and protected, are not applicable. If an interface is declare public, then the compiler will generate an error if the class is not stored in a file that has the same name as the interface plus the extension .java.  
11
a  b 
abstract  public 
The modifier, abstract, is applicable to an interface declaration, but its use is strongly discouraged; because every interface is implicitly abstract. If an interface is declare public, then the compiler will generate an error if the class is not stored in a file that has the same name as the interface plus the extension .java. The modifier, static, is applicable to a member interface, but not to an interface that is not nested. The modifier, synchronized, is applicable only to concrete implementations of methods. The modifiers, transient and volatile, are applicable only to variables.  
12
c  f 
final  public 
The modifier, abstract, is not applicable to a variable. All field declarations within an interface are implicitly public, static and final. Use of those modifiers is redundant but legal. Although const is a Java keyword, it is not currently used by the Java programming language. An interface member can never be private or protected.  
13
static 
All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. A field that is declared final can not also be declared volatile; so a field of an interface can not be declared volatile. The modifier, synchronized, is never applicable to a field.  
14
a  d  e  f 
public  abstract  static  final 
A class that is declared within an enclosing interface is implicitly public and static; so the access modifiers, protected and private, are not applicable.  
15
None of the above 
All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface.  









No comments:

Post a Comment