0
0
Javaprogramming~15 mins

Method calling in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is method calling in Java?
Method calling is the process of executing a method by using its name followed by parentheses, optionally passing arguments inside the parentheses.
touch_appClick 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.
touch_appClick 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.
touch_appClick 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>.
touch_appClick 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.
touch_appClick 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);
Which of these is the correct way to call a static method printMessage in class Utils?
Anew Utils.printMessage();
BUtils.printMessage();
CprintMessage();
DUtils->printMessage();
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.
Which symbol is used to call a method in Java?
A# (hash)
B-> (arrow)
C:: (double colon)
D. (dot)
If you have an object car of class Car, how do you call its method startEngine?
AstartEngine(car);
BCar.startEngine();
Ccar.startEngine();
Dcar->startEngine();
Describe how to call a method in Java and explain the difference between calling static and instance methods.
What errors can happen if you call a method incorrectly? Give examples.