Method calling is the process of executing a method by using its name followed by parentheses, optionally passing arguments inside the parentheses.
Click to reveal answer
beginner
How do you call a method named greet with no parameters in Java?
You call it by writing greet(); inside another method or block of code.
Click to reveal answer
beginner
What happens if a method requires parameters but you call it without any?
The Java compiler will show an error because the method call does not match the method's parameter list.
Click to reveal answer
intermediate
Explain the difference between calling a static method and an instance method.
A static method is called using the class name, like <code>ClassName.methodName()</code>. An instance method is called on an object, like <code>objectName.methodName()</code>.
Click to reveal answer
beginner
What is the output of this code?<br><pre>class Test {
static void sayHi() {
System.out.println("Hi!");
}
public static void main(String[] args) {
sayHi();
}
}</pre>
The output is:
Hi!
This happens because the static method sayHi() is called inside main without needing an object.
Click to reveal answer
How do you call a method named calculate that takes two integers as parameters?
Acalculate(5, 10);
Bcalculate();
Ccalculate[5, 10];
Dcall calculate(5, 10);
✗ Incorrect
You call the method by writing its name followed by parentheses with the arguments inside, separated by commas.
Which of these is the correct way to call a static method printMessage in class Utils?
Anew Utils.printMessage();
BUtils.printMessage();
CprintMessage();
DUtils->printMessage();
✗ Incorrect
Static methods are called using the class name followed by a dot and the method name.
What will happen if you call a method with the wrong number of arguments?
AThe compiler shows an error.
BThe program runs normally.
CThe method ignores extra arguments.
DThe method uses default values automatically.
✗ Incorrect
Java requires the number and types of arguments to match the method definition exactly.
Which symbol is used to call a method in Java?
A# (hash)
B-> (arrow)
C:: (double colon)
D. (dot)
✗ Incorrect
The dot operator is used to call methods on objects or classes.
If you have an object car of class Car, how do you call its method startEngine?
AstartEngine(car);
BCar.startEngine();
Ccar.startEngine();
Dcar->startEngine();
✗ Incorrect
Instance methods are called on objects using the dot operator.
Describe how to call a method in Java and explain the difference between calling static and instance methods.
Think about whether you need an object or just the class name.
You got /3 concepts.
What errors can happen if you call a method incorrectly? Give examples.
Consider what the compiler checks when you call a method.
You got /3 concepts.
Practice
(1/5)
1. What does calling a method in Java do? myMethod();
easy
A. Runs the instructions inside the method named myMethod.
B. Creates a new method called myMethod.
C. Deletes the method myMethod from the program.
D. Prints the name myMethod on the screen.
Solution
Step 1: Understand what a method call does
Calling a method means running the code inside that method.
Step 2: Analyze the example myMethod();
This runs the instructions saved inside the method named myMethod.
Final Answer:
Runs the instructions inside the method named myMethod. -> Option A
2. Which of the following is the correct way to call a method named displayMessage in Java?
easy
A. run displayMessage();
B. call displayMessage();
C. displayMessage;
D. displayMessage();
Solution
Step 1: Recall Java method call syntax
In Java, you call a method by writing its name followed by parentheses.
Step 2: Check each option
Only displayMessage(); matches the correct syntax for calling a method.
Final Answer:
displayMessage(); -> Option D
Quick Check:
Method call syntax = name() [OK]
Hint: Method call needs parentheses after name [OK]
Common Mistakes:
Omitting parentheses when calling methods
Adding extra words like 'call' or 'run'
Using semicolon without parentheses
3. What is the output of this Java code?
public class Test {
public static void sayHi() {
System.out.println("Hi there!");
}
public static void main(String[] args) {
sayHi();
}
}
medium
A. sayHi()
B. No output
C. Hi there!
D. Compilation error
Solution
Step 1: Identify the method call in main
The main method calls sayHi(), which prints "Hi there!".
Step 2: Understand what sayHi() does
It prints the text "Hi there!" to the screen.
Final Answer:
Hi there! -> Option C
Quick Check:
Method prints text = output text [OK]
Hint: Method prints text, so output is printed string [OK]
Common Mistakes:
Confusing method name with output
Expecting no output without print statement
Thinking method call causes error
4. Find the error in this Java code snippet:
public class Demo {
public static void greet() {
System.out.println("Hello!");
}
public static void main(String[] args) {
greet;
}
}
medium
A. Incorrect method name.
B. Missing parentheses when calling method greet.
C. Missing semicolon after greet.
D. Method greet is not defined.
Solution
Step 1: Check method call syntax in main
The code uses greet; which is missing parentheses.
Step 2: Understand correct method call
Method calls require parentheses even if no arguments: greet();.
Final Answer:
Missing parentheses when calling method greet. -> Option B
Quick Check:
Method call needs () [OK]
Hint: Always use () to call methods, even if empty [OK]
Common Mistakes:
Forgetting parentheses on method calls
Thinking semicolon alone calls method
Assuming method call without () is valid
5. Given this Java code:
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(5, 3);
System.out.println(result);
}
}
What will be printed when the program runs?
hard
A. 8
B. 53
C. add(5, 3)
D. Compilation error
Solution
Step 1: Understand method add parameters and return
The method add takes two integers and returns their sum.
Step 2: Analyze the call add(5, 3)
It returns 5 + 3 = 8, which is stored in result.
Step 3: Check the output statement
System.out.println(result); prints the value 8.
Final Answer:
8 -> Option A
Quick Check:
Sum of 5 and 3 = 8 [OK]
Hint: Method returns sum; print shows result [OK]
Common Mistakes:
Concatenating numbers as strings instead of adding
Expecting method name printed instead of result
Confusing return value with method call syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.