Review 6 (Year 3 Sem 1)

Review 6 is the sixth review in semester 1 of year 3, and the sixth review in the total of year 3. It is a review for arraylist and file i/o, and is considered to be easy.

Description
Please note that the purpose of Review Exercises is to prepare you for the WRITTEN TEST. Hence, please do the questions after revising the topics, and think through the options properly. You should NOT try to execute the code when attempting the questions.

Unlocked after completion

 * OCD (Score at least 60% for Review Exercise 6 [C10 & C11])
 * All reviewed (Score at least 80% for Review Exercise 6 [C10 & C11])
 * The Final Lap (Score at least 80% for Review Exercise 6 [C10 & C11])

Question
What is the result of the following code? import java.util.ArrayList; public class Main {    public static void main(String[] args){        ArrayList&lt;Integer&gt; alist = new ArrayList&lt;Integer&gt;;        alist.add( 1 );        alist.add( 2 );        alist.remove( 1 );        alist.add( 1, 3 );        alist.set( 1 , 4 );        alist.add( 5 );        System. out .println(alist);   }  }

Options

 * [1, 4, 5]
 * [1, 4, 3, 5]
 * [2, 4, 5]
 * [2, 4, 3, 5]
 * None of the above

Question
What is the result of the following code? import java.util.ArrayList; public class Main {    public static void main(String[] args){        ArrayList&lt;Integer&gt; nums = new ArrayList&lt;Integer&gt;;         nums.add( 0 );        nums.add( 0 );        nums.add( 4 );        nums.add( 2 );        nums.add( 5 );        nums.add( 0 );        nums.add( 3 );        nums.add( 0 );        int k = 0 ;        Integer zero = new Integer( 0 );        while (k &lt; nums.size) {            if (nums.get(k).equals(zero))                nums.remove(k);            k++;        }        System. out .println(nums);   }  }

Options

 * [0, 0, 4, 2, 5, 0, 3, 0]
 * [3, 5, 2, 4, 0, 0, 0, 0]
 * [0, 0, 0, 0, 4, 2, 5, 3]
 * [4, 2, 5, 3]
 * [0, 4, 2, 5, 3]

Question
Consider the following code: ArrayList&lt;String&gt; tasks = new ArrayList&lt;String&gt;( 10 ); tasks.add( "Eat" ); tasks.add( "Sleep" ); tasks.add( "Code" ); How many more items can be added to tasks?

Options

 * 0
 * 7
 * 10
 * as many as memory will allow, essentially unlimited
 * None of the above.

Question
Which of the following statements is false?

Options

 * When an array is passed to a method, only the reference is passed, and a copy of the array is not created in the method.
 * An individual array element cannot be passed to a method.
 * An ArrayList is resizeable.
 * The indices of an ArrayList begin from zero.
 * None of the above.

Question
Assuming that list, which is an ArrayList, contains at least 3 elements, and you want to shift its last element into its second position. For instance, if list originally contains [“A”, “B”, “C”, “D”, “E”], then after the shift it becomes [“A”, “E”, “B”, “C”, “D”]. Which of the following statements to perform the shift is correct?

Options

 * list.add(list.remove(list.size), 2);
 * list.add(list.remove(list.size-1), 1);
 * list.add(2, list.remove(list.size-1));
 * list.add(1, list.remove(list.size));
 * list.add(1, list.remove(list.size-1));

Question
Consider the following code: import java.util.ArrayList;  public class Main {    public static void main(String[] args){        int [] array = {1, 2 , 3 , 4 , 5 };        ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;;         for (int i=0 ; i&lt;<span style="color: rgb(0, 0, 255);">5 ; i++) {            list.add(i%<span style="color: rgb(0, 0, 255);">2 , array[i] + array[(i+<span style="color: rgb(0, 0, 255);">4 )%<span style="color: rgb(0, 0, 255);">5 ]);        }         System.<span style="color: rgb(102, 14, 122);  ">out .println(list);    } } Write down the output that is printed when the code is executed.

Question
What is the result when the following code is executed? import java.io.IOException; import java.io.PrintWriter; public class TestWrite {    public static void main(String[] args) throws IOException{            PrintWriter output = new PrintWriter( "something.txt" );            output.println( "hihi" );    } }

Options

 * There is a compile error as there is not try-catch to handle the IOException
 * The data cannot be written into the file as a File object is not passed into the constructor of PrintWriter.
 * The data cannot be written into the file as the PrintWriter output has not been closed.
 * There is no error and "hihi" is written into a text file named something.txt.
 * None of the above.

Question
What is the result when the following code is executed? import java.util.Scanner; public class TestRead {    public static void main(String[] args) throws Exception {        Scanner input = new Scanner( "something.txt" );        while (input.hasNextLine) {            System. out .println(input.nextLine);       }        input.close;    } } <p style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);">Below is the content of something.txt: hihi bye hello world

Options

 * There is a compile error as there is not try-catch to handle the IOException
 * There is a compile error as a File object is not passed into the constructor of Scanner.
 * The following output will be printed:  something.txt 
 * The following output will be printed:  hihi bye hello world 
 * None of the above.

Question
What is the result when the following code is executed? import java.util.ArrayList; public class Test {    public static void main(String[] args) {        ArrayList a = new ArrayList&lt;&gt;( 5 );        for ( int i= 0 ; i &lt; a.size; i++){            System. out .println(a.get(i));       }    } }

Options

 * Five 0s will be printed.
 * Nothing will be printed.
 * There will be a run-time error caused by a.get(i) as the ArrayList is empty.
 * There will be a compile error as the ArrayList is not constructed correctly.
 * None of the above

Question
What is the result when the following code is executed? import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TestRead {    public static void main(String[] args) {        BufferedReader br = null ;        try {            String line;            br = new BufferedReader( new FileReader( "something.txt" ));            while ((line = br.readLine) != null ) {                String[] tokens = line.split( "[#|]+" );                System. out .println(tokens. length );           }            br.close;        } catch (IOException e) {            e.printStackTrace;        }    } } <p style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);">Below is the content of something.txt: hihi#hello world java#python|C++ apple cat dog|monkey#||rabbit

Options

 * 2 3 1 3
 * 2 3 0 3
 * 2 3 1 5
 * 3 3 1 4
 * 3 3 1 5