Java Questions & Answers – Java.util – Array Class

This section of our 1000+ Java MCQs focuses on Array class under java.util library of Java Programming Language.

1. Which of these standard collection classes implements all the standard functions on list data structure?
a) Array
b) LinkedList
c) HashSet
d) AbstractSet
View Answer

Answer: a
Explanation: None.

2. Which of these classes implements Set interface?
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList
View Answer

Answer: b
Explanation: HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.

3. Which of these method is used to make all elements of an equal to specified value?
a) add()
b) fill()
c) all()
d) set()
View Answer

Answer: b
Explanation: fill() method assigns a value to all the elements in an array, in other words it fills the array with specified value.

4. Which of these method of Array class is used sort an array or its subset?
a) binarysort()
b) bubblesort()
c) sort()
d) insert()
View Answer

Answer: c
Explanation: None.

5. Which of these methods can be used to search an element in a list?
a) find()
b) sort()
c) get()
d) binaryserach()
View Answer

Answer: d
Explanation: binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays.

6. Which of these method is used to change an element in a LinkedList Object?
a) change()
b) set()
c) redo()
d) add()
View Answer

Answer: c
Explanation: An element in a LinkedList object can be changed by first using get() to obtain the index or location of that object and the passing that location to method set() along with its new value.

7. What is the output of this program?

  1.     import java.util.*;
  2.     class Arraylist {
  3.         public static void main(String args[]) {
  4.             ArrayList obj1 = new ArrayList();
  5.             ArrayList obj2 = new ArrayList();
  6.             obj1.add("A");
  7.             obj1.add("B");
  8.             obj2.add("A");
  9.             obj2.add(1, "B");
  10.             System.out.println(obj1.equals(obj2));
  11.         }
  12.     }

a) 0
b) 1
c) true
d) false
View Answer

Answer: c
Explanation: obj1 and obj2 are an object of class ArrayList hence it is a dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Both the objects obj1 and obj2 contain same elements i:e A & B thus obj1.equals(obj2) method returns true.
Output:
$ javac Arraylist.java
$ java Arraylist
true

8. What is the output of this program?

  1.     import java.util.*;
  2.     class Array {
  3.         public static void main(String args[]) {
  4.             int array[] = new int [5];
  5.             for (int i = 5; i > 0; i--)
  6.                 array[5 - i] = i;
  7.             Arrays.sort(array);
  8.             for (int i = 0; i < 5; ++i)
  9.             	System.out.print(array[i]);;
  10.         }
  11.     }

a) 12345
b) 54321
c) 1234
d) 5432
View Answer

Answer: b
Explanation: Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:
$ javac Array.java
$ java Array
12345

9. What is the output of this program?

  1.     import java.util.*;
  2.     class Array {
  3.         public static void main(String args[]) {
  4.             int array[] = new int [5];
  5.             for (int i = 5; i > 0; i--)
  6.                 array[5-i] = i;
  7.             Arrays.fill(array, 1, 4, 8);
  8.             for (int i = 0; i < 5 ; i++)
  9.                 System.out.print(array[i]);
  10.         }
  11.     }

a) 12885
b) 12845
c) 58881
d) 54881
View Answer

Answer: c
Explanation: array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
Output:
$ javac Array.java
$ java Array
58881

10. What is the output of this program?

  1.     import java.util.*;
  2.     class Array {
  3.         public static void main(String args[]) {
  4.             int array[] = new int [5];
  5.             for (int i = 5; i > 0; i--)
  6.                 array[5 - i] = i;
  7.             Arrays.sort(array);
  8.             System.out.print(Arrays.binarySearch(array, 4));
  9.         }
  10.     }

a) 2
b) 3
c) 4
d) 5
View Answer

Answer: b
Explanation: None.
Output:
$ javac Array.java
$ java Array
3

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry