首页 Java java教程 这十道10道Java题你会做吗?考考你的水平

这十道10道Java题你会做吗?考考你的水平

Jul 30, 2018 am 11:04 AM

/**

 *这10道题都是我做过的,做错或者觉得需要复习的有价值的,整理出来也考考大家
 *有更好的建议欢迎提出来
 */

1.代码输出结果是

public class Test {  
    public static void main(String [] args){  
        System.out.println(new B().getValue());  
    }  
    static class A{  
        protected int value;  
        public A(int v) {  
            setValue(v);  
        }  
        public void setValue(int value){  
            this.value = value;  
        }  
        public int getValue(){  
            try{  
                value++;  
                return value;  
            } catch(Exception e){  
                System.out.println(e.toString());  
            } finally {  
                this.setValue(value);  
                System.out.println(value);  
            }  
            return value;  
        }  
    }  
    static class B extends A{  
        public B() {  
            super(5);  
            setValue(getValue() - 3);  
        }  
        public void setValue(int value){  
            super.setValue(2 * value);  
        }  
    }  
}
登录后复制

A.11 17 34
B.22 74 74
C.6 7 7
D.22 34 17

2.下面的程序 编译运行后,在屏幕上显示的结果是()

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

3.有如下代码:请写出程序的输出结果。

public class Test
{
    public static void main(String[] args)
    {
        int x = 0;
        int y = 0;
        int k = 0;
        for (int z = 0; z < 5; z++) {
            if ((++x > 2) && (++y > 2) && (k++ > 2))
            {
                x++;
                ++y;
                k++;
            }
        }
        System.out.println(x + ”” +y + ”” +k);
    }
}
登录后复制

A.432
B.531
C.421
D.523

4.以下代码结果是什么?

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”。

5.给出以下代码,请给出结果. 0608

class Two{
    Byte x;
}
class PassO{
    public static void main(String[] args){
        PassO p=new PassO();
        p.start();
    }
    void start(){
        Two t=new Two();
        System.out.print(t.x+””);
        Two t2=fix(t);
        System.out.print(t.x+” ” +t2.x);
    }
    Two fix(Two tt){
        tt.x=42;
        return tt;
    }
}
登录后复制

A.null null 42
B.null 42 42
C.0 0 42
D.0 42 42

6. 下列代码片段中,存在编译错误的语句是()

byte b1=1,b2=2,b3,b6,b8;
final byte b4=4,b5=6,b7;
b3=(b1+b2);  /*语句1*/
b6=b4+b5;    /*语句2*/
b8=(b1+b4);  /*语句3*/
b7=(b2+b5);  /*语句4*/
System.out.println(b3+b6);
登录后复制

A.语句2
B.语句1
C.语句3
D.语句4

7.What will be printed when you execute the following code? 0611

class C {
    C() {
        System.out.print("C");
    }
}
class A {
    C c = new C();
    A() {
        this("A");
        System.out.print("A");
    } 
    A(String s) {
        System.out.print(s);
    }
}
class Test extends A {
    Test() {
        super("B");
        System.out.print("B");
    }
    public static void main(String[] args) {
        new Test();
    }
}
登录后复制

A.BB
B.CBB
C.BAB
D.None of the above

8.以下代码执行后输出结果为( )0611

public class ClassTest{
     String str = new String("hello");
     char[] ch = {&#39;a&#39;,&#39;b&#39;,&#39;c&#39;};
     public void fun(String str, char ch[]){
     str="world";
     ch[0]=&#39;d&#39;;
 }
 public static void main(String[] args) {
     ClassTest test1 = new ClassTest();
     test1.fun(test1.str,test1.ch);
     System.out.print(test1.str + " and ");
     System.out.print(test1.ch);
     }
 }
登录后复制

A.hello and dbc
B.world and abc
C.hello and abc
D.world and dbc

9.以下代码将打印出

 public static void main (String[] args) { 
    String classFile = "com.jd.". replaceAll(".", "/") + "MyClass.class";
    System.out.println(classFile);
}
登录后复制

A.com. jd
B.com/jd/MyClass.class
C.///////MyClass.class
D.com.jd.MyClass

10.对于如下代码段

class A{
    public A foo(){return this;}
}
class B extends A{
    public A foo(){
        return this;
    }
}
class C extends B
{
    _______
}
登录后复制

可以放入到横线位置,使程序正确编译运行,而且不产生错误的选项是( )
A.public void foo(){}
B.public int foo(){return 1;}
C.public A foo(B b){return b;}
D.public A foo(){return A;}
相关文章:

分享java中面试题汇总

10个经典的 Java main 方法面试题

相关视频:

Java参考文档

以上是这十道10道Java题你会做吗?考考你的水平的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

公司安全软件导致应用无法运行?如何排查和解决? 公司安全软件导致应用无法运行?如何排查和解决? Apr 19, 2025 pm 04:51 PM

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

如何优雅地获取实体类变量名构建数据库查询条件? 如何优雅地获取实体类变量名构建数据库查询条件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

如何使用MapStruct简化系统对接中的字段映射问题? 如何使用MapStruct简化系统对接中的字段映射问题? Apr 19, 2025 pm 06:21 PM

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本启动Spring...

如何将姓名转换为数字以实现排序并保持群组中的一致性? 如何将姓名转换为数字以实现排序并保持群组中的一致性? Apr 19, 2025 pm 11:30 PM

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

Java对象如何安全地转换为数组? Java对象如何安全地转换为数组? Apr 19, 2025 pm 11:33 PM

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

为什么Spring项目启动时会因为循环依赖导致随机性问题? 为什么Spring项目启动时会因为循环依赖导致随机性问题? Apr 19, 2025 pm 11:21 PM

理解Spring项目启动中循环依赖的随机性在进行Spring项目开发时,可能会遇到项目启动时由于循环依赖导致的随机...

如何将名字转换为数字以实现群组内排序? 如何将名字转换为数字以实现群组内排序? Apr 19, 2025 pm 01:57 PM

如何将名字转为数字以实现群组内排序?在群组中排序用户时,常常需要将用户的名字转化为数字,以便在不同...

See all articles