1. Which of the following is the correct syntax for a method header with paramet
ID: 3559771 • Letter: 1
Question
1. Which of the following is the correct syntax for a method header with parameters?
A. public static void example(x, y) {
B. public static void example(x, y) {
C. public static void example(int x, int y) {
2. What output is produced by the following program?
A. 10 25
B. 25 10
C. 10 15
D. 15 10
3. What is the output of this program?
public class MysteryReturn {
public static void main(String[] args) {
int x = 1;
int y = 2;
int z = 3;
z = mystery(x, z, y);
}
public static int mystery(int z, int x, int y) {
z--;
x = 2 * y + z;
y = x - 1;
System.out.println(y + " " + z);
return x;
}
}
A. 3 1
B. 5 1
C. 5 0
D. 3 0
4. Write a method called min that takes three integers as parameters and returns the smallest of the three values, such that a call ofmin(3, -2, 7) would return -2, and a call of min(19, 27, 6) would return 6. Use Math.min to write your solution.
5. Assuming that the following variables have been declared,
// index 0123456789012345
String str1 = "Frodo Baggins";
String str2 = "Gandalf the GRAY";
Evaluate the following expression. Make sure to give a value of the appropriate type (such as including quotes around a String or apostrophes around a char).
str1.indexOf("o")
A. 4
B. 2 4
C. error
D. 2
6. Assuming that the following variables have been declared,
// index 0123456789012345
String str1 = "Frodo Baggins";
String str2 = "Gandalf the GRAY";
Evaluate the following expression. Make sure to give a value of the appropriate type (such as including quotes around a String or apostrophes around a char).
str2.replace("gray", "white")
A. "Gandalf the GRAY"
B. "Gandalf the gray"
C. "gray"
Explanation / Answer
1. Which of the following is the correct syntax for a method header with parameters?
C. public static void example(int x, int y) {
2. What output is produced by the following program?
A. 10 25
3. What is the output of this program?
public class MysteryReturn {
public static void main(String[] args) {
int x = 1;
int y = 2;
int z = 3;
z = mystery(x, z, y);//mystery is called with (1,3,2)
}
public static int mystery(int z, int x, int y) {//here z=1,x=3,y=2
z--; //now z=0
x = 2 * y + z;//now x=2*2+0=4
y = x - 1;//y=4-1=3
System.out.println(y + " " + z);//prints 3 0
return x;
}
}
so the answer is
D. 3 0
4. Write a method called min that takes three integers as parameters and returns the smallest of the three values, such that a call ofmin(3, -2, 7) would return -2, and a call of min(19, 27, 6) would return 6. Use Math.min to write your solution.
public static int ofmin(int z, int x, int y) {
int smallest = Math.min(a, Math.min(b, c));
return smallest;
}
5. Assuming that the following variables have been declared,
The java.lang.String.indexOf(String str) method returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that: this.startsWith(str, k) is true.
D. 2
6. Assuming that the following variables have been declared,
A. "Gandalf the GRAY" (because in str2 gray is in BLOCK letter so no replacement occure )
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.