1、 下面的程序 编译运行后,在屏幕上显示的结果是(A 0)
public class test {
public static void main(String args[]) {
int x,y;
x=5>>2;
y=x>>>2;
System.out.println(y);
}
}
A 0 B 2 C 5 D 80
几种移位运算符(运行结果都是按16进制打印的):
(1)左移 <<: 左侧位不要了, 右侧补 0
(2)右移 >>: 右侧位不要了, 左侧补符号位(正数补0, 负数补1)
(3)无符号右移 >>>: 右侧位不要了, 左侧补 0.
2、以下代码结果是什么?(C 代码可以编译运行,输出“AB.B”)
public class foo {
public static void main(String sgf[]) {
StringBuffer a=new StringBuffer(“A”);
StringBuffer b=new StringBuffer(“B”);
operate(a,b);
System.out.println(a+”.”+b);
}
static void operate(StringBuffer x,StringBuffer y) {
x.append(y);
y=x;
}
}
A 代码可以编译运行,输出“AB.AB”。
B 代码可以编译运行,输出“A.A”。
C 代码可以编译运行,输出“AB.B”。
D 代码可以编译运行,输出“A.B”。
3、在JAVA中,假设A有构造方法A(int a),则在类A的其他构造方法中调用该构造方法和语句格式应该为(B this(x))
A this.A(x) B this(x) C super(x) D A(x)
4、下面代码的运行结果是(C 由于String s没有初始化,代码不能编译通过)
public static void main(String[] args){
String s;
System.out.println("s="+s);
}
A 代码编程成功,并输出”s=”
B 代码编译成功,并输出”s=null”
C 由于String s没有初始化,代码不能编译通过。
D 代码编译成功,但捕获到NullPointException异常
5、装箱、拆箱操作发生在: (C 引用类型与值类型之间)
A 类与对象之间
B 对象与对象之间
C 引用类型与值类型之间
D 引用类型与引用类型之间
6、一个以”.java”为后缀的源文件(C 只能有一个与文件名相同的public类,可以包含其他类)
A 只能包含一个类,类名必须与文件名相同
B 只能包含与文件名相同的类以及其中的内部类
C 只能有一个与文件名相同的public类,可以包含其他类
D 可以包含任意类
7、下列哪个说法是正确的(D SimpleDateFormat是线程不安全的)
A ConcurrentHashMap使用synchronized关键字保证线程安全
(ConcurrentHashMap使用segment来分段和管理锁,segment继承自ReentrantLock,因此ConcurrentHashMap使用ReentrantLock来保证线程安全)
B HashMap实现了Collction接口(实现了Map接口)
C Array.asList方法返回java.util.ArrayList对象(asList返回的是Arrays下的ArrayList 并不是Util下的ArrayList)
D SimpleDateFormat是线程不安全的
8、指出以下程序运行的结果是(B good and gbc)
public class Example{
String str=new String("good");
char[]ch={'a','b','c'};
public static void main(String args[]){
Example ex=new Example();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and ");
System.out.print(ex.ch);
}
public void change(String str,char ch[]){
//引用类型变量,传递的是地址,属于引用传递。
str="test ok";
ch[0]='g';
}
}
A good and abc
B good and gbc
C test ok and abc
D test ok and gbc
9、标题:用两个栈实现队列 | 时间限制:1秒 | 内存限制:32768K | 语言限制: [Javascript_V8, Python, C++, Javascript, Php, C#, Java]
【题目描述】
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
【思路】
(1)队列的特点:先进先出;栈的特点:后进先出
(2)定义两个栈A,B,分别用于入队和出队
(3)入队
①用栈A入队
②当栈B为空时,元素直接入队,即入栈A;
③当栈B不为空时,将B中的所有元素放到A中,再将元素入队
(4)出队
①用栈B出队
②若队为空直接返回null(A,B均为空,整个队列才为空)
③将A中所有元素放到B中,此时B的栈顶元素就是第一个元素,将该元素出队
【代码】
import java.util.Stack;
public class Solution {
Stack<Integer> A = new Stack<Integer>();
Stack<Integer> B = new Stack<Integer>();
public void push(int x) {
while (!B.isEmpty()) {
int t = B.pop();
A.push(t);
}
A.push(x);
}
public Integer pop() {
if(empty()){
return null;
}
while(!A.isEmpty()){
int t = A.pop();
B.push(t);
}
return B.pop();
}
public boolean empty(){
return A.isEmpty()&&B.isEmpty();
}
}