Moment For Technology

The output of a Java program | Java exercises 】 【 11 sets of (including parsing)

Posted on Dec. 3, 2022, 9:50 a.m. by Susan Baxter
Category: The back-end Tag: The back-end java The interview

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

Difficulty level: Intermediate

The program

Program a

Predict the output of the following Java program:

public class Base{
	private int data;
	public Base(a){
		data = 5;
	}
	public int getData(a){
		return this.data; }}class Derived extends Base{
	private int data;
	public Derived(a){
		data = 6;
	}
	private int getData(a){
		return data;
	}
	public static void main(String[] args){
		Derived myData = newDerived(); System.out.println(myData.getData()); }}Copy the code

A) 6 B) 5 C) compile time error D) runtime error

Click here to skip to the answer


Program 2

public class Test{
	private int data = 5;
	public int getData(a)	{
		return this.data;
	}
	public int getData(int value)	{
		return (data+1);
	}
	public int getData(int. value)	{
		return (data+2);
	}
	public static void main(String[] args)	{
		Test temp = new Test();
		System.out.println(temp.getData(7.8.12)); }}Copy the code

A) error B) 8 C) 10 d) 7

Click here to skip to the answer


Application of three

public class Base{
	private int multiplier(int data){
		return data*5; }}class Derived extends Base{
	private static int data;
	public Derived(a){
		data = 25;
	}
	public static void main(String[] args){
		Base temp = newDerived(); System.out.println(temp.multiplier(data)); }}Copy the code

A) 125 b) 25 C) runtime error D) compile time error

Click here to skip to the answer


Problem four

What are the following statements about finally blocks false?

A) There can be only one finally block for each try block. B) The finally block will not be executed if the program exits by calling system.exit (); C) If no exception is handled in the catch statement, the JVM executes the finally block before terminating the program. D) The finally block contains important code segments, so the code in the finally block executes if there is/is no try block in Java.

Click here to skip to the answer


Application of five

import java.io.IOException;
import java.util.EmptyStackException;

public class newclass{
	public static void main(String[] args){
		try{
			System.out.printf("%d".1);
			throw(new Exception());
		}
		catch(IOException e){
			System.out.printf("%d".2);
		}
		catch(EmptyStackException e){
			System.out.printf("%d".3);
		}
		catch(Exception e){
			System.out.printf("%d".4);
		}
		finally{
			System.out.printf("%d".5); }}}Copy the code

a) 12345

b) 15

c) 135

d) 145

Click here to skip to the answer


Program six

public class javaclass{
	static{
		System.out.printf("%d".1);
	}
	static{
		System.out.printf("%d".2);
	}
	static{
		System.out.printf("%d".3);
	}
	private static int myMethod(a){
		return 4;
	}
	private int function(a){
		return 5;
	}

	public static void main(String[] args){
		System.out.printf("%d", (newjavaclass()).function() + myMethod()); }}Copy the code

a) 123

b) 45

c) 12345

d) 1239

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:

When overriding a method of a superclass, a method declaration in a subclass cannot be more stringent than a method declared in a superclass.

Program two output

The answer:

(d)
Copy the code

Description:

When you do not know the number of input arguments but know the type of the argument (in this case, int), (int... Values) are passed to the method as parameters. When a new object is created in the main method, the variable data is initialized to 5. A call to the getData() method with attributes (7, 8,12) calls a third getData() method, which multiplicates the value data variable by 2 and returns 7.

Program three output

The answer:

(d)
Copy the code

Description:

Because the method multiplier is marked private, it is not inherited and therefore not visible to the derivants.

4.

Answer:

(d)
Copy the code

Description:

Statement (d) is incorrect because a finally block can exist only if a try or catch block succeeds. Using a finally block without a try block results ina compile-time error.

5.

Answer:

(d)
Copy the code

Description:

Catch statements are written in this order: more specific to more general. In the above code, a new Exception of type Exception is thrown. First, the code jumps to the first catch block to find the exception handler. But since the IOException is not of the same type, it moves down to the second catch block and finally to the third, where the exception is caught and the 4 is printed. Therefore, the answer is 145 because the block execution order is: try-catch-finally.

6.

Answer:

(d)
Copy the code

Description:

Static blocks in Java execute even before the compiler calls Main. In the main method, create a new Javaclass object and call its function() method to return 5. The static method myMethod() returns 4, which is 4+5=9. Therefore, the output of the program is 1239, because 123 is printed on the console even before the main method executes, and the main method returns 9 on execution.


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 of (gc)

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

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.