Prev Next

Quiz - 4

This quiz tests your knowledge of Java language semantics. Get a print out of this when you come to the class.

  • First try answering without running the program.
  • Then run the program to check if your understanding was indeed correct.
  • Enter both the answers in the last column and submit.
Concept Program Your answer first, followed by the actual answer
Interface
defines
method with
body
// N.java
public interface N {
  public void greet() {
    System.out.println("N");
  }
}
Implementation
of partial
methods of
an interface
// O.java
public interface O {
  public void m1();
  public void m2();
}
// OI.java
public class OI implements O {
  public void m1() {
    System.out.println("Implements m1");
  }
  // m2() not implemented by OI
}
Instantiating
interface
// P.java
public interface P {
  public void m();
}
// PTest.java
public class PTest {
  public static void main(String[] args) {
    P p = new P();
  }
}  
Declaring
constructor
in an
interface
// Q.java
public interface Q {
  public Q();
}
Defining
instance
attributes
in an
interface
// R.java
public interface R {
  public int x;
}
Defining
static
attributes
in an
interface
// S.java
public interface S {
  public static int x;
}
// STest.java
public class RTest {
  public static void main(String[] args) {
    S.x = 5;
    System.out.println(S.x);
  }
}
Assigning
implementation
type to an
interface type
// T.java
public interface T {
  public void print();
}
// TI.java
public class TI implements T {
  public void print() {
    System.out.println("TI");
  }
}
// TTest.java
public class TTest {
  public static void main(String[] args) {
    T t = new TI();
    t.print();
  }
}
Extending
interface
// U.java
public interface U {
  public void m1();
}
// UX.java
public interface UX extends U {
  public void m2();
}
// UTest.java
public class UTest {
  public static void main(String[] args) {
    public void m1() {
      System.out.println("m1");
    }
    public void m2() {
      System.out.println("m2");
    }
  }
}
Implementing
multiple
interfaces
// VA.java
public interface VA {
  public void ma();
}
// VB.java
public interface VB {
  public void mb();
}
// VI.java
public class VI implements VA, VB {
  public void ma() {
    System.out.println("VA -> ma()");
  }
  public void mb() {
    System.out.println("VB -> mb()");
  }
}
// VTest.java
public class VTest {
  public static void main(String[] args) {
    VI vi = new VI();
    vi.ma();
    vi.mb();
  }
}
Clash in
methods of
multiple
interfaces
while
implementing
// WA.java
public interface WA {
  public void m();
}
// WB.java
public interface WB {
  public void m();
}
// WI.java
public class WI implements WA, WB {
  public void m() {
    System.out.println("WA -> m()");
  }
}
// WTest.java
public class WTest {
  public static void main(String[] args) {
    VW wi = new WI();
    wi.m();
  }
}
Abstract
class
// X.java
public abstract class X {
  protected int x;
  public x() { x = 1; }
  public abstract void set(int x);
  public void print() {
    System.out.println(x);
  }
}
// XX.java
public class XX extends X {
  public void set(int x) {
    super.x = x; // OR this.x = x;
  }
} 
// XTest.java
public class XTest {
  public static void main(String[] args) {
    X x = new XX();
    x.print();
    x.set(5);
    x.print();
  }
}
Abstract
constructor
// Y.java
public abstract class Y {
  public int x;
  public abstract Y();
}
// YX.java
public class YX {
  public YX() {
    this.x = x; // OR super.x = x;
  }
}
// YTest.java
public class YTest {
  public static void main(String[] args) {
    Y y = new YX();
  }
}  
Instantiating
abstract
class
// Z.java
public abstract class Z {
  protected int x;
  public abstract void print();
}
// ZTest.java
public class ZTest extends Z {
  public static void main(String[] args) {
    Z x = new Z();
  }
}