“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Difficulty level: medium

Predict the output of the following Java program:

The problem

Problem a

class GfG
{
	public static void main(String args[])
	{
		String s1 = new String("geeksforgeeks");
		String s2 = new String("geeksforgeeks");
		if (s1 == s2)
			System.out.println("Equal");
		else
			System.out.println("Not equal"); }}Copy the code

Click here to skip to the answer

Question 2

class Person
{
	private void who(a)
	{
		System.out.println("Inside private method Person(who)");
	}

	public static void whoAmI(a)
	{
		System.out.println("Inside static method, Person(whoAmI)");
	}

	public void whoAreYou(a)
	{
		who();
		System.out.println("Inside virtual method, Person(whoAreYou)"); }}class Kid extends Person
{
	private void who(a)
	{
		System.out.println("Kid(who)");
	}

	public static void whoAmI(a)
	{
		System.out.println("Kid(whoAmI)");
	}

	public void whoAreYou(a)
	{
		who();
		System.out.println("Kid(whoAreYou)"); }}public class Gfg
{
	public static void main(String args[])
	{
		Person p = newKid(); p.whoAmI(); p.whoAreYou(); }}Copy the code

Click here to skip to the answer

Question 3

class GfG
{
	public static void main(String args[])
	{
		try
		{
			System.out.println("First statement of try block");
			int num=45/3;
			System.out.println(num);
		}
		catch(Exception e)
		{
			System.out.println("Gfg caught Exception");
		}
		finally
		{
			System.out.println("finally block");
		}
		System.out.println("Main method"); }}Copy the code

Click here to skip to the answer

Problem four

class One implements Runnable
{
	public void run(a)
	{ System.out.print(Thread.currentThread().getName()); }}class Two implements Runnable
{
	public void run(a)
	{
		new One().run();
		new Thread(new One(),"gfg2").run();
		new Thread(new One(),"gfg3").start(); }}class Three
{
	public static void main (String[] args)
	{
		new Thread(new Two(),"gfg1").start(); }}Copy the code

Click here to skip to the answer


Put a picture of a cute girl to relieve eye fatigue, the second half of the article is the output and analysis of the program


Output and parsing

Answer to question one

Output:

Not equal
Copy the code

Explanation:

Since S1 and S2 are two different objects, the references are not the same, and the == operator compares object references. So it prints “not equal” to compare the actual characters in the string, and must use the.equals() method.

Answer to question two

Output:

Inside static method, Person(whoAmI)
Kid(who)
Kid(whoAreYou)
Copy the code

Description:

Static binding (or compile-time) occurs on static methods. Here p.wihoami () calls the static method, so it is called at compile time, thus resulting in static binding and printing the method in the Person class. P.hoareyou () calls methods in the Kid class, because by default Java treats them as virtual methods, that is, dynamic bindings.

Answer to Question three

Output:

First statement of try block
15
finally block
Main method
Copy the code

Explanation:

Since there are no exceptions, the catch block is not called, but the finally block always executes after the try block, whether or not the exception is handled.

Answer to Question 4

Output:

gfg1gfg1gfg3
Copy the code

Description:

Initially the new thread starts with the name gfg1, and then in class 2, the first run method runs the thread named gfg1, and then creates a new thread that calls the run method, but since you can only create a new thread by calling the start method, the previous thread performs the action, And again gfg1 is printed.Now a new thread is started by calling the start method to create the gfG3 name, so print gfG3.


Related articles:

The output of a Java program | Java exercises 】 【 first (resolution)

The output of a Java program | Java exercises 】 【 a second (resolution)

The output of a Java program | Java exercises 】 【 a third (resolution)

The output of a Java program | Java exercises 】 【 4 sets (including parsing)

The output of a Java program | Java exercises 】 【 5 sets (including parsing)

The output of the Java program Java exercises 】 【 | 6 sets (including parsing)

The output of a Java program | Java exercises 】 【 7 sets (including parsing)

I have been writing a tech blog for a long time and this is one of my tech articles/tutorials. Hope you like it! Here is a summary of all my original and work source code: GitHub, Gitee

If you do learn something new from this post, like it, bookmark it and share it with your friends. 🤗 Finally, don’t forget ❤ or 📑 for support