有疑问的题目

What will happen if you attempt to compileand 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 

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


Which of the following statements are true?

 

1) System.out.println( -1 >>>2);will output a result larger than 10

2) System.out.println( -1 >> 2); willoutput a positive number  错误!

3) System.out.println( 2 >> 1); willoutput the number 1

4) System.out.println( 1 <<< 2);will output the number 4


1) System.out.println( -1 >>> 2);will output a result larger than10
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 class

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 << 2would output 4

Because of the way twos complement number representation works theunsigned right shift operation means a small shift in a negative number canreturn a very large value so the output of option 1 will be much larger than10.

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


What will happen when you attempt tocompile and run the following code?

 

public class Tux extendsThread{       

static String sName = "vandeleur";       

public static void main(String argv[]){       

Tux t = new Tux();       

t.piggy(sName);       

System.out.println(sName);               

}       

public void piggy(String sName){               

sName = sName + " wiggy";       

start();       

}       

public void run(){               

for(int i=0;i  <  4; i++){               

sName = sName + " " + i;                       

}       

}

}

 

1) Compile time error

2) Compilation and output of"vandeleur wiggy"

3) Compilation and output of"vandeleur wiggy 0 1 2 3"

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


4) Compilation and output of either "vandaleur", "vandaleur0", "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 thesystem that the underlying OS uses for allocating cycles for a Thread. Thechances are that once the thread has been spun off in the call to start in themethod piggy the main method will run to completion and the value of sName willstill be vandeluer before the Thread modifies it. You cannot be certain of thisthough.


Which statement is true of the followingcode?

 

public class Agg{

public static void main(String argv[]){    

Agg a = new Agg();      

a.go();    

public void go(){             

DSRoss ds1 = new DSRoss("one");            

ds1.start();     

}

}

class DSRoss extends Thread{

private String sTname="";

DSRoss(String s){    

sTname = s;

}

public void run(){     

notwait();      

System.out.println("finished");

}public void notwait(){    

while(true){          

try{                  

System.out.println("waiting");            

wait();                   

}catch(InterruptedException ie){}            

System.out.println(sTname);           

notifyAll();       

}   

}  }

1) It will cause a compile time error

2) Compilation and output of"waiting"

3) Compilation and output of"waiting" followed by "finished"

4) Runtime error, an exception will bethrown


4) Runtime error, an exception will be thrown

A call to wait/notify must be within synchronized code. With JDK1.2 thiscode throws the error message

java.lang.IllegalMonitorStateException: current threadnot owner
        at java.lang.Object.wait(NativeMethod)
        atjava.lang.Object.wait(Object.java:424)
        at DSRoss.notwait(Compiled Code)
        at DSRoss.run(Agg.java:21)


Which of the following methods can belegally inserted in place of the comment //Method Here ?

 

class Base{ public void amethod(int i) { }}

public class Scope extends Base{

public static void main(String argv[]){ }

//Method Here

}

1) void amethod(int i) throws Exception {}

2) void amethod(long i)throws Exception {}

3) void amethod(long i){}

4) public void amethod(int i) throwsException {} 


2,3

Options 1, & 4 will not compile as they attempt to throw Exceptionsnot declared in the base class. Because options 2 and 3 take a parameter oftype long they represent overloading not overriding and there is no suchlimitations on overloaded methods. 


What will happen if you attempt to compileand run the following code?

Integer ten=newInteg

Longnine=new Long (9);

System.out.println(ten+ nine);

int i=1;

System.out.println(i + ten);

1) 19 followed by 20

2) 19 followed by 11

3) Compile time error

4)10 followed by 1


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 havesimilar 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. Theobjectives do not specifically mention the wrapper classes but don't besurprised if they come up. 


If you run the code below, what getsprinted out?

 

String s=new String("Bicycle");

int iBegin=1;

char iEnd=3;

System.out.println(s.substring(iBegin,iEnd));

1) Bic

2) ic

3) icy

4) error: no method matchingsubstring(int,char) 


2) ic

This is a bit of a catch question. Anyone with a C/C++ background wouldfigure out that addressing in strings starts with 0 so that 1 corresponds to iin the string Bicycle. The catch is that the second parameter returns theendcharacter minus 1. In this case it means instead of the "icy"being returned as intuition would expect it is only "ic".


Given the following declarations

 

String s1=new String("Hello");

String s2=new String("there");

String s3=new String();

Which of the following are legaloperations?

 

1) s3=s1 + s2;

2) s3=s1-s2;

3) s3=s1 & s2;

4) s3=s1 && s2 


1) s3=s1 + s2;

Javadoes not allow operator overloading as in C++, but for the sake of conveniencethe + operator is overridden for strings.


What will happen when you attempt tocompile and run the following code?.

 

class Background implements Runnable{    

int i=0;             

public int run(){              

while(true){                        

i++;                         

System.out.println("i="+i);                           

} //End while                

return 1;    

}//End run}

//End class

1) It will compile and the run method willprint out the increasing value of i.

2) It will compile and calling start willprint out the increasing value of i.

3) The code will cause an error at compiletime.

4) Compilation will cause an error becausewhile cannot take a parameter of true. 


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.

Anyclass that is implements an interface must create a method to match all of themethods in the interface. The Runnable interface has one method called run thathas a void return type.The sun compiler gives the error

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


Which of the following statements aboutthis code are true?

 

public class Morecombe{public static voidmain(String argv[]){      

Morecombe m = new Morecombe();

m.go(new Turing(){});     

}

public void go(Turing t){ 

t.start();  

}

}

class Turing extends Thread{

       publicvoid run(){        

for(int i =0; i < 2; i++){                   

System.out.println(i);             }           

}      

}

 

1) Compilation error due to malformedparameter to go method

2) Compilation error, class Turing has nostart method

3) Compilation and output of 0 followed by1

4) Compilation but runtime error


3) Compilation and output of 0 followed by 1

The creation of an anonymous class as a parameter to go is fairly strangeas you would expect it to override a method in its parent class (Turing). Youdon't have to though. The fact that class Turing extends Thread means theanonymous instance that is passed to go has a start method which then calls therun method.


If you create a TextField  with a constructor to set it to occupy 5columns, what difference will it make if you use it with a proportional font(ie Times Roman) or a fixed pitch typewriter style font (Courier).

 

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

2)With a fixed font you will see 5characters,with a  proportional it willcause the field to expand to fit the text

3)The columns setting does not affect thenumber of characters displayed

4)Both will show exactly 5 characters 


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

Witha proportional font the letter w will occupy more space than the letter i. Soif you have all wide characters you may have to scroll to the right to see theentire text of a TextField. 


You need to create a class that will storeunique object elements. You do not need to sort these elements but they must beunique.

 

What interface might be most suitable tomeet this need?

 

1)Set

2)List

3)Map

4)Vector 


1) Set

The Set interface ensures that its elements are unique, but does not orderthe elements. In reality you probably wouldn't create your own class using theSet interface. You would be more likely to use one of the JDK classes that usethe Set interface such as HashSet or TreeSet. 


You have created a simple Frame andoverridden the paint method as follows

 

public void paint(Graphicsg){g.drawString("Dolly",50,10);}

 

What will be the result when you attempt tocompile and run the program?

 

1) The string "Dolly" will bedisplayed at the centre of the frame

2) An error at compilation complaining atthe signature of the paint method

3) The lower part of the word Dolly will beseen at the top of the frame, with the top hidden.

4) The string "Dolly" will beshown at the bottom of the frame. 


3) The lower part of the word Dolly will be seen at the top of the form

The Second parameter to the drawstring method indicates where the baselineof the string will be placed. Thus the 3rd parameter of 10 indicates the Ycoordinate to be 10 pixels from the top of the Frame. This will result in justthe bottom of the string Dolly showing up or possibly only the descending partof the letter y. 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值