Java工程师面试笔试题

1创建一个软链接

eg:ln -s /bin/hello.sh /usr/local/bin/hello 

在/usr/local/bin/hello下创建一个/bin/hello.sh的软链接,访问/usr/local/bin/hello即访问/bin/hello.sh

2软链接和硬链接的区别

软链接就是ln -s src dst,它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间。源文件丢失,软链接也失效
硬链接ln src dst,没有参数-s, 它会在你选定的位置上生成一个和源文件大小相同的文件
无论是软链接还是硬链接,文件都保持同步变化
3大端模式和小端模式

大端模式,是指数据的高位字节保存在内存的低地址中,而数据的低位字节保存在内存的高地址中

小端模式,是指数据的高位字节保存在内存的高地址中,而数据的低位字节保存在内存的低地址中

eg:
16bit宽的数字0x12 34 56 78在内存中的表示形式为:

1)大端模式:

低地址 -----------------> 高地址
0x12  |  0x34  |  0x56  |  0x78

2)小端模式:

低地址 ------------------> 高地址
0x78  |  0x56  |  0x34  |  0x12

内存地址小端模式存放内容大端模式存放内容
0x40000x780x12
0x40010x560x34
0x40020x340x56
0x40030x120x78
4为什么会出现大端模式和小端模式呢
在计算机系统中,我们是以字节为单位的,每个地址单元都对应着一个字节,一个字节为8bit。对于位数大于8位的处理器,那么必然存在着一个如果将多个字节安排的问题
5根据IP计算网络位、主机位、子网掩码、主机数、网络地址(网络号)、广播地址、地址范围
IPV4:4个字节,共32位

       IPV6:6个字节,共48位

       IP地址=网络位+主机位

(1)10.117.205.113/27

113=01110001

网络位:27

主机位:32-27=5

子网掩码:255.255.255.11100000=255.255.255.224

主机数=2的5次方-2(减去网络地址和广播地址)

网络地址=10.117.205.01100000=10.117.205.96

广播地址=10.117.205.01111111=10.117.205.127

地址范围=10.117.205.97-10.117.205.126

(2)ip=10.158.79.53,子网掩码=255.255.248.0,求网络位、主机位、主机数、网络地址、广播地址

248=11111000

79=01001111

网络位=21

主机位=11

主机数=2的11次方-2

网络地址=10.158.01001000.0=10.158.72.0

广播地址=10.158.01001111.255=10.158.79.255

(3)子网掩码为255.255.248.0,求主机数

248=11111000

主机数=2的11次方-2

6关于Java的静态有哪些?

静态类、静态变量、静态方法、静态代码块

(1)静态变量:由static修饰,在JVM中,静态变量的加载顺序在对象之前,因此静态变量不依附于对象存在,可以在不实例化类的情况下直接使用静态变量,如下代码所示。

public class StaticTest {
    static int a = 13;
    int b = 14;

    public static void main(String[] args) {
        int c = StaticTest.a;
        System.out.println(c);
    }
}

静态变量属于类,不属于类中任何一个对象,因此静态变量又叫做类变量,一个类不管创建多少个对象(对象是类的一个实例),静态变量在内存中有且仅有一个。

(2)实例变量:必须依附于对象存在,只有实例化类后才可以使用此类中的实例变量。

public class StaticTest {
    static int a = 13;
    int b = 14;

    public static void main(String[] args) {
        int d = new StaticTest().b;
        System.out.println(d);
    }
}

(3)静态方法:方法用static关键字修饰,静态方法与静态成员变量一样,属于类本身,在类装载的时候被装载到内存,不自动进行销毁,会一直存在于内存中,直到JVM关闭。使用时也是不需要实例化类,能够直接使用。静态方法无法被重写。

public class StaticTest {
    public static void MyStatic(){
        System.out.println("这是StaticTest的一个静态方法");
    }

    public static void main(String[] args) {
        StaticTest.MyStatic();
    }
}

需要注意的是:在静态方法中只能访问类中的静态成员跟静态方法,不能直接访问类中的实例变量跟实例方法,原因是静态方法在JVM中的加载顺序也在对象之前,直接使用实例变量跟实例方法的话,可能实例变量跟实例方法所依附的对象并没有被创建,会导致无法找到所使用的实例变量跟实例方法。 

(4).实例化方法:属于实例对象,实例化后才会分配内存,必须通过类的实例来引用。不会常驻内存,当实例对象被JVM 回收之后,也跟着消失。

public class StaticTest {

    public void MyMethod(){
        System.out.println("这是StaticTest的一个实例方法");
    }

    public static void main(String[] args) {
        new StaticTest().MyMethod();
    }
}

附加: 
1.线程安全:静态方法静态变量是线程不安全的。非静态方法非静态变量是线程安全的。 
2.静态方法静态方法是类加载时一起加载到JVM中,是常驻内存直到JVM关闭。静态变量方法若在系统中定义太多,会占用大量的资源,最后造成内存溢出,所以静态方法不能滥用。

一道Java笔试题:静态语句块、构造语句块和构造函数的执行顺序
  1. public class HelloA {  
  2.     //构造方法  
  3.     public HelloA(){  
  4.         System.out.println("Hello A!父类构造方法");  
  5.     }  
  6.     //非静态代码块  
  7.     {  
  8.         System.out.println("i'm A class.父类非静态代码块");  
  9.     }  
  10.     //静态代码块  
  11.     static{  
  12.         System.out.println("static A 父类静态代码块");  
  13.     }  
  14. }  
[java]  view plain  copy
  1. public class HelloB extends HelloA {  
  2.     //构造方法  
  3.     public HelloB(){  
  4.         System.out.println("Hello B! 构造方法");  
  5.     }  
  6.     //非静态代码块  
  7.     {  
  8.         System.out.println("i'm B class.非静态代码块");  
  9.     }  
  10.     //静态代码块  
  11.     static{  
  12.         System.out.println("static B 静态代码块");  
  13.     }     
  14.     public static void main(String[] args) {  
  15.         System.out.println("---start---");  
  16.         new HelloB();  
  17.         new HelloB();  
  18.         System.out.println("---end---");  
  19.     }  
  20. }  
结果为:
[java]  view plain  copy
  1. static A 父类静态代码块  
  2. static B 静态代码块  
  3. ---start---  
  4. i'm A class.父类非静态代码块  
  5. HelloA!父类构造方法  
  6. i'm B class.非静态代码块  
  7. HelloB! 构造方法  
  8. i'm A class.父类非静态代码块  
  9. HelloA!父类构造方法  
  10. i'm B class.非静态代码块  
  11. HelloB! 构造方法  
  12. ---end---  
总结就是,静态代码块在非静态代码块之前执行(静态代码块—>非静态代码块—>构造方法)。静态代码块只在第一次new执行一次,之后不再执行,而非静态代码块在每new一次就执行一次。
例如:
  1. public class SuperAndSub {  
  2.   
  3.     public static void main(String[] args) {  
  4.         Sub s3 = new Sub();  
  5.     }  
  6. }  
  7.   
  8. class Super {  
  9.   
  10.     static int a = getA();  
  11.   
  12.     static {  
  13.         System.out.println("加载Super的静态块");  
  14.     }  
  15.   
  16.     int b = getB();  
  17.   
  18.     {  
  19.         System.out.println("加载Super的普通块");  
  20.     }  
  21.   
  22.     Super() {  
  23.         System.out.println("加载Super的构造器");  
  24.     }  
  25.   
  26.     static int getA() {  
  27.         System.out.println("加载Super的静态变量");  
  28.         return 1;  
  29.     }  
  30.   
  31.     static int getB() {  
  32.         System.out.println("加载Super的实例变量");  
  33.         return 2;  
  34.     }  
  35.   
  36. }  
  37.   
  38. class Sub extends Super {  
  39.   
  40.     static int c = getC();  
  41.   
  42.     static {  
  43.         System.out.println("加载Sub的静态块");  
  44.     }  
  45.   
  46.     int d = getD();  
  47.   
  48.     {  
  49.         System.out.println("加载Sub的普通块");  
  50.     }  
  51.   
  52.     Sub() {  
  53.         System.out.println("加载Sub的构造器");  
  54.     }  
  55.   
  56.     static int getC() {  
  57.         System.out.println("加载Sub的静态变量");  
  58.         return 1;  
  59.     }  
  60.   
  61.     static int getD() {  
  62.         System.out.println("加载Sub的实例变量");  
  63.         return 2;  
  64.     }  
  65. }  

输出:

[html]  view plain  copy
  1. 加载Super的静态变量  
  2. 加载Super的静态块  
  3. 加载Sub的静态变量  
  4. 加载Sub的静态块  
  5. 加载Super的实例变量  
  6. 加载Super的普通块  
  7. 加载Super的构造器  
  8. 加载Sub的实例变量  
  9. 加载Sub的普通块  
  10. 加载Sub的构造器  

1.父类代码

 1 package com.hafiz.zhang;
 2 
 3 public class Fu
 4 {
 5     private int i = print("this is father common variable");
 6     private static int j = print("this is father static variable");
 7     static{
 8         System.out.println("this is father static code block");
 9     }
10     {
11         System.out.println("this is father common code block");
12     }
13     public Fu(){
14         System.out.println("this is father constructor");
15     }
16 
17     static int print(String str){
18         System.out.println(str);
19         return 2;
20     }
21 }

2.子类代码

 1 package com.hafiz.zhang;
 2 
 3 public class Zi extends Fu
 4 {
 5     private int l = print("this is son common variable");
 6     private static int m = print("this is son stati variable");
 7     static{
 8         System.out.println("this is son static code block");
 9     }
10     {
11         System.out.println("this is son common code block");
12     }
13     public Zi(){
14         System.out.println("this is son constructor");
15     }
16     public static void main(String[] args) {
17         new Zi();    
18     }
19 }

最后运行结果为:

下面让我们修改一下两个类中静态代码块和静态成员变量的位置并重新运行

3.修改后的父类代码

 1 package com.hafiz.zhang;
 2 
 3 public class Fu
 4 {
 5     static{
 6         System.out.println("this is father static code block");
 7     }
 8     {
 9         System.out.println("this is father common code block");
10     }
11     public Fu(){
12         System.out.println("this is father constructor");
13     }
14 
15     static int print(String str){
16         System.out.println(str);
17         return 2;
18     }
19     private int i = print("this is father common variable");
20     private static int j = print("this is father static variable");
21 }

4.修改后的子类代码

 1 package com.hafiz.zhang;
 2 
 3 public class Zi extends Fu
 4 {
 5     static{
 6         System.out.println("this is son static code block");
 7     }
 8     {
 9         System.out.println("this is son common code block");
10     }
11     public Zi(){
12         System.out.println("this is son constructor");
13     }
14     public static void main(String[] args) {
15         new Zi();    
16     }
17     private int l = print("this is son common variable");
18     private static int m = print("this is son stati variable");
19 }

修改后的运行结果:

最后得出类加载顺序为:先按照声明顺序初始化基类静态变量和静态代码块,接着按照声明顺序初始化子类静态变量和静态代码块,而后按照声明顺序初始化基类普通变量和普通代码块,然后执行基类构造函数,接着按照声明顺序初始化子类普通变量和普通代码块,最后执行子类构造函数。
7Java的集合类型有哪些?

List:有序可重复,有ArrayList、Vector、LinkedList

Map:键值对 key/value,有HashMap、HashTable、CocurrentHashMap

Set:无序不可重复,有HashSet、LinkedHashSet、TreeSet

8String类的方法有哪些?

代码:


结果如下:


9Oracle的递归查询

  1. create table tb_menu(  
  2.     id number(10) not null--主键id  
  3.     title varchar2(50), --标题  
  4.     parent number(10) --parent id  
  1. insert into tb_menu(id, title, parent) values(1, '父菜单1',null);  
  2. insert into tb_menu(id, title, parent) values(2, '父菜单2',null); 
  1. insert into tb_menu(id, title, parent) values(6, '一级菜单6',1);  
  2. insert into tb_menu(id, title, parent) values(7, '一级菜单7',1);  
  3. insert into tb_menu(id, title, parent) values(8, '一级菜单8',1);  
  4. insert into tb_menu(id, title, parent) values(9, '一级菜单9',2); 
  1. insert into tb_menu(id, title, parent) values(10, '二级菜单21',6);  
  2. insert into tb_menu(id, title, parent) values(11, '二级菜单22',6); 

1)、查找树中的所有顶级父节点(辈份最长的人)

select * from tb_menu m where m.parent is null

2)、查找一个节点的直属子节点(所有儿子)

select * from tb_menu m where m.parent=1;  

3)、查找一个节点的所有直属子节点(所有后代)。

实在难以理解,可以这样记忆,prior放在哪里,就找谁,这里放在id,就是找的后代。  

select * from tb_menu m start with m.id=1 connect by m.parent=prior m.id;  

4)、查找一个节点的直属父节点(父亲)

select c.id, c.title, p.id parent_id, p.title parent_title from tb_menu c, tb_menu p where c.parent=p.id and c.id=6 

5)、查找一个节点的所有直属父节点(祖宗)。

select * from tb_menu m start with m.id=38 connect by prior m.parent=m.id; 

10java 中有可能出现 i + 1 < i 的情况吗?为什么

存在的,i+1<i,假设i是int类型,那么当i=2^32-1时,i+1数据溢出变成负数,就达成了条件i+1<i,其他类型同理

11Java中i++和++i的区别

(1)

int i=1,a=0;

System.out.println("a=i++===> "+(a=i++));//1 a=i i++

//System.out.println("a=++i===> "+(a=++i));//2 i++ a=i

(2)

int y=0; //注意"="是赋值,"=="才是相等

y=++y; //y==0,++y==y+1; 结果y=++y == y+1 == 0+1 ==1

y=++y; //y==1,++y==y+1; 结果y=++y == y+1 == 1+1 ==2

y=++y; //y==2,++y==y+1; 结果y=++y == y+1 == 2+1 ==3

System.out.println("y="+y); //3

int i =0; //i==0

i=i++; //i=i++;的操作可能相当于以下三步操作:①把变量i的值取出来,放在一个临时变量temp里;②把变量i的值进行自加操作;③把temp的值赋值给i

i=i++;

i=i++;

System.out.println("i="+i); //0

12oracle的行转列

 

实现效果如下:

 

  1. select t.user_name as 姓名,  
  2.     MAX(decode(t.course,'语文',score,null)) as 语文,  
  3.     MAX(decode(t.course,'数学',score,null)) as 数学,  
  4.     MAX(decode(t.course,'英语',score,null)) as 英语  
  5. from RowToCol t group by t.user_name  order by t.user_name  
decode函数:decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

13Spring的注解有哪些?

@Controller @Service @Autowired @RequestMapping @RequestParam @PathVariable @ModelAttribute @ResponseBody

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值