Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer the Questions(Multiple Choice ): Question 21 Which statement will return

ID: 3860736 • Letter: A

Question

Answer the Questions(Multiple Choice):

Question 21

Which statement will return the number of rows in a two dimensional array a?

int a = a.rows

int a = a.length

int a = a[i].length

int a = a.size

2.5 points

Question 22

What statement(s) could you use to declare and instantiate an ArrayList object reference of Book objects named library with a default capacity of 10 elements?

ArrayList<Book > library = new ArrayList<Book>();

ArrayList<Book > library;

library = new ArrayList<Book>();

both a and b

neither a or b

2.5 points

Question 23

What method of an arraylist class object returns the number of elements in an ArrayList object?

size()

capacity

length

length( )

2.5 points

Question 24

After the following code sequence is executed, what are the contents at ArrayList index 1 in a?

ArrayList<Integer> a = new ArrayList<Integer>( );
a.add( 7 );
a.add( 10 );
a.add( 21 );
a.set( 1, 4 );
a.remove( 0 );

10

7

4

21

2.5 points

Question 25

Which of the following is inherited by a subclass

all instance variables and methods

public instance variables and methods only

protected instance variables and methods only

protected and public instance variables and methods

a.

int a = a.rows

b.

int a = a.length

c.

int a = a[i].length

d.

int a = a.size

Explanation / Answer

Question 21

Which statement will return the number of rows in a two dimensional array a?

Correct option: b. int a = a.length

Explanation: 2D arrays can be imagined as arrays of arrays. So a 2D array with 10 rows and 5 columns could be viewed as an array containing 10 subarrays that have 5 spots each. Hence a.length would give the number of subarryays that means number of rows in a 2D array named a. So b. int a = a.length would be the correct option. There are no such constant variables like size or rows for an Array; so other options are not correct one.

Question 22

What statement(s) could you use to declare and instantiate an ArrayList object reference of Book objects named library with a default capacity of 10 elements?

Correct option: d. neither a and b

Explanation: Default initial capacity of ArrayList is 10. But both statements of a and b only would not served the purpose. As soon as first element is added to the ArrayList then only it would initialized to it’s default capacity of 10. So to instantiate an ArrayList object reference of Book objects named library with a default capacity of 10 elements we have to add atleast one element to it.

Question 23

What method of an arraylist class object returns the number of elements in an ArrayList object?

Correct option: a. size()

Explanation: the size() method of ArrayList returns the number of elements in this list. Hence option a is correct one. There is no such method called length() and there is no such constant variable like length or capacity in ArrayList class by which we can get the number of elements in this list. So option b, c and d are incorrect.

Question 24

After the following code sequence is executed, what are the contents at ArrayList index 1 in a?

ArrayList<Integer> a = new ArrayList<Integer>( );

a.add( 7 );

a.add( 10 );

a.add( 21 );

a.set( 1, 4 );

a.remove( 0 );

Correct option: d.21

Explanation: a.add( 7 );a.add( 10 );a.add( 21 ); these three statement add 7,10 and 21 to array list a. Then arraylist a would look like: [7,10,21];

a.set( 1, 4 ); this statement will set 4 at index 1 in array list a. Then arraylist a would look like: [7,4,21];

a.remove( 0 ); this statement would remove the element at index 0 of array list a; then arraylist a would look like: [4,21];

Hence at this point array list a would contain 2 values 4 and 21 at index 0 and 1 respectively.

So, after execution of the given code snippet the contents at ArrayList index 1 in a would be 21.

Question 25

Which of the following is inherited by a subclass?

Correct option: d.protected and public instance variables and methods

Explanation: private instance variable and methods cannot be inherited by subclass so option a) is incorrect. Both public and protected variables and methods are inherited by a subclass hence option b and c are not true, correct option is option d.

/*Hope this would help you! Thank you!! */