Topics Index

Creating Method

If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Method
What will be the output of the following program?
public class FindOutput
    public static void main(String args[])
        int a = 5;
    int cube(int theNum)
        return theNum * theNum * theNum;

A. 125
B. Compilation Error because cube is already defined in the java.lang.Math class.
C. Throws an arithmetic exception.
D. Compilation Error or Runtime Error for some other reason.
Topic: Java Methods - Parameter Passing And Scope

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is D.
In the program the method cube() is a non-static method which cannot be accessed directly in the static context... If we want to call a non-static method then we have to create an object of the class and on that class we need to call that method.. If we call a non-static method from the static field directly then it will raise a compile-time error...

Posted by Gopinath Manchikanti    2015-03-09 16:02:33

ANS is D

--Here,the cube() method is defined in class which is non-static and main is static.

--After that a is assigned 5.inside cube() method cube of a number is found out and returned to main method().

--But the rule says,static method can access only static methods and variables only.

Here cube is non-static method and it is called in main().so it will throw compilation error.

Posted by Mânïshå Mùlchåndânï    2015-03-09 16:17:41

This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Switguy Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-03-10 03:51:39

© meritcampus 2019

All Rights Reserved.

Open In App