OCJP(1Z0-851) 模拟题分析(八)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam

以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑。

QUESTION 235
Given:
5. class Payload {
6. private int weight;
7. public Payload (int w) { weight = w; }
8. public void setWeight(int w) { weight = w; }
9. public String toString() { return Integer.toString(weight); }
10. }
11. public class TestPayload {
12. static void changePayload(Payload p) { /* insert code */ }
13. public static void main(String[] args) {
14. Payload p = new Payload(200);
15. p.setWeight(1024);
16. changePayload(p);
17. System.out.println("p is " + p);
18. } }
Which code fragment, inserted at the end of line 12, produces the output p is 420?
A. p.setWeight(420);
B. p.changePayload(420);
C. p = new Payload(420);
D. Payload.setWeight(420);
E. p = Payload.setWeight(420);
Answer: A
记住Java是的参数传递是值传递~~

QUESTION 236
Given:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; i<10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for
garbage collection?
A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.
Answer: D
注意第15行 的 new Integer(value),在16行的代码过了之后,又会被numbers引用~~

QUESTION 237
Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2. public class Commander {
3. public static void main(String[] args) {
4. // more code here
5. }
6. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that the
classpath contains "." (current directory). Which command line correctly runs Commander?
A. java Commander
B. java com.sun.sjcp.Commander
C. java com/sun/sjcp/Commander
D. java -cp com.sun.sjcp Commander
E. java -cp com/sun/sjcp Commander
Answer: B
运行命令,java 包名+类名

QUESTION 238
Given:
11. public static void test(String str) {
12. int check = 4;
13. if (check = str.length()) {
14. System.out.print(str.charAt(check -= 1) +", ");
15. } else {
16. System.out.print(str.charAt(0) + ", ");
17. }
18. } and the invocation:
21. test("four");
22. test("tee");
23. test("to");
What is the result?
A. r, t, t,
B. r, e, o,
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: C
第13行,if(boolean b)~~

QUESTION 240
Given:
1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) {}
5. } And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH
environment variable is set to "." (current directory).
Which two java commands entered at the command line will run MainClass? (Choose two.)
A. java MainClass if run from the /apps directory
B. java com.company.application.MainClass if run from the /apps directory
C. java -classpath /apps com.company.application.MainClass if run from any directory
D. java -classpath . MainClass if run from the /apps/com/company/application directory
E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
F. java com.company.application.MainClass if run from the /apps/com/company/application directory
Answer: BC

编译 :javac -cp 路径 类名.java   例如: javac -cp  /apps/com/company/application MainClass.java.

运行:java -cp 路径 包名.类名     例如:java -cp /apps com.company.application.MainClass

QUESTION 243
Given:
1. interface DoStuff2 {
2. float getRange(int low, int high); }
3.
4. interface DoMore {
5. float getAvg(int a, int b, int c); }
6.
7. abstract class DoAbstract implements DoStuff2, DoMore { }
8.
9. class DoStuff implements DoStuff2 {
10. public float getRange(int x, int y) { return 3.14f; } }
11.
12. interface DoAll extends DoMore {
13. float getAvg(int a, int b, int c, int d); }
What is the result?
A. The file will compile without error.
B. Compilation fails. Only line 7 contains an error.
C. Compilation fails. Only line 12 contains an error.
D. Compilation fails. Only line 13 contains an error.
E. Compilation fails. Only lines 7 and 12 contain errors.
F. Compilation fails. Only lines 7 and 13 contain errors.
G. Compilation fails. Lines 7, 12, and 13 contain errors.
Answer: A
abstract 类可以不实现implements 的接口中的方法~~

QUESTION 246
Given: public class NamedCounter {
private final String name;
private int count;
public NamedCounter(String name) { this.name = name; }
public String getName() { return name; }
public void increment() { count++; }
public int getCount() { return count; }
public void reset() { count = 0; }
}
Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose
three.)
A. declare reset() using the synchronized keyword
B. declare getName() using the synchronized keyword
C. declare getCount() using the synchronized keyword
D. declare the constructor using the synchronized keyword
E. declare increment() using the synchronized keyword
Answer: ACE
构造方法不需要synchronized 关键字。而ACE选项中的三个方法都操作同一个变量count,需要synchronized关键字。

QUESTION 247
Given that t1 is a reference to a live thread, which is true?
A. The Thread.sleep() method can take t1 as an argument.
B. The Object.notify() method can take t1 as an argument.
C. The Thread.yield() method can take t1 as an argument.
D. The Thread.setPriority() method can take t1 as an argument.
E. The Object.notify() method arbitrarily chooses which thread to notify.
Answer: E
Object.notify()的功能:Wakes up a single thread that is waiting on this object‘s monitor.

QUESTION 249
Given:
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B
多态性~~

QUESTION 251
Given:
11. public abstract class Shape {
12. private int x;
13. private int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19. }
Which two classes use the Shape class correctly? (Choose two.)
A. public class Circle implements Shape {
private int radius;
}
B. public abstract class Circle extends Shape {
private int radius;
}
C. public class Circle extends Shape {
private int radius;
public void draw();
}
D. public abstract class Circle implements Shape {
private int radius;
public void draw();
}
E. public class Circle extends Shape {
private int radius;
public void draw() {/* code here */}
F. public abstract class Circle implements Shape {
private int radius;
public void draw() { /* code here */ }
Answer: BE
抽象类只能extends~~如果子类不是抽象类则必须实现父抽象类的抽象方法

QUESTION 254
Given:
1. public class A {
2. public void doit() {
3. }
4. public String doit() {
5. return "a";
6. }
7. public double doit(int x) {
8. return 1.0;
9. }
10. }
What is the result?
A. An exception is thrown at runtime.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 4.
D. Compilation succeeds and no runtime errors with class A occur.
Answer: C
 doit()函数如果参数相同则返回类型必须相同~~

QUESTION 258
Given:
11. abstract class Vehicle { public int speed() { return 0; }
12. class Car extends Vehicle { public int speed() { return 60; }
13. class RaceCar extends Car { public int speed() { return 150; } ...
21. RaceCar racer = new RaceCar();
22. Car car = new RaceCar();
23. Vehicle vehicle = new RaceCar();
24. System.out.println(racer.speed() + ", " + car.speed()
25. + ", " + vehicle.speed());
What is the result?
A. 0, 0, 0
B. 150, 60, 0
C. Compilation fails.
D. 150, 150, 150
E. An exception is thrown at runtime.
Answer: D
Section: (none)
Explanation/Reference:
Explanation:
多态性~~

QUESTION 260
Given:
10. public class SuperCalc {
11. protected static int multiply(int a, int b) { return a * b;}
12. }
and:
20. public class SubCalc extends SuperCalc{
21. public static int multiply(int a, int b) {
22. int c = super.multiply(a, b);
23. return c;
24. }
25. }
and:
30. SubCalc sc = new SubCalc ();
31. System.out.println(sc.multiply(3,4));
32. System.out.println(SubCalc.multiply(2,2));
What is the result?
A. 12
4
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 22.
F. Compilation fails because of an error in line 31.
Answer: E
static方法不能被重写,但可以被隐藏~~由于根本没有重写,super.multiply(a, b);会出问题~~

OCJP(1Z0-851) 模拟题分析(八)

时间: 2024-11-12 18:39:44

OCJP(1Z0-851) 模拟题分析(八)的相关文章

OCJP(1Z0-851) 模拟题分析(五)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 134 Given:11. class Snoochy {12. Boochy booch;13. public Snoochy() { booch = new Boochy(this); }14. }15.16. class Boochy {1

OCJP(1Z0-851) 模拟题分析(七)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 201 Given:1. public class TestString3 {2. public static void main(String[] args) {3. // insert code here5. System.out.print

OCJP(1Z0-851) 模拟题分析(三)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 61Given:1. public class TestString1 {2. public static void main(String[] args) { 3. String str = "420";4. str += 42;5.

OCJP(1Z0-851) 模拟题分析(九)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 261Given:3. class Employee {4. String name; double baseSalary;5. Employee(String name, double baseSalary) {6. this.name = n

OCJP(1Z0-851) 模拟题分析(六)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 167Given:1. import java.util.*;2. public class WrappedString {3. private String s;4. public WrappedString(String s) { this.

OCJP(1Z0-851) 模拟题分析(二)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 31 Given:1. interface A { public void aMethod(); }2. interface B { public void bMethod(); }3. interface C extends A,B { pub

一道模拟题

问题:把英文单词表示的数字转换为阿拉伯数字,要求数字不超过整形范围,数字形如abc,def,hrg. 第一行表示有几组数据,第二行输入英文. 输出:相应的阿拉伯数字. 例如:input: 3 eleven one hundred and two output: 11 102 分析:要注意百万和千要断位,还有要从高位往低位查找,注意分情况讨论. 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 st

Gym 100625C 密文匹配-模拟题-(map)

题意:已知n个明文和一个密文,推出可能的匹配关系,然后输出字符串ss的密文. 分析: 一个模拟题,当时想偏了,还想着要同一字母可能在任意位置,然后要记录每个字母的位置,找密文的相应位置必须是同一字母,balabala的,不知道什么鬼. 其实就是简单的对应关系,不用管位置啥的,只管同一字母对应的密文是一样的就行了.26个字母,枚举一遍就是了.对应关系匹配啥的用map是最好不过的了.小tirck是,如果已知25个字母,那么剩下的一个也就知道了. 代码: #include<iostream> #in

hdu_1034(模拟题)

很久没有打模拟题了,再次总结一下模拟题的做法: 仔细分析题意,弄清楚过程 理清楚模拟步骤,严格按照步骤编写代码 添加中间输出测试每步结果 虽然这是一个很简单的水题,但是没有松哥帮忙还是卡了很久,因为没有做好第一步,题目中是老师先吹哨,然后一圈所有同学同事将自己现有糖的一般分给下一个同学,结束后老师将现手里为奇数的糖的同学再多给一颗糖最后判断每个人的糖是否一样,如果一样结束游戏 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include