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

Difficulty level: Easy

The program

Program a

1) What is the output of the following program?

class Helper
{
	private int data;
	private Helper(a)
	{
		data = 5; }}public class Test
{
	public static void main(String[] args)
	{
		Helper help = newHelper(); System.out.println(help.data); }}Copy the code

A) compile error B) 5 C) run time error D) None of these

Click here to skip to the answer


Program 2

2) What is the output of the following program?

public class Test implements Runnable
{
	public void run(a)
	{
		System.out.printf("The thread is running");
	}

	try
	{
		public Test(a)
		{
			Thread.sleep(5000); }}catch (InterruptedException e)
	{
		e.printStackTrace();
	}
	
	public static void main(String[] args)
	{
		Test obj = new Test();
		Thread thread = new Thread(obj);
		thread.start();
		System.out.printf(" juejin "); }}Copy the code

A) juejin thread is running b) Juejin thread is running C) compile error D) runtime error

Click here to skip to the answer


Application of three

3) What is the output of the following program?

class Temp
{
	private Temp(int data)
	{
		System.out.printf("Constructor call");
	}
	protected static Temp create(int data)
	{
		Temp obj = new Temp(data);
		return obj;
	}
	public void myMethod(a)
	{
		System.out.printf("Method call"); }}public class Test
{
	public static void main(String[] args)
	{
		Temp obj = Temp.create(20); obj.myMethod(); }}Copy the code

A) constructor call b) method call C) runtime error d) none of the above

Click here to skip to the answer


Application of four

4) What is the output of the following program?

public class Test
{
	public Test(a)
	{
		System.out.printf("1");
		new Test(10);
		System.out.printf("5");
	}
	public Test(int temp)
	{
		System.out.printf("2");
		new Test(10.20);
		System.out.printf("4");
	}
	public Test(int data, int temp)
	{
		System.out.printf("3");
		
	}
	
	public static void main(String[] args)
	{
		Test obj = newTest(); }}Copy the code

A) 12345 b) compiler error C) 15 d) Runtime error

Click here to skip to the answer


Application of five

5) What is the output of the following program?

class Base
{
	public static String s = "Super class";
	public Base(a)
	{
		System.out.printf("1"); }}public class Derived extends Base
{
	public Derived(a)
	{
		System.out.printf("2");
		super(a); }public static void main(String[] args)
	{
		Derived obj = newDerived(); System.out.printf(s); }}Copy the code

A) 21 B) 21 C) 12 D) 12

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:

(a)Copy the code

Description:

The private constructor cannot be used to initialize objects outside of the class, which is inside because it is no longer visible in the outer class defined.


Program two output

The answer:

(C)Copy the code

Description:

Constructors cannot be contained in try/catch blocks.


Program three output

The answer:

(a)
Copy the code

Description:

When a constructor is marked private, the only way to create a new object of that class from an external class is to use the method of creating a new object, as defined in the program above. The create() method is responsible for creating Temp objects from some other external class. Once an object is created, its methods can be called from the class that created it.


4.

Answer:

(a)
Copy the code

Description:

Constructors can be linked and overloaded. When Test() is called, it creates another Test object that calls the constructor Test(int Temp).


5.

Answer:

(c)
Copy the code

Description:

The constructor call to the superclass must be the first statement in the constructor of the derived class.


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) [Java exercises] | at the output of the Java program 12 sets (including the parse) the output of a Java program | Java exercises 】 【 13 sets (set)

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.