现在的公司开始变着花样的出面试题,大部分都是基础题,看了一下,觉得自己的基础还是不行,收藏这几个,以后备用
Question 1
class JMM119 {
public static void main (String[] args) {
int i = 0, j = 9;
l1:
do {
l2:
if (j < 4) {
break l2;
} else if (j-- < 7) {continue;}
i++;
} while (i++ < 7);
System.out.print(i + "," + j);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 4,7
b. Prints: 6,6
c. Prints: 6,5
d. Prints: 6,4
e. Prints: 7,5
f. Prints: 8,4
g. Run-time error
h. Compile-time error
i. None of the above
Question 2
package com.dan.chisholm;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}
class B {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}}
Assume that the code appears in a single file named A.java. What is the result of attempting to compile and run the program?
a. Prints: A.m1, A.m2, A.m3, A.m4,
b. Compile-time error at 1.
c. Compile-time error at 2.
d. Compile-time error at 3.
e. Compile-time error at 4.
f.None of the above
Question 3
class GFM13 {
static byte a; static short b; static char c;
static int d; static long e; static String s;
public static void main(String[] args) {
System.out.println(a+b+c+d+e+s);
}}
What is the result of attempting to compile and run the program?
a. Prints: 00000null
b. Prints: 00000
c. Prints: 0null
d. Prints: 0
e. Prints: null
f. Compile-time error
g. Run-time error
g.None of the above
Question 4
class GFC301 {
private String name;
public GFC301(String name) {this.name = name;}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
public static void m1(GFC301 r1, GFC301 r2) {
r1.setName("Bird");
r2 = r1;
}
public static void main (String[] args) {
GFC301 pet1 = new GFC301("Dog");
GFC301 pet2 = new GFC301("Cat");
m1(pet1,pet2);
System.out.println(pet1.getName() + "," + pet2.getName());
}}
What is the result of attempting to compile and run the program?
a. Prints: Dog,Cat
b. Prints: Dog,Bird
c. Prints: Bird,Cat
d. Prints: Bird,Bird
e. Run-time error
f. Compile-time error
h.None of the above
Question 5
public class MethodOver{
public void setVar(int a, int b, float c){}
}
}
which overload the setVar? (Please choice three)
A.private void setVar(int a, float c, int b){}
B.protected void setVar(int a, int b, float c){}
C.public int setVar(int a, float c, int b){return a;}
D.public int setVar(int a, float c){return a;}