Review 5 (Year 3 Sem 1)

Review 5 is the fifth review in semester 1 of year 3, and the fifth review in the total of year 3. It is a review for arrays, 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 5 [C9])
 * All reviewed (Score at least 80% for Review Exercise 5 [C9])
 * Final Sprint (Score at least 80% for Review Exercise 5 [C9])

Question
What is the result of the following code? public class Main{   public static void doSomething( int [] n){        int [] temp = new int [ 5 ];        temp = n;        for ( int i = 0 ; i &lt; n. length ; i++)            temp[i] = temp[i]* 2 ;     }     public static void main(String[] args) {            int [] num = { 5, 4 , 3 , 2 , 1 };            doSomething (num);            for ( int g: num)                System. out .println(g + " " );   } }

Options

 * There is no output
 * 5 4 3 2 1
 * 10 8 6 4 2
 * null pointer error
 * 0 0 0 0 0

Question
Which statement below will cause a compiler error?

Options

 * int [ ] scores = {3, 5, 7};
 * int [ ][ ] scores {2, 7, 6}, {9, 3, 4, 5};
 * String cats[ ] = {"Fluffy", "Spot", "Zeus"};
 * boolean results[ ] = {true, false, true};
 * Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

Question
Which of the following statement is true for the code segment below?  int[] values = new int[10];  for (int i = 1; i &lt;= 10; i++){      values[i] = i * i;  }  

Options

 * There will be compile error as the array values is not initialized.
 * There will be NullPointer exception
 * There will be ArrayIndexOutOfBoundsException exception
 * There is no error in the code segment.

Question
Consider the following array:  int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What are the contents of the array a after the following loops complete? <font face="Courier New">for (int i = 1; i &lt; 8; i++) { <font face="Courier New">   a[i] = a[i] + a[i - 1]; <font face="Courier New">} Write your answer in the format {a,b,c}

Question
Consider the following code: public class Main {       public static void f( int [] a, int m)        {            double [] b = f (a, 6.0 );            a[ 0 ] = a[ 0 ] * m;            a[ 2 ] = ( int ) (b[ 0 ] + b[ 1 ]);            m = m + 5 ;        }        public static double [] f( int [] a, double x)        {            double [] b = new double [ 2 ];            b[ 0 ] = a[ 0 ] + x;            b[ 1 ] = a[ 1 ] + x;            return b;        }        public static void main(String[] args)        {            int [] a = { 2, 3 , 4 };            int k = 5 ;            f (a, k);            System. out .print(k + " " );           System. out .print(a[ 0 ]+ " " );           System. out .print(a[ 1 ]+ " " );           System. out .print(a[ 2 ]);    }  } Write down the output that will be printed when the above code is executed.

Question
Which of the following are true of Java arrays?

Options
◻ Array entries are auto-initialized to 0.0 when creating a new array of double values.

◻ Can change the size of the array after creation.

◻ Given an array a[] that has been declared and initialized, accessing a[a.length] results in a runtime error.

◻ Can use an array as a return type from a function.

◻ Can pass an array to a function and have that function change the values stored in the array entries.

Question
Consider the following: double [][] values = { { 1.2, 9.0 , 3.2 },    { 9.2 , 0.5 , 1.5 , - 1.2 },    { 7.3 , 7.9 , 4.8 } } ; what is in ?

Options

 * 7.3
 * 7.9
 * 9.2
 * There is no such array element.

Question
Consider the following: int [][] items = { { 0, 1 , 3 , 4 }, { 4 , 3 , 99 , 0 , 7 }, { 3 , 2 } } ; Which of the following statements replaces the 99 with 77?

Options

 * items[1][2] = 77;
 * items[2][1] = 77;
 * items[ 99 ] = 77;
 * items[2][3] = 77;

Question
Consider the following code: public class Main {       public static void main(String[] args){            int [] a = { 1, 2 , 3 };            int [] b = { 1 , 2 , 3 };            System. out .println(a == b);       } } What is the output printed when the above code is executed?