wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Array String

Total questions: 67

Worksheet time: 43mins

Name
Class
Date
1.

Which of the following is FALSE about arrays on Java

a)

A java array is always an object

b)

Length of array can be changed after creation of array

c)

Arrays in Java are always allocated on heap

2.

Find the output

class Test {

public static void main(String args[]) {

int arr[2];

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

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

}

}

a)

Compile error

b)

Exception

c)

Garbage Value

Garbage Value

d)

0

0

3.

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

}

}

a)

Compile error

b)

Exception

c)

Garbage Value

Garbage Value

d)

0

0

4.

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

}}

a)

Same

b)

Not same

c)

Error

5.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
6.

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

}

}

}

a)

Compiler Error

b)

0

1 2

3 4 5

6 7 8 9

c)

0

0 0

0 0 0

0 0 0 0

d)

9

7 8

4 5 6

0 1 2 3

7.
What does the following segment of code print out? int[] numArray = { 2, 7, 4, 12, 0, 2 };
System.out.println("Number: " + numArray[0]);
a)
Number: 2
b)
Number: 7
c)
Number: 4
d)
Number: 12
8.
What value is at index 3 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
9.
What is the correct way to create an array with these three numbers: 12  8  15
a)
int[] = {12, 8, 15};
b)
int[] nums = {12  8  15};
c)
int[] nums = {12, 8, 15}
d)
int[] nums = {12, 8, 15};
10.

How will you identify a single dimensional array?

a)

single curly brackets

b)

single square brackets

c)

multiple squares brackets

d)

single angled brackets

11.
What is the correct way to create an array with these three numbers: 12  8  15
a)
int[] = {12, 8, 15};
b)
int[] nums = {12  8  15};
c)
int[] nums = {12, 8, 15}
d)
int[] nums = {12, 8, 15};
12.

Which of the following is FALSE about arrays on Java

a)

A java array is always an object

b)

Length of array can be changed after creation of array

c)

Arrays in Java are always allocated on heap

13.

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

}

}

}

a)

10 20 30 40 50

b)

Compiler Error

c)

10 20 30 40

14.

class Test {

public static void main(String args[]) {

int arr[2];

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

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

}

}

a)

0

0

b)

garbage value

garbage value

c)

Compiler Error

d)

Exception

15.

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

}

}

}

a)

Compiler Error

b)

0

1 2

3 4 5

6 7 8 9

c)

0

0 0

0 0 0

0 0 0 0

d)

9

7 8

4 5 6

0 1 2 3

16.

Which of the following is the correct way of importing an entire package ‘pkg’?

a)

import pkg.

b)

Import pkg.

c)

import pkg.*

d)

Import pkg.*

17.

Which of the following is/are advantages of packages?

a)

Packages avoid name clashes

b)

Classes, even though they are visible outside their package, can have fields visible to packages only

c)

We can have hidden classes that are used by the packages, but not visible outside.

d)

All of the above

18.

Which of the following is/are advantages of packages?

a)

Packages avoid name clashes

b)

Classes, even though they are visible outside their package, can have fields visible to packages only

c)

We can have hidden classes that are used by the packages, but not visible outside.

d)

All of the above

19.

Which of these is a mechanism for naming and visibility control of a class and its content?

a)

Object

b)

Packages

c)

Interfaces

d)

None of the above

20.

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?

a)

Public

b)

Protected

c)

No Modifier

d)

All of the mentioned

21.

Arrays in Java are dynamically allocated using the . . . . operator.

a)

create

b)

dynamic

c)

arrayList

d)

new

22.

Which of the following statements correctly declares a two-dimensional integer array?

a)

int Matrix[ ] = new int[5,4];

b)

int Matrix[ ]; Matrix = new int[5,4];

c)

int Matrix[ ][ ] = new int[5][4];

d)

int Matrix[ ][ ] = int[5][4];

23.

int len = "jump it!".length();


What is the value of len?

a)

8

b)

7

c)

9

d)

6

e)

This code does not compile.

24.

int x = "quarantine".indexOf("a");


What is the value of x?

a)

4

b)

3

c)

2

d)

This code does not compile.

e)

StringIndexOutOfBoundsException

25.

String s = "whatever".substring(1,4);


What is the value of s?

a)

"hate"

b)

"hat"

c)

"whate"

d)

"what"

e)

StringIndexOutOfBoundsException

26.

String s = "whatever".substring(1,4);


What is the value of s?

a)

"hate"

b)

"hat"

c)

"whate"

d)

"what"

e)

StringIndexOutOfBoundsException

27.

String s1 = "hello";

String s2 = new String("hello");

if (s1 == s2) System.out.print("Hey");

else System.out.print("Yo");


What is the output?

a)

Hey

b)

Yo

c)

There is no output.

d)

This code will not compile.

28.

int p = "Go Paw Patrol!".indexOf("p");


What is the value of p?

a)

0

b)

4

c)

3

d)

-1

e)

This code will not compile.

29.

String s = "cool cat".substring(6);


What is the value of s?

a)

"cat"

b)

"at"

c)

"cool c"

d)

This code does not compile.

e)

StringIndexOutOfBoundsException

30.

int x = "Mary".compareTo("Molly");

if (x>=0) System.out.print("Yes");

else System.out.print("No");


What is the output?

a)

Yes

b)

No

c)

There is no output.

d)

This code will not compile.

31.

String s1 = "goat";

String s2 = s1.substring(0,s1.length());

boolean z = s1.equals(s2);


What is the value of z?

a)

true

b)

false

c)

This code will not compile.

d)

StringIndexOutOfBoundsException

32.

String s = "good job".substring(3, 9);


What is the value of s?

a)

"d job"

b)

"od job"

c)

"d jo"

d)

"od jo"

e)

StringIndexOutOfBoundsException

33.

int x = "bob".compareTo("Bob");

if (x>=0) System.out.print("Good");

else System.out.print("Bad");


What is the output?

a)

Good

b)

Bad

c)

There is no output.

d)

This code will not compile.

34.

Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?

a)

str.length() -2

b)

str.length() -4

c)

str.length() - 3

d)

str.length() - 1

e)

str.length()

35.

Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?

a)

pos>0

b)

pos>=0

c)

pos<0

d)

pos<=0

e)

pos==0

36.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

37.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

38.

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

a)

double

b)

String

c)

int

d)

number

39.

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

40.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

41.

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

42.

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)

43.

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"

44.

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

45.

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

46.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

47.

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)

48.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

49.

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

50.
String word = "A few good men";
What is word.length()?
a)
14
b)
15
c)
13
51.
String word = "A few good men";
What is word.charAt(7) ?
a)
o
b)
g
c)
7
d)
good men
52.
String word = "A few good men";
What is word.startsWith("A few") ?
a)
true
b)
false
53.
String word = "A few good men";
What is word.endsWith("man") ?
a)
true
b)
false
54.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

55.
String word = "A few good men";
What is word.substring(3, 7) ?
a)
ew g
b)
ew go
c)
few
d)
few[space]
56.

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)

57.

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"

58.

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

59.

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

60.

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

61.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

62.

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)

63.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

64.

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

65.

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

66.

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

67.

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