Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java - arrays and String methods

Total questions: 12

Worksheet time: 9mins

Name
Class
Date
1.

Which of these is a correct way of declaring an array of integers?

a)

int[] arrayA = new int[10];

b)

int arrayB = new int[20];

c)

int arrayC[] = int[30];

d)

int[] arrayD = new int[40]

2.

Suppose you create a typical array of Strings, containing the subjects you will see next year. Which of the following is NOT true.

a)

You may remove one of the subjects (it will be replaced by null).

b)

You may add another subject to the array.

c)

You may replace one subject with another one different.

d)

You may copy the contents of that array into another array with the same size.

3.

Which are Java's primitive data types?

a)

byte, short, int, long, float, double, boolean, char, String

b)

byte, short, int, long, float, double, boolean, char, array

c)

byte, short, int, long, float, double, boolean, char, String, array

d)

byte, short, int, long, float, double, boolean, char

4.

Suppose the variable 'word' stores a String more than 2 letters long.

Suppose the variable 'number' stores an int greater than 4.

What is the correct way to replace 'word' with itself repeated 'number' times?

a)

word = word.repeat(number);

b)

word = word.repeat(number+1);

c)

word += word.repeat(number);

d)

word = word.repeat(number-1);

5.

How to get the first letter of a String saved in the variable 'abc'?

a)

abc.get(1);

b)

abc[1]

c)

abc.substring(0);

d)

abc.substring(0,1);

6.

You want to relabel your company from "9lionz" to "10lionz".

Which of these ways are valid ways to achieve that?

a)

company = "10lionz";

b)

String company = "10lionz";

c)

company.substring(0,1) = "10";

d)

company += "10" - "9";

7.

The code is supposed to return the initials from an array of names... Why is it not working properly?

a)

The return type should be an array of Strings, not int[].

b)

The substring method ca only be applied to Strings, not arrays themselves.

c)

The substring should look the first letter always: .substring(0,1);

d)

The method should return initials.

8.

How to code a conditional that verifies if a string called 'mystery' contains a website ending in ".com"?

a)

if (mystery.substring(-4,-1) == ".com")

b)

if (mystery.substring(-4,-1).equals(".com"))

c)

if (mystery.substring(mystery.length-4) == ".com")

d)

if (mystery.substring(mystery.length-4).equals(".com"))

9.

What is the output of this code inside the main method?

int[] myNums;

System.out.println(myNums[0]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0

10.

What is the output of this code inside the main method?

int[] myNums = new int[3];

System.out.println(myNums[1]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0

11.

What is the output of this code inside the main method?

String[] myWords = new String[5];

System.out.println(myWords[5]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0

12.

What is the output of this code inside the main method?

String[] myWords = new String[5];

System.out.println(myWords[4]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0