试题

  1. Given:
    Integer.valueOf(1).equals(Long.valueOf(1))
    which statement below is right?
    A.It doesn’t compile.
    B.The value is true.
    C.The value is false.
    D.It throws exception in run-time.

  2. What best describes the appearance of an application with the following code?

    public class FlowAp extends Frame
    {
    public static void main(String argv[])
    {
    FlowAp fa=new FlowAp();
    fa.setSize(400,300);
    fa.setVisible(true);
    }

    FlowAp()
    {
    add(new Button(“One”));
    add(new Button(“Two”));
    add(new Button(“Three”));
    add(new Button(“Four”));
    }
    }

A. A Frame with buttons marked One to Four placed on each edge.
B. A Frame with buutons marked One to four running from the top to bottom
C. A Frame with one large button marked Four in the Centre
D. An Error at run time indicating you have not set a LayoutManager

  1. What will happen when you attempt to compile and run the following code?

    public class Bground extends Thread{
    public static void main(String argv[]){
    Bground b = new Bground();
    b.run();
    }
    public void start(){
    for (int i = 0; i <10; i++){
    System.out.println("Value of i = " + i);
    }
    }
    }

A.A compile time error indicating that no run method is defined for the Thread class
B.A run time error indicating that no run method is defined for the Thread class
C.Clean compile and at run time the values 0 to 9 are printed out
D.Clean compile but no output at runtime

  1. Given:
    class Century implements Runnable {
    public void run() {
    for ( int year = 1900; year < 2000; year++ ) {
    System.out.println(year);
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    }
    System.out.println(“Happy new millennium!”);
    }
    }
    public class CountUp {
    public static void main(String[] args) {
    Century ourCentury = new Century();
    // put your line here
    }
    }
    There’s a missing line in CountUp.main(), which is to begin the thread defined in Century. Which is the proper code:
    A.Thread t = new Thread(this); t.start();
    B.Thread t = new Thread(ourCentury); ourCentury.start();
    C.Thread t = new Thread(this); t.start(ourCentur);
    D.Thread t = new Thread(ourCentury); t.start();

  2. Which most closely matches a description of a Java Map?
    A.A vector of arrays for a 2D geographic representation
    B.A class for containing unique array elements
    C.A class for containing unique vector elements
    D.An interface that ensures that implementing classes cannot contain duplicate keys

  3. For a class defined inside a method, what rule governs access to the variables of the enclosing method?
    A. The class can access any variable
    B. The class can only access static variables
    C. The class can only access transient variables
    D. The class can only access final variables

Class c1{
void func()
{
Final Int i=2;

   Class C2 {
      Void func2()
      {
        i
      }
   }

  C2 obj1; 

}
}

  1. Given the following main method in a class called Cycle and a command line of
    java Cycle one two
    what will be output?
    public static void main(String bicycle[]){
    System.out.println(bicycle[0]);
    }
  1. None of these options
  2. cycle
  3. one
  4. two
  1. What will happen when you attempt to compile and run the following code
    int Output=10;
    boolean b1 = false;
    if((b1==true) && ((Output+=10)==20)){
    System.out.println("We are equal "+Output);
    } else {
    System.out.println("Not equal! "+Output);
    }
    A. Compile error, attempting to peform binary comparison on logical data type
    B. Compilation and output of “We are equal 10”
    C. Compilation and output of “Not equal! 20”
    D. Compilation and output of “Not equal! 10”

  2. Which of the following statements are true?
    A. Adding more classes via import statements will cause a performance overhead, only import classes you actually use.
    B. Under no circumstances can a class be defined with the private modifier
    C. A inner class may under some circumstances be defined with the protected modifier
    D. An interface cannot be inherited.

  3. Which of the following best describes the use of the synchronized keyword?
    A. Allows two process to run in parallel but to communicate with each other
    B. Ensures only one thread at a time may access a method or object
    C. Ensures that two or more processes will start and end at the same time
    D. Ensures that two or more Threads will start and end at the same time

  4. Which of the following statements is true?
    A. The equals() method of any class determines if reference values refer to the same object.
    B. The == operator determines if the contents and type of two separate objects match.
    C. The equals() method of any class returns true only when the contents of two objects match.
    D. The class File overrides equals() to return true if the contents and type of two separate objects match.

  5. Which statements about the garbage collection are true?
    A. The program developer must create a thread to be responsible for free the memory.
    B. The garbage collection will check for and free memory no longer needed.
    C. The garbage collection allows the program developer to explicitly and immediately free the memory.
    D. The garbage collection can free the memory used by Java object at expect time.

  6. Which statement is true for the class java.util.ArrayList?
    A. The elements in the collection are ordered.
    B. The collection is guaranteed to be immutable.
    C. The elements in the collection are guaranteed to be unique.
    D. The elements in the collections are guaranteed to be synchronized

  7. How can you create a listener class that receives events when the mouse is moved?
    A. By extending MouseListener.
    B. By implementing MouseListener.
    C. By extending MouseMotionListener.
    D. By implementing MouseMotionListener.

15.Given:
ClassOne.java
package com.abc.pkg1;
public class ClassOne {
private char var = ‘a’;
char getVar() {return var;}
}
ClassTest.java
package com.abc.pkg2;
import com.abc.pkg1.ClassOne;
public class ClassTest extends ClassOne {
public static void main(String[]args) {
char a = new ClassOne().getVar();
char b = new ClassTest().getVar();
}
}
What is the result?
A. Compilation will fail.
B. Compilation succeeds and no exceptions are thrown.
C. Compilation succeeds but an exception is thrown at line 5 in ClassTest.java.
D. Compilation succeeds but an exception is thrown at line 6 in ClassTest.java.

16
6. Which of the following layout managers will respect all dimensions of a component’s preferred size?
A.FlowLayout
B.GridLayout
C.BorderLayout
D.None of the above

17
10. Which of the following statements is NOT true?
A.Strings can be initialized using the = operator with a string literal value.
B.The toString() method can be used to return a String value from an object of any class.
C.All strings are terminated with a null (‘\0’) character.
D.It is impossible to change the contents of a String object.

18
17. Which one below is correct to define and create an array?
A.int a[5];
B.int a[] = new [5];
C.int a[] = {1,2,3,4,5};
D.int a = new int[5];

III. (54%) Write the output of the code below:
1.
public class Class1 {
protected InnerClass1 ic;

public Class1() {
  ic = new InnerClass1();
}

public void displayStrings() {
  System.out.println(ic.getString() + ".");
  System.out.println(ic.getAnotherString() + ".");
}

public static void main(String[] args) {
    Class1 c1 = new Class1();
   c1.displayStrings();
}

protected class InnerClass1 {
 public String getString() {
    return "InnerClass1: getString invoked";
 }

 public String getAnotherString() {
    return "InnerClass1: getAnotherString invoked";
 }
}

}

public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
public static void main(String[] args) {
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
}
}

a.y = 5
b.y = 6
a.x = 2
b.x = 2
IdentifyMyParts.x = 2

class Value {
int i;
public static void main(String[] args) {
Value[] a = new Value[10];
for ( int i=0; i<a.length; i++ ) {
a[i] = new Value();
}
for ( Value v : a ) {
v.i += 5;
}
for ( int k = 0; k<a.length; k++ ) {
System.out.printl(a[k].i);
}
}
}

5
5
5
5
5
5
5
5
5
5

4
public class Test {
public int t=4;
public static void main(String[] args) {
new Test().NumberPlay();
}
public void NumberPlay() {
int t=2;
t = t+5;
this.t = this.t-2;
t = t-this.t;
System.out.println(t+this.t+”ok”);
}
}

7ok

public class PrintingContainers {
static Collection fill(Collection c) {
c.add(“dog”);
c.add(“dog”);
c.add(“cat”);
return c;
}
static Map fill(Map m) {
m.put(“dog”, “Bosco”);
m.put(“dog”, “Spot”);
m.put(“cat”, “Rags”);
return m;
}
public static void main(String[] args) {
System.out.println(fill(new ArrayList()));
System.out.println(fill(new HashSet()));
System.out.println(fill(new HashMap()));
}
}

[dog, dog, cat]
[cat, dog]
{cat=Rags, dog=Spot}

class ValHold
{
public int i = 10;
}

public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.print( v.i );
}

    public void another(ValHold v, int i){
            i=0;
            v.i = 20;
            ValHold vh = new ValHold();
            v =  vh;
            System.out.print(v.i);
    System.out.print(i);
    }

}

10020

6
class A {
public int data=5;
public void print() {
System.out.println(data);
}
}
class B extends A {
public int data=2;
public void print() {
System.out.println(data);
}
}
public class TestAB {
public static void main(String[] args) {
A a = new B();
a.print();
System.out.println(a.data);
}
}

2
5

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值