Home >Java >javaTutorial >Can you do these 10 Java questions? Test your level
/**
*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
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. After the following program is compiled and run, the result displayed on the screen is ()
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. There is the following code: Please write the output result of the program.
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. What is the result of the following code?
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. 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
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. In the following code snippet, the statement with compilation error is ()
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. Statement 2
B. Statement 1
C. Statement 3
D. Statement 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. After the following code is executed, the output result is ( ) 0611
public class ClassTest{ String str = new String("hello"); char[] ch = {'a','b','c'}; public void fun(String str, char ch[]){ str="world"; ch[0]='d'; } 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. The following code will print out
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. For the following The code segment
class A{ public A foo(){return this;} } class B extends A{ public A foo(){ return this; } } class C extends B { _______ }
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:
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!