Download Level 1.1 Which of the following declares an array of int named img

Document related concepts
no text concepts found
Transcript
Level 1.1
Which of the following declares an array of int named img?
A. int img;
B. int[] img;
C. new int img[];
D. int img = int[];
Correct Answer: B
What are the legal indexes for the array ar, given the following declaration?
int[] ar = {2, 4, 6, 8 };
A. 0,1,2,3
B. 1,2,3,4
C. 2,4,6,8
D. 0,2,4,6
Correct Answer: A
What is the output of the following code fragment:
int[] ar = {2, 4, 6, 8 };
System.out.println( ar[0] + " " + ar[1] );
A. 2 6
B. 8
C. 2 4
D. 6 8
Correct Answer: C
What is the output of the following code fragment?
int[] ar = {2, 4, 6, 8 };
ar[0] = 23;
ar[3] = ar[1];
System.out.println( ar[0] + " " + ar[3] );
A. 23 2
B. 2 8
C. 31
D. 23 4
Correct Answer: D
What is the output of the following code fragment:
int[] y = new int[5];
y[0] = 34;
y[1] = 88;
System.out.println( y[0] + " " + y[1] + " " + y[5] );
A. 34 88 0
B. 34 88 88
C. The program is defective and will not compile
D. 0 34 88
Correct Answer: C
Examine the following:
double[][] values = { {1.2, 9.0, 3.2},
{9.2, 0.5, 1.5, -1.2},
{7.3, 7.9, 4.8} } ;
what is in values[2][1] ?
A. 7.3
B. 7.9
C. 9.2
D. There is no such array element.
Correct Answer: B
Examine the following:
double[][] values = { {1.2, 9.0, 3.2},
{9.2, 0.5, 1.5, },
{7.3, 7.9, 4.8} } ;
what is in values[3][0] ?
A. 7.3
B. 7.9
C. 9.2
D. There is no such array element
Correct Answer: D
What is the output of the following code fragment:
int[] z = new int[9];
z[0] = 7;
z[1] = 3;
z[2] = 4;
System.out.println( z[0] + z[1] + " " + z[5] );
A. 10 0
B. 7 3 0
C. The program is defective and will not compile
D. 7 3 4
Correct Answer: A
What is the output of the following code fragment:
int[] zip = new int[5];
zip[0] = 7;
zip[1] = 3;
zip[2] = 4;
zip[3] = 1;
zip[4] = 9;
System.out.println( zip[ 2 + 1 ] );
A. 4 3
B. 3 7
C. 4
D. 1
Correct Answer: D
What is the output of the following code fragment:
int[] zip = new int[5];
zip[0] = 7;
zip[1] = 3;
zip[2] = 4;
zip[3] = 1;
zip[4] = 9;
int j = 3;
System.out.println( zip[ j-1 ] );
A. 7
B. 3
C. 4
D. 1
Correct Answer: C
Given:
int[][] items = { {0, 1, 3, 4},
{4, 3, 99, 0, 7 },
{3, 2} } ;
Which of the following fragments replaces row 0 of items with an entirely new row?
A.
items[0][0] = 8;
items[0][1] = 12;
items[0][2] = 6;
B.
items[0] = { 8, 12, 6 };
C.
items[0] = new { 8, 12, 6 };
D.
int[] temp = { 8, 12, 6 };
items[0] = temp;
Correct Answer: D
What is the meaning of null?
A. It is another name for zero.
B. It is the String object that contains no characters.
C. A reference variable that contains null is not referring to an object.
D. It is a special value used to indicate an error condition.
Correct Answer: C
What type parameter must the following method be called with?
int myMethod ( double[] ar )
{
....
}
A. An empty double array.
B. A reference to an array that contains elements of type double.
C. A reference to an array that contains zero or more elements of type int.
D. An array of any length that contains double and must be named ar.
Correct Answer: B
What does the following method do?
void spread ( int[] values )
{
for ( int index= 1 ; index < values.length ; index++ )
values[index] = values[0];
}
A. It changes an array by copying the element in cell 0 to all other cells.
B. It changes an array by making every element the same as the value of its index.
C. It changes an array by making every element zero.
D. It makes a change to the formal parameter but does not make a change to the caller's array.
Correct Answer: A
Given the following declaration, which expression returns the size of the array, assuming the array has
been initialized?
int[] array;
Select the one correct answer.
A. array[].length()
B. array.length()
C. array[].length
D. array.length
E. array[].size()
F. array.size()
Correct Answer: D
Explanation: In Java, arrays are objects. Each array object has a final field named length that stores the
size of the array.
Is it possible to create arrays of length zero?
Select the one correct answer.
A. Yes, you can create arrays of any type with length zero.
B. Yes, but only for primitive data types.
C. Yes, but only for arrays of object references.
D. No, you cannot create zero-length arrays, but the main() method may be passed a zero-length array
of String references when no program arguments are specified.
E. No, it is not possible to create arrays of length zero in Java.
Correct Answer: A
Explanation: Java allows arrays of length zero. Such an array is passed as an argument to the main()
method when a Java program is run without any program arguments.
What will be the content of array variable table after executing the following code?
for(int i = 0; i < 3; i + +)
for(int j = 0; j < 3; j + +)
if(j == i)
table[i][j] = 1;
else
table[i][j] = 0;
A. 0 0 0
000
000
B. 1 0 0
110
111
C. 0 0 1
010
100
D. 1 0 0
010
001
Correct Answer: D
Which one of the following will declare an array and initialize it with five numbers?
A.
Array a = new Array(5);
B.
int [] a = {23,22,21,20,19};
C.
int a [] = new int(5);
D.
int [5] array;
Correct Answer: B
Explanation:
Option B is the legal way to declare and initialize an array with five elements.
Option A is wrong because it shows an example of instantiating a class named Array, passing the integer
value 5 to the object's constructor. If you don't see the brackets, you can be certain there is no actual
array object! In other words, an Array object (instance of class Array) is not the same as an array object.
Option C is wrong because it shows a legal array declaration, but with no initialization.
Option D is wrong (and will not compile) because it declares an array with a size. Arrays must never be
given a size when declared.
Given:
55. int []x= {1, 2, 3, 4, 5};
56. int y[] =x;
57. System.out.println(y[2]);
Which is true?
A. Line 57 will print the value 2.
B. Line 57 will print the value 3.
C. Compilation will fail because of an error in line 55.
D. Compilation will fail because of an error in line 56.
Correct Answer: B
How many objects are present after the following code fragment has executed?
double[] ann = new double[ 7 ];
double[] bob;
bob = ann;
A. 2
B. 7
C. 14
D. 1
Correct Answer: D
For which of the following applications is an array NOT suitable:
A. Holding the scores on twelve midterms exams of a class.
B. Holding the name, social security number, age, and income of one individual.
C. Holding the temperature readings taken every hour throughout a day.
D. Holding the total sales a store made in each of twelve months.
Correct Answer: B
What is the length of the following array: byte[] data = { 12, 34, 9, 0, -62, 88 };
A. 1
B. 5
C. 6
D. 12
Correct Answer: C
What is the output of the following code fragment:
int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
for ( int index= 0 ; index < egArray.length ; index = index + 2 )
System.out.print( egArray[ index ] + " " );
A. 2 4 6 8 10 1 3 5 7 9
B. 4 8 1 5 9
C. 2 6 10 3 7
D. 2 6 10 3 7 0
Correct Answer: C
Does a programmer always know how long an array will be when the program is being written?
A. Yes---the program will not compile without the length being declared.
B. No---the array object is created when the program is running, and the length might change from run
to run.
C. Yes---otherwise the program will not run correctly.
D. No---arrays can grow to whatever length is needed.
Correct Answer: B
Level 1.2
Java uses call by value. What is the value that is being passed into routine by the method call in the
following?
double[] marks = {60, 30.40, 50,70};
routine( marks );
A.
A copy of the array marks.
B.
The value of the elements of marks.
C.
A reference to the array object marks.
D.
60
Correct Answer: C
Which of these array declarations and initialization are not legal?
Select all valid answers
A. int []i[] = {{1,2}, {1}, {}, {1,2,3}};
B. int i[]= new int[2]{1, 2};
C. int i[][] = new int[][]{{1,2,3}, {4,5,6}};
D. int i[][] = {{1, 2}, new int[2]};
E. int i[4] = {1, 2, 3, 4};
Correct Answer: B, E
Which of the following is a legal array initialization?
A.
Int a[]= new int[12]
B.
Int [12]= new a[]
C.
Int a[]=new int
D.
None of the above
Correct Answer: A
What does the following statement do?
int[] values = new int[10] ;
A. It declares values to be a reference to an array object and constructs an array object containing
10 integers which are initialized to zero.
B.
It declares values to be a reference to an array object, but initializes it to null.
C. It declares values to be a reference to an array object which does not yet exist, but will contain
10 zeros when it does.
D.
It declares values to be a reference to an array which contains 10 references to int variables.
Correct Answer: A
Which three are legal array declarations?
1. int [] myScores [];
2. char [] myChars;
3. int [6] myScores;
4. Dog myDogs [];
5. Dog myDogs [7];
Options:
A.
1, 2, 4
B.
2, 4, 5
C.
2, 3, 4
D.
All are correct.
Correct Answer: A
Explanation:
(1), (2), and (4) are legal array declarations. With an array declaration, you can place the brackets to the
right or left of the identifier. Option A looks strange, but it's perfectly legal to split the brackets in a
multidimensional array, and place them on both sides of the identifier. Although coding this way would
only annoy your fellow programmers, for the exam, you need to know it's legal.
(3) and (5) are wrong because you can't declare an array with a size. The size is only needed when the
array is actually instantiated (and the JVM needs to know how much space to allocate for the array,
based on the type of array and the size).
Which will legally declare, construct, and initialize an array?
Options:
A.
int [] myList = {"1", "2", "3"};
B.
int [] myList = (5, 8, 2);
C.
int myList [] [] = {4,9,7,0};
D.
int myList [] = {4, 3, 7};
Correct Answer: D
Explanation:
The only legal array declaration and assignment statement is Option D
Option A is wrong because it initializes an int array with String literals.
Option B is wrong because it use something other than curly braces for the initialization.
Option C is wrong because it provides initial values for only one dimension, although the declared array
is a two-dimensional array.
Which of the following statements are valid array declarations?
(1) int number();
(2) float average[];
(3) double[] marks;
(4) counter int[];
A. (1)
B. (1) & (3)
C. (2) & (3)
D. (4)
Correct Answer: C
Which of these array declarations and instantiations are not legal?
Select all valid Answers:
A. int []a[] = new int[4][4];
B. int a[][] = new int[4][4];
C. int a[][] = new int[][4];
D. int []a[] = new int[4][];
E. int[][] a = new int[4][4];
F. int [ ] [ ] a = new int[4][4];
Correct Answer: C
Consider the following code
int number[] = new int[5];
After execution of this statement, which of the following are true?
(1) number[0] is undefined
(2) number[5] is undefined
(3) number[4] is null
(4) number[2] is 0
(5) number.length() is 5
A. (2), (4) & (5)
B. (1) & (5)
C. (3) & (5)
D. (5)
Correct Answer: A
Examine the following:
double[][] values = { {1.2, 9.0, 3.2},
{9.2, 0.5, 1.5, -1.2},
{7.3, 7.9, 4.8} } ;
what is in values[2][1] ?
A.
7.3
B.
7.9
C.
9.2
D.
There is no such array element.
Correct Answer: B
Given the following:
double[][] things = { {1.2, 9.0},
{9.2, 0.5, 0.0},
{7.3, 7.9, 1.2, 3.9} } ;
What is the value of things.length ?
A.
2
B.
3
C.
4
D.
9
Correct Answer: B
Given the following:
double[][] things = { {1.2, 9.0},
{9.2, 0.5, 0.0},
{7.3, 7.9, 1.2, 3.9} } ;
What is the value of things[2].length ?
A.
2
B.
3
C.
4
D.
9
Correct Answer: C
Given the following:
int[][] items = { {0, 1, 3, 4},
{4, 3, 99, 0, 7 },
{3, 2} } ;
Which of the following statements replaces the 99 with 77?
A.
items[1][2] = 77;
B.
items[2][1] = 77;
C.
items[ 99 ] = 77;
D.
items[2][3] = 77;
Correct Answer: A
Fill in the blank in the following code fragment so that each element of the array is assigned twice the
value of its index.
int[] array = new int[10];
// scan the array
for ( int index=0; index < array.length; index++ )
{
_______________________
}
A.
index = 2*index;
B.
array[ 2*index ] = 2*index;
C.
array[ index ] = 2*array[ index ];
D.
array[ index ] = 2*index;
Correct Answer: D
What is the output of the following code fragment:
int[] y = new int[5];
y[0] = 34;
y[1] = 88;
System.out.println( y[0] + " " + y[1] + " " + y[5] );
A. 34 88 0
B. 34 88 88
C. The program is defective and will not compile.
D. 0 34 88
Correct Answer: C
Level 2.1
Examine the following program fragment:
int[] array = { 1, 4, 3, 6, 8, 2, 5};
int what = array[0];
// scan the array
for ( int index=0; index < array.length; index++ )
{
if ( array[ index ] > what )
what = array[ index ];
}
System.out.println( what );
What does the fragment write to the monitor?
A. 1
B. 5
C. 1 4 3 6 8 2 5
D. 8
Correct Answer: B
Examine the following program fragment:
int[] array = { 1, 4, 3, 6, 8, 2, 5};
int what = array[0];
// scan the array
for ( int index=0; index < array.length; index++ )
{
if ( array[ index ] < what )
what = array[ index ];
}
System.out.println( what );
What does the fragment write to the monitor?
A. 1
B. 5
C. 1 4 3 6 8 2 5
D. 8
Correct Answer: A
Examine the following program fragment:
int[] array = { 1, 4, 3, 6 };
int what = 0;
// scan the array
for ( int index=0; index < array.length; index++ )
{
what = what + array[ index ] ;
}
System.out.println( what );
What does the fragment write to the monitor?
A. 14
B. 1
C. 6
D. 1 4 3 6
Correct Answer: A
What is the output of the following?
class ChangeIt
{
static void doIt( int[] z )
{
z[0] = 0;
}
}
class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt( myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
A.
12345
B.
01234
C.
02345
D.
23450
Correct Answer: C
Examine the following program fragment:
public class ArrayClone {
public static void main(String[] args) {
int marks[] = {90, 80};
int duplicateMarks[] = marks.clone();
marks[1]+=10;
System.out.println(duplicateMarks[1]);
}
}
A. prints 90
B. prints 80
C. ArrayIndexOutOfBoundsException is thrown
D. None of these
Correct Answer: B
Fill in the blanks of the following code fragment so that the elements of the array are printed in reverse
order, starting with the last element.
int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
for ( int index= ________ ; _____________ ; ______________ )
System.out.print( egArray[ index ] + " " );
A.
index = 0; index < egArray.length; index--
B.
index = length; index < 0; index--
C.
index = length-1; index > 0; index--
D.
index = egArray.length-1; index >= 0; index--
Correct Answer: D
What is the output of the following code
class ex {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
A.
Error in code
B.
Caffein
C.
Decaf
D.
None of the above
Correct Answer: B
What is the output of the following code
class ex {
public static void main(String[] args) {
int[] ia = new int[101];
for (int i = 0; i < ia.length; i++)
ia[i] = i;
int sum = 0;
for (int i = 0; i < ia.length; i++)
sum += ia[i];
System.out.println(sum);
}
}
A.
24
B.
50
C.
10
D.
34
Correct Answer: C
Given:
12. public class Yippee2 {
13
14. static public void main(String [] yahoo) {
15. for(int x = 1; x < yahoo.length; x++) {
16. System.out.print(yahoo[x] + " ");
17.
18.
}
}
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A. a b
B. b c
C. a b c
D. Compilation fails.
E. An exception is thrown at runtime.
Correct Answer: B
Level 2.2
Given:
10. public class Bar {
11. static void foo( int... x ) {
12. // insert code here
13.
}
14. }
Which two code fragments, either of which when inserted at line 12, will allow the class to compile?
(Choose two)
A. foreach( x ) System.out.println(z);
B. for( int z : x ) System.out.println(z);
C. while( x.hasNext() ) System.out.println( x.next() );
D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);
Correct Answer: B, D
S
Say that names has been declared
String[] names = new String[10] ;
and that further statements (not shown) have put String references into some of the slots. Which of the
following fragments prints out every String, but skips null references?
A. for ( int j = 0; names[j] != null; j++ )
System.out.println( names[j] );
B.
for ( int j = 0; j < names.length; j++ )
System.out.println( names[j] );
C.
for ( int j = 0; j < names.length && names[j] != null ; j++ )
System.out.println( names[j] );
D.
for ( int j = 0; j < names.length; j++ )
if ( names[j] != null )
System.out.println( names[j] );
Correct Answer: D
Given:
11. public static void main(String[] args) {
12. Object obj =new int[] { 1,2,3 };
13. int[] someArray = (int[])obj;
14. for (int i: someArray) System.out.print(i +" ")
15. }
What is the result?
A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.
Correct Answer: A
Examine the following program fragment:
public class Sample {
public static void main(String[] args) {
int marks[][] = {{90, 80},{50,60}};
int duplicateMarks[][] = marks.clone();
System.out.println((marks==duplicateMarks)+","+(marks[0]==duplicateMarks[0]));
}
}
A.
prints "false,false"
B.
prints "true,false"
C.
prints "true,true"
D.
prints "false,true"
Correct Answer: D
Output of the following code
import java.util.*;
public class ex{
public static void main(String[] args){
int num[] = {50};
int i = num.length;
int i,j,t;
System.out.print("Given number : ");
System.out.print(" " + num);
}
}
A.
50
B.
Error in code
C.
Address of the array
D.
Garbage value
Correct Answer: C
Level 3:
What happens during execution of the following program?
public class OperandOrder {
public static void main(String[] args) {
int i = 0;
int[] a = {3,6};
a[i] = i = 9;
System.out.println(i + " " + a[0] + " " + a[1]);
}
}
Select the one correct answer.
A. Throws an exception of type ArrayIndexOutOfBoundsException
B. Prints "9 9 6"
C. Prints "9 0 6"
D. Prints "9 3 6"
E. Prints "9 3 9"
Correct Answer: B
Explanation: The element referenced by a[i] is determined based on the current value of i, which is zero,
that is, the element a[0]. The expression i = 9 will evaluate to the value 9, which will be assigned to the
variable i. The value 9 is also assigned to the array element a[0]. After the execution of the statement,
the variable i will contain the value 9, and the array a will contain the values 9 and 6. The program will
print 9 9 6 when run.
What is the output when executing the Java program given below?
public class Sample {
static void callMe(int[] arr) {
int[] otherNumbers = new int[20];
arr = otherNumbers;
System.out.print(arr.length + ",");
}
public static void main(String[] args) {
int[] numbers = new int[10];
callMe(numbers);
System.out.print(numbers.length);
}
}
A.
10,10
B.
20,20
C.
20,30
D.
20,10
Correct Answer: D
Examine the following program fragment:
public class Sample {
public static void main(String[] args) {
int[] numbers=new int[10];
numbers[0]=1000;
Object obj=numbers;
System.out.println("The first number is:"+((int[])obj)[0]);
}
}
A. Compilation error as Array cannot be assigned to an Object
B. Runtime error as an Object cannot be cast to an Array
C. The first number is: 1000
D. None of these
Correct Answer: C
What is the output of the following?
class ChangeIt
{
static void doIt( int[] z )
{
z[0] = 0;
}
}
class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt( myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
A. 1 2 3 4 5
B. 0 1 2 3 4
C. 0 2 3 4 5
D. 2 3 4 5 0
Correct Answer: C
What is the output of the following?
class ChangeIt
{
static void doIt( int[] z )
{
z[0] = z[z.length-1];
}
}
class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt( myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
A. 1 2 3 4 5
B. 0 2 3 4 5
C. 5 2 3 4 1
D. 5 2 3 4 5
Correct Answer: D
What is the output of the following?
class ChangeIt
{
static void doIt( int[] z )
{
int temp = z[ z.length-1 ] ;
z[0] = temp;
}
}
class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt( myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
A. 1 2 3 4 5
B. 0 2 3 4 5
C. 5 2 3 4 1
D. 5 2 3 4 5
Correct Answer: D
What is the output of the following?
class LowHighSwap
{
static void doIt( int[] z )
{
int temp = z[ z.length-1 ] ;
z[ z.length-1 ] = z[0] ;
z[0] = temp;
}
}
class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
LowHighSwap.doIt( myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
A. 1 2 3 4 5
B. 5 2 3 4 1
C. 1 2 3 4 1
D. 5 2 3 4 5
Correct Answer: B
What is the output of the following?
class ChangeIt
{
static void doIt( int[] z )
{
int[] A = z ;
A[0] = 99;
}
}
class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt( myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
A. 1 2 3 4 5
B. 99 2 3 4 5
C. 0 2 3 4 5
D. 99 99 99 99 99
Correct Answer: B
What is the output of the following?
class ChangeIt
{
static void doIt( int[] z )
{
z = null ;
}
}
class TestIt
{
public static void main ( String[] args )
{
int[] myArray = {1, 2, 3, 4, 5} ;
ChangeIt.doIt( myArray );
for (int j=0; j<myArray.length; j++ )
System.out.print( myArray[j] + " " ) ;
}
}
A. 1 2 3 4 5
B. Nothing will be printed.
C. The program will halt with a run time error.
D. The program will halt with a run time error.
Correct Answer: A
What will be the result of attempting to compile and run the following class?
public class Passing {
public static void main(String[] args) {
int a = 0; int b = 0;
int[] bArr = new int[1]; bArr[0] = b;
inc1(a); inc2(bArr);
System.out.println("a=" + a + " b=" + b + " bArr[0]=" + bArr[0]);
}
public static void inc1(int x) { x++; }
public static void inc2(int[] x) { x[0]++; }
}
Select the one correct answer.
A. The code will fail to compile, since x[0]++; is not a legal statement.
B. The code will compile and will print "a=1 b=1 bArr[0]=1" when run.
C. The code will compile and will print "a=0 b=1 bArr[0]=1" when run.
D. The code will compile and will print "a=0 b=0 bArr[0]=1" when run.
E. The code will compile and will print "a=0 b=0 bArr[0]=0" when run.
Correct Answer: D
Explanation: The variables a and b are local variables that contain primitive values. When these variables
are passed as parameters to another method, the method receives copies of the primitive values in the
variables. The original variables are unaffected by operations performed on the copies of the primitive
values within the called method. The variable bArr contains a reference value that denotes an array
object containing primitive values. When the variable is passed as a parameter to another method, the
method receives a copy of the reference value. Using this reference value, the method can manipulate
the object that the reference value denotes. This allows the elements in the array object referenced by
bArr to be accessed and modified in the method inc2().
Given the class
// Filename: Args.java
public class Args {
public static void main(String[] args) {
System.out.println(args[0] + " " + args[args.length-1]);
}
}
what would be the result of executing the following on the command line?
java Args In politics stupidity is not a handicap
Select the one correct answer.
A. The program will throw ArrayIndexOutOfBoundsException.
B. The program will print "java handicap".
C. The program will print "Args handicap".
D. The program will print "In handicap".
E. The program will print "Args a".
F. The program will print "In a".
Correct Answer: D
Explanation: The length of the array passed to the main() method corresponds exactly to the number of
command-line arguments given to the program. Unlike some other programming languages, the
element at index 0 does not contain the name of the program. The first argument given is retrieved
using args[0], and the last argument given is retrieved using args[args.length-1].
Which statements would cause a compilation error if inserted in the location indicated in the following
program?
public class ParameterUse {
static void main(String[] args) {
int a = 0;
final int b = 1;
int[] c = { 2 };
final int[] d = { 3 };
useArgs(a, b, c, d);
}
static void useArgs(final int a, int b, final int[] c, int[] d) {
// INSERT STATEMENT HERE.
}
}
Select the two answers.
A. a++;
B. b++;
C. b = a;
D. c[0]++;
E. d[0]++;
F. c = d;
Correct Answer: A, F
Explanation: Values can only be assigned once to final variables. A final formal parameter is assigned the
value of the actual parameter at method invocation. Within the method body, it is illegal to reassign or
modify the value of a final parameter. This causes a++ and c = d to fail. Whether the actual parameter is
final does not constrain the client that invoked the method, since the actual parameter values are
copied to the formal parameters.
What would be the result of attempting to compile and run the following program?
// Filename: MyClass.java
class MyClass {
public static void main(String[] args) {
int size = 20;
int[] arr = new int[ size ];
for (int i = 0; i < size; ++i) {
System.out.println(arr[i]);
}
}
}
Select the one correct answer.
A. The code will fail to compile because the array type int[] is incorrect.
B. The program will compile, but will throw an ArrayIndexOutOfBoundsException when run.
C. The program will compile and run without error, but will produce no output.
D. The program will compile and run without error and will print the numbers 0 through 19.
E. The program will compile and run without error and will print 0 twenty times.
F. The program will compile and run without error and will print null twenty times.
Correct Answer: E
Explanation: The array declaration is valid and will declare and initialize an array of length 20 containing
int values. All the values of the array are initialized to their default value of 0. The for loop will print all
the values in the array, that is, it will print 0 twenty times.
Given the following program, which statement is true?
class MyClass {
public static void main(String[] args) {
String[] numbers = { "one", "two", "three", "four" };
if (args.length == 0) {
System.out.println("no arguments");
} else {
System.out.println(numbers[ args.length ] + " arguments");
}
}
}
Select the one correct answer.
A. The program will fail to compile.
B. The program will throw a NullPointerException when run with zero program arguments.
C. The program will print "no arguments" and "two arguments" when called with zero and three
program arguments, respectively.
D. The program will print "no arguments" and "three arguments" when called with zero and three
program arguments, respectively.
E. The program will print "no arguments" and "four arguments" when called with zero and three
program arguments, respectively.
F. The program will print "one arguments" and "four arguments" when called with zero and three
program arguments, respectively.
Correct Answer: E
Explanation: The program will type "no arguments" and "four arguments" when called with 0 and 3
arguments, respectively. When the program is called with no arguments, the args array will be of length
zero. The program will in this case type "no arguments". When the program is called with three
arguments, the args array will have length 3. Using the index 3 on the numbers array will retrieve the
string "four", because the start index is 0.
What would be the result of trying to compile and run the following program?
public class DefaultValuesTest {
int[] ia = new int[1];
boolean b;
int i;
Object o;
public static void main(String[] args) {
DefaultValuesTest instance = new DefaultValuesTest();
instance.print();
}
public void print() {
System.out.println(ia[0] + " " + b + " " + i + " " + o);
}
}
Select the one correct answer.
A. The program will fail to compile because of uninitialized variables.
B. The program will throw a java.lang.NullPointerException when run.
C. The program will print "0 false NaN null".
D. The program will print "0 false 0 null".
E. The program will print "null 0 0 null".
F. The program will print "null false 0 null".
Correct Answer: D
Explanation: The program will print "0 false 0 null" when run. All the instance variables, including the
array element, will be initialized to their default values. When concatenated with a string, the values are
converted to their string representation. Notice that the null pointer is converted to the string "null"
rather than throwing a NullPointerException.
Say that names has been declared
String[] names = new String[10] ;
and that further statements (not shown) have put String references into some of the slots. Which of the
following fragments counts the number of non-null slots in the array?
A. int count = 0;
for ( int j = 0; j < names.length; j++ )
if ( names[j] != null )
count++ ;
B. int j = 0;
int count = 0;
while ( names[ ++j ] != null )
count++ ;
C. int count = 0;
while ( names[ count ] != null )
{
count++;
}
D. int j = 0;
for ( int count = 0; count < names.length; count++ )
if ( names[j] != null )
j++ ;
Correct Answer: A
Output of the following code
import java.io.*;
class ex {
static int [][] myArray = new int[3][];
public static void main(String[] args) {
myArray[0] = new int[3];
for(int i=0; i<1; i++)
fillArray(i, i+3);
System.out.println();
}
private static void fillArray(int row, int col) {
for( int i=0; i<col; i++)
myArray[row][i] = i;
for( int i=0; i<col; i++)
System.out.print(myArray[row][i]);
System.out.println();
}
}
A.
2
B.
012
1
0
C.
102
D.
000
Correct Answer: B