Question
What will the following Java code snippet output when
executed, which uses a simple constructor and method overloading ? class Calculator { Â Â Â int add ( int a, int b) { Â Â Â Â Â Â Â return a + b; Â Â Â } Â Â Â double add ( double a, double b) { Â Â Â Â Â Â Â return a + b; Â Â Â } Â Â Â public static void main (String[] args) { Â Â Â Â Â Â Â Calculator calc = new Calculator (); Â Â Â Â Â Â Â System.out.println(calc.add( 5 , 10 ));Â Â Â Â Â // Line 1 Â Â Â Â Â Â Â System.out.println(calc.add( 5.5 , 10.5 ));Â // Line 2 Â Â Â }}Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂSolution
In this example, the Calculator class defines two add() methods with the same name but different parameter types: one for integers and another for doubles. This is an example of method overloading . Let's examine why A is the correct answer and the other options are incorrect:
- Explanation of Correct Option (A):
- The method add(int a, int b) accepts integers and returns an integer result. When calc.add(5, 10)
A single-line comment starts withÂ
Which CPU scheduling algorithm gives preference to processes with shorter CPU burst times?
Which of the following is NOT a primary function of an operating system?
Consider the following statement regarding DIAC.
Statement (1): DIAC is unidirectional device.
Statement (2): DIAC can be used for trigger...
A queue initially empty undergoes the following sequence of operations:
1. enqueue(10)
2. enqueue(20)
3. dequeue()
4.�...
In Kubernetes, which storage type remains available even if the pod is deleted and recreated on a different node?
Router is responsible for packet forwarding, including routing. Router works at which layer?
What is a common disadvantage of greedy algorithms?
Which SQL command is primarily used to retrieve records from a database table?
What is a key characteristic of a "full binary tree"?