This is the 19th day of my participation in the August More Text Challenge

Difficulty level: Difficult

The program

Program a

1) What is the output of the following program?

public class Outer
{
	public static int temp1 = 1;
	private static int temp2 = 2;
	public int temp3 = 3;
	private int temp4 = 4;
	
	public static class Inner
	{
		private static int temp5 = 5;
		
		private static int getSum(a)
		{
			return(temp1 + temp2 + temp3 + temp4 + temp5); }}public static void main(String[] args)
	{
		Outer.Inner obj = newOuter.Inner(); System.out.println(obj.getSum()); }}Copy the code

A) 15 B) 9 C) 5 D) 15

Click here to jump to the answer


Program 2

2) What is the output of the following program?

public class Outer
{
	private static int data = 10;
	private static int LocalClass(a)
	{
		class Inner
		{
			public int data = 20;
			private int getData(a)
			{
				returndata; }}; Inner inner =new Inner();
		return inner.getData();
	}
	
	public static void main(String[] args)
	{ System.out.println(data * LocalClass()); }}Copy the code

A) compile error b) runtime error C) 200 d) None of the above

Click here to jump to the answer


Application of three

3) What is the output of the following program?

interface Anonymous
{
	public int getValue(a);
}
public class Outer
{
	private int data = 15;
	public static void main(String[] args)
	{
		Anonymous inner = new Anonymous()
				{
					int data = 5;
					public int getValue(a)
					{
						return data;
					}
					public int getData(a)
					{
						returndata; }}; Outer outer =newOuter(); System.out.println(inner.getValue() + inner.getData() + outer.data); }}Copy the code

A) 25 B) compilation error C) 20 d) runtime error

Click here to jump to the answer


Application of four

4) What is the output of the following program?

public class Outer
{
	private int data = 10;
	
	class Inner
	{
		private int data = 20;
		private int getData(a)
		{
			return data;
		}
		public void main(String[] args)
		{
			Inner inner = newInner(); System.out.println(inner.getData()); }}private int getData(a)
	{
		return data;
	}
	public static void main(String[] args)
	{
		Outer outer = new Outer();
		Outer.Inner inner = outer.new Inner(a);
		System.out.printf("%d", outer.getData()); inner.main(args); }}Copy the code

A) 2010 B) 1020 C) compilation error D) none of these

Click here to jump to the answer


Application of five

5) What is the output of the following program?

interface OuterInterface
{
	public void InnerMethod(a);
	public interface InnerInterface
	{
		public void InnerMethod(a); }}public class Outer implements OuterInterface.InnerInterface.OuterInterface
{
	public void InnerMethod(a)
	{
		System.out.println(100);
	}
	
	
	public static void main(String[] args)
	{
		Outer obj = newOuter(); obj.InnerMethod(); }}Copy the code

A) 100 b) compile error C) runtime error d) none of the above

Click here to jump to the answer


The second part is the output and analysis of the program


Output and parsing

Program one output

The answer:

(d)Copy the code

Description:

A static inner class cannot access the non-static fields of an external class.


Program two output

The answer:

(C)Copy the code

Description:

The LocalClass() method defines a local inner class. This method creates an object of class Inner and returns the value of the variable data residing in it.


Program three output

The answer:

(b)
Copy the code

Description:

The method getData() is not defined in the Anonymous class, resulting in a compilation error.


Procedure 4 Answer

Answer:

(b)
Copy the code

Description:

The inner class defined above can access the private variable data of the outer class, but declaring variable data in the inner class makes it specific to the inner class, and there is no conflict in variable declaration.


Procedure 5 Answer

Answer:

(a)
Copy the code

Description:

Nested interfaces are defined in Java. Since both interfaces have a declaration of InnerMethod(), implementing it once applies to both the InnerInterface and OuterInterface.


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) the output of a Java program | Java exercises 】 【 14 sets (constructor)

I’ve been writing tech blogs for a long time and this is one of my tech posts/tutorials. Hope you like it! Here is a summary of all my original and works of source: GitHub, and this is my recently just built blog: Haiyong. Site, there is no content, put some HTML games, interested can try, source code can be their own F12 copy, or directly find me to.

If you really learn something new from this article, like it, bookmark it and share it with your friends. 🤗 and finally, don’t forget ❤ or 📑.