wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

arrays and strings in java

Total questions: 25

Worksheet time: 19mins

Name
Class
Date
1.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

2.

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);

3.

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)

4.

What will be the output of below code?


String statement = "Outfit of the day";

System.out.println(statement.Substring(3,6));

a)

it of

b)

tfit

c)

fit

d)

FIT

5.

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

6.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

7.

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

8.

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];

9.

Each item in an array has a numbered position.

a)

True

b)

False

10.
What is the difference between an array and a list?
a)
An array holds numbers, whereas a list holds strings
b)
An array can only hold data of the same data type, whereas lists can hold values of different data types
c)
An array holds both numbers and strings, whereas a list only holds numbers
11.

Which of these operators is used to allocate memory to array variable in Java?

a)

malloc

b)

alloc

c)

new

d)

new malloc

12.

Which of these is necessary to specify at time of array initialization?

a)

Row

b)

Column

c)

Both Row and Column

d)

None of the mentioned

13.

Which of the following statements are incorrect?

a)

String is a class

b)

Strings in java are mutable

c)

Every string is an object of class String

d)

Java defines a peer class of String, called StringBuffer, which allows string to be altered

14.
What is the error in the following code fragment?
double[ ] data = new double[20];
data[20] = 15.25;
a)
A cast is required
b)
data not initialized
c)
A two-dimensional array is required
d)
Out-of-bounds error
16.

Which of these class is superclass of String and StringBuffer class?

a)

java.util

b)

java.lang

c)

ArrayList

d)

None of the mentioned

16.

Which of these class is superclass of String and StringBuffer class?

a)

java.util

b)

java.lang

c)

ArrayList

d)

None of the mentioned

17.

How many objects will be created?

String a = new String("Examveda");

String b = new String("Examveda");

String c = "Examveda";

String d = "Examveda";

a)

4

b)

3

c)

2

d)

none of these

18.

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 ]

19.
An array declaration can optionally omit the new specifier and explicit size setting if it contains a comma-delimited _______________ inside a set of _______________ after the assignment operator, which will imply its size.
a)
initialization list, parentheses
b)
parameter list, parentheses
c)
initialization list, curly braces
d)
None of the above
20.
An array uses ____________ indexing to keep track of each memory allocation unit in the array.
a)
one-based
b)
zero-based
c)
10-based
d)
100-based
21.

Which of these is an incorrect statement?

a)

String objects are immutable, they cannot be changed

b)

String object can point to some other reference of String variable

c)

StringBuffer class is used to store string in a buffer for later use

d)

None of the mentioned

22.

Which of the following is NOT a primitive data type?

a)

double

b)

float

c)

boolean

d)

String

23.

What do we use the Scanner Library in java for?

a)

Give permissions to the user

b)

Correcting software errors

c)

Allows user to read data

d)

Allows the user to enter data

24.
What is at the end of a line of code?
a)
#
b)
;
c)
)
d)
>
25.
Index values of an array range from ______________.
a)
0 to length – 1
b)
1 to length
c)
0 to length
d)
0 to length + 1