JavaSE程序代码题
1.阅读下面的代码,然后写出程序运行结果。
public class Test2{
static boolean foo(char c){
System.out.print ©;
return true;
}
public static void main(String[] argv) {
int i=0;
for (foo(‘A’);foo(‘B’)&&(i<2);foo(‘C’)){
i++;
foo(’D’);
}
}
}
结果:ABDCBDCB
2.如下程序代码
a=0;c=0;
do{
–c;
a=a-1
}while(a>0);
循环结束后,c的值是()
结果:-1
3.给出下面的代码:
if(x>0){System.out.println(“first”),)
else if(x>-3){System.out.println(“second”)😉
else{System.out.println(“third”)😉
x的取值在什么范围内时将打印字符串”second”
结果:0 > x > -3
4.阅读下面程序,写出其输出:
class Person{
Person(){
System.out.println(“1”);
}
public void printValue(int i,int j){
System.out.println(“2”);
}
public void printValue(int i){
System.out.println(“3”);
}
}
public class Teacher extends Person{
Teacher(){
System.out.println(“4”);
}
结果: 1
4
5.下面程序有什么问题,请指正。
public class Person{
int arr[] = new int [];
public static void main(String a[]){}
}
答:int arr[]=new int[]表示数组 ,arr组数未开辟一个存放整数的存储空间。
6.写出下列代码的输出结果。
public class Test {
public int aMethod() {
static int i = 0;
i++;
return i;
}
public static void main (String args[]) {
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
结果:1
7.下列代码编译运行结果。
public class Demo{
public int add(int a,int b){
try{
return a+b;
}catch(Exception e){
System.out.println(“catch 语句块”);
}finally{
System.out.println(“finally 语句块”);
}
return 0;
}
public static void main(String[] args){
Demo demo = new Demo();
System.out.println(“和是:”+demo.add(9,34));
}
}
结果: finally 语句块
和是:43
8.下列代码编译运行结果。
public static void main(String[] args) {
try {
String name = null;
System.out.println(name.length());
} catch (SQLException e) {
System.out.println(“有异常出现”);
} finally {
System.out.println(“执行结束”);
}
}
结果:编译出错,SQLException 使用错误
9.下列代码编译运行结果。
public class Animal {
public Animal() {
System.out.println(“I am an animal”);
}
}
public class Cat extends Animal {
public Cat () {
System.out.println(“I am a cat”);
}
}
public class Test {
public static void main (String[] args) {
Cat cat = new Cat();
}
}
结果: I am an animal
I am a cat
10.下列代码编译运行结果。
public class Person {
private String name;
public Person() {
name = “小明”;
}
public Person(String name) {
this();
System.out.println(“大家好,我是” + this.name);
}
public static void main(String[] args) {
Person person = new Person(“小红”);
}
}
结果:大家好,我是小明
11.下列代码编译运行结果。
public static void main(String[] args) {
int i= 3;
do {
if (i–%2 == 0) {
System.out.println(i);
break;
}
System.out.println(i);
} while (i> 0);
}
结果: 2
1
12.下列代码编译运行结果。
public class Vehicle {
public void run() {
System.out.println(“Vehicle is running”);
}
}
public class Car extends Vehicle {
public void run() {
System.out.println(“Car is running”);
}
public static void main(String[] args){
Vehicle vehicle = new Car();
vehicle.run();
}
}
结果:Car is running
13.下列代码编译运行结果。
double i = 25;
int j = 5;
int result = i / j;
System.out.println(result);
结果