ArrayList

Quiz Guidelines

 

Multiple Choice Question

QID: 
241

Which of the following are valid code fragments:

Select 2 options

A. new Object[]{ "aaa", new Object(), new ArrayList(), 10};
B. new Object[]{ "aaa", new Object(), new ArrayList(), {} };
C. new Object[]{ "aaa", new Object(), new ArrayList(), new String[] {""} }; 
D. new Object[1]{ new Object() };

Multiple Choice Question

QID: 
240

Given the complete contents of TestClass.java file:

package x;
public class TestClass {
    ArrayList<String> al;
    public void init(){
        al = new ArrayList<>();
        al.add("Name 1");
        al.add("Name 2");
    }
    public static void main(String[] args) throws Exception {
        TestClass tc = new TestClass();
        tc.init();
        System.out.println("Size = "+tc.al.size());
    }
}

Which import statement should be added to make it compile?

Select 1 option

A. import java.lang.*;
B. import java.lang.ArrayList;
C. import java.util.ArrayList; 
D. import java. Collections. ArrayList;
E. No import is necessary.

Multiple Choice Question

QID: 
239

What will the following code print when compiled and run?

import java.util.*;
public class TestClass {
    public static void main(String[] args) throws Exception {
        ArrayList<Integer> al = new ArrayList<>(); //1
        al.add(111); //2
        System.out.println(al.get(al.size()));  //3
     }
}

Select 1 option

A. It will not compile.
B. It will throw an exception at run time because of line //1
C. It will throw an exception at run time because of line //2 
D. It will throw an exception at run time because of line //3
E. null.

Multiple Choice Question

QID: 
238

Given:

import java.util.*;
public class TestClass {
    public static void main(String[] args) throws Exception {
        ArrayList<Double> al = new ArrayList<>();
        //INSERT CODE HERE
     }
}

What can be inserted in the above code so that it can compile without any error?

Select 3 options

A. al.add (111);
B. System.out.println (al.indexOf (1.0));
C. System.out.println (al.contains ("string")); 
D. Double d = al.get (al.length);
E. al.notifyAll();

Multiple Choice Question

QID: 
237

Identify the correct statements about ArrayList?

Select 3 options

A. ArrayList extends java.util.AbstractList.
B. It allows you to access its elements in random order.
C. You must specify the class of objects you want to store in ArrayList when you
declare a variable of type ArrayList.
D. ArrayList does not implement RandomAccess.
E. You can sort its elements using Collections.sort() method.

Pages