当前位置:文档之家› java练习题

java练习题

1 When you compile a java program you should use command:
A java *.java
B javab *.java
C javac *.java
D javad *.java
2 Which of the following lines will compile without error.
A float f=1.3; double
B char c="a"; string
C boolean b=null;
D double d=1.3;
3 Which statement will compile wrong:
A if(3>5)System.out.println("hello");
B if (3)System.out.println("hello");
C if(true)System.out.println("hello");
D if(false)System.out.println("hello");
4 Consider the following code:
Integer s = new Integer(9);
Integer t = new Integer(9);
Long u = new Long(9);
Which test would return true?
A (s.equals(new Integer(9))
B (s.equals(9))
C (s==u)
D (s==t)
5 All of the following are wrapper classes except:
A String
B Integer
C Character
D Double
6 When you use the assignment operator with variables of a class type, you are assigning a:
A value
B primitive type
C local variable
D reference
7 A class design requires that a particular member variable must be accesible for direct access by any subclasses of this class, otherwise not by classes which are not members of the same package. What should be done to achieve this?
A. The variable should be marked public
B. The variable should be marked private
C. The variable should be marked protected
D. The variable should have no special access modifier
8 Given:
class A {
A() { }
}
class B extends A {
}
Which statement is true?
A. Class B's constructor is public.
B. Class B's constructor has arguments.
C. Class B's constructor includes a call to this().
D. Class B's constructor includes a call to super().
9 class super {
public int getLength() {return 4;}
}
public class Sub extends Super {
public long getLength() {return 5;}
public static void main (String[]args) {
super sooper = new Super ();
Sub sub = new Sub();
System.out.printIn(sooper.getLength()+ "," + sub.getLength() };
}
What is the output?
A.4, 4
B.4, 5
C.5, 4
D.the code will not compile
10 in java which one is not a Layout Management class:
A. BorderLayout
B. FlowLayout
C. CardLayout
D. LayoutManage
11 which one can construct a BufferedOutputStream object:
A. new BufferedOutputStream();
B. new BufferedOutputStream("o.txt");
C. new BufferedOutputStream(new Writer("o.txt"));
D. new BufferedOutputStream(new FileOutputStream("o.txt"));
12 Which one is valid declaration within an interface definition?
A. public void methoda();
B. protected void methoda(double d1);
C. public final double methoda();
D. private void methoda(double d1);
13 Given:
1. public class Foo {
2. public static void main(String[] args) {
3. try {
4. return;
5. } finally {
6. System.out.println( "Finally" );
7. }
8. }
9. }
What is the result?
A. Finally
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.
14 Given:
public class A {
public int add(int a, int b){
return a+b
}
}
Which one overload the add method?
A. double add (int a, int b){ return a+b; }
B. public double add(double ax, double by) { return ax+by; }
C. public double add(int ax, int by) { return ax+by; }
D. public int add(int ax, int by){ return ax+by;}
15 Given:
1. public interface Foo {
2. int k = 4;
3. }
Which one is equivalent to line 2?
A.final int k = 4;
B abstract int;
C.private int k = 4;
D.protected int k=4;
16 Given:
1. class BaseClass {
2. Private float x = 1.0f ;
3. protected float getVar ( ) ( return x;)
4. }
5. class Subclass extends BaseClass (
6. private float x = 2.0f;
7. //insert code here
8. )
Which one is valid examples of method overriding?
A float getVar ( ) { return x;}
B public float getVar ( ) { return x;}
C double getVar ( ) { return x;}
D public float getVar (float f ) { return f;}。

相关主题