北京神舟航天软件笔试题和面试题答案(三)
public class Test {
public static void changeStr(String str){
str="welcome";
}
public static void main(String[] args) {
String str="1234";
changeStr(str);
System.out.println(str);
}
}
2、写出下面程序的运行结果(5分)
class Foo{
public static void main(String args[]){
int x=4,j=0;
switch(x){
case 1:j++;
case 2:j++;
case 3:j++;
case 4:j++;
case 5:j++;
default:j++;
}
System.out.println(j);
}
}
(四) 代码查错(10分)
1、指出下面程序的运行错误(4分)
public class OutClass{
private int varInOuterClass = 0;
public OutClass(){
}
public void callOutter(){
int varInOuterMethod = 0;
class InnerClass{
private int varInInnerClass = 0;
public InnerClass(){
}
public void print(){
System.out.println("varInOuterClass" + varInOuterClass);
System.out.println("varInInnerClass" + varInInnerClass);
System.out.println("varInInnerClass" + varInOuterMethod);
}
}
InnerClass inner = new InnerClass();
inner.print();
}
public static void main(String[] args){
OutClass out = new OutClass();
out.callOutter();
}
}
2、指出下面程序的运行错误(3分)
public class Something {
public static void main(String[] args) {
Something s = new Something();
System.out.println("s.doSomething() returns " + doSomething());
}
public String doSomething() {
return "Do something ...";
}
}
(五) 编程题(10分)
算法设计能力测试(10分)
1、编写一个类,该类封装了一元二次方程共有的属性和功能,即该类有刻画方程系数的3个成员变量以及计算实根的方法。
方程: 求根方法为 要求:该类的所有对象共享常数项。
下面给出了您在程序中可能会使用到的功能类,及其部分接口的API文档,在程序中可以进行使用,
java.lang.Math | |
staticfloat | signum(floatf) Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero. |
staticdouble | sin(doublea) Returns the trigonometric sine of an angle. |
staticdouble | sinh(doublex) Returns the hyperbolic sine of adoublevalue. |
staticdouble | sqrt(doublea) Returns the correctly rounded positive square root of adoublevalue. |
staticdouble | tan(doublea) Returns the trigonometric tangent of an angle. |
staticdouble | tanh(doublex) Returns the hyperbolic tangent of adoublevalue. |
staticdouble | toDegrees(doubleangrad) Converts an angle measured in radians to an approximately equivalent angle measured in degrees. |
staticdouble | toRadians(doubleangdeg) Converts an angle measured in degrees to an approximately equivalent angle measured in radians. |
(一)不定项选择
1, C 2, A D 3, A D 4, B 5, E 6, A C D 7, A E 8, A B 9, A B C D
10, A B C 11, C 12, C D 13, C 14, C 15, C 16, B 17, D, 18, A B D
19, D, 20, C D 21,B C 22, B C 23, A C E 24, A C D
(五)编程题
public class Equation {
public float a;
public float b;
public float c;
public double[] d;
public Equation(float a, float b, float c) {
this.a = a;
this.b = b;
this.c = c;
this.d = new double[2];
}
boolean getRealRoot() {
float temp = b * b - 4 * a * c;
if (temp < 0)
return false;
else {
this.d[0] = (-b + Math.sqrt(temp)) / (2 * a);
this.d[1] = (-b - Math.sqrt(temp)) / (2 * a);
return true;
}
}
public static void main(String[] arg) {
Equation e1 = new Equation(1, 2, 1);
if (e1.getRealRoot()) {
System.out.print(e1.d[0]);
System.out.print(";");
System.out.println(e1.d[1]);
} else {
System.out.print("no real root");
}
}
}