wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Array

Total questions: 23

Worksheet time: 12mins

Name
Class
Date
1.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

2.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

3.

Which data type gets returned from length() method in String class?

a)

double

b)

String

c)

int

d)

number

4.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


What is the correct way to find the length of "txt" string?

a)

int len = length(txt);

b)

float len = txt.length();

c)

int len = txt.length();

d)

double len = length(txt);

5.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

6.

String txt = "Hello World";

System.out.println(txt.toUpperCase());


Choose the correct output.

a)

"HELLO WORLD"

b)

HELLO WORLD

c)

Hello world

d)

Hello World

7.

Choose correct purpose of indexOf() in String class.

a)

returns the index (the position) of the last occurrence of a specified text in a string

b)

returns the character of the first occurrence of a specified character in a string

c)

returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)

d)

returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)

8.

String x = "10";

String y = "20";

String z = x + y;

System.out.println(z);

Guess the output.

a)

1020

b)

10 20

c)

30

d)

"10 20"

9.

Choose most appropriate purpose of trim() method in String?

a)

to remove white spaces

b)

to get a substring of the string

c)

to cut String at desired index

d)

to remove extra white spaces from start and end of String

10.

What is the most appropriate way to check if two Strings are identical?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

11.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

12.

int[] myArray = {11, 22, 33, 44, 55};

which code is correct to print length of above int-array?

a)

System.out.println(int[].myArray.length());

b)

System.out.println(myArray.length);

c)

System.out.println(myArray.length());

d)

System.out.println(int[].myArray.size());

13.

String[] friends = {"John", "Mike", "Samantha", "Monica"};

which code is correct to print "Samantha" using array functionality?

a)

System.out.println(friends[2]);

b)

System.out.println(friends[3]);

c)

System.out.println(friends["Samantha"]);

d)

System.out.println(friends{2});

14.

String[] friends = {"John", "Mike", "Samantha", "Monica"};

which code is correct to print "Samantha" using array functionality?

a)

System.out.println(friends[2]);

b)

System.out.println(friends[3]);

c)

System.out.println(friends["Samantha"]);

d)

System.out.println(friends{2});

15.

Select correct code to create an array of "double" datatype of length 5.

a)

double arr = double[5];

b)

double{} arr = new double{5};

c)

double[5] arr = new double[];

d)

double[] arr = new double[5];

16.

What is the correct relation between length and last-index of array?

a)

last-index is always 1 more than the length

b)

last-index is 1 less than the length (but not always)

c)

last-index is always 1 less than the length

d)

last-index is always equal to the length

17.

Java code to create a non-element array of student marks with the size of 20.

a)

double mark;

b)

double [20] mark = new double;

c)

double [ ] mark = new mark [20];

d)

double [ ] mark = new double [20];

18.

Java code to create a non-element array of student name with size 15.

a)

String [ ] name = new String;

b)

String [ ] name = new String [15];

c)

name = new String [15];

d)

String [15] name = new String [ ];

19.

Java code to create an array of student id with element 115331, 450055, 555641, 875560 and 987555.

a)

int [ ] id = new {115331, 450055, 555641, 875560, 987555};

b)

int [5] id = {115331, 450055, 555641, 875560, 987555};

c)

int [ ] id = {115331, 450055, 555641, 875560, 987555};

d)

int [ ] id = new int [5];

20.

Java code to display the size of an array name studentname[ ].

a)

System.out.println("The size is " + studentname[ index ]);

b)

System.out.println("The size is " + studentname.length);

c)

System.out.println("The size is " + length.length);

d)

System.out.println("The size is " + studentname);

21.

Java code to read elements into array studentname[ ].

a)
b)
c)
d)
22.

Based on the code segment below:- identify the name of the array.


for(int j=0; j<balance.length; j++)

{

System.out.print("Enter amount: ");

.......

}

a)

No answer.

b)

temp[ ]

c)

balance

d)

balance [ i ]

23.

Based on the code segment below:- identify the name of the array.


for(int j=0; j<balance.length; j++)

{

System.out.print("Enter amount: ");

.......

}

a)

No answer.

b)

temp[ ]

c)

balance

d)

balance [ i ]