Home Java javaTutorial Can you do these 10 Java questions? Test your level

Can you do these 10 Java questions? Test your level

Jul 30, 2018 am 11:04 AM

/**

*These 10 questions are all done by me. I have done them wrong or think they are valuable and need to be reviewed. I have compiled them to test everyone
*If you have better suggestions, please feel free to submit them.
*/

1. The code output result is

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

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); 

        

    

}

Copy after login

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

2. After the following program is compiled and run, the result displayed on the screen is ()

1

2

3

public class test {

public static void main(String args[]) {int x,y;x=5>>2;y=x>>>2;System.out.println(y);}

}

Copy after login

A .0
B.2
C.5
D.80

3. There is the following code: Please write the output result of the program.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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);

    }

}

Copy after login

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

4. What is the result of the following code?

1

2

3

4

5

6

7

8

9

10

11

12

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;

}

}

Copy after login

A. The code can be compiled and run, and "AB.AB" will be output.
B. The code can be compiled and run, and "A.A" will be output.
C. The code can be compiled and run, and "AB.B" will be output.
D. The code can be compiled and run, and "A.B" will be output.

5. Given the following code, please give the result. 0608

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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;

    }

}

Copy after login

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

6. In the following code snippet, the statement with compilation error is ()

1

2

3

4

5

6

7

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);

Copy after login

A. Statement 2
B. Statement 1
C. Statement 3
D. Statement 4

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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();

    }

}

Copy after login

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

8. After the following code is executed, the output result is ( ) 0611

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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);

     }

 }

Copy after login

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

9. The following code will print out

1

2

3

4

public static void main (String[] args) {

    String classFile = "com.jd.". replaceAll(".", "/") + "MyClass.class";

    System.out.println(classFile);

}

Copy after login

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

10. For the following The code segment

1

2

3

4

5

6

7

8

9

10

11

12

class A{

    public A foo(){return this;}

}

class B extends A{

    public A foo(){

        return this;

    }

}

class C extends B

{

    _______

}

Copy after login

can be placed in the horizontal line position so that the program can compile and run correctly without generating errors. The option is ( )
A.public void foo(){}
B.public int foo(){return 1;}
C.public A foo(B b){return b;}
D.public A foo(){return A;}
Related articles:

Share a summary of interview questions in java

10 classic Java main method interview questions

Related videos:

Java Reference Document

The above is the detailed content of Can you do these 10 Java questions? Test your level. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles