在java中,源文件Test.java中包含如下代码段,则编译运行结果是(b).(选择一项)
public class HelloWorld{
public static void main(String[] args){
System.out.print(“Hello World!”);
}
}
a) 输出:Hello Word!
b) 编译出错,提示“共有类HelloWorld 必须在HelloWorld.java文件中定义”
c) 运行正常,但没有输出内容
d) 运行时出现异常
以下程序运行后的输出的结果是__D__。(选择一项)
for (; i < 10; i++) {
System.out.println(i);
}
A. 55
B. 0到9依次输出
C. 10
D. 编译错误
在Java程序中,对于数据类型为boolean的变量x,可以采用赋值方式是(c)(选一项)
a) x=1;
b) x= =true;
c) x=(3= =3);
d) x=(3=3);
分析下面的Java程序,编译运行结果是(A)。(选一项)
public class B{
public static void main(String[]args){
String s = "y";
System.out.println(s+1+2);
System.out.println(1+2+s);
}
}
A) 输出:y12 3y
B) 输出:y12 12y
C) 输出:y3 3y
D) 程序编译出错
以下程序运行后的输出结果是__C__。(选一项)
int x = 10, y = 20, t = 0;
if (x == y++)
t = x;
x = y;
y = t;
System.out.println(x + "," + y);
A) 10,20 B) 10,0 C) 21,0 D) 20,10
给定如下Java程序,编译运行时,将在(d)出现错误。(选一项)
public class Test{
public static void main(String[]args){
int i= 0; //a语句
for(;i<10;i++){ //b语句
if(i>5){
String test = “hello”; //c语句
}
}
System.out.println(test); //d语句
}
}
A) a B) b C)c D) d
下面( bd)段Java语句在编译时不会出现异常和错误。 (选择二项)
a) char c="a";
b) int i=-1;
c) boolean b= null;
d) double d= 10.0;
给定某Java程序员的main方法如下所示,该程序员的运行结果是(a)。(选一项)
public static void main(String[]args) {
int i= 0;
System.out.println(i++);
}
a) 输出0 b) 输出1 c) 编译错误 d) 运行时出现异常
有下面程序,语句a=a+1执行的次数是(C)。(选一项)
public class Test {
public static void main(String[ ] args){
int x=8,a=1;
do{
a=a+1;
} while (x>0);
}
}
A. 0 B. 1 C.无限次 D.有限次
在Java中,下面(ab)语句能正确通过编译。(选二项)
a) System.out.println(1+1);
b) char i= 2+’2’;
System.out.println(i);
c) String s =”on”+’one’;
d) int b = 255.0;
关于break和continue语句,下面哪些说法是错误的?(AD)(选二项)
A. break只能用于循环语句中。
B. continue只能用于循环语句中。
C. 在循环语句中,break语句将终止循环;而continue是略过当前循环中剩余的语句进入下一次循环。
D. 在循环语句中,continue语句将终止循环;而break是略过当前循环中剩余的语句进入下一次循环。
在Java代码程序中,运行以下表达式后,变量a的值是(a)。(选择一项)
int a=1+2*3%4/5;
a) 1 b) 2 c) d) 4 e) 5
在Java中,以下程序编译运行后的输出结果为(d)(选一项)
public static void main(String[] args){
int a=5;
int s=0;
switch(a){
case 5:
s=s+2;
case 3:
s=s+5;
case 8:
s=s+6;
default:
s=s+10;
break;
}
System.out.print(s);
}
a) 2 b) 0 c) 7 d) 23
给定如下代码,编译运行后,输出结果(c)。(选择一项)
public class Test{
public static void main(String args[]){
int output=10;
boolean b1=false;
if((b1==true)&&((output+=10)==20)){
System.out.println(“Equal”+output);
}else{
Sytem.out.println(“Not equal”+output);
}
}
}
a) Equal 10
b) Equal 20
c) Not equal 10
d) Not equal 20
以下程序的输出结果是__A__。(选择一项)
int i = 10, j = 10;
System.out.println(++i + ", " + j--);
A. 11,10
B. 9,10
C. 010,9
D. 10,9
&&和&的区别?
&不管前面条件是否正确,后面条件都执行,可作为位运算符
&&只要前面条件错误,后面就不执行,不可作为位运算符
&&和||的特点?
&&和||为短路与和短路或
&&:只要前面条件错就不执行后面,前面条件对,则需要满足后面条件为true,整个判断语句为true否则为false
||:只要前面条件对就不执行后面,前面条件错,则需要满足后面条件为true,整个判断语句为true否则为false
请说明switch语句中的表达式只能是什么数据类型的值?
byte char short int和String
break和continue的区别?
break是终止整个循环,不再执行循环语句,执行后续操作
continue:结束本次循环,而不终止整个循环的执行。而是结束本次循环,进行下一次循环
while,do-while,for的区别。
while循环是先判断后执行,可以不执行不执行和中间循环体
do-while循环是先执行后判断,执行次数至少一次
for循环是先执行后判断,可以不执行中间循环体
从控制台输入任意4位整数,输出该数字的各位数字之和。
public class FourSum {
public static void main(String[] args) {
System.out.println("请输入一个四位整数:");
int sum=0;
Scanner input=new Scanner(System.in);
int n=input.nextInt();
boolean flag=true;
System.out.print(n);
while(flag==true) {
if(n!=0) {
int p=n%10;
n/=10;
sum=sum+p;
}else {
flag=false;
break;
}
}
System.out.println("各位数之和为"+sum);
}
}
写出冒泡排序法代码?
public class BubbleSort {
public void Bubble(int[] arr) {
boolean flag=true;
for(int j=0;j<arr.length;j++)
for(int i=0;i<arr.length-1;i++) {
if(arr[i]>arr[i+1]) {
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
flag=false;
}
}
for(int i=0;i<arr.length;i++)
System.out.print(" "+arr[i]);
}
public static void main(String[] args) {
int a[]=new int[] {10,9,1,6,3};
new BubbleSort().Bubble(a);
}
}
九九乘法表?
public class NineMul {
public static void main(String[] args) {
for(int i=1;i<=9;i++) {
for(int j=1;j<=i;j++) {
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println();
}
}
}
由1,2,3,4四个数字能组成多少个不重复的3位数字?
public class Combination {
public int Per(int n,int m) {
int result=1;
for(int i=m;i>0;i--) {
result*=n;
n--;
}
return result;
}
}
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入数据:");
int n=sc.nextInt();
Scanner s =new Scanner(System.in);
System.out.println("请输入需要组合数据量:");
int m=sc.nextInt();
int a=new Combination().Per(n, m);
System.out.println("取三个数排列有"+a+"种组合");
}
}
求 s=2/1+3/2+5/3+8/5+……(20项)和?
public class Item_Number {
public static void main(String[] args) {
double a[]=new double[20];
double b[]=new double[20];
double sum=0;
a[0]=2;
a[1]=3;
b[0]=1;
b[1]=2;
for(int i=2;i<a.length;i++) {
a[i]=a[i-1]+a[i-2];
b[i]=b[i-1]+b[i-2];
}
for(int i=0;i<a.length;i++) {
sum+=a[i]/b[i];
}
System.out.println(sum);
}
}
定义一个存储5个数的整数数组,将此数组元素反转输出。
public class Item_Number {
public static void main(String[] args) {
int a[]=new int[5];
for(int i=0;i<a.length;i++) {
Scanner input =new Scanner(System.in);
a[i]=input.nextInt();
}
for(int i=0;i<a.length/2;i++)
{
int temp=a[a.length-i-1];
a[a.length-i-1]=a[i];
a[i]=temp;
}
for(int i=0;i<a.length;i++)
System.out.print(a[i]);
}
}
看程序说结果,请不要提前运行?
public class Test1 {
public static void main(String[] args) {
int x = 4;
int y = (--x)+(x--)+(x*10);
System.out.println("x = " + x + ",y = " + y);
}
}
答:x=2
y=26