JAVA EXAM 1---25
JAVA 测试I http://www.jianshu.com/p/76b36d18844d
JAVA 测试II http://www.jianshu.com/p/e7f7d29b185a
JAVA 测试III http://www.jianshu.com/p/64d3495989a5
1
public class Test {
public void main(){
System.out.println("Hi");
}
public static void main (String [] args)
{
Test t=new Test();
t.main();
}
}
What will be happen if you compile and run above code?
- It will not compile
- It will not run
- It will compile but will not run
- It will output “Hi”
ANS: 4, 因为两个函数参数不同, 属于函数重载. 函数重载只考虑参数问题.
2
After execution of the code fragment below, what are the value of the variables x1, y1, and z1?
int x=10; int y =10; int z=10; int x1, y1, z1;
x1=++y; //x1 = 11
y1=z++; //y1 = 10
z1=z; // z1 = 11
Choose the one below:
- x1 = 10 , y1 = 10 and z1=10
- x1 = 10, y1 = 11, and z1=10
- x1=10, y1 = 10, and z1=11
- x1=11, y1=10, and z1=11
选4
3
What will be the output?
public class Test {
public int addTest(int x, int y) {
x = x + 1;
y = y + 1;
int z = (x + y);
return z;
}
public static void main(String[] args) {
int x = 10;
int y = 10;
int z = 0;
Test t = new Test();
z = t.addTest(x, y);
System.out.println("x =" + x + ",y =" + y + ",z = " + z);
}
}
Choose the one below:
- x=10, y=10, z=22
- x=11, y=11, z=22
- x=10, y=10, z=20
- x=11, y=11, z=20
选1, 因为函数传的是☞, 不是地址.
4
What will be the output of the program.?
Assume that MyList class is declared in MyList.java
and ListManager class is declared in ListManager.java file.
public class Test {
int size = 1;
public static void main(String[] args)
{
Test list = new Test();
list.size = 10;
ListManager lm = new ListManager();
lm.expandList(list);
System.out.println("list.size = " + list.size);
}
} //end of MyList
public class ListManager {
public void expandList(Test l)
{
l.size = l.size + 10;
}
}
Choose the one below:
- size=0
- size=10
- size=11
- size=20
选4, 因为传的是类, 属于址传递.
5
If int x = -1 then which of the following expression results in a positive value in x?
- x=x>>>32 // --> -1
- x=x>>>5 // --> 134217727
- x=x>>5 // ---> -1
- x=~x // ---> 0
选2, 因为>>>是无符号右移,空位补0, -1的二进制是32个1, >>是有符号右移空位补符号位-1的符号位是1,4不正确, 题目是问正数.
6
6 . Which of the following lines of code would print “Equal” when you run it?
- int x=1; float y=1.0F; if(x==y){ System.out.println("Equal");} //Equal
- int x=1; Integer y= new Integer(1); if(x==y) {System.out.println("Equal");} // Equal
- Integer x=new Integer(1); Integer y=new Intger(1); if(x==y){ System.out.println("Equal");} //不输出
- String x="1"; String y="1"; if (x==y) { System.out.println("Equal");} //Equal
1 2 4都会输出 Equal,
7
Which of the following declarations are correct for the top level class?
- public synchronized class MyTest extends Thread
- private class MyTest extends Thread
- public abstract class MyTest extends Thread
- class MyTest extends Thread
选择3, 4
8
Consider the following lines of code:
public class Test {
String x;
public void testDemo(int n) {
String y;
if (n > 0) {
y = "Hello";
}
System.out.println(x + y);
}
public static void main(String[] args) {
Test test = new Test();
test.testDemo(2);
}
} //end of MyList
- It will produce compiler warning that variable y may not have been initialized
- It will produce compiler warning that variable x may not have been initialized
- It will output “Hello”
- It will output “nullHello”
选2, 因为没有初始化y,编译失败
9
Consider that Parent and Child classes are defined in two different files as below:
class Parent {
public Parent() {
System.out.println("I am Parent");
}
}
class Child extends Parent {
public Child(int x) {
System.out.println("I am Child");
}
public static void main(String[] args) {
Child c = new Child(10);
}
}
- It will not compile.
- It will compile successfully. It will output “I am Parent” and then “I am Child.”
- It will compile successfully. It will output “I am Child” and then “I am Parent.”
- It will compile successfully, but will not run.
这个题目有问题, Child和Parent首先得在一个包,第二Child类前得有public才能运行.
所以,要么编译成功,输出2, 要么编译成功但不会输出选4,
10
Consider following code:
import java.util.Vector;
public class Test {
static int size;
public expandList(int newSize) {
ListExpander lexp = new ListExpander();
Vector expandedList = lexp.expand();
class ListExpander {
public Vector expand() {
Vector v = new Vector(this.size + newSize);
return v;
}
}
}
}
- compiler error, “cannot refer inside an inner class to a static variable.”
- compiler error, “cannot refer inside an inner class to to a non-final variable newSize defined in a different method.”
- Both of the above
- None of the above
如果expandList函数前有返回类型, 那么就选2 newSize需要声明为final类型, 如果没有,就选4
11
Consider following code:
public class Parent{
public int size =0;
static class InnerClass{
public void incrementParentSize(){
XXX=XXX+10;
}
}
}
In above code, how can you access ‘size’ variable (of outer class Parent) inside innerclass at the place of ‘XXX’ ?
- super.size
- this.size
- Parent.size
- Can not access it`
选4, 静态内部类无法访问非静态成员, super是父类, this是当前类
12
Assume that Parent and Child classes are in different files:
public class Parent {
public Parent(int x, int y)
{
System.out.println("Created Parent");
}
}//end of Parent class
public class Child extends Parent {
public Child(int x, int y) {
super(x, y);
}
public Child(int x, int y, int z) {
System.out.println("Creating child");
this(x, y);
}
public static void main(String[] args) {
Child c = new Child(1, 2, 3);
}
}
What will happen if you try to compile and run above program?
- It will compile successfully. It will output “Created Parent” and then “Creating child”
- It will compile successfully. It will output “Creating child” and then “Created Parent”
- It will not compile giving warning, “Explicit constructor invocation must be first statement in constructor.”
- It will not compile giving warning, “Expression is not a valid block statement.”
一定不会编译的,选3, 4
13
Consider following code:
public class OuterClass{
class InnerClass{
}
public void innerClassDemo(){
//Explicit instance of InnerClass
}
}
In above code, how can you explicitly create an instance of InnerClass?
- InnerClass i=InnerClass();
- InnerClass i=OuterClass.InnerClass();
- InnerClass i=new OuterClass ().new InnerClass();
- InnerClass i=new OuterClass.InnerClass();
前两个都没有new关键字, 选3, 4
14
Please select valid(有效) array declaration(s):
- int x[20];
- int []x=new int[20];
- int [][] x=new int [20][];
- int [][][] x=new int[20][20][20];
- int [] x={1,2,3};
2, 3, 4, 5都正确, 1错误
15
Consider following code:
public class Test {
protected void demo() throws NumberFormatException, ArrayIndexOutOfBoundsException {
//something here
}
public void demo(String s) {
//something here
}
}//end of Test class
Please select true statement(s) for demo code?
- It is an example of overloading method
- It is an example of overriding method
- Both of the above
- None of the above
选1, 方法重载, overloading
16
For the following code, please consider that super class is defined in question #15:
class MyTest extends Test {
private void demo() throws IndexOutOfBoundsException, ClassNotFoundException
{
//something here
}
}//end of MyTest class
What will happen if you try to compile above code ?
- It will compile successfully.
- Compiler error: Exception java.lang.ClassNotFoundException in throws clause of void MyTest.demo() is not compatible with void Test.demo().
- Compiler error: Cannot reduce visibility of the inherited method from Test.
- Both B and C
选4, MyTest中的demo()无法覆盖Test中的demo()
正在尝试分配更低的访问权限; 以前为protected
17
Consider the following code:
public class Test {
public void demo(String[] list) {
try {
String s = list[list.length + 1];
System.out.println(s);
} catch (ArrayIndexOutOfBoundsException e) {
return;
} finally {
System.out.println("Finally here.");
}
}
public static void main(String[] args) {
Test t = new Test();
String[] list = {"one","two"};
t.demo(list);
System.out.println("Done !");
}
}//end of Test class
What happen if you try compile and run above code ?
- It will not compile.
- It will output “null” and then “Finally here.”
- It will output “Done!”
- It will output “Finally here” and then “Done!”
选4, 遇到catch里面return的时候,不执行,finally一定会执行!
18
Please consider following code:
public class Test{
public static void demo(String s)
{
debug("In demo:"+s);
}
private void debug(String s){
System.out.println(s);
}
public static void main(String [] args){
Test.demo(“Hello”);
}
}
What will happen if you try to compile and run above code ?
- It will compile successfully, but will not run.
- It will compile successfully, and outputs “In demo:Hello.”
- It will not compile with error message “Can not make a static reference to the instance method named.”
- None of the above
选3, 静态方法不能调用非静态方法
19
Consider the following code:
/**
* File Drawable.java
*/
public interface Drawable {
public void draw();
public void fill();
} /**
* End of file Drawable.java
*/
/** File Circle.java */
public class Circle implements Drawable {
int center=0;
public void draw(){
System.out.println(“Drawing circle”);
}
public static void main(String [] args){
Circle c=new Circle();
c.draw()
}
} /** End of file Circle.java */
If you attempt to compile and run Circle class what will be output?
- It will compile successfully, and outputs “Drawing circle.”
- It will not compile, and reports error: “class Circle must implement inherited abstract method void Drawable.fill.”
- It will not compile, and reports error: “Method Drawable.fill requires a body instead of a semicolon.”
- None of the above
选2, 因为只实现了接口的一个方法.
20
What will be the output?
int x=2; int y=3; int z=4;
if(x>2){
System.out.println(“Tested x”);
}if(y<3){
System.out.println(“Tested y”);
}if (z<=3){
System.out.println(“Tested z”);
}
Choose the one below:
- Tested x.
- Tested y.
- Tested z.
- None of the above.
选4
21
Consider the following code:
for(int i=0;i<2;i++)
{
for(int j=i;j<3; j++)
{
if (i==j)
{
continue;
}
System.out.println("i="+i+" j="+j);
}
}
Which lines would be part of the output?
- i = 0 j = 1
- i = 0 j = 2
- i = 1 j = 2
- None of the above
选1 2 3,
22
Consider the following code:
int j=0;
for( int i=0;i<2;i++)
{
for (j=i; j<3; j++)
{
continue;
}
System.out.println(“i = “+i+” j = “+j);
}
Which lines would be part of the output?
- i = 0 j = 0
- i = 1 j = 1
- i = 0 j = 3
- i = 1 j =3
选3 4
23
Consider the following code:
int i=0; int j=0;
for( i=0;i<2;i++)
{
for (j=i; j<3; j++)
{
break;
}
System.out.println("i = "+i+" j = "+j);
}
```
Which **lines** would be part of the output?
1. i = 0 j = 0
2. i = 1 j = 1
3. i = 0 j = 3
4. i = 1 j = 3
> 选1 2
#### 24
Consider the following code:
```
int i, j=0;
outer:
for( i=0;i<2;i++)
{
for (j=i; j<3; j++)
{
continue outer;
}
System.out.println("i = "+i+" j = "+j);
}
```
Which lines would be part of the output?
1. i = 0 j = 0
2. i = 1 j = 1
3. i = 0 j = 3
4. None of the above
> 选4, 什么都输出, 这里是 java label标号的用法
#### 25
Consider the following code:
```
int i, j=0;
for( i=0;i<2;i++)
{
inner:
for(j=i; j<3; j++)
{
break inner;
}
System.out.println("i = "+i+" j = "+j);
}
```
Which **lines** would be part of the output?
1. i = 0 j = 0
2. i = 1 j = 1
3. i = 0 j = 3
4. None of the above
> 选 1 2,
网友评论