Difficulty level: medium

Predict the output of the following Java program:

The problem

Problem a

class First
{
	public First(a) { System.out.println("a"); }}class Second extends First
{
	public Second(a) { System.out.println("b"); }}class Third extends Second
{
	public Third(a) { System.out.println("c"); }}public class MainClass
{
	public static void main(String[] args)
	{
		Third c = newThird(); }}Copy the code

Click here to skip to the answer

Question 2

class First
{
	int i = 10;

	public First(int j)
	{
		System.out.println(i);
		this.i = j * 10; }}class Second extends First
{
	public Second(int j)
	{
		super(j);
		System.out.println(i);
		this.i = j * 20; }}public class MainClass
{
	public static void main(String[] args)
	{
		Second n = new Second(20); System.out.println(n.i); }}Copy the code

Click here to skip to the answer

Question 3

import java.util.*;
class I
{
	public static void main (String[] args)
	{
		Object i = new ArrayList().iterator();
		System.out.print((i instanceof List) + ",");
		System.out.print((i instanceof Iterator) + ",");
		System.out.print(i instanceofListIterator); }}Copy the code

Click here to skip to the answer

Problem four

class ThreadEx extends Thread
{
	public void run(a)
	{
		System.out.print("Hello...");
	}
	public static void main(String args[])
	{
		ThreadEx T1 = newThreadEx(); T1.start(); T1.stop(); T1.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:

a
b
c
Copy the code

To create a new ‘Third’ object, call the superclass’s default constructor (Second class) before calling the default constructor of the Third class, and then call the superclass’s default constructor (First class) again before calling the superclass’s default constructor (First class). So that gives you this output.

Answer to question two

Output:

10 
200 
400
Copy the code

Note: Since it does not have its own ‘I’ in the ‘Second’ class, this variable is inherited from the superclass. In addition, the parent constructor is called when we create the Second object.

Answer to Question three

Output:

false.true.false
Copy the code

Note: The iterator() method returns iterators to the elements of a List in the appropriate order. It does not return List or ListIterator objects. A ListIterator can be obtained by calling the ListIterator method.

Answer to Question 4

Run Time Exception
Copy the code

Description: a Thread in the “main” Java. Lang. IllegalThreadStateException at Java. Lang. Thread. Start a Thread cannot start twice.


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)

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