Answers for the sample test

  1. How many stars will the following loop print:
    
    
    for (int star = 9; star < 0; star++){
      System.out.print(`*');
    }
    
    
    Answer: no stars, since the loop counter star starts at 9 and the condition for the loop to be executed is (star < 0).
  2. Below is a complete class definition. Tick lines which would cause compiler to report errors.
    
    class Bugs{
       float x, y, z;           (1)
       min = 10;                (2)
       float x = 6;             (3)
       int 2ndPrize = 100;      (4)
       int thirdPrize = 50;     (5)
    }
    
    You can check this yourself by compiling the class. Line (1) is fine, on line (2) the type of min is not declared, on (3) x is declared a second time, on (4) the variable name starts with an integer which confuses the compiler, (5) is fine.
  3. Given the following (and only) declaration of a display() method:
    
    public void display(int size; char symbol);
    
    tick the lines containing invalid calls (assume x is an object of the right type):
    
    x.display(10, '10'); (1)
    x.display(10, 'A');  (2)
    x.display(10);       (3)
    
    Answer: (1) is illegal because '10' is not a character, (3) is illegal because the second parameter is missing.
  4. Write a method to swap elements at positions i and j in an array arr. The simplest solution is:
    
    public void swap(int[] arr, int i, int j){
       int temp = arr[i];
       arr[i] = arr[j];
       arr[j] = temp;  
    }
    
  5. What is wrong with the following method:
    
    void simpleTest(int x){
      if (x > 0) return true;
      else return false;
    }
    
    Answer: it is declared as void but tries to return a value (a boolean).
  6. Will System.exit() be executed if input is equal to 'q':
    
    if (input != 'q' | input != 'Q'){ 
       System.exit();
    }
    
    Answer: if input is equal to 'q' then the value of (input != 'Q') is true, so the whole condition (input != 'q' | input != 'Q') is true, so the statement will be executed.
  7. Write a constructor for the following class (there are several possibilities, write one which takes four arguments):
    
    
    class Film{
       String title;
       int year;
       String director;
       String[] actors;
    
       Film (String s, int y, String d, String[] a){
          title = s;
          year = y,
          director = d;
          actors = a; 
    }
    
    Answer: the answer above is most simple-minded and assumes that you are never going to do anything to s, y, d and a, so you don't need to worry about making proper copies. A better answer is:
    
    
    class Film{
       String title;
       int year;
       String director;
       String[] actors;
    
       Film (String s, int y, String d, String[] a){
          title = new String(s);
          year = y,
          director = new String(d);
    // create a new array to hold actors, same size as a
          actors = new String[a.length];
    // fill it with copies of Strings in a
          for (int i =0; i < a.length; i++){
             actors[i] = new String(a[i]);
          }
    }
    
  8. Write a line of code which creates an array of characters of size 10.
    
    char[] requiredArray = new char[10];
    
  9. What will be the result of trying to compile and run an application in which the following is the only declaration of a main() method? (Assume the rest of the class is correct.)
    
    public static void main(){
      System.out.println("Hello world");
    }
    
    1. The class will compile without error but the program will not run.
    2. The class will compile and run, writing "Hello world" to the standard output.
    3. The compiler will report an error.
    Answer: the first one. The method declaration is perfectly correct, but the signature is different from the main() method which is expected by the JVM (does not take an array of Strings as a parameter). iT will compile fine but you will get an error message when you attempt to run it saying that main is not found.
  10. Assume that the following program has been compiled and the Demo.class is in the current directory.
    
    public class Demo {
      public static void main(String[] args){
         int n = 1;
         System.out.println("The word is " + args[n]);
      }
    }
    
    Select the correct command line to execute the program and produce the output line: "The word is gamma".
    1. Demo alpha beta gamma delta
    2. java Demo alpha beta gamma delta
    3. java Demo beta gamma delta
    4. java Demo.class beta gamma delta
    5. java Demo.class alpha beta gamma delta
Answer: 3, since the program prints args[1] which is the second String passed to the main() method.