This is the 8th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Difficulty level: Intermediate

The program

Program a

1) What is the output of the following program?

public class Test implements Runnable
{
	public void run(a)
	{
		System.out.printf("%d".3);
	}
	public static void main(String[] args) throws InterruptedException
	{
		Thread thread = new Thread(new Test());
		thread.start();
		System.out.printf("%d".1);
		thread.join();
		System.out.printf("%d".2); }}Copy the code

a) 123

b) 213

c) 132

d) 321

Click here to skip to the answer


Program 2

2) What is the output of the following program?

public class Test
{
	private static int value = 20;
	public int s = 15;
	public static int temp = 10;
	public static class Nested
	{
	private void display(a)
	{ System.out.println(temp + s + value); }}public static void main(String args[])
	{
	Test.Nested inner = newTest.Nested(); inner.display(); }}Copy the code

A) error b) 1020 c) 101520 d) Error

Click here to skip to the answer


Application of three

3) What is the output of the following program?

import java.io.*;
public class Test
{
	public void display(a) throws IOException
	{
		System.out.println("Test"); }}class Derived extends Test
{
	public void display(a) throws IOException
	{
		System.out.println("Derived");
	}
	public static void main(String[] args) throws IOException
	{
		Derived object = newDerived(); object.display(); }}Copy the code

A) test B) derived C) compile error d) run time error

Click here to skip to the answer


Application of four

4) What is the output of the following program?

public class Test extends Thread
{
	public void run(a)
	{
		System.out.printf("Test ");
	}
	public static void main(String[] args)
	{
		Test test = newTest(); test.run(); test.start(); }}Copy the code

A) compile error B) run error C) Test D) Test Test

Click here to skip to the answer


Application of five

5) What is the output of the following program?

public class Test extends Thread
{
	public static void main(String[] args)
	{
		String a = "Haiyong";
		String b = new String(a);
		int value = 0;
		value = (a==b) ? 1:2;
		if(value == 1)
		{
			System.out.println("Haiyong");
		}
		else if(value == 2)
		{
			System.out.println("Blog");
		}
		else
		{
			System.out.println("HY"); }}}Copy the code

A) Haiyong b) Blog c) HY d) none of the above

Click here to skip to the answer


Program six

6) What is the output of the following program?

public class Test
{
	try
	{
		public Test(a)
		{
			System.out.println("Haiyong");
			throw newException(); }}catch(Exception e)
	{
		System.out.println("HY");
	}
	public static void main(String[] args)
	{
		Test test = newTest(); }}Copy the code

A) Haiyong b) HY C) compilation error d) none of the above

Click here to skip to the answer


Application of seven

7) For a given code, choose the correct answer.

public interface Test
{
	public int calculate(a);
	protected interface NestedInterface
	{
		public void nested(a); }}Copy the code

A) compile-time error due to NestedInterface b) compile-time error due to NestedInterface’s access modifier C) no compile-time error d) NestedInterface cannot hold any function declarations.

Click here to skip to the answer


Application of eight

8) Which of the following statements about the constructor declaration is true?

A) Constructors can be declared final. B) Constructors can be surrounded by try/catch blocks. C) Constructors cannot throw exceptions. D) Constructors can hold synchronous code (so that each thread can access the constructors sequentially).

Click here to skip to the answer


The second half of the article is the output and parsing of the program


Output and parsing

Program one output

The answer:

(c)
Copy the code

Description:

The parent thread uses join to wait for the newly created thread to complete. The join() method allows one thread to wait for another thread to complete its execution. Therefore, the parent thread prints 1 and waits for the child thread to complete. The child thread prints 3 on the console, and finally the parent thread prints 2.


Program two output

The answer:

(a)
Copy the code

Description:

You cannot access non-static variables in a statically nested inner class. “Nesting” cannot access non-static variables [in this case variables]. Therefore error:

10: Error: cannot reference a non-static variable from a static context s system.out.println (temp + s + value); ^Copy the code

Program three output

The answer:

(b)
Copy the code

Description:

If a superclass method declares an exception, a method overridden by a subclass can declare the same, a subclass exception, or no exception, but cannot declare a superclass exception.


4.

Answer:

(d)
Copy the code

Description:

Test.run () executes the run method. Test.start () creates a new Thread and executes the overridden run method of the Thread class. The thread.start () method always starts a new Thread whose entry point is the run() method. If you call run() directly it will execute in the same Thread, but it is always recommended to logically call thread.start () to start a new Thread of execution, followed by the run() method.


5.

Answer:

(b)
Copy the code

Description:

The == operator checks whether two variables point to the same object. Here a and B refer to two different objects. ? Condition: if true then do this: else do this.


6.

Answer:

(c)
Copy the code

Description:

Constructors cannot be contained in try/catch blocks


Procedure 7.

Answer:

(b)
Copy the code

Description:

The access modifier for NestedInterface must only be public. Therefore error:

4: Error: Invalid combination of modifiers: public and protected protected interface NestedInterface ^1errorCopy the code

8.

Answer:

(d)
Copy the code

Description:

The constructor allows data to be accessed sequentially between threads.


That’s all for this article

Related articles:

Java program Java exercises 】 【 | at the output of the first set of (analysis) the output of the Java program Java exercises 】 【 | second (analysis) the output of a Java program | Java exercises 】 【 a third (parsing) 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) the output of a Java program | Java exercises 】 【 8 sets (including parsing) The output of a Java program | Java exercises 】 【 9 sets of (including parsing) the output of a Java program | Java exercises 】 【 10 sets (recycling) the output of a Java program | Java exercises 】 【 11 sets of (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, and this is I recently just set up the blog: Haiyong. Site, there is no content, put some HTML games, interested in can try, source code can own F12 copy, or directly find me to.

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.