格林模拟试题二参考答案
answer 1)
1) the code will compile and run, printing out the words "my func"
a class that contains an abstract method must be declared abstract itself, but may contain non abstract methods.
answer 2)
4) the code will compile but will complain at run time that main is not correctly defined
in this example the parameter is a string not a string array as needed for the correct main method
answer 3)
1) public
2) private
4) transient
the keyword transient is easy to forget as is not frequently used. although a method may be considered to be friendly like in c++ it is not a java keyword.
answer 4)
2) the compiler will complain that the base class is not declared as abstract.
if a class contains abstract methods it must itself be declared as abstract
answer 5)
1) to get to access hardware that java does not know about
3) to write optimised code for performance in a language such as c/c++
answer 6)
4) success in compilation and output of "amethod" at run time.
a final method cannot be ovverriden in a sub class, but apart from that it does not cause any other restrictions.
answer 7)
4) compilation and execution without error
it would cause a run time error if you had a call to amethod though.
answer 8)
1)compile time error: base cannot be private
a top leve (non nested) class cannot be private.
answer 9)
4) p1 compiles cleanly but p2 has an error at compile time
the package statement in p1.java is the equivalent of placing the file in a different directory to the file p2.java and thus when the compiler tries to compile p2 an error occurs indicating that superclass p1 cannot be found.
answer 10)
2) an error at run time
this code will compile, but at run-time you will get an arrayindexoutofbounds exception. this becuase counting in java starts from 0 and so the 5th element of this array would be i[4].
remember that arrays will always be initialized to default values wherever they are created.
answer 11)
2)myarray.length;
the string class has a length() method to return the number of characters. i have sometimes become confused between the two.
answer 12)
3) a frame with one large button marked four in the centre
the default layout manager for a frame is the borderlayout manager. this layout manager defaults to placing components in the centre if no constraint is passed with the call to the add method.
answer 13)
4) do nothing, the flowlayout will position the component
answer 14)
1) use the setlayout method
answer 15)
1) ipadx
2) fill
3) insets
answer 16)
2) the buttons will run from left to right along the top of the frame
the call to the setlayout(new flowlayout()) resets the layout manager for the entire frame and so the buttons end up at the top rather than the bottom.
answer 17)
2) it is a field of the gridbagconstraints class for controlling component placement
3) a valid settting for the anchor field is gridbagconstraints.north
answer 18)
4) clean compile but no output at runtime
this is a bit of a sneaky one as i have swapped around the names of the methods you need to define and call when running a thread. if the for loop were defined in a method called public void run() and the call in the main method had been to b.start() the list of values from 0 to 9 would have been output.
answer 19)
2) false
you can re-use the same instance of the gridbagconstraints when added successive components.
answer 20)
4) an interface that ensures that implementing classes cannot contain duplicates
answer 21)
2) the add method returns false if you attempt to add an element with a duplicate value
i find it a surprise that you do not get an exception.
answer 22)
1) the program exits via a call to exit(0);
2) the priority of another thread is increased
3) a call to the stop method of the thread class
note that this question asks what can cause a thread to stop executing, not what will cause a thread to stop executing. java threads are somewhat platform dependent and you should be carefull when making assumptions about thread priorities. on some platforms you may find that a thread with higher priorities gets to "hog" the processor. you can read up on this in more detail at
answer 23)
4) the class can only access final variables
answer 24)
1) to call from the currently running thread to allow another thread of the same or higher priority to run
option 3 looks plausible but there is no guarantee that the thread that grabs the cpu time will be of a higher priority. it will depend on the threading algorithm of the java virtual machine and the underlying operating system
answer 25)
4) compilation and running with output 0 to 9
answer 26)
1) none of these options
because of the lack of a break statement after the break 10; statement the actual output will be
"ten" followed by "twenty"
answer 27)
4) system.gc();
answer 28)
1) compilation succeeds and at run time an output of 0 and false
the default value for a boolean declared at class level is false, and integer is 0;
answer 29)
1) compile time error
you will get an error saying something like "cant make a static reference to a non static variable". note that the main method is static. even if main was not static the array argv is local to the main method and would thus not be visible within amethod.
answer 30)
3) output of "not equal"
despite the actual character strings matching, using the == operator will simply compare memory location. because the one string was created with the new operator it will be in a different location in memory to the other string.
answer 31)
4) compile and run with output of "pausing" and "continuing" after a key is hit
an overriden method in a sub class must not throw exceptions not thrown in the base class. in the case of the method amethod it throws no exceptions and will thus compile without complain. there is no reason that a constructor cannot be protected.
answer 32)
4) compile time error because of the line creating the instance of inner
this looks like a question about inner classes but it is also a reference to the fact that the main method is static and thus you cannot directly access a non static method. the line causing the error could be fixed by changing it to say
inner i = new outer().new inner();
then the code would compile and run producing the output "inner"
answer 33)
1) error at compile time
if you implement an interface you must create bodies for all methods in that interface. this code will produce an error saying that mywc must be declared abstract because it does not define all of the methods in windowlistener. option 4 is nonsense as comments can appear anywhere. option 3 suggesting that it might compile but not produce output is ment to mislead on the basis that what looks like a constructor is actually an ordinary method as it has a return type.
answer 34)
4) compile time error
an error will be caused by attempting to define an integer as static within a method. the lifetime of a field within a method is the duration of the running of the method. a static field exists once only for the class. an approach like this does work with visual basic.
answer 35)
4)int z = 015;
the letters c and s do not exist as literal indicators and a string must be enclosed with double quotes, not single as in this case.
answer 36)
1)double
4)instanceof
note the upper case s on switch means it is not a keyword and the word then is part of visual basic but not java. also, instanceof looks like a method but is actually a keyword,
answer 37)
4) -3.0
answer 38)
3) one
command line parameters start from 0 and from the first parameter after the name of the compile (normally java)
answer 39)
4) the set is designed for unique elements.
collection is an interface, not a class. the collection interface includes a method called iterator. this returns an instance of the iterator class which has some similarities with enumerators.
the name set should give away the purpose of the set interface as it is analogous to the set concept in relational databases which implies uniquness.
answer 40)
2) if multiple listeners are added to a component the events will be processed for all but with no guarantee in the order
4) you may remove as well add listeners to a component.
it ought to be fairly intuitive that a component ought to be able to have multiple listeners. after all, a text field might want to respond to both the mouse and keyboard
answer 41)
1) b=m;
3) d =i;
you can assign up the inheritance tree from a child to a parent but not the other way without an explicit casting. a boolean can only ever be assigned a boolean value.
answer 42)
2) you can obtain a mutually exclusive lock on any object
3) a thread can obtain a mutually exclusive lock on an object by calling a synchronized method on that object.
4) thread scheduling algorithms are platform dependent
yes that says dependent and not independent.
answer 43)
2) ask for a re-design of the hierarchy with changing the operating system to a field rather than class type
this question is about the requirement to understand the difference between the "is-a" and the "has-a" relationship. where a class is inherited you have to ask if it represents the "is-a" relationship. as the difference between the root and the two children are the operating system you need to ask are linux and windows types of computers.the answer is no, they are both types of operating systems. so option two represents the best of the options. you might consider having operating system as an interface instead but that is another story.
of course there are as many ways to design an object hierarchy as ways to pronounce bjarne strousjoup, but this is the sort of answer that sun will proabably be looking for in the exam. questions have been asked in discussion forums if this type of question really comes up in the exam. i think this is because some other mock exams do not contain any questions like this. i assure you that this type of question can come up in the exam. these types of question are testing your understanding of the difference between the is-a and has-a relationship in class design.
answer 44)
1) an inner class may be defined as static
4) an inner class may extend another class
a static inner class is also sometimes known as a top level nested class. there is some debate if such a class should be called an inner class. i tend to think it should be on the basis that it is created inside the opening braces of another class. how could a programmer provide a constructor for an anonymous class?. remember a constructor is a method with no return type and the same name as the class. inner classes may be defined as private
answer 45
4) compilation and output of "not equal! 10"
the output will be "not equal 10". this illustrates that the output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false. if you change the value of b1 to true processing occurs as you would expect and the output is "we are equal 20";.
answer 46)
2)j= i<<j;
4)j=i<<l;
answer 47)
4) 12
as well as the binary or objective this questions requires you to understand the octal notation which means that the leading letter zero (not the letter o)) means that the first 1 indicates the number contains one eight and nothing else. thus this calculation in decimal mean
8|4
to convert this to binary means
1000
0100
----
1100
----
which is 12 in decimal
the | bitwise operator means that for each position where there is a 1, results in a 1 in the same position in the answer.
answer 48)
2)s+=i;
only a string acts as if the + operator were overloaded
answer 49)
although the objectives do not specifically mention the need to understand the i/o classes, feedback from people who have taken the exam indicate that you will get questions on this topic. as you will probably need to know this in the real world of java programming, get familiar with the basics. i have assigned these questions to objective 10.1 as that is a fairly vague objective.
1) file f = new file("/","autoexec.bat");
2) datainputstream d = new datainputstream(system.in);
3) outputstreamwriter o = new outputstreamwriter(system.out);
option 4, with the randomaccess file will not compile because the constructor must also be passed a mode parameter which must be either "r" or "rw"
answer 50)
1)o1=o2;
2)b=ob;
4)o1=b;
answer 51)
4) 10020
in the call
another(v,i);
a reference to v is passed and thus any changes will be intact after this call.
answer 52)
1) public void amethod(string s, int i){}
4) public void amethod(int i, string s) {}
overloaded methods are differentiated only on the number, type and order of parameters, not on the return type of the method or the names of the parameters.
answer 53)
4)base b = new base(10);
any call to this or super must be the first line in a constructor. as the method already has a call to this, no more can be inserted.
answer 54)
1) system.out.println(s);
4) system.out.println(iargs);
a class within a method can only see final variables of the enclosing method. however it the normal visibility rules apply for variables outside the enclosing method.
answer 55)
1) yield()
2) sleep
4) stop()
note, the methods stop and suspend have been deprecated with the java2 release, and you may get questions on the exam that expect you to know this. check out the java2 docs for an explanation
answer 56)
1) addelement
answer 57)
the import statement allows you to use a class directly instead of fully qualifying it with the full package name, adding more classess with the import statement does not cause a runtime performance overhad. i assure you this is true. an inner class can be defined with the protected modifier, though i am not certain why you would want to do it. an inner class can be defined with the private modifier, try compiling some sample code before emailing me to ask about this.
3)a inner class may under some circumstances be defined with the protected modifier
4) an interface cannot be instantiated
answer 58)
1) mousepressed(mouseevent e){}
4) componentadded(containerevent e){}
answer 59)
1) iterator
2) isempty
3) toarray
answer 60)
2) ensures only one thread at a time may access a method or object
end of document