此题为判断题(对,错)。
第1题:
下列程序的功能是为变量赋值,程序运行后,输出i=51。请改动main方法中的错误,使程序能够正确编译、运行并输出正确的结果。
注意:不改动程序结构。
class A
{
private int a;
public void setA (int x)
{
a=x;
}
public int getA()
{
return a;
}
}
public class MethodTest
{
public static void main(String args[])
{
A a=A();
a.getA(51);
int i=a.getA();
System.out.println ("i="+i);
}
}
第2题:
下列代码的执行结果是public class Test{ public int aMethod(){ static int i=0; i++; System.out.println(i); } public static void main(String args[]){ Test test= new Test(); test. aMethod(); }}
A.编译错误
B.0
C.1
D.运行成功,但不输出
第3题:
以下代码的运行结果是______。
public class exl5
{
public static void main(String args [])
{
int i = 5;
do
{
i--;
if(i > 2)
continue;
}while(i < 0);
System.out.println(i);
}
}
第4题:
下列代码的执行结果是( )。
public class Test{
public int aMethod( ){
static int i=0;
i++;
System.out.println(i):
}
public static void main (String args[]){
Trest test=new Test ( );
test aMethod( ):
}
}
A.编译错误
B.0
C.1
D.运行成功,但不输出
B.
C.
D.
第5题:
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的
x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。
对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可
以通过A.x 来明确。
第6题:
下列程序的执行结果是 ( ) public class Test { public int aMethod() { satic int i=0; i++; System.out.println(i); } public static void.main(String args[]) { Test test=new Test(); test.aMethod(); }
A.编译错误
B.0
C.1
D.运行成功,但不输出
第7题:
public class Something {
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) {
o.i++;
}
}
class Other {
public int i;
}
和上面的很相似,都是关于final 的问题,这有错吗?
正确。在addOne method 中,参数o 被修饰成final。如果在addOne method 里我们修
改了o 的reference
(比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o 的member vairable
(成员变量),而o 的reference 并没有改变。
第8题:
下列程序段的输出结果是【 】。
public class Test {
void printValue(int m) {
do {
System.out.println("The value is"+m);
}while (--m>10);
}
public static void main (String args[]) {
int i=10;
Test t= new Test();
t.printValue(i);
}
}
第9题:
4 . 写出程序的输出结果
class Class1 {
private string str = "Class1.str";
private int i = 0;
static void StringConvert(string str) {
str = "string being converted.";
}
static void StringConvert(Class1 c) {
c.str = "string being converted.";
}
static void Add(int i) {
i++;
}
static void AddWithRef(ref int i) {
i++;
}
static void Main() {
int i1 = 10;
int i2 = 20;
string str = "str";
Class1 c = new Class1();
Add(i1);
AddWithRef(ref i2);
Add(c.i);
StringConvert(str);
StringConvert(c);
Console.WriteLine(i1);
Console.WriteLine(i2);
Console.WriteLine(c.i);
Console.WriteLine(str);
Console.WriteLine(c.str);
}
}
第10题:
下列语句执行后,i的值是( )。 public class Test { public static void main(String[ ] args) { int i =0; for(int j=10; j>5&&i<5; j-=3,i+=2) i=i+j; System.out.println(i); } }
A.8
B.9
C.10
D.12