UPDATED Study QUESTIONS AND
CORRECT ANSWERS
How does compilation work in Java? - CORRECT ANSWER Most programming
languages translate high-level programs directly into the machine language for a particular
computer.
In contrast, the Java compiler translates Java programs into byte-code, a machine language
for a fictitious computer called the Java Virtual machine.
Once compiled to byte-code, a Java program can be used on any computer, making it very
portable.
Interpreters are what translate Java byte-code into machine code for a particular computer.
How do you compile a Java program or Class? - CORRECT ANSWER Each class
definition must be in a file whose name is the same as the class name followed by .java
Each class is compiled with the command javac followed by the name of the file in which the
class resides.
This results in a byte-code program whose filename is the same as the class name followed
by .class
How can you initialize a 1D array in Java? - CORRECT ANSWER //Initialize with
specific values
int[] arr = {1,2,3,4,5}
// Initialize with a specific size and default values
int size = 5;
, int[] arr = new int[size];
// Initialize after declaration
int[] arr;
arr = new int[]{1,2,3,4,5}
How can you initialize a 2D array in Java? - CORRECT ANSWER // Initialize with
specific values
int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
// Initialize with a specified size and default values
int rows = 3;
int cols = 3;
int[][] arr = new int[rows][cols];
// Initialize after declaration
int[][] arr;
arr = new int[][] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
How can you access array contents in Java? - CORRECT ANSWER // Get value at
index
valueAtN = arr[N];
// Set value at index
arr[N] = newValueForIndexN;
// Loop through all values of an array
for(int i = 0; i < arr.length; i++)
{