Find Output of Java program - 1 (Mixed topics)

Find Output of Java program – 1: This section contains programs/code snippets on java and with the outputs and explanations; you have to find the output and match with the correct output.
Submitted by Amit Shukla, on July 31, 2017

1. Find the output of following java program.

class MainClass
{
	public static void main(String arg[])
	{
		int arr[][]={{4,3},{2,1}};
		int i,j;
		for(i=1;i>-1;i--)
		{
			for(j=1;j>-1;j--)
			{
				System.out.print(arr[i][j]);
			}
		}
	}
}

Output

1234

Explanation

The above java program can be solved in following steps:

Step 1 –
A 2 dimensional array is initialized

Step 2 –
Nested for loop is used to print values of array.

Step 3 –
Print function is used to print the value, first value is arr[1][1]; as we can see that in this 2 dimensional array value of arr[1]={ 2,1 }; that’s why value of arr[1][1]=1;
Similarly values of arr[1][0]=2; arr[0][1]=3; and value of arr[0][0]=4;

Hence the resultant output of this program will be "1234".



2. Find the output of following java program.

class access
{
	static int x;
	void increament()
	{
		++x ;
	}
}
class MainClass
{
	public static void main (String arg[])
	{
		access obj1 = new access();
		access obj2 = new access();
		obj1.x = 0;
		obj1.increament();
		obj2.increament();
		System.out.print(obj1.x + obj2.x);
		System.out.println();
	}
}

Output

4

Explanation

We can solve this problem in following steps:

Step 1 –
Object 1 and object 2 of class access is initialized.

Step 2 –
Value of x is initialized as 0 by calling it using object 1.

Step 3 –
Function increament() of class access is used by object 1. After this the value of static variable x is increased to 1.

Step 4 –
Function increament() of class access is used by object 2. After this the value of static variable x is increased to 2.

Step 5 –
Print function is used to print the sum of variable x accessed by object 1 and object 2.

As we know that variable x is a static variable hence its value for both object 1 and object 2 is same. Hence the values of x for both object 1 and object 2 is 2 so the output is 2+2=4.


3. Find the output of following java program.

class MainClass
{
	public static void main (String arg[])
	{
		System.out.print('h' + 'i');
		System.out.println();
	}
}

Output

209

Explanation

In this program the output is sum of ASCII values of h and i. The ASCII value of h is 104 and ASCII value of i is 105. The addition of 104 and 105 is 209. Hence the output is 209. This happens because in print function if we use arithmetic operations between the characters then print function performs the arithmetic operations between the ASCII values of given characters.



4. Find the output of following java program.

class MainClass
{
	public static void main (String arg[])
	{
		int i;
		for( i=1; 1; i++)
		{
			System.out.print(i);
			break;
		}
		System.out.println();
	}
}

Output

The output of this code will not generated due to compilation error.

Explanation

Like C and C++, we cannot use 0 and 1 for checking condition in case of java. In java we have to use bool variable 'true' and 'false' to check conditions of the conditional operators.

The correct code should be:

class MainClass
{
	public static void main (String arg[])
	{
		int i;
		for( i=1; true; i++)
		{
			System.out.print(i);
			break;
		}
		System.out.println();
	}
}

5. Find the output of following java program.

class MainClass
{
	public static void main (String arg[])
	{
		System.out.print( func() );
		System.out.println();
	}
	int func()
	{
		int test = 100;
		return test;
	}
}

Output

The output of this code will not generate due to compilation error.

Explanation

Like C and C++, we cannot call non static methods in a static method in java. To remove compilation error in this program we have to put static keyword before the function name. If we make the function static then this function can be call by main method in java.

The correct code should be:

class MainClass
{
	public static void main (String arg[])
	{
		System.out.print( func() );
		System.out.println();
	}
	static int func()
	{
		int test = 100;
		return test;
	}
} 
// This code returns 100 as output;




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.