Java Quiz, Foundations of Computing, Spring

Name:


  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?

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

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

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

    5. What is the command to run the program?

    6. What output does the program produce?




  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?






  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.



  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?





  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?

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

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

    4. What package is System in?