1 How can you force garbage collection of an object?
A. Garbage collection cannot be forced
B. ().
C. (), passing in a reference to the object to be garbage collected.
D. ().
E. Set all references to the object to new values(null, for example).
A. Garbage collection cannot be forced
B. ().
C. (), passing in a reference to the object to be garbage collected.
D. ().
E. Set all references to the object to new values(null, for example).
2 What happens when you try to compile and run the following application? Choose all correct options.
public class Z {
public static void main(String[] args) {
new Z();
}
Z() {
Z alias1 = this;
Z alias2 = this;
synchronized(alias1) {
try {
alias2.wait();
System.out.println("DONE WAITING");
}
catch (InterruptedException e) {
System.out.println("INTERR
UPTED");
}
catch (Exception e) {
System.out.println("OTHER EXCEPTION");
}
finally {
Z alias2 = this;
synchronized(alias1) {
try {
alias2.wait();
System.out.println("DONE WAITING");
}
catch (InterruptedException e) {
System.out.println("INTERR
UPTED");
}
catch (Exception e) {
System.out.println("OTHER EXCEPTION");
}
finally {
System.out.println
("FINALLY");
}
}
System.out.println("ALL DONE");
}
}
A. The application compiles but doesn't print anything.
B. The application compiles and print "DONE WAITING"
C. The application compiles and print "FINALLY"
D. The application compiles and print "ALL DONE"
E. The application compiles and print "INTERRUPTED"
3 What will happen when you attempt to compile and run the following code?
("FINALLY");
}
}
System.out.println("ALL DONE");
}
}
A. The application compiles but doesn't print anything.
B. The application compiles and print "DONE WAITING"
C. The application compiles and print "FINALLY"
D. The application compiles and print "ALL DONE"
E. The application compiles and print "INTERRUPTED"
3 What will happen when you attempt to compile and run the following code?
public class Static{
static{
int x = 5;
}
static int x,y;
public static void main(String args[]){
int x = 5;
}
static int x,y;
public static void main(String args[]){
x--;
myMethod();
System.out.println(x + y + ++x);
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod(){
y = x++ + ++x;
}
}
Apiletime error
public static void myMethod(){
y = x++ + ++x;
}
}
Apiletime error
B.prints: 1
C.prints: 2
D.prints: 3
E.prints: 7
F.prints: 8
C.prints: 2
D.prints: 3
E.prints: 7
F.prints: 8
4 Given the following code, what will be the output?
class Value{
public int i = 15;
}
public class Test{
public static void main(String argv[]){
public int i = 15;
}
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.first();
}
public void first(){
int i = 5;
Value v = new Value();
Value v = new Value();
v.i = 25;
second(v, i);
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i){
i = 0;
public void second(Value v, int i){
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
A.15 0 20
B.15 0 15
C.20 0 20
D.0 15 20
}
A.15 0 20
B.15 0 15
C.20 0 20
D.0 15 20
5 What will happen when you attempt to compile and run the following code?
interface MyInterface {}
public class MyInstanceTest implements MyInterface {
public class MyInstanceTest implements MyInterface {
static String s;
public static void main(String args[]) {
MyInstanceTest t = new MyInstanceTest();
if (t instanceof MyInterface) {
if (t instanceof MyInterface) {
System.out.println("I am true interface");
} else {
System.out.println("I am false interface");
}
if (s instanceof String) {
if (s instanceof String) {
System.out.println("I am true String");
} else {
System.out.println("I am false String");
}
}
}
Apile time error
B.runtime error
C.prints: “I am true interface” followed by “I am true String”
D.prints: “I am false interface” followed by “I am false String”
E.prints: “I am true interface” followed by “I am false String”
F.prints: “I am false interface” followed by “I am true String”
Apile time error
B.runtime error
C.prints: “I am true interface” followed by “I am true String”
D.prints: “I am false interface” followed by “I am false String”
E.prints: “I am true interface” followed by “I am false String”
F.prints: “I am false interface” followed by “I am true String”
6 What will be the result of executing the following code?
public static void main(String args[]){
char digit = 'a';
for (int i = 0; i < 10; i++){
for (int i = 0; i < 10; i++){
switch (digit){
case 'x' :{
int j = 0;
int j = 0;
System.out.println(j);
}
default :{
int j = 100;
default :{
int j = 100;
System.out.println(j);
}
}
}
int i = j;
int i = j;
System.out.println(i);
}
A.100 will be printed 11 times.
B.100 will be printed 10 times and then there will be a runtime exception
C.The code will not compile because the variable i cannot be declared twice within the mani() method.
D.The code will not compile because the variable j cannot be declared twice within the switch statement.
E.None of these
A.100 will be printed 11 times.
B.100 will be printed 10 times and then there will be a runtime exception
C.The code will not compile because the variable i cannot be declared twice within the mani() method.
D.The code will not compile because the variable j cannot be declared twice within the switch statement.
E.None of these
7 What will be printed when you execute the following code?
class X {
Y b = new Y();
Y b = new Y();
X() {
System.out.print("X");
}
}
class Y {
}
}
class Y {
Y() {
System.out.print("Y");
}
}
public class Z extends X {
}
}
public class Z extends X {
Y y = new Y();
Z() {
System.out.print("Z");
}
public static void main(String[] args) {
new Z();
}
}
A.Z
B.YZ
C.XYZ
D.YXYZ
A.Z
B.YZ
C.XYZ
D.YXYZ
8 What will happen when you attempt to compile and run the following code snippet?
Boolean b = new Boolean("Alcatel");
if(b.booleanValue()){
if(b.booleanValue()){
System.out.println("Yes : " + b);
}else{
System.out.println("No : " + b);
}
A.The code will not compile.
B.It will print – Yes: true
C.It will print – Yes: TRUE
D.It will print – No: false
E.It will print – No: FALSE
}else{
System.out.println("No : " + b);
}
A.The code will not compile.
B.It will print – Yes: true
C.It will print – Yes: TRUE
D.It will print – No: false
E.It will print – No: FALSE
9 What will be the output on compiling/running the following code?
public class MyThread implements Runnable {
String myString = "Yes ";
public void run() {
public void run() {
String = "No ";
}
public static void main(String[] args) {
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t).start();
for (int i=0; i < 10; i++)
new Thread(t).start();
for (int i=0; i < 10; i++)
System.out.String);
}
}
A. compile error
B. prints: yes yes yes yes yes yes and so on
C. prints: no no no no no no no no and so on
D. prints: yes no yes no ye no ye no and so on
E. the output cannot be determinated
A. compile error
B. prints: yes yes yes yes yes yes and so on
C. prints: no no no no no no no no and so on
D. prints: yes no yes no ye no ye no and so on
E. the output cannot be determinated
10 What is displayed when the following code is compiled and executed?
>java面试题及答案2020
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论