wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Programming Methods Worksheet

Total questions: 60

Worksheet time: 30mins

Name
Class
Date
1.

What is the result of "Hello".length()?

a)

4

b)

5

c)

6

d)

0

2.

Which of the following creates a string in Java?

a)

String str = "Hello";

b)

String str = new String("Hello");

c)

Both A and B

d)

None of the above

3.

What will be the output of System.out.println("abc" + "def");?

a)

A) abcdef

b)

B) abc def

c)

C) ab cdef

d)

D) Compilation Error

4.

Which method checks if a string starts with a specific prefix?

a)

startsWith()

b)

endsWith()

c)

includes()

d)

indexOf()

5.

What will System.out.println("Java".charAt(0)); print?

a)

J

b)

a

c)

Java

d)

0

6.

Which of the following is a method to convert a string to lowercase?

a)

toLowerCase()

b)

lowerCase()

c)

makeLower()

d)

downcase()

7.

How do you compare two strings in Java?

a)

==

b)

equals()

c)

compareTo()

8.

What does str.trim() do?

a)

Removes all characters

b)

Removes leading and trailing whitespace

c)

Converts the string to lowercase

d)

Splits the string into an array

9.

Which of the following will return a substring from a string?

a)

str.sub()

b)

str.substring()

c)

str.slice()

d)

str.split()

10.

What will be the output of "hello".concat(" world")?

a)

hello world

b)

hello

c)

world

d)

Compilation Error

11.

Which method is used to replace a character in a string?

a)

replace()

b)

change()

c)

modify()

d)

update()

12.

What will be the result of "Java".compareTo("Java")?

a)

1

b)

-1

c)

0

d)

A compilation error

13.

Which of the following will check if a string contains a specific sequence of characters?

a)

find()

b)

includes()

c)

contains()

d)

has()

14.

What will be the output of "abc".substring(1, 3)?

a)

ab

b)

bc

15.

Which method converts a string into an array of characters?

a)

toArray()

b)

split()

c)

toCharArray()

d)

charArray()

16.

Which method would you use to get the index of a character in a string?

a)

findIndex()

b)

indexOf()

c)

getIndex()

d)

charAtIndex()

17.

What is the result of "hello".equalsIgnoreCase("Hello")?

a)

true

b)

false

c)

0

d)

Compilation error

18.

How can you check if a string is empty?

a)

str.isEmpty()

b)

str.length() == 0

c)

Both A and B

d)

str.equals("")

19.

What does the StringBuilder class do?

a)

It creates immutable strings.

b)

It creates mutable strings.

c)

It converts strings to numbers.

d)

It is not a class in Java.

20.

Which method would you use to split a string into an array based on a delimiter?

a)

split()

b)

divide()

c)

slice()

d)

substring()

21.

21. The output of "abc".repeat(3) will be:

a)

abcabcabc

b)

abcabc

c)

abc3

d)

abc abc abc

22.

Which of the following will convert a string to a number?

a)

parseInt()

b)

valueOf()

c)

toNumber()

d)

Both A and B

23.

What does the StringBuilder method append() do?

a)

Adds a new string to the beginning

b)

Replaces a string

c)

Adds a new string to the end

d)

Deletes a string

24.

Which of the following methods can be used to compare two strings lexicographically?

a)

compareTo()

b)

equals()

c)

==

25.

What is the output of "Hello World".indexOf('W')?

a)

A) 0

b)

B) 5

c)

C) 6

d)

D) -1

26.

Which method is used to replace all occurrences of a character in a string?

a)

replaceAll()

b)

replaceChar()

c)

changeAll()

d)

substitute()

27.

How do you convert a string to a character array?

a)

str.toArray()

b)

str.charArray()

c)

str.toCharArray()

d)

str.getCharArray()

28.

What is the result of "abcdef".substring(2)?

a)

abc

b)

cd

c)

cdef

d)

bcdef

29.

What is the output of "Hello".replace('e', 'a')?

a)

Hallo

b)

Hello

c)

Halla

d)

Hella

30.

Which method would you use to convert a string to uppercase?

a)

toUpperCase()

b)

makeUpper()

c)

uppercase()

d)

capitalize()

31.

What will be the result of "a" + "b" + "c"?

a)

abc

b)

a b c

c)

ab c

d)

Compilation Error

32.

What will be the output of "abc".lastIndexOf('a')?

a)

0

b)

1

c)

2

d)

-1

33.

Which of the following creates a new string object?

a)

String s = "Hello";

b)

String s = new String("Hello");

c)

Both A and B

d)

None of the above

34.

Which method will return a new string that is a substring of this string?

a)

split()

b)

substring()

c)

slice()

d)

getString()

35.

What will be the output of System.out.println("Java".toUpperCase());?

a)

java

b)

Java

c)

JAVA

d)

Compilation Error

36.

Which method removes all white spaces from a string?

a)

trim()

b)

strip()

c)

removeSpaces()

d)

deleteWhitespace()

37.

What will be the output of "apple".compareTo("Apple")?

a)

Positive number

b)

Negative number

c)

0

d)

Compilation error

38.

What is the purpose of String.format()?

a)

To format strings

b)

To concatenate strings

c)

To convert strings to numbers

d)

To compare strings

39.

Which of the following methods checks if two strings are equal, ignoring case differences?

a)

equalsIgnoreCase()

b)

equals()

c)

compareTo()

d)

compareToIgnoreCase()

40.

What does the StringBuilder method insert() do?

a)

Inserts a string at the specified index

b)

Deletes a string

c)

Replaces a substring

d)

Appends a string to the end

41.

What is the output of "abc".concat("def")?

a)

abcdef

b)

abc def

c)

ab cdef

d)

Compilation Error

42.

Which of the following methods can split a string into multiple strings?

a)

substring()

b)

split()

c)

charAt()

d)

indexOf()

43.

What does the method String.valueOf() do?

a)

Converts a string to an integer

b)

Converts an object to a string representation

44.

How would you check if a string contains only digits?

a)

isDigit()

b)

matches("[0-9]+")

c)

isNumeric()

d)

isDigitsOnly()

45.

What will be the output of "Hello".indexOf('l')?

a)

A) 1

b)

B) 2

c)

C) 3

d)

D) -1

46.

Which of the following will give you the last character of a string?

a)

str.charAt(str.length() - 1)

b)

str.lastChar()

c)

str.charAtLast()

d)

str.length() - 1

47.

What does str.split(",") do?

a)

Joins strings

b)

Splits a string by commas

c)

Replaces commas with spaces

d)

None of the above

48.

Which method can be used to check if a string is not empty?

a)

!str.isEmpty()

b)

str.length() > 0

c)

Both A and B

d)

str.notEmpty()

49.

What is the output of System.out.println("Java".substring(0, 2));?

a)

Ja

b)

av

c)

Java

d)

Jav

50.

Which of the following creates a new string object in the heap?

a)

A) String str = "Hello";

b)

B) String str = new String("Hello");

c)

C) Both A and B

d)

D) None of the above

51.

What does the String.isBlank() method do?

a)

Checks if the string is empty

b)

Checks if the string contains only whitespace

c)

Both A and B

d)

None of the above

52.

What is the output of " ".isBlank()?

a)

true

b)

false

c)

null

d)

Compilation Error

53.

Which method can be used to remove leading and trailing whitespace from a string?

a)

trim()

b)

strip()

54.

What is the primary purpose of StringBuffer in Java?

a)

To create immutable strings

b)

To create mutable strings

c)

To store string literals

d)

To concatenate strings

55.

Which method in String can be used to get a stream of lines from a string?

a)

lines()

b)

getLines()

c)

splitLines()

d)

streamLines()

56.

What will be the output of "Hello\nWorld".lines().count()?

a)

A) 1

b)

B) 2

c)

C) 3

d)

D) Compilation Error

57.

What does it mean for a string to be immutable in Java?

a)

The string can be changed after it is created

b)

The string cannot be changed after it is created

c)

The string can only be modified using StringBuffer

d)

None of the above

58.

Which of the following statements about immutable strings is true?

a)

Strings are mutable and can be modified.

b)

Modifying a string creates a new object.

c)

Immutable strings are more memory-efficient.

d)

Both B and C

59.

Which of the following methods can be used on an immutable string?

a)

append()

b)

insert()

c)

substring()

d)

set()

60.

Fill in the blank: The substring() method can be used to create a new string from an ________ string without changing the original.

a)

immutable

b)

mutable

c)

temporary

d)

dynamic