Font size
WorksheetsArrays in Java
Total questions: 112
Worksheet time: 1hrs 6mins
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
Predict the output?
public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}
10 20 30 40 50
Compiler Error
10 20 30 40
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
0
0
garbage value
garbage value
Compiler Error
Exception
public class Main {
public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
Compiler Error
0
1 2
3 4 5
6 7 8 9
0
0 0
0 0 0
0 0 0 0
9
7 8
4 5 6
0 1 2 3
Which of the following is the correct way of importing an entire package ‘pkg’?
import pkg.
Import pkg.
import pkg.*
Import pkg.*
Which of the following is/are advantages of packages?
Packages avoid name clashes
Classes, even though they are visible outside their package, can have fields visible to packages only
We can have hidden classes that are used by the packages, but not visible outside.
All of the above
Which of these is a mechanism for naming and visibility control of a class and its content?
Object
Packages
Interfaces
None of the above
Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?
Public
Protected
No Modifier
All of the mentioned
Arrays in Java are dynamically allocated using the . . . . operator.
create
dynamic
arrayList
new
Which of the following statements correctly declares a two-dimensional integer array?
int Matrix[ ] = new int[5,4];
int Matrix[ ]; Matrix = new int[5,4];
int Matrix[ ][ ] = new int[5][4];
int Matrix[ ][ ] = int[5][4];
ar, given the following declaration:int[] ar = {2, 4, 6, 8 }0, 1, 2, 31, 2, 3, 40, 2, 4, 6What is the first index of an array?
1
0
2
3
For the array:
int stats[3];
What is the range of the index?
0 to 3
0 to 2
1 to 3
0 to 4
int [] nums = {2, 3, 5, 8, 9, 11};
How would you access the fourth element in nums?
nums[4]
nums[3]
nums(4)
nums(3)
a = {0, 2, 3, 4} and v = 1?
Find the output
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Compile error
Exception
Garbage Value
Garbage Value
0
0
Is String a primitive data type?
Yes
No
Both
what is the output of this question?
class Test1 {
public
static void main(String[] args)
{
int x = 20;
System.out.println(x);
}
static
{
int x = 10;
System.out.print(x + " ");
}
}
10 20
20 10
10 10
20 20
Predict the output of the following program.
class A{
int a=40;//non static
public static void main(String args[]){
System.out.println(a);
}
}
Compilation Error
40
0
None of the above
what is the output of this question?
class Test1 {
static int x = 10;
public
static void main(String[] args)
{
Test1 t1 = new Test1();
Test1 t2 = new Test1();
t1.x = 20;
System.out.print(t1.x + " ");
System.out.println(t2.x);
}
}
10 10
20 20
10 20
20 10
What is the output of this question?
class Test1 {
static int i = 1;
public static void main(String[] args)
{
for (int i = 1; i < 10; i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
3 6 9
3 6 9 …. 27
Error
none
What is the output of this question?
class Test1 {
static int i = 1;
public static void main(String[] args)
{
int i = 1;
for (Test1.i = 1; Test1.i < 10; Test1.i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
1 3 9
1 2 3 … 9
3 5 7 9 11 13 15 17 19
None
Which one of the following is a valid statement?
char[] c = new char();
char[] c = new char[5];
char[] c = new char(4);
char[] c = new char[];
What will be the output?
public class Test
{
public static void main(String args[])
{
int[] x=new int[3];
System.out.print("x[0] is" + x[0]);
}}
The program has a compile error because the size of the array wasn't specified when declaring the array.
The program has a runtime error because the array elements are not initialized.
The program runs fine and displays x[0] is 0.
The program has a runtime error because the array element x[0] is not defined
An Array in Java is a collection of elements of ___ data type.
Same
Different
Which is the correct way of knowing Array Size in Java?
int[] ary;
ary.length()
int[] ary;
ary.length
int[] ary;
ary->length()
int[] ary;
ary->length
What is the output of the below Java program with arrays?
String[] colors = {"RED";"YELLOW";"WHITE"};
System.out.print(colors[2]);
RED
YELLOW
WHITE
Compiler error
Find the output
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Compile error
Exception
Garbage value
Garbage value
0
0
Find the output
class Test {
public static void main(String args[]) {
int arr [ ] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Compile error
Exception
Garbage value
Garbage value
0
0
class Demo1
{
public static void main(String args[])
{
int i[] = new int[0];
System.out.println(i[0]);
}
}
0
Exception
Garbage value
class Demo1
{
public static void main(String args[])
{
int i[] = new int[10];
System.out.println(i[10]);
}
}
0
Garbage value
Out of bound exception
Error
How will you identify a single dimensional array?
single curly brackets
single square brackets
multiple squares brackets
single angled brackets
If the data is in order the best search to use would be a______
Binary Search
Linear Search
What is returned by values[5]?
2
12
6
8
Which option represents: An array holding String data?
var names = {"Raj", "Simran", "Divya"};
var names = {Raj, Simran, Divya};
ar nums ={2, 3, 5, 8, 9, 11};
How would you access the second element in nums
num(2)
num[2]
num(1)
num[1]
What is the result of :
nums[1]+nums[3]
3
14
6
5
What will be the array after executing this line ?
names[1] = "Sarah";
What does the following segment of code print out?
int[] numArray = { 2, 7, 4, 12, 0, 2 };
System.out.println("Number: " + numArray[numArray.length - 1]);
Number: 2
Number: 0
Number: 12
Number: 6
What is the correct way to initialize an array with 10 positions?
int numbers = new int[10];
int[] numbers = new int[];
int[] numbers = new int[10];
int[10] numbers = new int[10];
What value is stored in each of the 5 positions in this array?
String[] numbers = new String[5];
null
1
0
nothing
What is an Array?
Variable
Data type
Pointer
String
The total number of elements in the array is called...............
index
length
subscript
memory
for(int i=0; i<10; i++)
numbers[i] = 2 * i + 1;
// Which index of the array holds the value of 5?
1
2
3
4
What will be the memory size of the array given below:
double[] fees = new double[15];
What will be the memory size of the array given below:
int[][] num = new int[3][5];
In an array, the index of the first element is (a)
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
Find the output
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Compile error
Exception
Garbage Value
Garbage Value
0
0
Find the output
class Test {
public static void main(String args[]) {
int arr [ ] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Compile error
Exception
Garbage Value
Garbage Value
0
0
Output of following Java program?
class Test{
public static void main (String[] args) {
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}}
Same
Not same
Error
public class Main {
public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
Compiler Error
0
1 2
3 4 5
6 7 8 9
0
0 0
0 0 0
0 0 0 0
9
7 8
4 5 6
0 1 2 3
System.out.println("Number: " + numArray[0]);
How will you identify a single dimensional array?
single curly brackets
single square brackets
multiple squares brackets
single angled brackets
Dia, Avyaan, and Aashi are on a thrilling coding adventure, battling the challenges of a Java project. They stumble upon a task where they need to store multiple grades of the same type for a student. Can you help them choose the right Java data type for this task?
A mystical data type that can store multiple values of different data types
A magical data type that can store multiple values of the same data type
A peculiar data type that can store a single value
A strange data type that can store a boolean value
Imagine Krish, Kabir, and Shreya are on a thrilling quest to complete their Java project. They stumble upon a challenge where they need to store multiple treasures (values) in a single magical box (variable). Can you help them by choosing the correct incantation (syntax) to conjure an array in Java?
int[] magicalBox = new int[];
int magicalBox = new int[];
int[] magicalBox = new int[length];
int magicalBox = new int[length];
Arnav, Krish, and Dhruv are on a thrilling adventure in a magical library. They discover that the books are arranged in a mystical array. If they start their quest from the first magical book, what would be the index of that book in the array?
0
1
-1
length-1
Imagine Tisha, Shaan, and Arjun are on a thrilling mission to design a spaceship! They decide to use an array to map out the rows and columns of seats in their spaceship. What type of array should they use to successfully complete their mission?
An array with only one subscript
An array with multiple subscripts
An array with no subscripts
An array with variable length
Riyaan, Akhil and Aashi are working together on a Java project for their school's coding competition. They need to safeguard their array data. Why do you think they are considering cloning their array in Java?
To create a new array with the same elements
To create a copy of the array
To resize the array
To sort the array
Imagine Vanya, Mira, and Akhil are working together on a Java program for their school project. They are having a blast, but suddenly they hit a roadblock. They try to access an index in an array that doesn't exist. Oops! What exception will Java throw in this fun-filled coding adventure?
ArrayIndexOutOfBoundsException
NullPointerException
IndexOutOfBoundsException
IllegalArgumentException
Aanya, Mira, and Prisha are having a fun coding session on Java. Suddenly, they stumble upon a concept called 'anonymous array'. Aanya believes it's an array without any elements, Mira thinks it's an array without a name, and Prisha is of the opinion that it's an array with a single element. Can you help them figure out who is correct?
Aanya: An array without any elements
Mira: An array without a name
Prisha: An array with a single element
None of them
Imagine Nikita, Aashi, and Anika are working together on a massive Java project. They need to store a colossal amount of data in a single variable and are considering using an array. Can you guess one advantage they would get if they use an array in their Java project?
It would turn their code into a super-efficient machine
It would magically allow for dynamic size
It would grant them the power of random access
It would automatically manage memory like a pro
Imagine Nikita, Dia and Kabir are working together on a thrilling Java project. They are in a situation where they need to store a wild mix of data like integers, strings, and booleans all in a single array. Is this possible in Java?
Yes, absolutely!
No, not at all.
Only if the data types are having a party together.
Only if the array is declared as an Object, the master of all.
Aarav, Shreya, and Mira are on a thrilling adventure to build a magical project. They stumbled upon an idea to use an Array, a mystical Non-Primitive Datatype, to store their collection of magical items, which includes both primitive and Non-Primitive datatypes. Can they make this magical array?
True
False
ar, given the following declaration:int[] ar = {2, 4, 6, 8 }0, 1, 2, 31, 2, 3, 40, 2, 4, 6What is the first index of an array?
1
0
2
3
For the array:
int stats[3];
What is the range of the index?
0 to 3
0 to 2
1 to 3
0 to 4
int [] nums = {2, 3, 5, 8, 9, 11};
How would you access the fourth element in nums?
nums[4]
nums[3]
nums(4)
nums(3)
a = {0, 2, 3, 4} and v = 1?
a = {0, 2, 3, 4} and v = 3?
a = {0, 2, 3, 4} and v = 2?
System.out.println("Number: " + numArray[2]);
System.out.println("Number: " + numArray[numArray.length - 1]);
int[] numbers = new int[5];
what is an Array?
variable
data type
pointer
string
why arrays are required?
searching
sorting
reading
passing
The total number of elements in the array is called...............
Length
memory
index
subscript
Arrays are ...................data types?
built-in
user defined
computer
analysis
which statement is true about Arrays
1) Arrays can store a large number of value with single name.
2) Arrays are used to process many value easily and quickly.
3) The values stored in an array can be sorted easily.
4) The search process can be applied on arrays easily.
all above are true
How many types of arrays are there?
One-Dimension Array
Two-Dimension Array
Three Dimension Array
five Dimension Array
which control structure are required for Array
while loop
for loop
if......else
switch
sequential search is known as linear search.? True or False
true
false
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
System.out.println("Number: " + numArray[numArray.length - 1]);
Java array is a collection of ________.
similar type of elements
different type of element
heterogeneous data
Both A and C
Array data access using _____.
Operator
Variable
index
Pointer
At time of array initialization which is necessary to specify?
Row
Column
Row and Column
None of the above
Java Array can allocate __________.
Dynamic Memory
Static Memory
Both A and B
None of the above
Which of the following is an incorrect array declaration?
int [] arr = new int[5]
int [] arr = new int[5]
int arr[] = new int[5]
int arr[] = int [5] new
Index in array start with ______.
-1
0
1
Infinite
We can calculate the length of an array using ________.
sizeof(array)
array.len
array.length
array.sizeof()
