1.下列说法正确的有()
A.class中的constructor不可省略
B.constructor必须与class同名,但方法不能与class同名
C.constructor在一个对象被new时执行
D.一个class只能定义一个constructor
解析:
A:无参构造器是可以省略的
B:constructor不一定与class同名,因为存在内部类的原因
D:方法的重载
2.下列代码的输出结果是()
public class Test{
public static void main(){
People ab = new Child();
}
}
class People{
static{
System.out.println("1");
}
{
System.out.println("2");
}
public People(){
System.out.println("3")
}
}
class Child extends People{
static{
System.out.println("a");
}
{
System.out.println("b");
}
public People(){
System.out.println("c")
}
}
A:cab312 B:312cab C:a123bc D:1a23bc
解析:首先入口是main()方法,运用了多态,既然涉及到了多态那么就涉及到了
实例化对象时候顺序的问题,先执行父类的方法,在执行子类的方法,其中静态代
码块是最先初始化的,它是在类加载到内存的时候就可以执行的,其次是代码块
所以,通过前面的分析我们可以知道前面两步的执行顺序为:1a,之后再次进行
分析静态代码块之后是代码块
3.下面代码输出结果()
public static void main(){
Integer a = new Integer(3);
Integer b = 3;
int c = 3;
System.out.println(a == b);
System.out.println(a == c);
}
A:true true B:true false C:false true D:false false
分析:Integer是一个对象,int是8个基本数据类型里面的也就是常量
第一条语句:生成了一个对象
第二条语句:Integer b = 3 其实在底层调用了Integer.valueOf()方法
其中Integer.valueOf()返回一个Integer对象
且==就是比较的是地址 所以第一条为 false
第三条语句:int c = 3;在与Integer a = 3;相比较的时候会自动拆箱
比较的是数值,所以返回true
所以选择C
4.在第三行中生成的object在第几行执行后会成为garbage collection的对象
1.public class MyClass{
2. public StringBuffer aMethod(){
3. StringBuffer sf = new StringBuffer("Hello");
4. StringBuffer[] sf_arr = new StringBuffer[1];
5. sf_arr[0] = sf;
6. sf = null;
7. sf_arr[0] = null;
8. return sf;
9. }
}
A: 第五行 B:第六行 C:第七行 D:第八行
分析:被垃圾回收机制回收的对象存在以下的特点:
1.没有被其他对象所引用
2.当前对象为null
所以正确答案:C
5.下面程序的输出结果是()
public static void main(String[] args){
Thread t = new Thread(){
public void run(){
pong();
}
};
t.run();
System.out.println("ping");
}
Static void pong(){
System.out.println("pong");
}
A:pingpang B:pongping C:pingpang和pangping都用可能 D:d都不输出
分析:顺序执行没有什么好说的B
解答题
1.整型数的反转.例如输入123,返回321,输入 -1234返回-4321超过最大值返回-1
public class InverseString {
public static String InverseMethod(String strValue){
char[] charArrayofString = strValue.toCharArray();
String strInverse;
String charBuffer = new String();
for(int i = charArrayofString.length - 1; i >= 0; i--){
strInverse = String.valueOf(charArrayofString[i]);
charBuffer +=strInverse;
}
return charBuffer;
}
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int integerValue = cin.nextInt();
String inverseStringValue = new String();
if(integerValue > 0){
String stringValue = integerValue + "";
inverseStringValue = InverseMethod(stringValue);
}else if(integerValue < 0){
integerValue = Math.abs(integerValue);
String stringValue = integerValue + "";
inverseStringValue = InverseMethod(stringValue);
inverseStringValue = "-"+inverseStringValue;
}
System.out.println("结束:" + inverseStringValue);
}
}
2.有两张表
学生基本信息表student_info(student_no,name,age,sex)
学生成绩表score_info(student_no,subject,score)
问题:请用sql查询出没有挂科的学生的姓名以及该学生的平均成绩
分析:完整解答过程如下
CREATE TABLE student_info(
student_no INT PRIMARY KEY,
student_name VARCHAR(20),
student_age INT,
student_sex VARCHAR(20)
)
COMMIT;
CREATE TABLE score_info(
student_no INT,
score_subject VARCHAR(20),
score_score INT
)
COMMIT;
DROP TABLE score_info;
DESC student_info;
DESC score_info;
INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1001,'baidu',18,'male');
INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1002,'TX',19,'male');
INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1003,'ali',20,'female');
INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1004,'google',21,'female');
INSERT INTO student_info(student_no,student_name,student_age,student_sex) VALUES(1005,'windows',22,'male');
COMMIT;
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'java',80);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'php',81);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'ui',82);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1001,'python',83);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'java',59);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'php',58);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'ui',61);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1002,'python',53);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'java',61);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'php',62);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'ui',50);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1003,'python',53);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'java',80);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'php',90);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'ui',100);
INSERT INTO score_info(student_no,score_subject,score_score) VALUES(1004,'python',99);
COMMIT;
-- 嵌套子查询
SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st WHERE st.`student_no` = so.`student_no` GROUP BY so.`student_no` HAVING AVG(so.`score_score`) > 60;
SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st
-- 一会再用左联接的方式进行查询
SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so
LEFT JOIN student_info st ON so.`student_no`=st.`student_no`
GROUP BY so.`student_no`
HAVING AVG(so.`score_score`) > 60;
-- 只要是有一科不及格就GG的
-- 请用sql查询出没有挂科的学生的姓名
-- 以及该学生的平均成绩
SELECT * FROM student_info st WHERE st.`student_no` NOT IN (SELECT so.`student_no` FROM score_info so WHERE so.`score_score` < 60);
-- 嵌套子查询
SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st WHERE st.`student_no` = so.`student_no` GROUP BY so.`student_no` HAVING AVG(so.`score_score`) > 60;
SELECT st.`student_name`,AVG(so.`score_score`) FROM score_info so,student_info st WHERE st.`student_no` NOT IN (SELECT so.`student_no` FROM score_info so WHERE so.`score_score` < 60) AND so.`student_no`=st.`student_no` GROUP BY so.`student_no`;