In Java, what mathematical operators are represented by the symbols *, /, and %?
In Java, what symbols are used to represent the logical operators AND, NOT, and OR?
True or False?
Java checks for out-of-bounds array indices at run time.
Arrays can contain strings as elements.
Arrays can use strings as subscripts.
The method heading
public void someFunc( double[] x )
causes a compile-time error because the size of the array is missing.
An array can be declared without specifying its size by initializing it with a list of elements enclosed in braces, as in
int[] primes = { 2, 3, 5, 7, 11, 13 };A Java source file can contain at most one public class definition.
In the following code fragment, the programmer mistakenly placed a semicolon at the end of the for statement heading. What is the result?
System.out.print("A");
for ( int count = 1; count <= 3; count++ );
System.out.print("B");
System.out.print("C");
a compile-time error
an infinite loop
the output ABC
the output ABBC
the output ABBBC
How often does each of the following loops execute? Assume that i is not changed in the loop body.
for ( int i = 1; i <= 10; i++ ) ...
for ( int i = 0; i < 16; i++ ) ...
for ( int i = -10; i <= 10; i++ ) ...
for ( int i = 8; i > 0; i-- ) ...
for ( int i = 1; i <= 10; i = i + 2 ) ...
What are the values of s and n after each of the following loops?
int s = 1;
int n = 1;
while ( s < 10 )
{
s = s + n;
n++;
}
int n; int s = 1; for ( n = 1; n < 6; n++ ) s = s + n;
int s = 1;
int n = 1;
do
{
s = s + n;
n++;
} while ( s < 3 * n )
After execution of the code fragment
int[] arr = new int[5];
for ( int i = 0; i < 5; i++ )
{
arr[i] = i + 2;
if ( i >= 3 )
arr[i-1] = arr[i] + 3;
}
what is contained in arr[1] and arr[3]?
What is the output of the following program fragment?
int[] hundreds = { 100, 200, 300, 400, 500 };
for ( int i = 4; i > 0; i-- )
System.out.print( hundreds[i] + " " );
Given the following declarations
double x = 2.5; double y = -1.5; int m = 16; int n = 4; String s = "silly"; String t = "thing";
what is the value of each of the following expressions?
x + n * y - (x + n) * y
Math.sqrt( Math.sqrt(m))
(int) Math.round(x)
1 - (1 - (1 - (1 - n)))
s + t
s + n
s.length() + t.length()
Given the following declarations
double x = 12.8; double y = -1.5; int m = 18; int n = 4;
what is the value of each of the following expressions?
x / n + y
m / n + m % n
4 * y - n / 5
(int) x
x + n * y
Rewrite the following section of code using a for loop instead of a while loop.
int sum = 0;
int count = -5;
while ( count <= 15 )
{
sum = sum + count;
count++;
}
Fix the error in the following code fragment. It is intended that the loop will find the minimum element of array v.
int[ ] v = new int[10];
. . . // values are assigned to elements of v
int min = v[0];
for ( int i = 1; i <= 10; i++ )
if ( v[i] < min )
min = v[i];
Which of the following expressions would correctly extract the digit in the tens column of a four-digit decimal number store in value?
value / 100
10 * value % 100
value % 100 / 10
value - value % 10
none of the above
Correct the error (or errors) in each of the following code excerpts by removeing, inserting, or changing one or more characters as required. You may assume that the variable x has been correctly declared and initialized.
if ( x > 5 );
System.out.print("bigger");
if ( x == 0 )
return 0;
else if ( x < 0 )
return -1;
else ( x > 0 )
return 1;
if ( x == 0 ) || ( x == 1 ) return 1;
return ( -0.5 <= x <= 0.5 );
public static double average( int a; int b )
{
return ( a + b ) / 2.0;
}
if ( x = 1 ) x++; else x = x + 2;
int z = 5.0e-3;
while x < 10
x = x + 2;