Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. double sum = 0; double d; for (d = 0; d < 10; d = d + 0.1 ) { sum = sum + d;

ID: 3866096 • Letter: 1

Question

1.

double sum = 0;

double d;

for (d = 0; d < 10; d = d + 0.1 )

{

              sum = sum + d;

}

a. The program has a syntax error because the semicolons in the for loop are in the wrong place

b. The program has a syntax error because the control variable in the for loop cannot be double.

c.  The program compiles but does not stop because d would always be less than 10.

d. .The program compiles and runs fine.

2.

What is y after the following for loop statement is executed?

int y = 0;

for (int i=0; i < 10; i ++)

{

   y ++;  

}

a. 9

b. 10

c. 11

d. 12

3.

Array arr is declared and initialized in the following line of code:

                                                int [ ] arr = {10, 7, 3, 5, 9};

What is arr[3] - arr[1]?

a. -7

b. 7

c. -2

d. 2

4.

Which of the following is a valid array declaration and initialization?

a.     char [ ] charArray;

        charArray = new char[26];

b.     int [ ] words;

        words = new words[10];

c.     char [ ] charArray;

        charArray = 15;

d.     double [3] nums;

5.

Consider the following code fragment:

int [ ] list;

list = new int[10];

for (int i = 0; i <= 10; i = i + 1)

{

list[i] = 0;

}

Which of the following statements is true?

a. The loop body will execute 9 times.

b. The program has a syntax error because the control variable, i, in the for loop is not defined in the body of the loop.

c. The loop body will execute 10 times, filling up the array with zeros.

d. The code has a runtime error indicating that the array is out of bound.

6.

Assume the declaration of the method xMethod is as follows. This means that xMethod is sent one object that is an array of double values.

            public static void xMethod(double [ ] a);

            Which of the following could be used to call xMethod?

        a.     xMethod(5.7);

       

       b.     xMethod(“this one is correct”);

       

       c.     double [ ] z = {8.4, 9.3, 10.7};

                xMethod( z[2] );

       

        d.     double [ ] z;

                z = new double[6];

                xMethod( z );

7.

Analyze the following code:

public class Test

{

public static void main(String[] args)

{

    int [ ] x = {0, 1, 2, 3, 4, 5};

    xMethod(x, 5);

}

public static void xMethod(int [ ] x, int len)

{

    for (int i = 0; i < len; i = i + 1)

    {

      System.out.print( " " + x[i] );

    }

}

}

a. The program displays 0 1 2 3 4.

b. The program displays 0 1 2 3 4 and then raises a runtime exception.

c. The program displays 0 1 2 3 4 5.

d. The program displays 0 1 2 3 4 5 and then raises a runtime exception.

8.

Given the following statements:

            int [ ] list;

             list = new int[10];

b. The array variable list contains an object that refers to an array of 9 int values

d. The above has a compiler error.

9.

Given the following statements, length is a property of all arrays. By writing the name of an array followed by a dot, followed by the word length, it will return the total number of locations in the array.

   

            int [ ] list = new int[10];

            int n = list.length;

            What is the value of n?

a. 10

b. 9

c. The value depends on how many integers are stored in the array list

d. 0

10. True or false?

When a variable is sent to a method and the variable has a primitive data type, then the method can change the value of that variable.

a. The array variable list contains an object that refers to an array of 10 int values

Explanation / Answer

1.D --> The program compiles and runs fine.

Explaination:

The code is syntactically correct and error free. When you run this code it gives a output of 504.99999999999955

2. B --> 10

Explaination:

The code is increment the value of y by 1 for every iteration of the for loop until the value of i reaches 10 where it fails under the for loop condition

3. C --> -2

Explaination:

arr[3] = 5, arr[1] = 7, arr[3] - arr[1] = 5 - 7 = -2

4. A

Explaination:

Option A is the correct way to declare a char array of size 26

5. D --> d. The code has a runtime error indicating that the array is out of bound.

Explaination:

The size of the array is 10 but the last index will be 9. But in the for loop the array is iterated for the index 10. So it is a array out of bounds exception.

6. C

Explaination:

That is the correct of declaring and initalising an array of double elements. So it doesn't throw any error

7. A --> The program displays 0 1 2 3 4.

Explaination:

The program passes an array and its length to the method. The method prints all the elements of the array

8. A --> The array variable list contains an object that refers to an array of 10 int values

Explaination:

This is a declaration and initalisation of an array of integers of size 10

9. A --> 10

Explaination:

list.length returns the length of the array which is 10

10. True

Explaination:

When a variable is passed to a method, it can change its value.