• 首页
  • 小学语文
  • 中学语文
  • 中学英语
  • 免费论文
  • 教学随笔
  • 学生作文
  • 综合考试
  • 试题教案
  • 育儿话题
  • 教学资源
  • 编程技术
  • 博客
  • SCJP模拟试题[2]

    日期:2005-07-03  地址:  作者:
    此套试题由60道题组成。

    Questions
    Question 1)

    Which of the following lines will compile without warning or error.

    a) float f = 1.3;
    b) char c = a;
    c) byte b = 257;
    d) boolean b = null;
    e) int i = 10;

    Question 2)

    What will happen if you try to compile and run the following code

    public class MyClass {
    public static void main(String arguments[])
    {
    amethod(arguments);
    }
    public void amethod(String[] arguments)
    {
    System.out.println(arguments);
    System.out.println(arguments[1]);
    }
    }
    1) error Can't make static reference to void amethod.
    2) error method main not correct
    3) error array must include parameter
    4) amethod must be declared with String

    answer


    --------------------------------------------------------------------------------

    Question 3)

    Which of the following will compile without error

    1) import java.awt.*;
    package Mypackage;
    class Myclass {}

    2) package MyPackage;
    import java.awt.*;
    class MyClass{}

    3) /*This is a comment */
    package MyPackage;
    import java.awt.*;
    class MyClass{}

    answer

     

    --------------------------------------------------------------------------------

    Question 4)

    A byte can be of what size

    1) -128 to 127
    2) (-2 power 8 )-1 to 2 power 8
    3) -255 to 256
    4) depends on the Java Virtual machine

    answer


    --------------------------------------------------------------------------------

    Question 5)

    What will be printed out if this code is run with the following command line
    java myprog good morning

    public class myprog{
    public static void main(String argv[])

    {
    System.out.println(argv[2])
    }

    }

    1) myprog
    2) good
    3) morning
    4) Exception raised: java.lang.ArrayIndexOutOfBoundsException: 2

    answer

     

    --------------------------------------------------------------------------------

    Question 6)

    Which of the following are java reserved words

    1) if
    2) then
    3) goto
    4) while
    5) case

    answer


    --------------------------------------------------------------------------------

    Question 7)


    Which of the following are legal identifiers

    1) 2variable
    2) variable2
    3) _whatavariable
    4) _3_
    5) $anothervar
    6) #myvar

    answer

     

    --------------------------------------------------------------------------------

    Question 8)

    What will happen when you compile the following code

    public class MyClass{
    static int i;
    public static void main(String argv[]){
    System.out.println(i);
    }
    }

    1) Error Variable i may not have been initialized
    2) null
    3) 1
    4) 0

    answer


    --------------------------------------------------------------------------------

    Question 9)

    What will happen if you try to compile and run the following code

    public class Q {
    public static void main(String argv[]){
    int anar[]= new int[]{1,2,3};

    System.out.println(anar[1]);
    }
    }

    1) 1
    2) Error anar is referenced before it is initialized
    3) 2
    4) Error size of array must be defined

    answer

     

    --------------------------------------------------------------------------------

    Question 10)

    What will happen if you try to compile and run the following code

    public class Q {
    public static void main(String argv[]){
    int anar[]= new int[5];

    System.out.println(anar[0]);
    }
    }

    1) Error: anar is referenced before it is initialized
    2) null
    3) 0
    4) 5

    answer

     

    --------------------------------------------------------------------------------


    Question 11)

    What will be the result of attempting to compile and run the following code

    abstract class MineBase {
    abstract void amethod();
    static int i;
    }

    public class Mine extends MineBase
    {
    public static void main(String argv[]){
    int[] ar = new int[5];
    for(i = 0;i < ar.length;i++)
    System.out.println(ar[i]);
    }
    }

    1) a sequence of 5 0's will be printed
    2) Error: ar is used before it is initialized
    3) Error Mine must be declared abstract
    4) IndexOutOfBoundes Error i

    answer

     

    --------------------------------------------------------------------------------


    Question 12)

    What will be printed out if you attempt to compile and run the following code
    int i = 1;
    switch (i) {
    case 0:
    System.out.println(zero);
    break;
    case 1:
    System.out.println(one);
    case 2:
    System.out.println(two);
    default:
    System.out.println(default);
    }

    1) one
    2) one, default
    3) one, two, default
    4) default

    answer

     

    --------------------------------------------------------------------------------

    Question 13)

    What will be printed out if you attempt to compile and run the following code

    int i = 9;
    switch (i) {
    default:
    System.out.println(default);
    case 0:
    System.out.println(zero);
    break;
    case 1:
    System.out.println(one);
    case 2:
    System.out.println(two);
    }

    1) default
    2) default, zero
    3) error default clause not defined
    4) no output displayed

    answer

     

    --------------------------------------------------------------------------------

    Question 14)

    Which of the following lines of code will compile without error

    1) int i=0;
    if(i)
    {
    System.out.println(Hello);
    }
    2) boolean b = true;
    boolean b2 = true;
    if(b==b2)
    {
    System.out.println(So true);
    }

    3) int i=1;
    int j = 2;
    if(i ==1|| j==2)
    System.out.println(OK);

    4)
    int i=1;
    int j = 2;
    if(i ==1 &| j==2)
    System.out.println(OK);

    answer


    --------------------------------------------------------------------------------


    Question 15)

    What will be output if you try to compile and run the following code and and there is
    no file called Hello.txt in the current directory.

    import java.io.*;
    public class Mine
    {
    public static void main(String argv[]){

    Mine m = new Mine();
    System.out.println(m.amethod());
    }
    public int amethod()
    {
    try {

    FileInputStream dis = new FileInputStream(Hello.txt);
    }catch (FileNotFoundException fne) {
    System.out.println(No such file found);
    return -1;
    }catch(IOException ioe) {
    } finally{
    System.out.println(Doing finally);
    }
    return 0;
    }
    }

    1) No such file found
    2 No such file found ,-1
    3) No such file found, doing finally, -1
    4) 0

    answer


    --------------------------------------------------------------------------------

    Question 16)

    What tags are mandatory when creating HTML to display an applet

    1) name, height, width
    2) code, name
    3) codebase, height, width
    4) code, height, width

    answer


    --------------------------------------------------------------------------------

    Question 17)

    What will happen if you attempt to compile and run the following code

    class Base {}
    class Sub extends Base {}
    class Sub2 extends Base {}

    public class CEx{

    public static void main(String argv[]){

    Base b = new Base();

    Sub s = (Sub) b;

    }

    }

    1) Compile and run without error
    2) Compile time Exception
    3) Runtime Exception

    answer

     

    --------------------------------------------------------------------------------

    Question 18)

    If the following HTML code is used to display the applet in the code MgAp what will be displayed at the console.

    <applet code = MgAp.class height=400 width=400 parameter HowOld=30 >
    </applet>

    import java.applet.*;
    import java.awt.*;
    public class MgAp extends Applet{

    public void init(){
    System.out.println(getParameter(age));
    }
    }

    1) Error no such parameter
    2) 0
    3) null
    4) 30

    answer


    --------------------------------------------------------------------------------

    Question 19)

    You are browsing the Java HTML documentation for information on the
    java.awt.TextField component. You want to create Listener code to respond to focus
    events. The only Listener method listed is addActionListener. How do you go about
    finding out about Listener methods.

    1) Define your own Listener interface according to the event to be tracked
    2) Use the search facility in the HTML documentation for the listener needed
    3) Move up the hierarchy in the HTML documentation to locate methods in base
    classes
    4) Subclass awt.event with the appropriate Listener method

    answer

     

    --------------------------------------------------------------------------------

    Question 20)

    What will be displayed when you attempt to compile and run the following code

    //Code start
    import java.awt.*;

    public class Butt extends Frame{

    public static void main(String argv[]){
    Butt MyBut= new Butt();
    }

    Butt(){
    Button HelloBut = new Button(Hello);
    Button ByeBut = new Button(Bye);
    add(HelloBut);
    add(ByeBut);
    setSize(300,300);
    setVisible(true);
    }
    }
    //Code end

    1) Two buttons side by side occupying all of the frame, Hello on the left and Bye on
    the right
    2) One button occupying the entire frame saying Hello
    3) One button occupying the entire frame saying Bye
    4) Two buttons at the top of the frame one saying Hello the other saying Bye

    answer

     

    --------------------------------------------------------------------------------

    Question 21-50's answers are here.

    Question 21:

    In the following code fragment from an applet, we know that the getParameter call may return a null if there is no parameter named size. Which logical operator should replace X in line 5 to ensure that a NullPointerException is not generated if tmp is null.

    1. int sz;
    2. public void init(){
    3. sz=10;
    4. String tmp=getParameter(size);
    5. if(tmp!=null X tmp.equals(BIG)) sz=20;
    6. }

    a). Replace X with '&'
    b). Replace X with '&&'
    c). Replace X with '|'
    d). Replace X with '||'


    --------------------------------------------------------------------------------

    Question 22:
    Which of the following statements results in C containing the special Not a Number value?

    a. float C=1234.0F/0.0F;
    b. float C=(float)java.lang.Math.sqrt(-1.0);
    c. float C=Float.MIN_VALUE/Float.MAX_VALUE;


    --------------------------------------------------------------------------------

    Question 23: [Check all correct answers]
    You are writing a java class in a file named MyClass.java, this class must be accessible by all classes in a large project. Which of the following would be correct class declarations?

    a. private class MyClass extends Object
    b. class myclass extends Object
    c. public class MyClass
    d. public class MyClass extends Object


    --------------------------------------------------------------------------------

    Question 24: [Check all correct answers]
    Once created, some Java objects are immutable, meaning they can not have their contents changed. Which of the following classed produce immutable objects?

    a. java.lang.Double
    b. java.lang.StringBuffer
    c. java.lang.Boolean
    d. java.lang.Math


    --------------------------------------------------------------------------------

    Question 25:
    Given the following class definitions:
    1. class BaseWidget extends Object{
    2. String name=BaseWidget;
    3. void speak(){System.out.println(I am a +name);}
    4. }
    5. class TypeAWidget extends BaseWidget{
    6. TypeAWidget(){name=TypeA;}
    7. }

    Which of the following code fragments will compile and execute without error?
    a. Object A=new BaseWidget();
    A.speak();
    b. BaseWidget B=new TypeAWidget();
    B.speak();
    c. TypeAWidget C=new BaseWidget();
    C.speak();


    --------------------------------------------------------------------------------

    Question 26: [Check all correct answers]
    Pick the keyword(s) which can NOT be used as modifiers in declaration of a method in a Java class.

    a) private
    b) friend
    c) protected
    d) static
    e) synchronized
    f) generic

     


    --------------------------------------------------------------------------------

    Question 27: [Check all correct answers]
    Which of the following would be an illegal identifier for a Java method?

    a) do_it_now
    b) _Substitute
    c) 9thMethod
    d) $addMoney
    e) %getPath

     


    --------------------------------------------------------------------------------

    Question 28:
    Given the following code for the Demo class:

    public class Demo{
    private int[] count;
    public Demo(){ count=new int[10];}
    public void setCount(int ct,int n){ count[n]=ct;}
    public void showCount(int n){
    System.out.println(Count is +count[n]);
    }
    public int getCount(int n){ return count[n];}
    }

    what would be the result of calling the showCount method with a parameter of 9 immediately after creating an instance of Demo?

    a) A NullPointerException would be thrown, halting the program.
    b) Standard output would show Count is 0.
    c) An ArrayIndexOutOfBoundsException would be thrown, halting the program.
    d) Standard output would show Count is null.

     


    --------------------------------------------------------------------------------

    Question 29:

    What happens when we attempt to compile and run the following code?
    1. public class Logic{
    2. static long sixteen=0x0010;
    3. static public void main(String args[]){
    4. long N=sixteen>>4:
    5. System.out.println(N= +N);
    6. }
    7. }

    a) The compiler will object to line 4 combining a long with an int.
    b) The program will compile and run, producing the output N=0.
    c) The program will compile and run, producing the output N=1.
    d) A rutime exception will be thrown.

     


    --------------------------------------------------------------------------------

    Question 30:
    What will happen on trying to compile and run the following application?
    1. public class Example{
    2. public Boolean flags[]=new Boolean[4];
    3. public static void main(String[] args){
    4. Example E=new Example();
    5. System.out.println(Flag 1 is +E.flags[1]);
    6. }
    7. }

    a) The text Flag 1 is true will be written to standard output.
    b) The text Flag 1 is false will be written to standard output.
    c) The text Flag 1 is null will be written to standard output.
    d) The compiler will object to line 2.

     


    --------------------------------------------------------------------------------

    Question 31: [Check all correct answers]

    Which of the following code fragments are legal Java code?

    a) String A=abcdefg;
    A-=cde;
    b) String A=abcdefg;
    A+=cde;
    c) Integer J=new Integer(27);
    J-=7;
    d) Integer J=new Integer(27);
    J--;

     


    --------------------------------------------------------------------------------

    Question 32:

    Given the following code for the code for Demo class, where XXXX represents an access modifier:

    public class Demo extends Base{
    XXXX String userName;
    public void setName(String s){ userName=s;}
    public void showName(){
    System.out.println(Name is +userName):
    }
    public String getName(){ return userName; }
    }


    Select the modifier which would be used to give only classes in the default package or classes derived from Demo, direct access to the userName String variable.

    a) public
    b) blank (ie - the line would read String userName :)
    c) protected
    d) private


    --------------------------------------------------------------------------------

    Question 33:

    Given the following method in an application:

    1. public String setFiletype(String fname){
    2. int p=fname.indexOf('.');
    3. if(p>0) fname=fname.substring(0,p);
    4. fname+=.TXT;
    5. return fname;
    6. }

    and given that another part of the class has a the following code:
    7. String TheFile=Program.java;
    8. File F=new File(setFileType(TheFile));
    9. System.out.println(Created +TheFile);
    What will be printed by the statement in line 9?

    a) Created Program.java
    b) Created Program.txt
    c) Created Program.java.txt


    --------------------------------------------------------------------------------

    Question 34:

    What happens on trying to compile and run the following code?
    1. public class EqualsTest{
    2. public static void main(String args[]){
    3. byte A=(byte)4096;
    4. if(A==4096)System.out.println(Equal);
    5. else System.out.println(Not Equal);
    6. }
    7. }

    a) The compiler objects to the loss of accurcy in the cast in line 3.
    b) The program compiles and prints Not Equal.
    c) The program compiles and prints Equal.


    --------------------------------------------------------------------------------

    Question 35:

    What happens on trying to compile and run the following code?

    1. public class EqualsTest{
    2. public static void main(String args[]){
    3. Long LA=new Long(7);
    4. Long LB=new Long(7);
    5. if(LA==LB) System.out.println(Equal);
    6. else System.out.println(Not Equal);
    7. }
    8. }

    a) The program compiles but throws a runtime exception in line 5.
    b) The program compiles and prints Not Equal;
    c) The program compiles and prints Equal.


    --------------------------------------------------------------------------------

    Question 36: [Check all correct answers]

    Given the following code for the Demo class:

    public class Demo extends Base{
    private int count;
    public Demo(){
    System.out.println(A Demo object has been created);
    }
    protected void addOne() {count++; }
    }

    Which of the following statements about the count variable is correct?

    a) When a new Demo object is created, the value of count is zero.
    b) When a new Demo object is created, the value of count is undefined.
    c) An object of the Base class can have methods which modify the count variable.
    d) The only way the count variable can be modified is by calling the addOne method.


    --------------------------------------------------------------------------------

    Question 37: [Check all correct answers]

    Which of the following statements will cause a compiler error.

    a) float F=4096.0;
    b) double D=4096.0;
    c) byte B=4096;
    d) char C=4096;


    --------------------------------------------------------------------------------

    Question 38:

    In the following method, which may be called with any kind of Object, we want to short circuit the logical test in line 2 if the object is not a Long. Which logical operator should replace the X in line 2 to accomplish this?

    1. long Test(Object ob){
    2. if(Ob instanceof Long X ((Long)Ob).longValue()>999){
    3. return((Long)Ob).longValue();
    4. }
    5. return -1L;
    6. }

    a) Replace 'X' with '&&'.
    b) Replace 'X' with '||'.
    c) Replace 'X' with '&'.
    d) Replace 'X' with '|'.


    --------------------------------------------------------------------------------

    Question 39:

    Which of the following statements is the correct form fro determining whether the float primitive X has the specail Not a Number value?

    a) if(X instanceof Float.NaN)
    b) if(X==Float.NaN)
    c) if(Float.isNaN(X))

     


    --------------------------------------------------------------------------------

    Question 40:

    Which of the following are organizing principles of Java's javadoc format documentation for a given class?

    a) Relative frequency of use of the various methods.
    b) Lists of methods in a class in alphabetic order.
    c) Separate listing of private, protected and public methods in a calss.

     


    --------------------------------------------------------------------------------

    Question 41:

    The following program is compiled and then run with this command line:

    java Demo alpha beta gamma

    public class Demo{
    public static void main(String args[]){
    int n=3;
    System.out.println(The word is +args[n]);
    }
    }

    What happens?

    a) The word is beta is written to standard output.
    b) The word is gamma is written to standard output.
    c) The runtime system reports an ArrayIndexOutOfBoundsException in the main method.
    d) The runtime system reports a NullPointerException in the main method.

     


    --------------------------------------------------------------------------------

    Question 42:

    What will be the result of running the following method with an input of 67?

    public int MaskOff(int N){
    return N^3;
    }

    a) The method will return 3.
    b) The method will return 64.
    c) The method will return 67.
    d) The method will return 0.

     


    --------------------------------------------------------------------------------

    Question 43:

    What happens on trying to compile and run the following code?

    public class EqualsTest{
    public static void main(String args[]){
    char A='/u0005';
    if(A==0x0005L) System.out.println(Equal);
    else System.out.println(Not Equal);
    }
    }

    a) The compiler reports Invalid character in input in line 3.
    b) The program compiles and prints Not Equal.
    c) The program compiles and prints Equal.

     


    --------------------------------------------------------------------------------

    Question 44:

    Which of the following Java statements correctly declare an array of int primitives?

    a) int scores[];
    b) int[] scores;
    c) int scores={0,0,0,0};

     


    --------------------------------------------------------------------------------

    Question 45:

    Assume that the following program has been compiled and the Demo.class file is in the current directory.

    public class Demo{
    public static void main(String args[]){
    int n=1;
    System.out.println(The word is +args[n]);
    }
    }

    Select the correct command line to execute the program and produce the following output line: The word is gamma

    a) Demo alpha beta gamma delta
    b) java Demo alpha beta gamma delta
    c) java Demo beta gamma delta
    d) java Demo.class beta gamma delta
    e) java Demo.class alpha beta gamma delta

     


    --------------------------------------------------------------------------------

    Question 46:

    What happens on trying to compile and run the following code?

    public class EqualsTest{
    public static void main(String args[]){
    Object A=new Long(7);
    Long L=new Long(7);
    if(A.equals(L)) System.out.println(Equal);
    else System.out.println(Not Equal);
    }
    }

    a) The program compiles and prints Equal.
    b) The program compiles and prints Not Equal.
    c) The compiler objects to line 5.
    d) A runtime cast error occurs at line 5.

     


    --------------------------------------------------------------------------------

    Question 47:

    What will be the result of compiling the following code?

    public class SiteInfo{
    String webSite=http://www.lanw.com+'/'+default.htm;
    public String getSite(){
    return webSite;
    }
    }

    a) The code will compile without problem.
    b) The compiler will report an error ':' expected for the statement in line 2.
    c) The compiler will object to the char literal in line 3.

     


    --------------------------------------------------------------------------------

    Question 48:

    You are writing code for a class which will be in the default package and will use the graphic components in the AWT package. Select the correct code fragment to start the source code file.

    Fragment A: import java.awt.*;
    Fragment B: package default; import java.awt.*;
    Fragment C: import java.awt.*; package default;

    a) Code Fragment A
    b) Code Fragment B
    c) Code Fragment C

     


    --------------------------------------------------------------------------------

    Question 49:

    The following lists the complete contents of the file named Derived.java.

    public class Base extends Object{
    String objType;
    public Base(){ objType=I am a Base type;
    }
    }

    public class Derived extends Base{
    public Derived() { objType=I am a Derived type;
    }
    public static void main(String args[]){
    Derived D=new Derived();
    }
    }

    What will happen when this file is compiled?

    a) Two class files, Base.class and Derived.class will be created.
    b) The compiler will object to line one.
    c) The compiler will object to line seven.


    --------------------------------------------------------------------------------

     

    Q. 50

    Consider the following program:

     

     
    public class Test { public static void main (String args []) { boolean a = false; if (a = true)
    System.out.println(Hello);

    Else

    System.out.println(Goodbye);

    }

    }

    What is the result:

    A. Program produces no output but terminates correctly.

    B. Program does not terminate.

    C. Prints out Hello

    D. Prints out Goodbye

    Select the most appropriate answer.

     

    Q. 51

    Examine the following code which includes an inner class:

     

    public final class Test4 implements A {

    class Inner {

    void test() {

    if (Test4.this.flag); {

    sample();

    }

    }

    }

    private boolean flag = false;

    public void sample() {

    System.out.println(Sample);

    }

    public Test4() {

    (new Inner()).test();

    }

    public static void main(String args []) {

    new Test4();

    }

    }

     

    What is the result:

    A. Prints out Sample

    B. Program produces no output but terminates correctly.

    C. Program does not terminate.

    D. The program will not compile

    Select the most appropriate answer.

     

     

    Q. 52

    Carefully examine the following class:

     

    public class Test5 { public static void main (String args []) { /* This is the start of a comment

    if (true) {

    Test5 = new test5();

    System.out.println(Done the test);

    }

    /* This is another comment */

    System.out.println (The end);

    }

    }

     

    What is the result:

    A. Prints out Done the test and nothing else.

    B. Program produces no output but terminates correctly.

    C. Program does not terminate.

    D. The program will not compile.

    E. The program generates a runtime exception.

    F. The program prints out The end and nothing else.

    G. The program prints out Done the test and The end

    Select the most appropriate answer.

     

     

    Q. 53

    The following code defines a simple applet:

     

    import java.applet.Applet;

    import java.awt.*;

     

    public class Sample extends Applet {

    private String text = Hello World;

    public void init() {

    add(new Label(text));

    }

    public Sample (String string) {

    text = string;

    }

    }

     

    It is accessed form the following HTML page:

     

    <html>

    <title>Sample Applet</title>

    <body>

    <applet code=Sample.class width=200 height=200></applet>

    </body>

    </html>

     

    What is the result of compiling and running this applet:

    A. Prints Hello World.

    B. Generates a runtime error.

    C. Does nothing.

    D. Generates a compile time error.

    Select the most appropriate answer.

     

    Q. 54

    Examine the following code:

     

    public class Calc {

    public static void main (String args []) {

    int total = 0;

    for (int i = 0, j = 10; total > 30; ++i, --j) {

    System.out.println( i = + i + : j = + j);

    total += (i + j);

    }

    System.out.println(Total + total);

    }

    }

     

    Does this code:

    A. Produce a runtime error

    B. Produce a compile time error

    C. Print out Total 0

    D. Generate the following as output:

    i = 0 : j = 10

    i = 1 : j = 9

    i = 2 : j = 8

    Total 30

    Please select the most appropriate answer.


    --------------------------------------------------------------------------------

    Q. 55

    With which I/O operation can we append, update a file?

    a) RandomAccessFile()
    b) Outputstream()
    c) DataOutputstream()


    --------------------------------------------------------------------------------

    Q. 56

    What does it mean when the handleEvent() returns the true boolean?

    a) the event will be handled by the componet.
    b) the action() method will handle the event.
    c) the event will be handled by the super.handleEvent()
    d) do nothing


    --------------------------------------------------------------------------------

    Q. 57

    Given is this class:

    class Loop{
    public static void main(String[] agrs){
    int x=0;
    int y=0;
    outer:
    for(x=0;x<100;x++){
    middle:
    for(y=0;y<100;y++){
    System.out.println(x=+x+; y=+y):
    if(y==10){
    <<<insert code>>>
    }
    }
    }
    } // main
    } // class

    The question is which code must replace the <<<insert code>>> to finish the outerloop?

    a) continue middle;
    b) break outer;
    c) break middle;
    d) continue outer;
    e) none of these


    --------------------------------------------------------------------------------

    Q. 58

    What is the target variable in an Event?

    a) the Object() where the event came from
    b) the Object() where the event is destined for
    c) what the Object() that generated the event was doing.


    --------------------------------------------------------------------------------

    Q. 59

    The following code resides in the source?

    class StringTest{
    public static void main(String[] args){
    //
    // String comparing
    //
    String a,b;
    StringBuffer c,d;
    c=new StringBuffer(Hello);
    a=new String(Hello);
    b=a;
    d=c;
    if( <<<operator>>> ) {}
    }
    } // class

    Which of the following statement return true for the <<<operator>>> line in StringTest.class?

    a) b.equals(a)
    b) b==a
    c) d==c
    d) d.equals(c)


    --------------------------------------------------------------------------------

    Q. 60

    Which are valid identifiers?

    a) %fred
    b) *fred
    c) thisfred
    d) 2fred
    e) fred


    --------------------------------------------------------------------------------

    Answers from 1 to 20

    1) e

    Back to question 1)

    explanation:
    a) float f = 1.3;
    Will not compile because the default type of a number with a floating point component is a double. This would compile with a cast as in

    float f = (float) 1.3


    b) char c = a;

    Will not compile because a char (16 bit unsigned integer) must be defined with single quotes. This would compile if it were in the form

    char c= 'a';

    c) byte b = 257;

    Will not compile because a byte is eight bits. Take of one bit for the sign component you can define numbers between

    -128 to +127

    d) a boolean value can either be true of false, null is not allowed.


    Answer 2)

    Back to question 2)

    1) Can't make static reference to void amethod.

    Because main is defined as static you need to create an instance that the main exists in before you can call any of its methods. Thus a typical way to do this would be.


    MyClass m = new MyClass();

    m.amethod();


    Answer 2 is an attempt to confuse because the convention is for a main method to be in the form

    String argv[]

    That argv is just a conventiion and any acceptable identifier for a string array can be used. Answers 3 and 4 are just nonsense.

     

    Answer 3)

    back to Question 3)

    2 and 3 will compile without error.


    1 will not compile because any package declaration must come before any other code. Comments may appear anywhere.


    Answer 4)

    Back to question 4)

    1) A byte is a signed 8 bit integer.


    Answer 5)


    4) Unlike C/C++ java does not start the parameter count with the program name. It does however start from zero. So in this case zero starts with good, morning would be 1 and there is no parameter 2 so an exception is raised.

    Back to question 5)


    Answer 6)

    Back to question 6)

    1) if
    3) goto
    4) while
    5) case


    then is not a Java keyword, though if you are from a VB background you might think it was. Goto is a reserved word in Java.

    Answer 7)

    Back to Question 7)


    2) variable2
    3) _whatavariable
    4) _3_
    5) $anothervar


    An identifier can begin with a letter (most common) or a dollar sign($) or an underscore(_). An identifier cannot start with anything else such as a number, a hash, # or a dash -. An identifier cannot have a dash in its body, but it may have an underscore _. Choice 4) _3_ looks strange but it is an acceptable, if unwise form for an identifier.


    Answer 8)

    Back to Question 8)

    3) 1

    Class level variables are always initialised to default values. In the case of an int this will be 0. Method level variables are not given default values and if you attempt to use one before it has been initialised it will cause the

    Error Variable i may not have been initialized

    type of error.

    Answer 9)

    Back to Question 9)

    3 ) 2

    No error will be triggered.

    Like in C/C++ arrays are always referenced from 0. Java allows an array to be populated at creation time. The size of array is taken from the number of initializers. If you put a size within any of the square brackets you will get an error.


    Answer 10)

    Back to question 10)

    3)

    Arrays are always initialised when they are created. As this is an array of ints it will be initalised with zeros.


    Answer 11)

    Back to Question 11)


    3) Error Mine must be declared abstract


    A class that contains an abstract method must itself be declared as abstract. It may however contain non abstract methods. Any class derived from an abstract class must either define all of the abstract methods or be declared abstract itself.


    Answer 12)

    Back to Question 12)

    3) one, two, default

    Code will continue to fall through a case statement until it encouters a break.


    Answer 13)

    1) default

    Back to Question 13)

    Although it is normally placed last the default default statement does not have to be the last item as you fall through the case bock Because there is no case label found matching the expression the default label is executed and the code continues to fall through until it encounters a break.


    Answer 14)

    Back to Question 14)

    2,3

    Example 1 will not compile because if must always test a boolean. This can catch out C/C++ programmers who expect the test to be for either 0 or not 0.


    Answer 15)

    Back to Question 15)

    3) No such file found, doing finally, -1

    The no such file found message is to be expected, however you can get caught out if you are not aware that the finally clause is almost always executed, even if there is a return statement.

    Answer 16)

    Back to Question 16)


    4) code, height, width


    Answer 17)


    Back to Question 17)

    3) Runtime Exception

    Without the cast to sub you would get a compile time error. The cast tells the compiler that you really mean to do this and the actual type of b does not get resolved until runtime. Casting down the object hierarchy as the compiler cannot be sure what has been implemented in descendent classes. Casting up is not a problem because sub classes will have the features of the base classes. This can feel counter intuitive if you are aware that with primitives casting is allowed for widening operations (ie byte to int).


    Answer 18)

    Back to question 18)

    3) null

    If a parameter is not available the applet will still run, but any attempt to access the parameter will return a null.


    Answer 19)

    Back to Question 19)


    3) Move up the hierarchy in the HTML documentation to locate methods in base classes

    The documentation created by JavaDoc is based on tags placed into the sourcecode. The convention for documentation is that methods and fields of ancestors are not duplicated in sub classes. So if you are looking for something and it does not appear to be there, you move up the class hierarchy to find it.


    Answer 20)

    Back to Question 20)

    3) One button occupying the entire frame saying Bye

    The default layout manager for a Frame is a border layout. If directions are not given (ie North, South, East or West), any button will simply go in the centre and occupy all the space. An additional button will simply be placed over the previous button. What you would probably want in a real example is to set up a flow layout as in

    setLayout(new FlowLayout()); which would.


    Applets and panels have a default FlowLayout manager

     


    --------------------------------------------------------------------------------

    Answers from 21 to 60

    21. b 22. b 23. c,d 24. a,c 25. b 26. b,f
    27. c,e 28. b 29. c 30. c 31. b 32. c
    33. a 34. b 35. b 36. a,d 37. a,c 38.a
    39.c 40.b 41.c 42.b 43.c 44.a,b
    45.c 46.a 47.a 48.a 49.b 50.c
    51. a 52. f 53. b 54. c 55. a  56. a
    57. b 58. a 59. a,b,c,d 60. c,e    

    对 SCJP模拟试题[2] 文章的评论    [查看网友评论]

    验证码:
    匿名发表: