Topics Index

Class with multiple static methods

If you need explanation Read this topic If you need explanation Read this topic

If you need Answer Take test on this topic

Class with multiple static methods
What will be the output of the following program?
public class PowerExample {
    static int i, j, k;
    final int result1 = 0;
    final double result = 1;
    public static void main(String[] args) {
        i = 1;
        j = 2;
        k = 3;
        int val = getResult(i, j, k);
        System.out.println("final result is " + val);

    private static int getResult(int i, int j, int k) {
        int result = getDiv(i, j, k);
        return result;
    private static int getDiv(int i, int j, int k) {
        int result = (getpow(i, 2, result) + getpow(j, 3, result)) / k;
        return result;
    private static int getpow(int i, int j, double result) {
        result = 1;
        for (int k = 0; k < j; k++) {
            result *= i;
        return (int) result;

A. final result is 3
B. final result is 2
C. final result is 0
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Multiple Methods In One Class Static Keyword In Java

If you need explanation Read this topic 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 E compilation error error: variable result might not have been initialized...since the method is declared as private result ie private static int  getDiv(int i,int j,int k) here in the above result value is initializedbut it is only for public

Posted by Ramya Reddy Addulla    2014-10-27 12:14:36

Ans:E. This code shows compilation error,because intially the values of i,j and k are initialized to
1,2,3 and then the
getResult(i,j,k) is called.(i=1.j=2.k=3)
Now in this method it again calls
getDiv(i,j,k) method with same values.
Now in this method it got another method
getpow(i,2,result) here the value of i=2 will be assigned but as the result value will be null as the result field is
not initialized and it not got any value from the function getDiv(i,j,k) till now.

Posted by Varun Kumar    2014-10-27 12:18:37


Posted by Sudhakar Dhs    2014-10-27 12:37:20


Posted by Sudhakar Dhs    2014-10-27 12:37:33

Ans is  we have 3 static varibles...

static varibles wil create only once...and these are common to all Objects...we can access these variables using the class name..

we have result1 which is final variable so that we cant change the value of this variable throughout the program...sam for result also

in main we are assigning i=1,j=2,k=3..
here we have a local variable named with val which is ready to take the return value of getResult()

in getResult() we are calling method getDiv() by passing the values which it is received...

in getDiv()

we have local variable named with result didnt get java local variables didnt Have any default values...if you want use u must intilize it...

here we are calling getPow() by passing the i,2,result...

here we have 2 variables named with result 1--instance variable

here compiler wil give preference to the local variable but here local variable didnt get that we cant use that compiler wil raise an error Saying result may not have been initilized....

Posted by Uday Kumar    2014-10-27 15:47:14


Posted by Shubham Bansal    2014-10-27 18:10:41

This dose is now closed and the winners are Varun Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-10-28 04:04:03

© meritcampus 2019

All Rights Reserved.

Open In App