Difficulty level: Novice

Predict the output of the following Java program.

Considering that if you post the answer immediately after the question, students may accidentally see the result before thinking, so I put the question and the answer separately, with something in the middle, I hope it will not bring you difficulty in reading

The problem

Problem a

// File name main.java
class Test {
	protected int x, y;
}

class Main {
	public static void main(String args[]) {
		Test t = new Test();
		System.out.println(t.x + ""+ t.y); }}Copy the code

Click here to skip to the answer

Question 2

// filename Test.java
class Test {
	public static void main(String[] args) {
		for(int i = 0; 1; i++) {
			System.out.println("Hello");
			break; }}}Copy the code

Click here to skip to the answer

Question 3

// filename Main.java
class Main {
	public static void main(String args[]) {
		System.out.println(fun());
	}
	int fun(a) {
		return 20; }}Copy the code

Click here to skip to the answer

Problem four

// filename Test.java
class Test {
public static void main(String args[]) {
	System.out.println(fun());
}
static int fun(a) {
	static int x= 0;
	return++x; }}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

The output

0 0
Copy the code

In Java, protected members can be accessed by all classes in the same package and by classes inherited from other packages. Because Test and Main are in the same package, there are no access-related issues in the above program. In addition, the default constructor initializes integer variables to 0 in Java. And that’s why we get the output 0, 0.

Answer to question two

The output

Compiler Error
Copy the code

Compiler Error: Compiler Error in the for loop’s conditional check expression. Java differs here from C++ (or C). C++ treats all non-zero values as true and treats 0 as false. Unlike C++, integer numeric expressions cannot be placed where Boolean values are required in Java. The following is the revised procedure.

// File name test.java
class Test {
	public static void main(String[] args) {
		for(int i = 0; true; i++) {
			System.out.println("Hello");
			break; }}}// Output: Hello
Copy the code

Answer to Question three

Compiler Error
Copy the code

Compiler Error: Compiler Error. In Java, as in C++, non-static methods cannot be called inside static methods. If we set fun() to static, the program will compile without any compiler errors. The following is the revised procedure.

// File name main.java
class Main {
	public static void main(String args[]) {
		System.out.println(fun());
	}
	static int fun(a) {
		return 20; }}// Output: 20
Copy the code

Answer to Question 4

Compiler Error
Copy the code

Compiler Error: Compiler Error. Unlike C++, static local variables are not allowed in Java. See this GFact for more information. We can use class static members to calculate the number of function calls served by C++ local static variables, among other purposes. The following is the revised procedure.

class Test {
private static int x;
public static void main(String args[]) {
	System.out.println(fun());
}
static int fun(a) {
	return++x; }}// Output: 1
Copy the code

The related content

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)

I’ve been writing a technology blog for a long time, and this is one of my Java worksheets. Hope you like it! More related articles and my contact information I put here:

Github.com/wanghao221 gitee.com/haiyongcsdn…

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