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

1. The same Java array can store primitive types and reference types. (True or f

ID: 3543710 • Letter: 1

Question

1. The same Java array can store primitive types and reference types. (True or false)


2. Are Java arrays categorized as primitive types or reference types?


3. What is the output of the following program?

int[] a = new int[5];
a[0] = 0;
for (int i = 1; i <= 4; i++)
{
   a[i] = a[i-1] + i;
   System.out.print(a[i] + " ");
}


4. What is the output of the following program?

int[] a = new int[5];
for (int i = 0; i < a.length; i++)
{
   a[i] = i*3-1;
}
for (int i = a.length-1; i >= 0; i--)
{
    System.out.print(a[i] + " " );
}


5. What is the output of the following program?

int[] a = new int[10];
for (int i = 0; i < a.length; i++)
{
   a[i] = 2*i;
}
for (int i = 0; i < a.length; i += 3)
{
    System.out.print(a[i] + " ");
}


6. Given any String referenced by str, write code that puts each character of that String into an array of type char. There must be no unused elements in the array of char (the array capacity must match the length of the String). The first element of the array must be the same value as the first character in the string, and so on.

Example: String input = "Java language";

J a v a l a n g u a g e

Explanation / Answer

1) False primitive types (i.e. int, boolean, double, short, long, byte, char and float ) they are seprate meathods than an array

2) reference types, see the first question to help with this one

3) 1 3 6 10 (run the program below to check the answer)

class NAME{

public static void main (String[] args)

{

int[] a = new int[5];
a[0] = 0;
for (int i = 1; i <= 4; i++)
{
   a[i] = a[i-1] + i;
   System.out.print(a[i] + " ");
}

}

4) 11 8 5 2 -1 (run the program below to check the answer)

import java.util.*;
import java.lang.*;
import java.io.*;

class NAME{

public static void main (String[] args)

{

int[] a = new int[5];
for (int i = 0; i < a.length; i++)
{
   a[i] = i*3-1;
}
for (int i = a.length-1; i >= 0; i--)
{
    System.out.print(a[i] + " " );
}

}

5) 0 6 12 18 (run the program below to check the answer)

import java.util.*;
import java.lang.*;
import java.io.*;

class NAME{

public static void main (String[] args)

{

int[] a = new int[10];
for (int i = 0; i < a.length; i++)
{
   a[i] = 2*i;
}
for (int i = 0; i < a.length; i += 3)
{
    System.out.print(a[i] + " ");
}

}

6) see below, make sure to save the file under the correct class name you create

import java.util.*;
import java.lang.*;
import java.io.*;

class NAME {

public static void main (String[] args)

{

String str="java language";

char[] arr = new char[str.length()];

for(int i=0;i<str.length();i++)

{

arr[i]=str.charAt(i);

}

System.out.println( Arrays.toString(arr));

}

}