11) 已知如下代码: 1: class Example{
2: String str;
3: public Example(){
4: str= "example";
5: }
6: public Example(String s){
7: str=s;
8: }
9:} }
10: class Demo extends Example{
11: }
12: public class Test{
13:public void f () {
14:Example ex = new Example("Good");
15:Demo d = new Demo("Good");
16:} }
哪句语句会导致错误?
下列给出的代码段运行结果是?
public class Example {
int maxElements;
void Example() {
maxElements = 100;
System.out.println(maxElements);
}
public Example(int i) {
maxElements = i;
System.out.println(maxElements);
}
public static void main(String[] args) {
Example a=new Example();
Example b=new Example(999);
}
}
以下代码的执行结果是:
public class Example {
String s = "Outer";
public static void main(String[] args) {
S2 s2 = new S2();
s2.display();
}
}
class S1 {
String s = "S1";
void display() {
System.out.println(s);
}
}
class S2 extends S1 {
String s = "S2";
}
以下代码的执行结果是:
public class Example {
static int i = 1, j = 2;
static {
display(i);
i = i + j;
}
static void display(int n) {
System.out.println(n);
}
public static void main(String[] args) {
display(i);
}
}
下列给出代码的运行结果是?
public class Example {
public static void main(String[] args) {
int m = 2;
int p = 1;
int i = 0;
for (; p < 5; p++) {
if (i++ > m) {
m = p + i;
}
}
System.out.println("i equals " + i);
}
}
给出以下代码,假设arr数组中只包含正整数值,请问下列代码段实现了什么功能?
public int guessWhat(int arr[]) {
int x = 0;
for (int i = 0; i < arr.length; i++) {
x = x < arr[i] ? arr[i] : x;
}
return x;
}