WorksheetsMastering Java Methods and Strings
Total questions: 25
Worksheet time: 13mins
Which of the following is the correct way to declare a method in Java that does not return any value?
void myMethod() { }
int myMethod() { }
return myMethod() { }
method myMethod() { }
What is the return type of the following method?
void
int
String
boolean
Which keyword is used to send a value back from a method in Java?
send
return
output
give
What is the result of the following code?
4
5
6
Error
Which of the following is a valid method declaration for a method that takes an integer parameter and returns a double?
double myMethod(int x)
int myMethod(double x)
void myMethod(int x)
double myMethod()
What does the following method return?
The sum of a and b
The product of a and b
The difference of a and b
Nothing
Which method call correctly passes two string parameters to a method named combine?
combine("Hello", "World");
combine("Hello" "World");
combine("Hello" + "World");
combine("Hello" " World");
Which of the following statements will convert all characters in a string variable `word` to uppercase?
word.toUpperCase();
word.toLowerCase();
word.upper();
word.capitalize();
What is the output of the following code?
"Ja"
"av"
"va"
"Jav"
Given the method
Which of the following calls will print "Hello"?
printMessage("Hello");
printMessage();
printMessage(Hello);
printMessage("Hello", "World");
Which of the following variable declarations in Java is valid?
boolean is Active;
What will be printed by the following code?
7
12
34
1
Which of the following best describes parameter passing in Java?
All parameters are passed by value.
All parameters are passed by reference.
Primitive types are passed by value, objects are passed by reference.
Primitive types are passed by reference, objects are passed by value.
Given the following method:
What is the value of `y` after this code runs?
5
10
2
0
A student writes the following method but gets a compilation error:
The method should return an int, not void.
The method should not have parameters.
The method should not use return with void.
The method should use System.out.println instead of return.
You are given the following method:
What will `repeat("Hi", 3)` return?
"Hi"
"HiHiHi"
"Hi3"
"3Hi"
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?
Passing an empty string as the argument
Passing a string with one character
Passing a string with spaces
Passing a string with numbers
Given the following method: What will `reverse("dog")` return?
"dog"
"god"
"ogd"
"dgo"
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?
return s.equals(reverse(s));
return s == reverse(s);
return s.equalsIgnoreCase(s);
return s.contains(reverse(s));
What type should the method be?
Int
Boolean
No type needed
String
Why do we use methods in Java programming?
Break down our program into smaller parts
Avoid repeating code
Make our program more readable
All of the above
What are parameters in Java?
Parameters are variables that allow methods to receive input values.
Parameters are methods that return output values.
What is a Javadoc?
Javadoc is a documentation tool for Java.
How are methods called when the code is ran in Java?
Does the return type have to match the type of method in Java?
