JAVA 题 笔试题
问答题
1 下列代码的输出结果是:
public class StringTest1 {
public static void modify(String value) {
value.toUpperCase();
value += "3.0";
System.out.println(value);
}
public static void main(String[] args) {
String value = new String("SequoiaDB");
StringTest1.modify(value);
System.out.println(value);
}
}
2 下列代码的输出结果是:
public class StringTest2 {
public static void modify(StringBuilder value) {
value.append("3.0");
System.out.println(value);
}
public static void main(String[] args) {
StringBuilder value = new StringBuilder("SequoiaDB");
StringTest2.modify(value);
System.out.println(value);
}
}
3 下列代码的输出结果是:
public class ThreadTest1{
public static void main(String args[]) {
Thread t = new Thread() {
@Override
public void run() {
pong();
}
};
t.run();
System.out.print("ping");
}
static void pong() {
System.out.print("pong");
}
}
A pingpong B pongping c pingpong 和 pongping 都有可能
4 下列代码的输出结果是:
class BaseExceptionextends Exception{}
class MyException extends BaseException{}
public class ExceptionTest1 {
public static void main(String[] args) throws Exception {
try {
try {
throw new MyException();
}
catch (MyException e) {
System.out.println("Caught MyException1");
BaseException newException = (BaseException)e;
throw newException;
}
}
catch (MyException e) {
System.out.println("Caught MyException2");
return;
}
catch (BaseException e) {
System.out.println("Caught BaseException3");
throw e;
}
finally {
System.out.println("finally");
}
}
}
5 下列代码的输出结果是:
class MyKey {
private int key;
public MyKey(int key) {
this.key = key;
}
}
public class MapTest1 {
public static void main(String[] args) {
Map<MyKey, Integer> myMap = new HashMap();
myMap.put(new MyKey(1), 1);
myMap.put(new MyKey(2), 2);
Integer value = myMap.get(new MyKey(1));
System.out.println("value=" + value);
}
}
6 下列代码的输出结果是:
class HelloA {
public HelloA() {
System.out.println("HelloA");
}
{ System.out.println("I'm A class");}
static { System.out.println("static A"); }
}
class HelloB extends HelloA {
public HelloB() {
System.out.println("HelloB");
}
{ System.out.println("I'm B class");}
static { System.out.println("static B"); }
}
public class ConstructTest1 {
public static void main(String[] args) {
new HelloB();
}
}
7 请描述下面这个二叉树的后序遍历结果
8 给定一组数据10,2,99,4,13,8,1,7,经过快速排序第一趟排序之后,其新的序
列为:(假定选择10 作为第一轮快排的 key,从小到大排序):
9 有int 型变量 a 和 b,在不引入新的变量情况下,实现变量 a 和 b 的交换
10分别画出适配器模式和代理模式的UML 图
11下列代码输出结果是:
public class TestGetValue {
public static int getValue(int i) {
int result = 0;
switch (i) {
case1:
result = result + i;
case2:
result = result + i * 2;
case3:
result = result + i * 3;
}
return result;
}
public static void main(String[] args) {
System.out.println(getValue(2));
}
}
判断题(说明原因)
1 下列代码是否有错误?如果有错误请指出位置并说明原因
public class Something
{
void doSomething ()
{
private String s = "";
int l = s.length();
}
}
2 下列代码是否有错误?如果有错误请指出位置并说明原因
public class Something
{
public static void main(String[] args)
{
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o)
{
o.i++;
}
}
class Other
{
public int i;
}
3 下列代码是否有错误?如果有错误请指出位置并说明原因
public class Something
{
public static void main(String[] args)
{
Something s = new Something();
System.out.println("toString() returns " +toString());
}
public String toString()
{
return "Do something ...";
}
}
4 下列代码是否有错误?如果有错误请指出位置并说明原因
interfacePlayable {
int playtime = 0;
void play();
}
class Footballimplements Playable{
public void setPlayTime(int playtime) {
this.playtime = playtime;
}
public void play() {
System.out.println("play football");
}
}
编程题
1 编码实现一个单例模式的代码,要求:延迟加载,线程安全,高性能
2 请设计并编写一个函数,能够把给定输入的 N 个整数按照从小到大的顺序进
行排列,要求不能使用标准库排序函数及第三方排序函数。如给定:
1,8,5,10,90,0,排序后为:0,1,5,8,10,90。
加分 题
1 有7克、2 克砝码各一个,天平一只,如何只用这些物品三次将 140 克的盐
分成50、90 克各一份?(需要给出具体操作步骤)
2 一普查员问一女人,“你有多少个孩子,他们多少岁?”
女人回答:“我有三个孩子,他们的岁数相乘是 36,岁数相加就等于旁边屋
的门牌号码。“普查员立刻走到旁边屋,看了一看,回来说:“我还需要多少
资料。”女人回答:“我现在很忙,我最大的孩子正在楼上睡觉。”普查员说:”
谢谢,我己知道了。”
问题:那三个孩子的岁数分别是多少?(需要给出逻辑推理过程)
==============================================================================================
答案
问答题
1、
SequoiaDB3.0
SequoiaDB
2、
SequoiaDB3.0
SequoiaDB3.0
3、
选A pongping
4、
Caught MyException1
Caught MyException2
finally
5、
value=null
6、
static A
static B
I'm A class
HelloA
I'm B class
HelloB
7、
后序遍历
DBGEHIFCA
8、
第一趟排序后
7, 2, 1, 4, 8, 10, 13, 99
9、
ab交换
a=a+b;
b=a-b; //(a+b)和-b==原a
a=a-b; //(a+b)和-b === b是原a
10、
UML图,是单独照片
11、
结果 10
===========================
判断题(说明原因)
================
1、
错误:private String s = "";
原因:
方法中不能使用private修饰变量
2、
错误:没发现错误,,,
原因:其中addOne(final Other o)有点特殊, final修饰的是 o,传递的是对象的引用,能修改i的值,其中int型默认值是0,所以修改后i的值为1
3、
错误:调用toString()方法有问题
原因:主方法是静态的,toString是非静态的,不能在静态方法中调用非静态方法,因为静态方法是属于类的,且在非静态方法之前执行。
4、
错误:
this.playtime = playtime;
原因:在子类中不能改变父类的变量的值
=================
编程题
1、单例
===========================
package com.demo;
/**
* @Desc 累行客
* 双重检测锁模式的-懒汉式单例(DCL懒汉式,Double Check Lock)
* @Author luolei
* @Web http://www.leixingke.com/
* @Date 2020/11/17 15:35
*/
public class LazyMan {
//构造器私
private LazyMan(){
System.out.println(Thread.currentThread().getName());
}
//初始为null
private volatile static LazyMan lazyMan;
//当调用的时候才创建,所以懒
public static LazyMan getInstance(){
//最外层的判断为了提高效率。等待锁很费时
if(lazyMan == null){
synchronized (LazyMan.class){
if(lazyMan == null) {
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(()->{
System.out.println(LazyMan.getInstance());
}).start();
}
}
}
===========================
2、目的是排序==用快速排序
===========================
package com.demo;
import java.util.Arrays;
/**
* @Desc 累行客
* 快速排序
* @Author luolei
* @Web http://www.leixingke.com/
* @Date 2020/11/17 15:46
*/
public class QuickSortDemo {
public static void main(String[] args) {
/* 待排序数组 */
int a[] = new int[]{1,8,5,10,90,0};
QuickSort(a);
System.out.println(Arrays.toString(a));
}
private static void QuickSort(int[] a) {
QuickS(a, 0, a.length-1);
}
private static void QuickS(int[] a, int low, int high) {
if(low<high){
int ploc = Partition(a, low, high); //System.out.println(Arrays.toString(a));
/* 左右子表继续Partition */
QuickS(a, low, ploc-1);
QuickS(a, ploc+1, high);
}
}
/**
* @Desc: 快排核心
* @param a: 待排序数组
* @param low:
* @param high:
* @return: int 返回枢轴插入的位置
*/
private static int Partition(int[] a, int low, int high) {
/*枢轴*/
int pivotkey = a[low];
while(low<high){
while (low<high && a[high]>=pivotkey) high--;
a[low] = a[high];
while (low<high && a[low]<=pivotkey) low++;
a[high] = a[low];
}
a[low]=pivotkey;
return low;
}
}
===========================
加分题
===========================
1、
思路
要求:用7g和2g和天平三次
关键:盐平均分不需要称。
140/2=70
70/2=35
(20 + 2) = (15 + 7) = 22 平了
35 + 2 + 7 = (20 + 2) + (15 + 7);
20+70=90
35+15=50
===========================