IC211 6 Week Exam Practice


  1. How does java attempt to provide the following advantages?

  2. What is a virtual machine?

    A virtual machine is a program that simulates a computer. It can be programmed as if it were a real computer, and those programs do not know they are not running directly on a computer. Examples include both the Java Virtual Machine, and VirtualBox.

  3. Given the following code:
             Scanner in = new Scanner(System.in);
             System.out.print("Please input an integer ");
             int n = in.nextInt();
    	 System.out.println("you input : " + n++);
    

  4. What does it mean when you declare a method static?
     

  5. Circle the correct option in each sentence:
  6. Assume you're given the following file:
    Ex1.java
    public class Ex1
    {
      public static int cat;
      public int pig;
      public static int rat() { return 0; }
      public int dog() { return 0; }
    }

    Circle the valid calls of the function rat in the code below:
    Valid calls bold below
    public class App1 {
      public static void 
      main(String[] args) {
        Ex1 a = new Ex1();
        a.rat();
        Ex1.rat();
        Ex1.a.rat();
        a.Ex1.rat();
      }
    }
    Circle the valid calls of the function dog in the code below:
    Valid calls in bold
    public class App1 {
      public static void
      main(String[] args) {
        Ex1 a = new Ex1();
        a.dog();
        Ex1.dog();
        Ex1.a.dog();
        a.Ex1.dog();
     }
    }
    Circle the valid assignments to pig code below:
    Valid assignments in bold
    public class App1
    {
      public static void 
      main(String[] args) {
        a.pig = 3;
        Ex1.pig = 3;
        Ex1.a.pig = 3;
        a.Ex1.pig = 3;
      }
    }
  7. Of the following, which variables are base types, and which are pointers to objects. Some variables might refer to objects defined in lecture. Label base for base type and object for object.
     int i; base
    
     String s;;object
    
     Angle a;;object
    
     double d; base
    
     boolean b; base
    
     Scanner in;;object
    
     char c; base
    
     Node n;;object
    
     Player p;object

  8. Find and fix the errors in this code:
    import java.util.*
    
    class Main {
    
      public void main(String args) {
    
        int myArray[10];
    
        for (i=0;i<=10;i++) {
    
          myArray[i] = i;
    
      }
    
    };
    import java.util.*;
    
    class Main {
    
      public void main(String [] args) {
    
        int myArray[] =new int[10];
    
        for (i=0;i<10;i++) {
    
          myArray[i] = i;
    
      }
    
    }
     

  9. In the typical case, where do we define new classes?
    We define new classes in a new file with the same name as the class.

  10. There are 3 versions of 'swap' below. One works, the other two do not. Identify the correct version and describe why the broken versions are broken:
    public class Foo 
    {
    int i;
    public static void swap(int i, int j)
    {
      int tmp = i;
      i = j;
      j = tmp;
    }
    }
    
    Assuming a and b are Foo's, call as:
    Foo.swap(a.i,b.j);
    public class Foo 
    {
    int i;
    public static void swap(Foo i, Foo j)
    {
      Foo tmp = i;
      i = j;
      j = tmp;
    }
    }
    
    Assuming a and b are Foo's, call as:
    Foo.swap(a,b);
    
    public class Foo
    {
    int i;
    public static void swap(Foo i, Foo j)
    {
      int tmp;
      tmp = i.i;
      i.i = j.i;
      j.i = tmp;
    }
    }
    Assuming a and b are Foo's, call as:
    Foo.swap(a,b);
     

  11. Only the last version of swap works. Java is always pass by value, so if ints are passed as arguments, you cannot swap them. If pointers to objects are passed as arguments, you cannot swap them either (the pointers are still passed by value), but the contents of an object can be swaped, because we are given the pointers to the locations of the contents.
  12. Assuming a 40 array of characters named charlie, circle code segment correctly prevents an ArrayIndexOutOfBoundsException.
    if (i < 40 || charlie[i] == 'x') {
    //do something...
    if (i <= 40 || charlie[i] == 'x') {
       //do something...
    
    if (charlie[i] = 'x' || i <= 40 ) {
       //do something...
    
    if (charlie[i] = 'x' || i < 40 ) {
       //do something...
    
    };
    import java.util.*;
    
    class Main {
    
      public void main(String [] args) {
    
        int myArray[] =new int[10];
    
        for (i=0;i<10;i++) {
    
          myArray[i] = i;
    
      }
    
    }
     

  13. In the typical case, where do we define new classes?
    We define new classes in a new file with the same name as the class.

  14. There are 3 versions of 'swap' below. One works, the other two do not. Identify the correct version and describe why the broken versions are broken:
    public class Foo 
    {
    int i;
    public static void swap(int i, int j)
    {
      int tmp = i;
      i = j;
      j = tmp;
    }
    }
    
    Assuming a and b are Foo's, call as:
    Foo.swap(a.i,b.j);
    public class Foo 
    {
    int i;
    public static void swap(Foo i, Foo j)
    {
      Foo tmp = i;
      i = j;
      j = tmp;
    }
    }
    
    Assuming a and b are Foo's, call as:
    Foo.swap(a,b);
    
    public class Foo
    {
    int i;
    public static void swap(Foo i, Foo j)
    {
      int tmp;
      tmp = i.i;
      i.i = j.i;
      j.i = tmp;
    }
    }
    Assuming a and b are Foo's, call as:
    Foo.swap(a,b);
     

  15. Only the last version of swap works. Java is always pass by value, so if ints are passed as arguments, you cannot swap them. If pointers to objects are passed as arguments, you cannot swap them either (the pointers are still passed by value), but the contents of an object can be swaped, because we are given the pointers to the locations of the contents.
  16. Assuming a 40 array of characters named charlie, circle code segment correctly prevents an ArrayIndexOutOfBoundsException.
    if (i < 40 || charlie[i] == 'x') {
    //do something...
    if (i <= 40 || charlie[i] == 'x') {
       //do something...
    
    if (charlie[i] = 'x' || i <= 40 ) {
       //do something...
    
    if (charlie[i] = 'x' || i < 40 ) {
       //do something...
    

  17. What is the output of the program Main?
    Main.javaIndependency.java
    public class Main {
      public static void main(String []args) {
        int i = -1;
        Independency j = new Independency(1,2,3);
        int k = j.i(i,5);
        System.out.println("result: " + i + " " + j + " " + k);
      }
    }
    public class Independency {
      int i;
      int j;
      int k;
    
      public Independency(int i,int j, int k) {
        this.i = i;
        this.j = j;
        this.k = k;
      }    
        public int i(int i,int k) {
        int j =4;
        return i + j;
      }
    }
    
    result: -1 Independency@e53108 3
     

  18. Add appropriate javadoc comments to this code:
    /** Class representing a book
     * @author Prof Crabbe
     * @version 0.1
    */
    public class Book {
      private int year;
      private int length;
      private Page []pages;
    /** year and length constructor
     * @param y year of publication
     * @param l length of book
     */ 
      public Book(int y,int l) {
        year= y;
        length = l;
        pages = new Page[MAXLENGTH];
      }
    /** Adds a new page to the book
     * @param the page object
     */ 
      public void addPage(Page p) {
        pages[size++] = p;
      }   
    /** Finds the page number a string occurs on to help make an index.
     * @param s the sring to find
     * @return the page number
     */  
      public int index(String s) {
        for(int i = 0; i<size; i++) {
          if(pages[i].contains(s))
            return i;
        }
        return MAXLENGTH;
      }
    }