Quiz
02-Objective-Wise-Creating-Using-Array
Crie uma conta grátis para terminar este quiz
Salve seu progresso e veja sua nota final.
Já tem uma conta? Inicie sessão
Perguntas do quiz (20)
1public class TestClass {
int[ ] [ ] matrix = new int[ 2] [ 3] ;
int a[ ] = { 1, 2, 3} ;
int b[ ] = { 4, 5, 6} ;
public int compute( int x, int y) {
//1 : Insert Line of Code here
}p
ublic void loadMatrix( ) {
for( int x=0; x<matrix. length; x++) {
for( int y=0; y<matrix[ x] . length; y++) {
//2: Insert Line of Code here
}
}
}
}What can be inserted at //1 and //2?- Areturn a( x) *b( y) ;
and
matrix( x, y) = compute( x, y) ; - Breturn a[ x] *b[ y] ;
and
return a[ x] *b[ y] ; - Creturn a[ x] *b[ y] ;
and
matrix[ x] [ y] = compute( x, y) ; - Dreturn a( x) *b( y) ;
and
matrix( x) ( y) = compute( x, y) ; - Ereturn a[ x] *b[ y] ;
and
matrix[ [ x] [ y] ] = compute( x, y) ;
- Areturn a( x) *b( y) ;
2Consider the following array definitions:
int[ ] array1, array2[ ] ;
int[ ] [ ] array3;
int[ ] array4[ ] , array5[ ] ;
Which ofthe following are valid statements?- Aarray2 = array3;
- Barray2 = array4;
- Carray1 = array2;
- Darray4 = array1;
- Earray5 = array3
3What will be the result ofattempting to run the following program?
public class StringArrayTest{
public static void main( String args[ ] ) {
String[ ] [ ] [ ] arr ={ { { " a" , " b" , " c" } , { " d" , " e" , null } } ,
{ { " x" } , null } , { { " y" } } , { { " z" , " p" } , { } }
} ;
System. out. println( arr[ 0] [ 1] [ 2] ) ;
}
}- AIt will throw NullPointerBoundsException.
- BIt will throw ArrayIndexOutOfBoundsException.
- CIt will print null.
- DIt will run without any error but will print nothing.
- ENone ofthe above.
4Given the following declaration:
int[ ] [ ] twoD = { { 1, 2, 3} , { 4, 5, 6, 7} , { 8, 9, 10 } } ;
What will the following lines ofcode print?
System. out. println( twoD[ 1] . length) ;
System. out. println( twoD[ 2] . getClass( ) . isArray( ) ) ;
System. out. println( twoD[ 1] [ 2] ) ;- A4
true
6 - B3
true
3 - C3
false
3 - D4
false
6
- A4
5Consider the following program:
public class TestClass{
public static void main( String[ ] args) {
String tom = args[ 0] ;
String dick = args[ 1] ;
String harry = args[ 2] ;
}
}Wha
t will the value of'harry' ifthe program is run from the command line:
java TestClass 111 222 333- A111
- B222
- C333
- DIt will throw an ArrayIndexOutOfBoundsException
- ENone ofthe above.
6What will be the result oftrying to compile and execute ofthe following program?
public class TestClass{
public static void main( String args[ ] ) {
int i = 0 ;
int[ ] iA = { 10, 20} ;
iA[ i] = i = 30 ;
System. out. println( " " + iA[ 0 ] + " " + iA[ 1 ] + " " +i) ;
}
}- AIt will throw ArrayIndexOutofBoundsException at Runtime.
- BCompile time Error.
- CIt will prints 10 20 30
- DIt will prints 30 20 30
- EIt will prints 0 20 30
7What will be the result ofattempting to compile and run the following class?
public class TestClass{
public static void main( String args[ ] ) {
int i = 1;
int[ ] iArr = { 1} ;
incr( i) ;
incr( iArr) ;
System. out. println( " i = " + i + " iArr[ 0] = " + iArr [ 0 ] ) ;
}p
ublic static void incr( int n ) { n++ ; }
public static void incr( int[ ] n ) { n [ 0 ] ++ ; }
}- AThe code will print i = 1 iArr[ 0] = 1;
- BThe code will print i = 1 iArr[ 0] = 2;
- CThe code will print i = 2 iArr[ 0] = 1;
- DThe code will print i = 2 iArr[ 0] = 2;
- EThe code will not compile.
8What will the following program print?
class Test{
public static void main( String[ ] args) {
int i = 4;
int ia[ ] [ ] [ ] = new int[ i] [ i = 3] [ i] ;
System. out. println( ia. length + " , " + ia[ 0] . length+" , " + ia[ 0] [ 0] . length) ;
}
}- AIt will not compile.
- B3, 4, 3
- C3, 3, 3
- D4, 3, 4
- E4, 3, 3
9What will the following program print?
public class TestClass{
public static void main( String[ ] args) {
String str = " 111" ;
boolean[ ] bA = new boolean[ 1] ;
if( bA[ 0] ) str = " 222" ;
System. out. println( str) ;
}
}- A111
- B222
- CIt will not compile as bA[ 0] is uninitialized
- DNone ofthe above.
10What sequence ofdigits will the following program print?
import j ava. util. * ;
public class ListTest{
public static void main( String args[ ] ) {
List s1 = new ArrayList( ) ;
s1. add( " a" ) ;
s1. add( " b" ) ;
s1. add( 1, " c" ) ;
List s2 = new ArrayList( s1. subList( 1, 1) ) ;
s1. addAll( s2) ;
System. out. println( s1) ;
}
}- AThe sequence a, b, c is printed.
- BThe sequence a, b, c, b is printed.
- CThe sequence a, c, b, c is printed.
- DThe sequence a, c, b is printed.
- ENone ofthe above.
11Consider the following program...
class ArrayTest{
public static void main( String[ ] args) {
int ia[ ] [ ] = { { 1, 2} , null } ;
for ( int i = 0; i < 2; i++)
for ( int j = 0; j < 2; j ++)
System. out. println( ia[ i] [ j ] ) ;
}
}- AIt will not compile
- BIt will throw an ArrayIndexOutOfBoundsException at Runtime
- CIt will throw a NullPointerException at Runtime
- DIt will compile and run without throwing any exceptions.
- ENone ofthe above.
12Given the following declaration, select the correct way to get the number ofelements
in the array, assuming that the array has been initialized.
int[ ] intArr;- AintArr[ ] . length( )
- BintArr. length( )
- CintArr. length
- DintArr[ ] . size( )
13Consider the following class...
class Test{
public static void main( String[ ] args) {
int[ ] a = { 1, 2, 3, 4 } ;
int[ ] b = { 2, 3, 1, 0 } ;
System. out. println( a [ ( a = b) [ 3] ] ) ;
}
}Wha
t will it print when compiled and run ?- AIt will not compile.
- BIt will throw ArrayIndexOutOfBoundsException when run.
- CIt will print 1.
- DIt will print 3.
- EIt will print 4
14What would be the result ofcompiling and running the following program?
class SomeClass{
public static void main( String args[ ] ) {
int size = 10;
int[ ] arr = new int[ size] ;
for ( int i = 0 ; i < size ; ++i) System. out. println( arr[ i] ) ;
}
}- AThe code will fail to compile, because the int[ ] array declaration is incorrect.
- BThe program will compile, but will throw an IndexArrayOutOfBoundsException
when run. - CThe program will compile and run without error, and will print nothing.
- DThe program will compile and run without error and will print null ten times.
- EThe program will compile and run without error and will print 0 ten times.
15Given the following program, which statements are true?
// Filename: TestClass. j ava
public class TestClass{
public static void main( String args[ ] ) {
A[ ] a, a1;
B[ ] b;
a = new A[ 10] ; a1 = a;
b = new B[ 20] ;
a = b; // 1
b = ( B[ ] ) a; // 2
b = ( B[ ] ) a1; // 3
}
}c
lass A { }
class B extends A { }- ACompile time error at line 3.
- BThe program will throw a java.lang.ClassCastException at the line labelled 2 when
run. - CThe program will throw a java.lang.ClassCastException at the line labelled 3 when
run. - DThe program will compile and run ifthe (B[ ] ) cast in the line 2 and the whole line
3 is removed. - EThe cast at line 2 is needed.
16Which ofthe following statements are valid ?
- AString[ ] sa = new String[ 3] { " a" , " b" , " c" } ;
- BString sa[ ] = { " a " , " b" , " c" } ;
- CString sa = new String[ ] { " a" , " b" , " c" } ;
- DString sa[ ] = new String[ ] { " a" , " b" , " c" } ;
- EString sa[ ] = new String[ ] { " a" " b" " c" } ;
17Which ofthe following statements about an array are correct?
- AAn array can dynamically grow in size.
- BArrays can be created only for primitive types.
- CEvery array has a built in property named 'size' which tells you the number of
elements in the array. - DEvery array has in implicit method named 'length' which tells you the number of
elements in the array - EElement indexing starts at 0.
18What would be the result oftrying to compile and run the following program?
public class Test{
int[ ] ia = new int[ 1] ;
Obj ect oA[ ] = new Obj ect[ 1] ;
boolean bool;
public static void main( String args[ ] ) {
Test test = new Test( ) ;
System. out. println( test. ia[ 0] + " " +
test. oA[ 0] +" " +test. bool) ;
}
}- AThe program will fail to compile, because ofuninitialized variable 'bool'.
- BThe program will throw a java.lang.NullPointerException when run.
- CThe program will print "0 null false".
- DThe program will print "0 null true".
- EThe program will print null and false but will print junk value for ia[0].
19Which ofthe following correctly declare a variable which can hold an array of10
integers?- Aint[ ] iA
- Bint[ 10] iA
- Cint iA[ ]
- DObj ect[ ] iA
- EObj ect[ 10] iA
20Which ofthe following code fragments will successfully initialize a two-dimensional
array ofchars named cA with a size such that cA[2][3] refers to a valid element?
1.
char[ ] [ ] cA = { { ' a' , ' b' , ' c' } , { ' a' , ' b' , ' c' } } ;
2.
char cA[ ] [ ] = new char[ 3] [ ] ;
for ( int i=0; i<cA. length; i++) cA[ i] = new char[ 4] ;
3.
char cA[ ] [ ] = { new char[ ] { ' a' , ' b' , ' c' } , new char[ ] {
' a' , ' b' , ' c' } } ;
4
char cA[ 3] [ 2] = new char[ ] [ ] { { ' a' , ' b' , ' c' } , { ' a' , ' b' ,
' c' } } ;
5.
char[ ] [ ] cA = { " 1234" , " 1234" , " 1234" } ;- A1, 3
- B4, 5
- C2, 3
- D1, 2, 3
- E2
Informações
- Descrição
- OCA-SE-7 Objective-Wise-Ethuware-OCA-SE-7 Quiz sobre 02-Objective-Wise-Creating-Using-Array, criado por Wong Fei Hung em 30-07-2017.
- Autor
- Wong Fei Hung
- Idioma
- Português
- Visualizações
- 0
- Etiquetas
- Avaliação
- Ainda sem avaliações Avalie este quiz