Prev Next

Quiz - 3

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
Visibility of
super class
attributes
// A.java
public class A {
  private float f;  // Try 'protected'
}
// AX.java
public class AX extends A {
  public AX() {  this.f = 1.0f;  }
}
// ATest.java
public class ATest {
  public static void main(String[] args) {
    AX ax = new AX();
  }
}
Accessing
super and
sub class
attributes
alike
// B.java
public class B {
  protected int x;
}
// BX.java
public class BX extends B {
  protected int y;
  public BX() {
    this.x = 1; 
    this.y = 2;
  }
  public String toString() {
    return this.x + " " + this.y;
  }
}
// BTest.java
public class BTest {
  public static void main(String[] args) {
    BX bx = new BX();
    System.out.println(bx);
  }
}
Accessing
super class
attributes
if hidden
// C.java
public class C {
  protected int x;
}
// CX.java
public class CX extends C {
  protected int x;
  public CX() {
    super.x = 1; 
    this.x = 2;
  }
  public String toString() {
    return super.x + " " + this.x;
  }
}
// CTest.java
public class CTest {
  public static void main(String[] args) {
    CX cx = new CX();
    System.out.println(cx);
  }
}
Explicit
call to
super class
constructor
necessary
// D.java
public class D {
  protected int x;
  public D() { this.x = 1; }
}
// DX.java
public class DX extends D {
  protected int y;
  public DX() {
    super(); // What if we comment this
    this.y = 1;
  }
public String toString() {
    return this.x + " " + this.y;
  }
}
// DTest.java
public class DTest {
  public static void main(String[] args) {
    DX dx = new DX();
    System.out.println(dx);
  }
}
Overriding
// E.java
public class E {
  protected int x;
  public void set(int x) { 
    this.x = x; 
  }
  public String toString() {
    return this.x;
  }
}
// EX.java
public class EX extends E {
  public void set(int x) {
    this.x = 2*x;
  }
  public String toString() {
    return this.x;
  }
}
// ETest.java
public class ETest {
  public static void main(String[] args) {
    E e = new E();
    e.set(4);
    System.out.println(e);
    EX ex = new EX();
    ex.set(4);
    System.out.println(ex);
  }
}
Overriding
a private
method
// F.java
public class F {
  protected int x;
  private void set(int x) { 
    this.x = x; 
  }
  public String toString() {
    return this.x;
  }
}
// FX.java
public class FX extends F {
  public void set(int x) {
    this.x = 2*x;
  }
  public String toString() {
    return this.x;
  }
}
// FTest.java
public class FTest {
  public static void main(String[] args) {
    F f = new F();
    f.set(4);
    System.out.println(f);
    FX fx = new FX();
    fx.set(4);
    System.out.println(fx);
  }
}
Overriding
a private
method
(Use of
@override)
// G.java
public class G {
  protected int x;
  private void set(int x) { 
    this.x = x; 
  }
  public String toString() {
    return this.x;
  }
}
// GX.java
public class GX extends G {
  // @override
  public void set(int x) {
    this.x = 2*x;
  }
  public String toString() {
    return this.x;
  }
}
// GTest.java
public class GTest {
  public static void main(String[] args) {
    G g = new G();
    g.set(4);
    System.out.pgrintln(g);
    GX gx = new X();
    gx.set(4);
    System.out.println(gx);
  }
}
Assignment
of subclass
type to a
superclass
type and
vice-versa
// H.java
public class H {
  protected int x;
  public H() { this.x = 1; }
  public String toString() {
    return x;
  }
}
// HX.java
public class HX extends H {
  protected int y;
  public HX() {
    super();
    this.y = 1;
  }
  public String toString() {
    return x + " " + y;
  }
}
// HTest.java
public class HTest {
  public static void main(String[] args) {
    H h = new H();
    System.out.println(h);
    HX hx = new HX();
    h = hx;
    System.out.println(h);
    // hx = h; // Uncomment this and check
  }
}
Assignment
of subclass
type to a
superclass
type during
creation of
object and
vice-versa
// I.java
public class I {
  protected int x;
  public I() { this.x = 1; }
  public String toString() {
    return x;
  }
}
// IX.java
public class IX extends I {
  protected int y;
  public IX() {
    super();
    this.y = 1;
  }
  public String toString() {
    return x + " " + y;
  }
}
// ITest.java
public class ITest {
  public static void main(String[] args) {
    I i1 = new I();
    I i2 = new IX();
    System.out.println(i1);
    System.out.println(i2);

    // IX ix1 = new IX(); // Uncomment and
    // IX ix2 = new I();  // check 1-by-1
  }
}
Passing
subclass
type to
a method
that takes
superclass
type as a
parameter
and
vice-versa
// J.java
public class J {
  protected int x;
  public J() { x = 1; }
  public String toString() {
    return x;
  }
}
// JX.java
public class JX extends J {
  protected int y;
  public JX() {
    super();
    y = 1;
  }
  public String toString() {
    return x + " " + y;
  }  
}
// JTest.java
public class JTest {
  public static void main(String[] args) {
    JTest jt = new JTest();
    jt.print(new J());
    jt.print(new JX());

    // jt.display(new JX()); // Uncomment
    // jt.display(new J());  // and check 
  }
  public void print(J j) {
    System.out.println(j);
  }
  public void display(JX jx) {
    System.out.println(jx);
  }
}
Explicit
casting of
superclass
type while
assigning to
a subclass
type
// K.java
public class K {
  protected int x;
  public K() { x = 1; }
  public String toString() {
    return x;
  }
}
// KX.java
public class KX extends K {
  protected int y;
  public KX() {
    super();
    y = 1;
  }
  public String toString() {
    return x + " " + y;
  }
}
// KTest.java
public class KTest {
  public static void main(String[] args) {
    K k = new K();
    KX kx = (KX) k;
    System.out.println(kx);
    			// Check the output!
  }
}
Overriding
static
method
does not
make sense
// L.java
public class L {
  public static void greet() {
    System.out.println("L");
  }
}
// LX.java
public class LX extends L {
  public static void greet() {
    System.out.println("LX");
  }
}
// LTest.java
public class LTest {
  public static void main(String[] args) {
    L.greet();
    LX.greet();
  }
}
Multiple
inheritance
is not
supported
// MA.java
public class MA {
  protected int a;
  public MA() { a = 1; }
}
// MB.java
public class MB {
  protected int b;
  public MB() { b = 1; }
} 
// MX.java
public class MX extends MA, MB {
  MX() {
    a = 2;
    b = 2;
  }
}