Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Mastering Java Methods and Strings

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

Which of the following is the correct way to declare a method in Java that does not return any value?

a)

void myMethod() { }

b)

int myMethod() { }

c)

return myMethod() { }

d)

method myMethod() { }

2.

What is the return type of the following method?

a)

void

b)

int

c)

String

d)

boolean

3.

Which keyword is used to send a value back from a method in Java?

a)

send

b)

return

c)

output

d)

give

4.

What is the result of the following code?

a)

4

b)

5

c)

6

d)

Error

5.

Which of the following is a valid method declaration for a method that takes an integer parameter and returns a double?

a)

double myMethod(int x)

b)

int myMethod(double x)

c)

void myMethod(int x)

d)

double myMethod()

6.

What does the following method return?

a)

The sum of a and b

b)

The product of a and b

c)

The difference of a and b

d)

Nothing

7.

Which method call correctly passes two string parameters to a method named combine?

a)

combine("Hello", "World");

b)

combine("Hello" "World");

c)

combine("Hello" + "World");

d)

combine("Hello" " World");

8.

Which of the following statements will convert all characters in a string variable `word` to uppercase?

a)

word.toUpperCase();

b)

word.toLowerCase();

c)

word.upper();

d)

word.capitalize();

9.

What is the output of the following code?

a)

"Ja"

b)

"av"

c)

"va"

d)

"Jav"

10.

Given the method

Which of the following calls will print "Hello"?

a)

printMessage("Hello");

b)

printMessage();

c)

printMessage(Hello);

d)

printMessage("Hello", "World");

11.

Which of the following variable declarations in Java is valid?

a)
float y;
b)
string name;
c)
int x;
d)

boolean is Active;

12.

What will be printed by the following code?

a)

7

b)

12

c)

34

d)

1

13.

Which of the following best describes parameter passing in Java?

a)

All parameters are passed by value.

b)

All parameters are passed by reference.

c)

Primitive types are passed by value, objects are passed by reference.

d)

Primitive types are passed by reference, objects are passed by value.

14.

Given the following method:

What is the value of `y` after this code runs?

a)

5

b)

10

c)

2

d)

0

15.

A student writes the following method but gets a compilation error:

a)

The method should return an int, not void.

b)

The method should not have parameters.

c)

The method should not use return with void.

d)

The method should use System.out.println instead of return.

16.

You are given the following method:

What will `repeat("Hi", 3)` return?

a)

"Hi"

b)

"HiHiHi"

c)

"Hi3"

d)

"3Hi"

17.

A method is supposed to return the first character of a string, but the following code gives an error:

What could cause an error when this method is called?

a)

Passing an empty string as the argument

b)

Passing a string with one character

c)

Passing a string with spaces

d)

Passing a string with numbers

18.

Given the following method: What will `reverse("dog")` return?

a)

"dog"

b)

"god"

c)

"ogd"

d)

"dgo"

19.

A method is intended to check if a string is a palindrome (reads the same forwards and backwards). Which of the following implementations is correct?

a)

return s.equals(reverse(s));

b)

return s == reverse(s);

c)

return s.equalsIgnoreCase(s);

d)

return s.contains(reverse(s));

20.

What type should the method be?

a)

Int

b)

Boolean

c)

No type needed

d)

String

21.

Why do we use methods in Java programming?


a)
  1. Break down our program into smaller parts

b)
  1. Avoid repeating code

c)
  1. Make our program more readable

d)
  1. All of the above

22.

What are parameters in Java?

a)

Parameters are variables that allow methods to receive input values.

b)
Parameters are constants that cannot change during execution.
c)
Parameters are data types used to define variables.
d)

Parameters are methods that return output values.

23.

What is a Javadoc?

a)
Javadoc is a programming language.
b)
Javadoc is a database management system.
c)
Javadoc is a web development framework.
d)

Javadoc is a documentation tool for Java.

24.

How are methods called when the code is ran in Java?

a)
Methods are called using their variable names.
b)
Methods are called by their name followed by parentheses.
c)
Methods are invoked with a dot operator only.
d)
Methods are executed by typing their return type.
25.

Does the return type have to match the type of method in Java?

a)
Yes, the return type must match the type of the method.
b)
Yes, but only for void methods.
c)
The return type can be ignored if not used.
d)
No, the return type can be any type.