Prev Next

Quiz - 1

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
// A.java
public class A {
  private double d;
  protected char c;
  public float f;
}
// ATest.java
public class ATest {
  public static void main(String[] args) {
    A a = new A();
    a.d = 5;
    a.c = 'x';
    a.f = 1.0f;
  }
}
Default
values of
instance
variables
// BTest.java
public class BTest {
  private int i;
  private long l;
  private double d;
  private char c;
  private float f;
  private String s;
  public static void main(String[] args) {
    BTest b = new BTest();
    System.out.println("The default values"
            + "instance/object variables are: "
    		+ "b.i = " + b.i
    		+ "b.l = " + b.l
    		+ "b.f = " + b.f
    		+ "b.d = " + b.d
    		+ "b.c = " + b.c
    		+ "b.s = " + b.s);
  }
}
Default
values of
static
variables
// CTest.java
public class CTest {
  private static int i;
  private static long l;
  private static double d;
  private static char c;
  private static float f;
  private static String s;

  public static void main(String[] args) {
    System.out.println("The default values"
            + "static/class variables are: "
    		+ "i = " + i
    		+ "l = " + l
    		+ "f = " + f
    		+ "d = " + d
    		+ "c = " + c
    		+ "s = " + s);
  }
}
Default
values of
local
variables
// DTest.java
public class DTest {

  public static void main(String[] args) {
    int i;
    long l;
    double d;
    char c;
    float f;
    String s;

    System.out.println("The default values"
            + "local variables are: "
    		+ "i = " + i
    		+ "l = " + l
    		+ "f = " + f
    		+ "d = " + d
    		+ "c = " + c
    		+ "s = " + s);
  }
}
Private
(outer)
class
// E.java
private class E {
  /* 1. Is it possible to create a private class?
     2. How about protected class?
     3. Does it make sense to declare a public
        attribute in a private class? */
}
Equality
of object
instances
// F.java
public class F {
  private int x;
  F(int xi) { x = xi; }
  // equals() not defined
}

// FTest.java
public class FTest {
  public static void main(String[] args) {
    F f1 = new F(2);
    F f2 = new F(2);
    System.out.println(f1==f2);
    System.out.println(f1.equals(f2));
    /* How is it possible to call equals()
       method without defining it even? */
  }
}
Equality
of object
instances
// G.java
public class G {
  private int x;
  G(int xi) { x = xi; }
  public boolean equals(G g) {
    if (this.x == g.x)
      return true;
    return false;
  }
}

// GTest.java
public class GTest {
  public static void main(String[] args) {
    G g1 = new G(2);
    G g2 = new G(2);
    System.out.println(g1==g2);
    System.out.println(g1.equals(g2));
  }
}
Private
main()
// HTest.java
public class HTest {
  private static void main(String[] args) {
    System.out.println("main is private");
  }
}
Non-static
main()
// ITest.java
public class ITest {
  public void main(String[] args) {
    System.out.println("main is not static");
  }
}
main()
with no
arguments
// JTest.java
public class JTest {
  public static void main() {
    System.out.println("main without arguments");
  }
}
main() with
a return
type
// KTest.java
public class KTest {
  public static int main(String[] args) {
    System.out.println("main returns a value");
    return 0;
  }
}
Overloaded
main()
// LTest.java
public class LTest {
  public static void main(String[] args) {
    System.out.println("From real main");
    main(10);
  }

  public static void main(int x) {
    System.out.println("From overloaded main");
  }
}
main()
calling
itself
// MTest.java
public class MTest {
  public static void main(String[] args) {
    System.out.println("Inside main");
    String[] s;
    main(s);
  }
}