Find output programs (Java String class)

Java String class | Find output of the programs: Here, we will practice the Java String class with some of the programs, we have to find output of the given java programs.
Submitted by IncludeHelp, on July 04, 2018

Program 1

public class iHelp
{
	public static void main (String[] args)
	{
		System.out.println("Google".charAt(3));
	}
}

Output

    g

Explanation

String.charAt() is a library function of String class, it returns character from given index of the string. Here, in the statement "Google".charAt(3) will return 'g', because 'g' exists on the 3rd index.


Program 2

public class iHelp
{
	public static void main (String[] args)
	{
		System.out.println("Google"+3);
	}
}

Output

    Google3

Explanation

In java String, "+" is a concatenation operator, if we concatenate any number or string with the string, it returns that number/string concatenated to the string.

Here, "Google" is a string and 3 is a number. The resultant string of "Google"+3 will be "Google3".


Program 3

public class iHelp
{
	public static void main (String[] args)
	{
		System.out.println(10+20);
	}
}

Output

    30

Explanation

10+20 is a simple numeric expression, addition will be performed. Therefore, the result will be 30.



Program 4

public class iHelp
{
	public static void main (String[] args)
	{
		if(System.out.println(10) == System.out.println(10))
			System.out.println("TRUE");
		else
			System.out.println("FALSE");
	}
}

Output

Compilation error:

    Main.java:6: error: 'void' type not allowed here
		    if(System.out.println(10) == System.out.println(10))
		                         ^
    1 error

Explanation

The statement System.out.println() does not return anything. Therefore, no value will be for compression. Error will be "void" type not allowed here.


Program 5

public class iHelp
{
	public static void main (String[] args)
	{
		System.out.println(10+20+""+40);
	}
}

Output

    3040

Explanation

10+20 will be added first, and result of 10+20 will be 30. Then, " " will be concatenated to the 30, so it will become "30" in string. After that, "30"+40 will be "3040" (because if any string/number is added to the string it will concatenate as string).

Therefore, output will be "3040".





Comments and Discussions!

Load comments ↻





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