龙空技术网

「Java练习题」Java 程序的输出 | 第十一套(含解析)

小心程序猿QAQ 109

前言:

目前同学们对“java读程序题”大概比较重视,看官们都想要学习一些“java读程序题”的相关内容。那么小编也在网络上搜集了一些关于“java读程序题””的相关资讯,希望看官们能喜欢,大家快快来学习一下吧!

程序程序一

预测一下 Java 程序的输出:

public class Base{	private int data;	public Base(){		data = 5;	}	public int getData(){		return this.data;	}}class Derived extends Base{	private int data;	public Derived(){		data = 6;	}	private int getData(){		return data;	}	public static void main(String[] args){		Derived myData = new Derived();		System.out.println(myData.getData());	}}复制代码

a) 6

b) 5

c) 编译时错误

d) 运行时错误

程序二

public class Test{	private int data = 5;	public int getData()	{		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));	}}复制代码

a) 编译时或运行时错误

b) 8

c) 10

d) 7

程序三

public class Base{	private int multiplier(int data){		return data*5;	}}class Derived extends Base{	private static int data;	public Derived(){		data = 25;	}	public static void main(String[] args){		Base temp = new Derived();		System.out.println(temp.multiplier(data));	}}复制代码

a) 125

b) 25

c) 运行时错误

d) 编译时错误

问题四

以下关于 finally 块的说法错误的是?

a) 对于每个 try 块,只能有 1 个 finally 块。

b) 如果程序通过调用 System.exit() 退出,则 finally 块将不会被执行;

c) 如果在 catch 语句中没有处理异常,在终止程序之前,JVM 会执行 finally 快

d) finally 块包含重要的代码段,因此 finally 块中的代码在 java 中存在/不存在 try 块的情况下执行代码。

程序五

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);		}	}}复制代码

a) 12345

b) 15

c) 135

d) 145

程序六

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(){		return 4;	}	private int function(){		return 5;	}	public static void main(String[] args){		System.out.printf("%d", (new javaclass()).function() + myMethod());	}}复制代码

a) 123

b) 45

c) 12345

d) 1239

文章后半部分是程序的输出及解析

输出及解析程序一输出

答案:

(c)复制代码

说明:

当覆盖超类的方法时,子类中的方法声明不能比超类中声明的方法更严格。

程序二输出

答案:

(d)复制代码

说明:

当您不知道输入参数的数量但知道参数的类型(在本例中为 int)时,(int... values) 作为参数传递给方法。现在 main 方法中创建一个新对象时,变量数据被初始化为 5。对具有属性 (7, 8 ,12) 的 getData() 方法的调用会调用第三个 getData() 方法,该方法会增加值数据变量乘以 2 并返回 7。

程序三输出

答案:

(d)复制代码

说明:

由于方法乘数被标记为私有,因此它不是继承的,因此对派生者不可见。

程序四答案

回答 :

(d)复制代码

说明:

语句(d) 是错误的,因为finally 块只有在try 或catch 块成功时才能存在。使用没有 try 块的 finally 块会产生编译时错误。

程序五答案

回答 :

(d)复制代码

说明:

catch 语句的编写顺序是:更具体到更一般。在上面的代码中,抛出了一个 Exception 类型的新异常。首先,代码跳转到第一个 catch 块以查找异常处理程序。但是由于 IOException 不是同一类型,它向下移动到第二个 catch 块,最后移动到第三个,在那里捕获异常并打印 4。因此,答案是 145,因为按照块的执行顺序是:try->catch->finally。

程序六答案

回答 :

(d)复制代码

说明:

Java 中的静态块甚至在编译器调用 main 之前就已执行。在main方法中,新建一个javaclass对象,调用它的function()方法返回5,静态方法myMethod()返回4,即4+5=9。因此,程序的输出为1239,因为 123 甚至在 main 方法执行之前就被打印在控制台上,并且 main 方法在执行时返回 9。

以上就是本篇文章的所有内容了

作者:海勇

链接:

来源:掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签: #java读程序题 #java代码图案