初级java笔试题
1.public static void main(String[] args){
short s1 = 1;
? ? ? ? s1 = s1 + 1;
System.out.println(s1);
? }。判断是否错误?
?
s1 + 1 运算结果是int型,赋值给s1的时候需要强制转换类型。
?
?
public static void main(String[] args) {
short s1 = 1;
s1 += 1;
System.out.println(s1);
? }。判断是否错误?
?可以正确编译并与运行。
?
2.在web应用开发过程中经常遇到某种编码的字符,如ISO-8859-1等,如何输出一个某种编码的字符串?
?
public String output(String s){
String str = "";
str = new String(s.getBytes("ISO-8859-1"),"GBK");
str = str.trim();
?
return str;
}
?
?
代码查错
?
3.abstract class Test{
private String name;
public abstract boolean ttt(String name){}
? ?}
错,抽象方法必须以分号结尾,且不带花括号4.public class Test{void ttt(){private String s = "";int i = s.length();}? }
错,局部变量前不能有修饰词,final可以用来修饰局部变量
?
5.abstract class Test{
private abstract String ttt();??
}
错,抽象方法不能是私有的
?
6.public class Test{
public int ttt(final int i){
return ++x;
}
}
错,i被修饰为final,则表示i不能被修改。
?
7.public class Test{
public static void main(String args[]){
Inner inner = new Inner();
?
new Test.add(inner);
}
?
public void add(final Inner inner){
inner.i++;
}
}
class Inner{
?
public int i;
}
对,final修饰的是Inner对象,修改的是这个对象的成员变量,这个对象并没有被修改
?
?
8.class Test{
int i;
public void ttt(){
System.out.println("i="+i);
}
}
对,成员变量初始值为0.
?
9.class Test{
final int i;
public void ttt(){
System.out.println("i+"+i);
}
}
错,i是final型的成员变量,final的成员变量没有默认值,修改为final int i= 0即正确
?
10.public class Test{
public static void main(String args[]){
Test t = new Test();
System.out.println(ttt());
}
?
public String ttt(){
return "ttt..."
}
}
错,静态的方法不能直接电泳非静态的方法
?
11.class Something {
private static void main(String[] something_to_do) {?
System.out.println("ttt");
}
}
正确
?
12.interface A{
int x = 0;
}
?
class B{
int x = 1;
}
?
interface C extends B implements A{
public void pX(){
System.out.println(x);
}
?
public static void main(String args[]){
new C().pX();
}
}
错误,未明确指明x的调用。
?
13.interface A {
void play();
}
interface B{
void play();
}
interface C extends A, B{
Ball ball = new Ball("PingPang");
}
class Ball implements C{
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;?
}
public void play() {
ball = new Ball("123");
System.out.println(ball.getName());
}
}
错,因为接口中的变量,默认加上了public static final 关键字,表示不可改变的,在Ball类继承了以后,程序中试图改变ball对象,所以会出错。