Followers

Sunday, 5 August 2012

Interface


Question 16

interface A {  
  protected int e = 5;              // 1
  private int f = 6;                // 2
  volatile int g = 7;               // 3
  transient int h = 8;              // 4
  public static final int d = 9;    // 5
}
Which of the field declarations does not result in a compile-time error?
a.   1
b.  2
c.   3
d.  4
e.   5
f.   None of the above

Question 17

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

Question 18

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

Question 19

interface A {void m1();}                      // 1
class B implements A {public void m1() {}}    // 2
class C implements A {protected void m1() {}} // 3
class D implements A {private void m1() {}}   // 4
class E implements A {void m1() {}}           // 5 
Compile-time errors are generated at which lines?
a.   1
b.  2
c.   3
d.  4
e.   5

Question 20

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

Question 21

interface A {
  final void m1();        // 1
  synchronized void m2(); // 2
  native void m3();       // 3
  abstract void m4();     // 4 
  public void m5();       // 5
}
Compile-time errors are generated at which lines?
a.   1
b.  2
c.   3
d.  4
e.   5

Question 22

interface A {void main(String[] args);}               // 1
interface B {public void main(String[] args);}        // 2
interface C {public static void main(String[] args);} // 3
interface D {protected void main(String[] args);}     // 4
interface E {private void main(String[] args);}       // 5 
Which interface declarations generate a Compile-time error?
a.   1
b.  2
c.   3
d.  4
e.   5

Question 23

interface F {abstract void main(String[] args);}      // 1
interface G {synchronized void main(String[] args);}  // 2
interface H {final void main(String[] args);}         // 3
interface I {native void main(String[] args);}        // 4 
Which interface declaration does not generate a compile-time error?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 24

 
interface A {String s1 = "A"; String m1();}
interface B implements A {String s1 = "B"; String m1();}
class C implements B {
  public String m1() {return s1;}
  public static void main(String[] args) {
    A a = new C(); System.out.print(a.m1());
}}
What is the result of attempting to compile and run the program?
a.   Prints: A
b.  Prints: B
c.   Compile-time error
d.  Run-time error
e.   None of the above

Question 25

interface A {int i = 1; int m1();}
interface B extends A {int i = 10; int m1();}
class C implements B {
  public int m1() {return ++i;}
  public static void main(String[] args) {
    System.out.print(new C().m1());
}}
What is the result of attempting to compile and run the program?
a.   Prints: 2
b.  Prints: 11
c.   Compile-time error
d.  Run-time error
e.   None of the above

Question 26

interface Z {void m1();}  // 1
class A implements Z {void m1() {}} // 2
class B implements Z {public void m1() {}} // 3
abstract class C implements Z {public abstract void m1();} // 4
A Compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 27

interface Z {void m1();}  // 1
class D implements Z {public final void m1() {}}    // 2
class E implements Z {public synchronized void m1() {}} // 3
class G implements Z {public native void m1();}     // 4
A Compile-time error is generated at which line?
a.   1
b.  2
c.   3
d.  4
e.   None of the above

Question 28

 
interface I10 {String name = "I10"; String s10 = "I10.s10";}
interface I20 {String name = "I20"; String s20 = "I20.s20";}
class C10 implements I10, I20 {            // 1
  public static void main(String[] args) {
    System.out.print(s10+",");             // 2
    System.out.print(s20+",");             // 3
    System.out.print(name);                // 4
}}
What is the result of attempting to compile and run the program?
a.   Prints: I10.s10,I20.s20,I10
b.  Prints: I10.s10,I20.s20,I20
c.   Prints: I10.s10,I20.s20,
d.  Prints: I10.s10,I20.s20,null
e.   Compile-time error at line 1
f.   Compile-time error at line 2
g.  Compile-time error at line 3
h.  Compile-time error at line 4
i.   Run-time error
j.   None of the above

Question 29

 
interface I10 {String name = "I10"; String s10 = "I10.s10";}
interface I20 {String name = "I20"; String s20 = "I20.s20";}
class C20 implements I10, I20 {         // 1
  public static void main(String[] args) {
    System.out.print(I10.s10+",");      // 2
    System.out.print(I20.s20+",");      // 3
    System.out.print(I20.name);         // 4
}}
What is the result of attempting to compile and run the program?
a.   Prints: I10.s10,I20.s20,I10
b.  Prints: I10.s10,I20.s20,I20
c.   Prints: I10.s10,I20.s20,
d.  Prints: I10.s10,I20.s20,null
e.   Compile-time error at line 1
f.   Compile-time error at line 2
g.  Compile-time error at line 3
h.  Compile-time error at line 4
i.   Run-time error
j.   None of the above


Question30

 
class GRC10 {
  public static void main (String[] s) {
    System.out.print(s[1] + s[2] + s[3]);
}}
java GRC10 A B C D E F
What is the result of attempting to compile and run the program using the specified command line?
a.   Prints: ABC
b.  Prints: BCD
c.   Prints: CDE
d.  Prints: A B C
e.   Prints: B C D
f.   Prints: C D E
g.  Compile-time error
h.  Run-time error
i.   None of the above


Answers


16
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.  
17
a  e 
abstract  public 
All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. An abstract method can not also be declared private, static, final, native or synchronized; so the same restriction applies to methods declared within an interface.  
18
None of the above 
All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. An abstract method can not also be declared private, static, final, native or synchronized; so the same restriction applies to methods declared within an interface. Transient and volatile are not method modifiers.  
19
c  d  e 
3  4  5 
Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error.  
20
c  d 
3  4 
All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared.  
21
a  b  c 
1  2  3 
All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. The final, synchronized and native modifiers can not appear in the declaration of an abstract method, and can not be applied to an abstract method declared within an interface.  
22
c  d  e 
3  4  5 
All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared.  
23
All methods declared within an interface are implicitly abstract. The final, synchronized and native modifiers can not appear in the declaration of an abstract method, and can not be applied to an abstract method declared within an interface.  
24
Compile-time error 
In the declaration of interface B, the keyword, extends, has been replaced by the keyword, implements.  
25
Compile-time error 
Fields declared within an interface are implicitly public, final, and static. A compile-time error is generated in response to the attempt to increment the value of i.  
26
All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error. An abstract class that implements an interface is free to override any of the inherited method declarations with another abstract method declaration.  
27
None of the above 
All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration within an interface, the usage is redundant and is discouraged. The modifiers, final, synchronized and native, can not appear in the declaration of an abstract method, but they can be added to an implementation of an abstract method.  
28
Compile-time error at line 4 
Class C10 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C10; then, no compile-time error occurs. Line 4 generates the compile-time error, because it is the first to access the name field as a member of class C10.  
29
Prints: I10.s10,I20.s20,I20 
Class C20 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C20; then, no compile-time error occurs. Although line 4 may appear to generate the compile-time error it does not, because name is accessed directly as a member of interface I20. Therefore, the compiler does not encounter an ambiguity.  
30
Prints: BCD 
The index for the first element of an array is zero so the first argument printed by this program is the second argument on the command line following the name of the class.  

No comments:

Post a Comment