无忧首页企业系统我的无忧
无忧服务:
兼职活动培训
娱乐交友:
交友社区资讯
全职实习:
实习暑假寒假
微信号:school51
扫一下,立即关注
加关注
在线支付,立省10元
下载新版APP
===大学生成长生活平台===

格林模拟试题一参考答案

2012-12-26来源/作者:卫凯点击次数:878

 

answer 1)

5) int i=10;

explanation:
1) 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
 

2) 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';

3) 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

4) a boolean value can either be true or false, null is not allowed


answer 2)

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

because main is defined as static you need to create an instance of the class in order to call any non-static 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 convention and any acceptable identifier for a string array can be used. answers 3 and 4 are just nonsense.

 

answer 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)

1) a byte is a signed 8 bit integer.
 + l`64qm^nIpL^kRo=[本资料来源于贵州学习网 http://www.gzu521.com]+ l`64qm^nIpL^kRo=


answer 5)

4) exception raised: "java.lang.arrayindexoutofboundsexception: 2"

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.


answer 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)

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)

4) 0

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)

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)

3) 0

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


answer 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)

3) one, two, default

code will continue to fall through a case statement until it encounters a break.


answer 13)

2) default, zero

although it is normally placed last the default statement does not have to be the last item as you fall through the case block. 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)

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)

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)

1) methods cannot be overriden to be more private

static methods cannot be overriden but they can be overloaded. if you have doubts about that statement, please follow and read carefully the link given to the sun tutorial below. there is no logic or reason why private methods should not be overloaded. option 4 is a jumbled up version of the limitations of exceptions for overriden methods


answer 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 is a problem, 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)

1) system.out.println( -1 >>> 2);will output a result larger than 10
2) system.out.println( -1 >>> 2); will output a positive number
3) system.out.println( 2 >> 1); will output the number 1

you can test this with the following classm

public class shift{
static int i=2;
public static void main(string argv[]){
system.out.println( -1 >>> 2);
system.out.println( -1 >>> 2);
system.out.println( 2 >> 1);
}

}

java does not have a <<< operator. the operation 1 << 2 would output 4

because of the way twos complement number representation works the unsigned right shift operation means a small shift in a negative number can return a very large value so the output of option 1 will be much larger than 10.

the unsigned right shift places no significance on the leading bit that indicates the sign. for this shift the value 1 of the bit sign is replaced with a zero turning the result into a positive number for option 2.

 

answer 19)

4) compilation and output of either "vandaleur", "vandaleur 0", "vandaleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"

if that seems a vauge answer it is because you cannot be certain of the system that the underlying os uses for allocating cycles for a thread. the chances are that once the thread has been spun off in the call to start in the method piggy the main method will run to completion and the value of sname will still be vandeluer before the thread modifies it. you cannot be certain of this though.


answer 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 allow the buttons to both appear side by side, given the appropriate font and size.
applets and panels have a default flowlayout manager


answer 21)

1,2

value for i=1 value for j=1
value for i=2 value for j=1
 

the statement continue outer causes the code to jump to the label outer and the for loop increments to the next number.


answer 22)

4) runtime error, an exception will be thrown

a call to wait/notify must be within synchronized code. with jdk1.2 this code throws the error message

java.lang.illegalmonitorstateexception: current thread not owner
at java.lang.object.wait(native method)
at java.lang.object.wait(object.java:424)
at dsross.notwait(compiled code)
at dsross.run(agg.java:21)


answer 23)

2,3

options 1, & 4 will not compile as they attempt to throw exceptions not declared in the base class. because options 2 and 3 take a parameter of type long they represent overloading not overriding and there is no such limitations on overloaded methods.

 

answer 24)

3) system.out.println(math.ceil(-4.7));

options 1 and 2 will produce -5 and option 4 will not compile because the min method requires 2 parameters.
 


answer 25)

3) compile time error

the wrapper classes cannot be used like primitives.

depending on your compiler you will get an error that says someting like "error: can't convert java lang integer". wrapper classes have similar names to primitives but all start with upper case letters.

thus in this case we have int as a primitive and integer as a wrapper. the objectives do not specifically mention the wrapper classes but don't be surprised if they come up.


answer 26)

2) ic

this is a bit of a catch question. anyone with a c/c++ background would figure out that addressing in strings starts with 0 so that 1 corresponds to i in the string bicycle. the catch is that the second parameter returns the endcharacter minus 1. in this case it means instead of the "icy" being returned as intuition would expect it is only "ic".


answer 27)

3) s.indexof('v');

charat returns the letter at the position rather than searching for a letter and returning the position, mid is just to confuse the basic programmers, indexof(s,'v'); is how some future vb/j++ nightmare hybrid, might perform such a calculation.


answer 28)

1) s3=s1 + s2;

java does not allow operator overloading as in c++, but for the sake of convenience the + operator is overridden for strings.


answer 29)

4) 7

the | is known as the or operator, you could think of it as the either/or operator. turning the numbers into binary gives

4=100

3=011

for each position, if either number contains a 1 the result will contain a result in that position. as every position contains a 1 the result will be

111

which is decimal 7.

 

answer 30)

1,2,3

public, private, static are all legal access modifiers for this inner class.


answer 31)

3) output of first0, first1, second0, second1

note that this code overrides and calls the start method. if you wished to get the output mixed you would need to override the run method but call the start method.


answer 32) 

2) setlayout(new gridlayout(2,2));
 

changing the layout manager is the same for an applet or an application. answer 1 is wrong though it might have been a reasonable name for the designers to choose. answers 3 and 4 are incorrect because changing the layout manager always requires an instance of one of the layout managers and these are bogus methods.

instead of creating the anonymous instance of the layout manager as in option 2 you can also create a named instance and pass that as a parameter. this is often what automatic code generators such as borland/inprise jbuilder do.


answer 33)

3) the code will cause an error at compile time

the error is caused because run should have a void not an int return type.

any class that is implements an interface must create a method to match all of the methods in the interface. the runnable interface has one method called run that has a void return type.the sun compiler gives the error

method redefined with different return type: int run() was defined as void run();
 


answer 34)


3) compilation and output of 0 followed by 1

the creation of an anonymous class as a parameter to go is fairly strange as you would expect it to override a method in its parent class (turing). you don't have to though. the fact that class turing extends thread means the anonymous instance that is passed to go has a start method which then calls the run method.

 

answer 35)

4) compile time error
 

the only operator overloading offered by java is the + sign for the string class. a char is a 16 bit integer and cannot be concatenated to a string with the + operator.


answer 36)

3) if(s.equalsignorecase(s2))

string comparison is case sensitive so using the equals string method will not return a match. using the==operator just compares where memory address of the references and nocasematch was just something i made up to give me a fourth slightly plausible option.


answer 37)

1) s.setbackground(color.pink);

for speakers of the more british spelt english note that there is no letter u in color. also the constants for colors are in lower case.


answer 38)

4) the file class does not support directly changing the current directory.

this seems rather surprising to me, as changing the current directory is a very common requirement. you may be able to get around this limitation by creating a new instance of the file class passing the new directory to the constructor as the path name.


answer 39)

1)with a fixed font you will see 5 characters, with a  proportional it will depend on the width of the characters

with a proportional font the letter w will occupy more space than the letter i. so if you have all wide characters you may have to scroll to the right to see the entire text of a textfield.


answer 40)

3) on the line after //two put super(10);

constructors can only be invoked from within constructors.

 

answer 41)

3) 10 and 40

when a parameter is passed to a method the method receives a copy of the value. the method can modify its value without affecting the original copy. thus in this example when the value is printed out the method has not changed the value.


answer 42)

4) for(int i=0; i< ia.length;i++)

although you could control the looping with a literal number as with the number 4 used in option 3, it is better practice to use the length property of an array. this provides against bugs that might result if the size of the array changes. this question also checks that you know that arrays starts from zero and not one as option 3 starts from one. remember that array length is a field and not a function like the string length() method.


answer 43)

1) error at compile time

this is a slightly sneaky one as it looks like a question about constructors, but it is attempting to test knowledge of the use of the private modifier. a top level class cannot be defined as private. if you didn't notice the modifier private, remember in the exam to be real careful to read every part of the question.


answer 44)

3)10

the name of the class might give you a clue with this question, oct for octal. prefixing a number with a zero indicates that it is in octal format. thus when printed out it gets converted to base ten. 012 in octal means the first column from the right has a value of 2 and the next along has a value of one times eight. in decimal that adds up to 10.


answer 45)

1) error at compile time

the variable i is created at the level of amethod and will not be available inside the method multi. 
 


answer 46)

1) set

the set interface ensures that its elements are unique, but does not order the elements. in reality you probably wouldn't create your own class using the set interface. you would be more likely to use one of the jdk classes that use the set interface such as hashset or treeset.

 

answer 57)

4) a collection for storing bits as on-off information, like a vector of bits

this is the description given to a bitset in bruce eckels "thinking in java" book. the reference to unique sequence of bits was an attempt to mislead because of the use of the word set in the name bitset. normally something called a set implies uniqueness of the members, but not in this context.


answer 58)

4)compile error: superclass class1.base of class class1.class1 not found

using the package statement has an effect similar to placing a source file into a different directory. because the files are in different packages they cannot see each other. the stuff about file1 not having been compiled was just to mislead, java has the equivalent of an "automake", whereby if it was not for the package statements the other file would have been automatically compiled.


answer 59)

4) output of over.amethod()

the names of parameters to an overridden method is not important, but as the version of amethod in class base is set to be private it is not visible within over (despite over extending base) and thus does not take part in overriding. 
 


answer 60)

1) set the gridy value of the gridbagconstraints class to a value increasing from 1 to 4

answer 4 is fairly obviously bogus as it is the gridbagconstraints class that does most of the magic in laying out components under the gridbaglayout manager. the fill value of the gridbagconstraints class controls the behavior inside its virtual cell and the ipady field controls the internal padding around a component.

if you have a copy of the roberts and heller java2 guide that says the exam does not cover the gridbaglayout, this is an error. you can confirm this by looking at the online errata at





相关阅读



关于我们 | 联系我们 | 用户指南 | 网站地图 | 意见建议 | 会员注册 | 用户协议 | 隐私政策