OCJP考试习题(1z0-808)(一)

这篇博客包含了20道OCJP(1z0-808)考试的相关问题和答案解析,涉及Java语言的各个方面,如static变量的特性、字符串操作、构造器的使用、数组、日期格式转换、三元运算符、方法重载、switch语句、以及构造函数中的super()和this()等知识点。
摘要由CSDN通过智能技术生成

Question1:

Given the code fragment:

public class Test {
	static int count = 0; //static会保留值
	int i = 0;
public void changeCount() {
		while (i < 5) {
			i++;
			count++;
		}
	}
	public static void main(String[] args) {
		Test check1 = new Test(); //5
		Test check2 = new Test(); //10
		check1.changeCount(); //5
		check2.changeCount(); //10
		System.out.print(check1.count + ":" + check2.count);
	}
}
What is the result?
A.10 : 10
B.5 : 5
C.5 : 10
D.Compilation fails
Answer: A
标签:static关键字

Static的值只有一份 并且会保留永远执行最新

 Question2:

public static void main(String[] args) {
	String product = "Pen";
	product.toLowerCase(); //仅小写
	product.concat(" BOX".toLowerCase()); //连接 中间有空格
	System.out.println(product.substring(4, 6));
}
What is the result?
A.box
B.nbo
C.bo
D.nb
E.An exception is thrown at runtime
Answer: E
标签:String关键字

System.out.println(product.substring(4, 6));

Product是指String product = "Pen";

所以截取不到

 

 Question3:

Which three are advantages of the Java exception mechanism?
A.Improves the program structure because the error handling code is separated fromthe normal program function
B.Provides a set of standard exceptions that covers all the possible errors 
C.Improves the program structure because the programmer can choose where to handle exceptions 
D.Improves the program structure because exceptions must be handled in themethod in which they occurred 
E.Allows the creation of new exceptions that are tailored to the particular program being created 
Answer: ACE
标签:异常  

 Question4:

Given the code fragment:
public class Person {
	String name;
	int age = 25;
	public Person(String name) {
		this(); // line n1 //this()里面没有参数 而方法有参数
		setName(name);
	}
	public Person(String name, int age) {
		Person(name); // line n2 //不能直接调用方法 需要new
		setAge(age);
	}
	// setter and getter methods go here
	public String show() {
		return name + "" + age + "";
	}
	public static void main(String[] args) {
		Person p1 = new Person("Jesse");
		Person p2 = new Person("Walter", 52);
		System.out.println(p1.show());
		System.out.println(p2.show());
	}
}
What is the result?
A.Jesse 25Walter 52
B.Compilation fails only at line n1
C.Compilation fails only at line n2
D.Compilation fails at both line n1 and line n2
Answer: D
标签:this关键字

This()关键字里面的参数要和上边的参数对应

不能直接调用方法名 需要new

 

Question5:

Given:
class Mid {
	public int findMid(int n1, int n2) {
		return (n1 + n2) / 2;
	}
}
class Calc extends Mid {
	public static void main(String[] args) {
		int n1 = 22, n2 = 2;
		// insert code here
	}
}
Which code fragments, when inserted at // in
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

layroy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值