CSC 222 Final Exam Review Questions
and Answers
What is an Array? - answer It is an ordered list of items of a given data type
What is each item in an array called? - answer An element
How do you declare an Array in Java? - answer dataType[] arrayName = new
dataType[numElements];
Why does the array declaration use [] after the data type? - answer To indicate that
the variable is an array refence
All arrays are which type? Primitive or Reference? - answer Reference
True or False? Arrays are Immutable - answer True
The array reference variable is assigned to do what? - answer To refer to that newly
allocated array
What is one powerful aspect of arrays? (provides an example to help) - answer The
index is an expression. Ex: userNums[i] uses the value held in the int variable i as the
index. As such, an array is useful to easily lookup the Nth item in a list
True or False: The array's index must be a double type - answer False. It must be
integer
How can you tell how many elements are in an array? - answer Using
arrayname.length
Is arrayname.length a method or a property? - answer A property
The default element for int and double is ___ and the default element for boolean is
___? What about char? - answer 0, false, '\u0000'
How do you initialize an array and declare element values? Do you need to specify how
many elements are in the array? - answer To initialize array elements with non-
default values, you will specify them with brackets and seperate with commas.
For example,
int[] array = {2, 4, 6, 8};
,When declaring an array like this, you can make the array as long as you want with as
many elements without specifying.
An array is a data structure that does what? - answer Represent a collection of the
same types of data.
Can I declare an array like this?
double array[]; - answer You are allowed to, but it is not preferred.
How do you create an array of 10 doubles? How do you reference the first element?
How do you reference the last element? - answer Creating:
double[] array = new double[10];
First:
array[0] //(array elements are 0-bound)
Last:
array[9]
True or False: Once an array is created, its size is fixed. - answer True
How can I find an array's size? - answer array.length
How can you access the array's elements? - answer You can access them through
the index. The indices are 0-bound.
Can you treat an array element like a variable? - answer Yes you can.
For example,
array[2] = array[0] + array[1];
In this example, we added the first and second array element, and placed their sum in
the third element.
What are the two ways to initialize and declare array elements? - answer 1:
int[] array = new int[4];
array[0] = 2;
array[1] = 4;
array[2] = 6;
array[3] = 8;
2:
int[] array = {2, 4, 6, 8};
Will this work?
int[] array;
,array ={2, 4, 6, 8}; - answer No, it won't. It is incorrect syntax.
Use a for loop with an array of 4 elements. This for loop will give all element in the array
the value of x. - answer Ex.
int[] array = new int[4];
x = 5;
for(int i = 0; i < array.length; i++) {
array[i] = x;
}
To print:
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
/*This will print every element of the array on a new line*/
Use this card to demonstrate different things you can do with an array. - answer
What do you call for loop that enables you to traverse the complete array sequentially
without using an index variable?
(A loop that doesn not need i) - answer Enhanced for loop (for-each loop)
Ex.
for (int value: array) {
System.out.println(value);
}
//This is an easier way to print an array
For copying arrays... - answer Refer to lecture 7, slides 47-49
For passing an array through a method... - answer Refer to lecture 7, slides 50-78
How do you initialize a 2D-Array? - answer 1:
int[][] array = new int[3][3];
array[0][0] = 1;
2:
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
Can a 2D array have rows with different lengths?
, If so, what is that called? - answer Yes, they can. Each row is an array itself. They
are called ragged arrays.
They are declared as such:
int[][] array = {
{1, 2, 3},
{4, 5},
{6}
}
Use a for loop to initialize all elements of the array with variable x. And print them -
answer int[][] array = new int[4][4];
x = 5;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = x;
System.out.println(array[i][j]);
}
}
True or false: You can have multidimensional arrays. (like a 3D or 4D array) - answer
True
What does IPO stand for? - answer Input, Output, and Processing
What is IPO needed for? - answer Used to identify three key elements of the
problem.
Inputs needed to obtain the desired output.
Processes needed to convert the input into the desired output
Outputs needed to solve the problem. May include user prompts.
What are Algorithms and Flowcharts used for? - answer They are used to map the
logic needed to solve the problem
What is the command line/terminal? - answer The command line or terminal provides
a direct interface with the Operating System to call and run programs
What is the Programming Language and Compiler/Interpret used for? - answer A
programming language is needed to give instructions to a computer.
This includes the ability to write, compile, and run programs.
A programming language is a human readable source code that can then be converted
into machine code that can beexecuted by the computer.
What is Version control? - answer Version control software is used to keep track of
different versions of the same piece of software.