Java Quiz Solutions, Foundations of Computing, Spring

  1. A single file contains the following Java code (exactly as it appears here, there is nothing else in the file). The file is compiled by the Java compiler and the resulting Java program is run.

    class Foo {
        public static void println() {
    	System.out.println("Foo");
        }
    }
    
    public class FooDemo {
        public static void main(String[] argv) {
    	System.out.println("FooDemo");
    	Foo.println();
        }
    }
    
    1. What is the name of this file?     FooDemo.java

    2. What is the command to compile this file with the Java compiler?     javac FooDemo.java

    3. What is (are) the name(s) of the file(s) that the Java compiler produces?     FooDemo.class Foo.class

    4. What do(es) the file(s) contain?     Java bytecodes

    5. What is the command to run the program?     java FooDemo

    6. What output does the program produce?
      FooDemo
      Foo
      
  2. A single file contains the following Java code (exactly as it appears here, there is nothing else in the file). The file is compiled by the Java compiler and the resulting Java program is run.

    public class Loop {
        public static void main(String[] argv) {
    	String a[] = new String[4];
    	a[0] = "Alpha"; a[1] = "Beta"; a[2] = "Delta"; a[3] = "Gamma"; // init
    	for (int i = 0; i < 4; i++) {
    	    System.out.println((i+1) + ".  " + a[i]);
    	}
        }
    }
    

    What output does the program produce?

    1.  Alpha
    2.  Beta
    3.  Delta
    4.  Gamma     
    
  3. In the preceding program (in class Loop), what happens if the statement a[4] = "Epsilon"; is added to the line marked // init? Give your answer in one sentence.

    When the interpreter reaches this statement, the program will throw an ArrayIndexOutOfBoundsException and stop running.

  4. This program is compiled and run

    class Thing {
        int x;
        public Thing(int i) { x = i; }
        public void speak() { System.out.print(x); }
    }
    
    public class ThingDemo {
        public static void main(String[] argv) {
    	Thing a, b;
    	a = new Thing(1); 
    	b = a;
    	System.out.print("a "); a.speak(); System.out.print("  b "); b.speak();
     	System.out.println();
    	b.x = 2;
    	System.out.print("a "); a.speak(); System.out.print("  b "); b.speak();
    	System.out.println();
        }
    }
    
    

    What output does this program produce?

    a 1  b 1
    a 2  b 2        
    
  5. Answer the following questions accurately but briefly. There is a correct one word answer to each question.

    1. System.out.println is the name of a thing in the Java platform. Which Java language construct is this thing an example of?     Method

    2. System.out is the name of a thing in the Java platform. Which Java language construct is this thing an example of?     variable (or object or PrintStream or ...)

    3. System is the name of a thing in the Java platform. Which Java language construct is this thing an example of?     Class

    4. What package is System in?     java.lang