Java Methods Quiz Questions And Answers

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Raphord
R
Raphord
Community Contributor
Quizzes Created: 2 | Total Attempts: 12,294
| Attempts: 7,255 | Questions: 15
Please wait...
Question 1 / 15
0 %
0/100
Score 0/100
1. What is the correct way to define a method in Java that calculates the square of an integer and returns the result?

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.

Submit
Please wait...
About This Quiz
Java Methods Quiz Questions And Answers - Quiz

Can you pass this test that has basic Java methods quiz questions and answers? Try your luck, buddy! In order to learn how to use and write methods... see morein Java, we must first start by knowing some of the words. This quiz provides you with a different way of learning the terms so that you can read the questions that you will have on the test and final exam. So, let's find out how much you know!
see less

2. In the following method declaration, what is the name of the method? public static void showMenu(String category) { }

Explanation

The name of the method is always the word just before the "(".

Submit
3. In the following method declaration, what is the return type? public static int myMethod(int count, double value) {     return 4; }

Explanation

The return type of a method is the word given just before the name of the method.

Submit
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 integer
2. the studentName which must be a String
3. the testScore which must be a double.

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.

Submit
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();      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"); }

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".

Submit
6. In the following method declaration, how many formal parameters are there?

public static double squareRoot(double value) {
  return 2.3;
}

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.

Submit
7. 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;
}

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.

Submit
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"); }

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".

Submit
9. 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; }

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".

Submit
10. In the following method, how many values are returned? public static void syncPhone(int idNumber) { }

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.

Submit
11. 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
}

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.

Submit
12. 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; }

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".

Submit
13. 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;
}

Explanation

The depositMoney() method declaration has no formal parameter listed, therefore we cannot call the depositMoney() method and pass in a parameter.

Submit
14. 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() {
}

Explanation

The return type of createGameCharacter() is void. You cannot store the return into the variable value.

Submit
15. 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"); }

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".

Submit
View My Results

Quiz Review Timeline (Updated): Jul 5, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jul 05, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 12, 2011
    Quiz Created by
    Raphord
Cancel
  • All
    All (15)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the correct way to define a method in Java that calculates the...
In the following method declaration, what is the name of the method? ...
In the following method declaration, what is the return type? ...
Write a method declaration (just the first line but include the {) for...
What is the output of this main() method?  Put the letters into...
In the following method declaration, how many formal parameters are...
What is the value that is returned in the following method?public...
What is the output of this main() method?  Put the letters into...
What is the output of this main() method?  Put the letters into...
In the following method, how many values are returned? ...
True or False. The call to the method installApplication() is...
What is the output of this main() method?  Put the letters into...
The following code in the main is valid:public static void main(String...
The following code in the main is valid:public static void main(String...
What is the output of this main() method?  Put the letters into...
Alert!

Advertisement