1.
In the following method declaration, what is the return type?
public static int myMethod(int count, double value) {
return 4;
}
Correct Answer
A. Int
Explanation
The return type of a method is the word given just before the name of the method.
2.
In the following method declaration, what is the name of the method?
public static void showMenu(String category) {
}
Correct Answer
D. ShowMenu
Explanation
The name of the method is always the word just before the "(".
3.
What is the output of this main() method? Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)
public static void main(String [] args) {
System.out.print("A");
processRequest();
System.out.print("B");
System.out.print("C");
showMenu();
System.out.print("D");
}
public static void showMenu() {
System.out.print("Q");
}
public static void eraseFile() {
System.out.print("T");
}
public static void processRequest() {
System.out.print("2");
}
Correct Answer
A. A2BCQD
Explanation
The main method first prints "A", then calls the processRequest method which prints "2". After that, it prints "B" and "C" in order. Then, it calls the showMenu method which prints "Q". Finally, it prints "D". Therefore, the output of the main method is "A2BCQD".
4.
Write a method declaration (just the first line but include the {) for the following description. The wording must be exact and correct!The method must be called calculateFinalExam. The method must return a double. The method accepts 3 parameters:1. the studentId which must be an integer2. the studentName which must be a String3. the testScore which must be a double.
Correct Answer
A. Public static double calculateFinalExam(int studentId, String studentName, double testScore) {
Explanation
The correct answer is "public static double calculateFinalExam(int studentId, String studentName, double testScore) {". This is the correct method declaration because it follows all the requirements given in the description. The method is named calculateFinalExam, it returns a double, and it accepts three parameters: an integer for studentId, a String for studentName, and a double for testScore.
5.
What is the output of this main() method? Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)
public static void main(String [] args) {
System.out.print("A");
processRequest();
processRequest();
showMenu();
eraseFile();
System.out.print("B");
}
public static void showMenu() {
System.out.print("Q");
}
public static void eraseFile() {
System.out.print("T");
}
public static void processRequest() {
System.out.print("2");
}
Correct Answer
D. A22QTB
Explanation
The output of the main() method is "A22QTB". This is because the main() method first prints "A", then calls the processRequest() method twice, which prints "2" twice. Then, the main() method calls the showMenu() method, which prints "Q". After that, the main() method calls the eraseFile() method, which prints "T". Finally, the main() method prints "B".
6.
What is the correct way to define a method in Java that calculates the square of an integer and returns the result?
Correct Answer
A. Int square(int num) { return num * num; }
Explanation
Option A is the correct syntax for defining a method in Java. The method square takes an integer parameter num and returns its square. The return type int indicates that the method returns an integer value. Options B and C use syntaxes not valid in Java, and option D incorrectly uses void, which signifies no return value, yet the method is supposed to return an integer.
7.
What is the output of this main() method? Put the letters into the answer box provided with no spaces between (i.e. ABCEDFG)
public static void main(String [] args) {
int value;
System.out.print("@");
v = getRemoteId();
System.out.print(v);
System.out.print("E");
v = getStudentCount();
System.out.print(v);
System.out.print("F");
v = getMonitorCount();
System.out.print("W");
}
public static int getRemoteId() {
return 3;
}
public static int getMonitorCount() {
return 2;
}
public static int getStudentCount() {
return 6;
}
Correct Answer
C. @3E6FW
Explanation
The main() method starts by printing "@" and then calls the getRemoteId() method, which returns the value 3. The value 3 is then printed. Next, the method prints "E" and calls the getStudentCount() method, which returns the value 6. The value 6 is printed. Finally, the method prints "F" and calls the getMonitorCount() method, which returns the value 2. However, the value is not printed as there is a missing print statement. Therefore, the correct output is "@3E6FW".
8.
What is the output of this main() method? Put the letters into the answer box provided with no spaces between (i.e. ABCEDFG)
public static void main(String [] args) {
showMenu();
eraseFile();
System.out.print("B");
showMenu();
}
public static void showMenu() {
System.out.print("Q");
eraseFile();
}
public static void eraseFile() {
System.out.print("T");
}
public static void processRequest() {
System.out.print("2");
}
Correct Answer
D. QTTBQT
Explanation
The output of the main() method is "QTTBQT".
The showMenu() method is called twice in the main() method. Inside the showMenu() method, "Q" is printed and then the eraseFile() method is called, which prints "T". This sequence is repeated twice, resulting in "QTT" being printed. Then, "B" is printed from the main() method. Finally, the showMenu() method is called again, printing "Q" and then the eraseFile() method is called, printing "T". So, the overall output is "QTTBQT".
9.
What is the output of this main() method? Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)
public static void main(String [] args) {
int value;
int x;
x = 3;
System.out.print("A");
value = checkLength(x);
System.out.print("B");
System.out.print(x);
System.out.print(value);
}
public static void loadSong(int id) {
System.out.print(id);
}
public static int checkLength(int id) {
System.out.print(id);
id = id + 4;
System.out.print(id);
return 9;
}
public static int getStudentCount() {
return 6;
}
Correct Answer
A. A37B39
Explanation
The output of the main() method is "A37B39". The program first prints "A" and then calls the checkLength() method with the value of x (which is 3) as an argument. Inside the checkLength() method, the value of id is printed (3), then it is incremented by 4 (resulting in 7), and then it is printed again (7). The checkLength() method returns 9. Back in the main() method, "B" is printed, followed by the value of x (3), and then the value returned by the checkLength() method (9). Therefore, the final output is "A37B39".
10.
In the following method declaration, how many formal parameters are there?public static double squareRoot(double value) { return 2.3;}
Correct Answer
B. 1
Explanation
The parameter list is all the things between the "(" and the ")". You just need to count how many items are in the list... each one will have a comma between them.
11.
In the following method, how many values are returned?
public static void syncPhone(int idNumber) {
}
Correct Answer
A. 0
Explanation
A method can return only one type! This time the return type is void, so there is nothing returned from this method at all.
12.
The following code in the main is valid:public static void main(String [] args) { int value; value = createGameCharacter(); // rest of program}public static void createGameCharacter() {}
Correct Answer
B. False
Explanation
The return type of createGameCharacter() is void. You cannot store the return into the variable value.
13.
True or False. The call to the method installApplication() is valid:public static void main(String [] args) { installApplication("Angry Birds", "version 1.2");}public static void installApplication(String appName, int appVersion) { // rest of method not important}
Correct Answer
B. False
Explanation
The second parameter called appVersion is an integer. In the main method we are trying to call with the method with a string as the second parameter.
14.
The following code in the main is valid:public static void main(String [] args) { int value; value = depositMoney(amount); // rest of program}public static int depositMoney() { return 4;}
Correct Answer
B. False
Explanation
The depositMoney() method declaration has no formal parameter listed, therefore we cannot call the depositMoney() method and pass in a parameter.
15.
What is the value that is returned in the following method?public static int getVolume() { int v; v = 23; if (v < 50) { return 50; } return v;}
Correct Answer
A. 50
Explanation
The if statement is true so the first return takes place. Once you call return the method is finished and will never perform anything else.