Java Exams

该博客包含Java Exam1(Module1 - Module14)的50道题目,涵盖Panel组件添加、类的继承关系、MouseEvent对象、方法调用结果、代码编译运行情况等内容,并给出了各题的选项。这些题目有助于检验和提升对Java知识的掌握。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java Exam1 (Module1-Module14)

Q1.Suppose you have a working program that has a Panel, p1, with a BorderLayout which already has components in the NORTH, SOUTH, EAST, WEST and CENTER positions.

Now you add the following statement to the end of the method that creates the panel:

p1.add( new Button("Help") ) ;

When you try to compile and run the program, what happens?

A.          The compiler objects to the statement because p1 is already full.

B.           The Button labeled "Help" is shown in the CENTER position of the Panel.

C.           The original CENTER component is shown in the CENTER position of the Panel.

D.          The first component added to the container is discarded and all components move up one position. The "Help" button ends up in the CENTER.

 

Q2.Which of the following statements about the ancestry of the class java.awt.Applet is WRONG?

A.          An Applet is a kind of Container.

B.           An Applet is a kind of Window.

C.           An Applet is a kind of Component.

D.          An Applet is a kind of Panel.

 

Q3.Which of the following statements about MouseEvent objects is false?

A.          Every MouseEvent carries information about the location of the mouse in terms of int x and y position relative to the screen Component the event occurs in.

B.           A given MouseEvent may be dispatched to both MouseMotionListner and MouseListener objects that have registered with the screen Component.

C.           Every MouseEvent carries a timestamp.

D.          MouseEvents of the MOUSE_DRAGGED type may report negative values for x and y position.

 

Q4.What will be the result of calling the following method with an input of 2?

1. public int adder( int N ){

2.   return  0x100 + N++ ;

3. }

A.          The method will return 258.

B.           The method will return 102.

C.           The method will return 259.

D.          The method will return 103.

 

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

1. public class EqualsTest{

2.   public static void main(String args[]){

3.     Long L = new Long( 7 );

4.     if( L.equals( 7L )) System.out.println("Equal");

5.     else System.out.println("Not Equal");

6.   }

7. }

A.          The program compiles and prints "Equal".

B.           The program compiles and prints "Not Equal".

C.           The compiler objects to line 4.

D.          A runtime cast error occurs at line 4.

 

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

1. public int MaskOff( int N ){

2.    return N | 3 ;

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.

 

Q7.Suppose we have two classes defined as follows:

class  ApBase  extends Object implements Runnable

class  ApDerived  extends ApBase implements Observer

Given two variables created as follows:

  ApBase  aBase = new  ApBase() ;

  ApDerived  aDer = new  ApDerived();

Which of the following Java statements will compile and execute without error?

A.          Runnable rn = aDer ;

B.           Runnable rn2 = (Runnable) aBase ;

C.           Observer ob = aBase ;

D.          Observer ob2 = (Observer) aBase ;

 

Q8.Here is the class hierarchy showing the ActionEvent family tree:

java.lang.Object

  |--- java.util.EventObject

           |--- java.awt.AWTEvent

                        |--- java.awt.event.ActionEvent

Suppose we have the following code to count events and save the most recent event.

1. int  evtCt = 0 ;

2. AWTEvent  lastE ;

3. public void  saveEvent( AWTEvent evt ){

4.   lastE = evt ;

5.   evtCt++ ;

6. }

Which of the following calls of saveEvent would run without causing an exception.

A.          call with an AWTEvent object reference

B.           call with an ActionEvent object reference

C.           call with an EventObject object reference

D.          call with null value

 

Q9.What happens when we attempt to compile and run the following code?

1. public class Logic {

2.   static int minusOne = -1 ;

3.   static public void main(String args[] ){

4.      int N = minusOne >>> 31 ;

5.      System.out.println("N = " + N );

6.   }

7. }

A.          The program will compile and run, producing the output "N = -1".

B.           The program will compile and run, producing the output "N = 1".

C.           A runtime ArithmeticException will be thrown.

D.          The program will compile and run, producing the output "N = 0".

 

Q10. You are creating a ToolBase class  which will be extended by other  programmers. The ToolBase class contains a single abstract method, createTool.

Which of the following statements are true?

A.          The ToolBase class must be declared abstract.

B.           Classes  extending ToolBase must not be declared abstract.

C.           The ToolBase class must not be declared final.

D.          The following variable declaration is illegal in any context:  ToolBase  myTB ;

 

Q11.Given the following method which will be called with various input values.

public void soundOff( int x ){

   switch(x){

     case 1: System.out.print("One ");

     case 2: System.out.print("Two "); break ;

     case 3: System.out.print("Three ");

     default: System.out.print("Do what?");

   }

 }

Which of these input and output pairs will be observed?

A.          Input = 1, Output = "One"

B.           Input = 0, Output = "One Two"

C.           Input = 3, Output = "Three Do What?"

D.          Input = 4, Output = "Do What?"

Q12..Given the following code

class Base{

static int oak=99;

}

public class Doverdale extends Base{

public static void main(String argv[]){

        Doverdale d = new Doverdale();

        d.amethod();

        }

        public void amethod(){

        //Here

        }       

}

Which of the following if placed after the comment //Here, will compile and modify the value of the variable oak?

A.          super.oak=1;

B.           this.oak=33;

C.           Base.oak=22;

D.          oak=50.1;

Q13.How many String objects are created in the following code:

1. String A, B, C ;

2. A = "1234" ;

3. B = A ;

4. C = A + B ;

A.          1

B.           2

C.           3

D.          4

Q14. Given the following variables

char c = 'c';

int i = 10;

double d = 10;

long l = 1;

String s = "Hello";

Which of the following will compile without error?

A.          c=c+i;

B.           s+=i;

C.           i+=s;

D.          c+=s;

 

Q15. Which of the following are reserved words in Java?

A.          implements

B.           finally

C.           final

D.          sizeof

 

Q16.Which of these attempts to assign a value to a byte primitive variable is out of the byte range?

A.          byte b = (byte) 255;

B.           byte b = (byte) 128 ;

C.           byte b = (byte) -128 ;

D.          byte b = (byte) 127 ;

 

Q17.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

D.          Compile error

 

Q18.Which of the following does NOT inherit from java.awt.MenuItem? Check all correct answers.

A.          java.awt.MenuComponent

B.           java.awt.Menu

C.           java.awt.CheckboxMenuItem

D.          java.awt.MenuSeparator

Q19.Given the folowing classes which of the following will compile without error?

interface IFace{}

class CFace implements IFace{}

class Base{}

public class ObRef extends Base{

    public static void main(String argv[]){

        ObRef ob = new ObRef();

        Base b = new Base();

        Object o1 = new Object();

        IFace o2 = new CFace();

    }

}

A.          o1=o2;

B.           b=ob;

C.           o1=b;

D.          ob=b;

 

Q20. The code defines the class BigWidget as extending Widget.

In line 4, the XXXXXX stands for an access modifier for the method setWidth.

Which of the following modifiers, used in line 4 instead of XXXXXX would allow the BigWidget class  to access the setWidth method (as in line 10) but prevent unrelated classes in other packages from calling setWidth?

1. public class Widget extends Object{

2.     protected static final int maxWidth = 40 ;

3.     private int myWidth ;

4.     XXXXXX void setWidth( int n ) {

5.         myWidth = n ;

6.     }

7. }

8. class BigWidget extends Widget {

9.     BigWidget() {

10.        setWidth( maxWidth * 4 );

11.    }

12.}

A.          private

B.           protected

C.           blank - i.e., the method declaration would read   void setWidth( int n )

D.          friend

 

Q.21Given the following class:

1. class Widget extends Thingee{

2.    static final int maxWidgetSize = 40 ;

3.    static String Title ;

4.    public Widget( int mx, String T  ){

5.       maxWidgetSize = mx ;

6.       Title = T ;

7.    }

8.       // other code

9. }

What will happen on trying to compile this code and run a program which creates a Widget as follows?

10.  Widget myWidget = new Widget( 50, "Bigger Widget");

A.          The program compiles and runs fine.

B.           The program compiles but gets a runtime error in line 5.

C.           The compiler objects to line 5.

D.          The compiler objects to line 6.

 

Q22. Your assignment is to write an application with a typical graphic user  interface  which will have its  own Thread to carry out computations.

Which of the following class declarations would be appropriate?

A.          class MyApp extends Window implements Runnable

B.           public class MyApp extends Runnable

C.           public class MyApp extends Frame implements Runnable

D.          abstract class MyApp extends Frame, Runnable

 

Q23.You have written a method which takes a URL input, reads a single line from a file and returns a String. The method may throw an IOException but there are no try - catch statements in it.

Check all of the following which are legal declarations for this method?

A.          public String readMe( URL u) may throw IOException

B.           public String readMe( URL u) throws Exception

C.           public String readMe( URL u) throws RunTimeException

D.          public String readMe( URL u) throws IOException

 

Q24. What happens when we try to compile this code and execute the method with a negative input number?

1. public String lookup( int n ){

2.    if( n > keys.length ){

3.      String key = "out of range" ;

4.    }

5.    else {

6.      key = keys[n] ;

7.    }

8.    return key ;

9.}

A.          An ArrayIndexOutOfBoundsException exception is thrown in line 6.

B.           The "out of range" message is returned.

C.           A compiler error prevents compilation.

D.          The null value is returned.

 

Q25.The method shown takes a char input and outputs an int value.

Given that a program creates and starts a TimeOut object, which of the following statements is true?

1. public int maze( char d ){

2.   if( d <= 'N' ){

3.     if( d == 'E' ) return 2 ;

4.     return 1 ;

5.   }

6.   else if( d == 'S' ) return 3;

7.   else if( d == 'W' ) return 4;

8.   return 0 ;

9. }

A.          Input of 'A' produces output of 1

B.           Input of 'X' produces output of 0

C.           Input of 'D' produces output of 0

D.          The method fails to compile due to syntax errors.

 

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

1. public int MaskOff( int N ){

2.     return N ^ 3 ;

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.

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

class Base{

    public void Base(){

                    System.out.println("Base");

    }

}

public class In extends Base{

    public static void main(String argv[]){

                    In i=new In();

    }

}

A.          Compile time error Base is a keyword

B.           Runtime error Base has no valid constructor

C.           Compilation and no output at runtime

D.          Output of Base

Q28.Which of the following methods can be legally 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

}

A.          void amethod(int i) throws Exception {}

B.           void amethod(long i)throws Exception {}

C.           public void amethod(int i) throws Exception {}

D.          void amethod(long i){}

 

Q29. The GenericFruit class declares the following method.

public void setCalorieContent( float f )

You are writing a class Apple to extend GenericFruit and wish to add methods which overload the method in GenericFruit.

Select all of the following which would constitute legal declarations of overloading methods.

A.          protected float setCalorieContent(String s )

B.           protected void setCalorieContent( float x )

C.           public void setCalorieContent( double d )

D.          public void setCalorieContent(String s ) throws NumberFormatException

 

Q30. 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-- ;

 

Q31.If you construct a Panel with a GridLayout and add Buttons to it as follows:

Panel p = new Panel();

p.setLayout( new GridLayout() );

for( int i = 1 ; i <= 6 ; i++ ){

  p.add( new Button( Integer.toString( i ) ) );

}

What will be the result when the Panel is resized to 200 x 200 and is shown?

A.          Only the Button "6" would be showing, filling the Panel.

B.           Six narrow buttons in a row.

C.           Six thin buttons in a column.

D.          The number showing would depend on the font size.

 

Q32. What happens when we try to compile the class and use multiple Widget objects in a program?

1. class Widget extends Thingee{

2.    static private int widgetCount = 0 ;

3.    public String wName ;

4.    int wNumber ;

5.

6.    static synchronized int addWidget(){ widgetCount++ ;

7.        wName = "I am Widget # " + widgetCount ;

8.        return widgetCount ;

9.    }

10.    public Widget(){

11.      wNumber = addWidget();

12.    }

13. }

A.          The class compiles and each Widget will get a unique wNumber  and  wName reflecting the order in which the Widgets were created.

B.           The compiler objects to line 7.

C.           A runtime error occurs in the addWidget method.

D.          The class compiles and each Widget gets the same wNumber and wName.

 

Q33.Which of the following class declarations are incorrect use of modifier keywords?

A.          public  synchronized class FastClass  extends Thread

B.           private  protected class FastClass

C.           public  abstract class  FastClass

D.          class  FastClass  extends  Thread

 

Q34. 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

 

Q35.A method to compute the sum of all elements in an array of int is needed.

The following proposed method is incomplete, select the correct statement for line 3 from the options provided.

1. public int total( int[] x ){

2.   int i, t = 0 ;

3.   -select statement to go here

4.   { t += x[ i++ ] ;

5.   }

6.   return t ;

7. }

A.          for( int i = 0 ; i < x.length ; )

B.           for( i = 0 ; i < x.length ; )

C.           for( i = 0 ; i < x.length ; i++ )

D.          for( i = 1 ; i <= x.length ; i++ )

 

Q36. Which of the following would be an illegal identifier for a Java variable?

A.          _yourStuff

B.           $money

C.           %path

D.          3enchantedEvening

 

Q37.What happens when we try to compile and run code containing the following lines:

1. String s = "12345" ;

2. String t = new String( s ) ;

3. if( s == t ) System.out.println( t + "==" + s);

4. else System.out.println( t + "!=" + s );

A.          The compiler objects to the use of == with reference variables in line 3.

B.           The program compiles and prints "12345==12345".

C.           The program compiles and prints "12345!=12345".

D.          A runtime exception occurs in line 3.

 

Q38. What will happen when we try to compile and run this code?

1. public void testX() {

2.   Integer nA = new Integer( 4096 );

3.   Long nB = new Long( 4096 ) ;

4.   if( nA.equals( nB )) System.out.println("equals");

5.   if( nA.intValue() == nB.longValue() ){

6.       System.out.println("EQ");

7.   }

8. }

A.          The compiler will object to line 4 because the object types of nA and nB don't match.

B.           The program will compile and run, producing "EQ".

C.           The program will compile and run, producing "equalsEQ".

D.          The program will compile and run, producing "equals".

 

Q39.You attempt to compile and run the following code with an input value of 5.What happens?

1.public void countdown(int n){

2.    while(n) System.out.println(“– ” + n--);

3.}

A.          The program outputs “–5 –4 –3 –2 –1”.

B.           The program outputs “–4 –3 –2 –1 –0”.

C.           The program fails to compile

D.          The program fails at runtime.

 

Q40.Consider the following partial class definitions of a Base class and a class that extends it:

class Base extends Object{

           int count=9;

           //more methods here

}

class XBase extends Base{

           int count;

           //constructor and more methods here

}

Assuming that nothing in the XBase class constructor modifier the count variable,what will the following code fragment print?

XBase x=new XBase();

Base b=x;

System.out.println(“Count is ”+b.count);

A.          Count is null

B.           Count is 0

C.           Count is 9

D.          None of the above

 

Q41.Consider the partial class definitions of a Base class and a class, XBase,that extends it. Note that XBase has a getCount method that overrides a methods in Base.

class Base extends Object{

           private int count=9;

           int getCount(){return count;}

           //constructor and more methods here

}

class XBase extends Base{

           int count;

           int getCount(){return count;}

           //constructor and more methods here

}

Suppose that you need to add a method to XBase to call the getCount method in the Base class.Which of the following would accomplish this?

A.          int getParentCount(){return super.getCount();}

B.           int getParentCount(){return Base.getCount();}

C.           int getParentCount(){return ((Base)this).getCount();}

D.          None of the above

 

Q42. Given the following hierarchical relationship of several classes:

Object

  |---TypeA

  |     |-----TypeAB

  |     |-----TypeAC

  |--------TypeY

And given the following method definition:

public sayType(Object x ){

 if(x instanceof Object )System.out.print("Object,");

 if(x instanceof TypeA  )System.out.print("TypeA,");

 if(x instanceof TypeAB )System.out.print("TypeAB,");

 if(x instanceof TypeAC )System.out.print("TypeAC,");

}

What would the program output be if the following line was executed:

  sayType( new TypeAB() );

A.          Object,

B.           Object,TypeA,TypeAB,

C.           TypeAB,

D.          Object,TypeAB,

 

Q43.Given a situation in which three Threads area in a wait for the same object, which of the following statements describes what happens when this object’s notify methods is called?

A.          The Thread that has been waiting longest becomes runnable

B.           The Thread that was created first becomes runnable

C.           All Threads become runnable

D.          One Thread becomes runnable,but you can’t predict which one.

 

Q44.What will happen when you attempt to compile and run this code?

public class Mickle implements Runnable{

           public static void main(String args[]){

                      Mickle m=new Mickle();

                      Thread t=new Thread(m);

           }

           public void run(){

                      System.out.println(“Run method called”);

           }

}

A.          Compile time error

B.           Runtime error

C.           Compilation and output of “Run method called”

D.          Compilation and no output at runtime

 

Q45.Which of the following is the immediate parent class of the java.awt.Frame class?

A.          java.awt.Panel

B.           java.awt.Window

C.           java.awt.Dialog

D.          java.awt.Container

 

Q46. Which of the following can you perform using the File class?

A.          Change the current directory

B.           Return the name of the parent directory

C.           Delete a file

D.          Find if a file contains text or binary information

 

Q47.What will the following program print out on the first line of output?

public class MyStaticClass{

           static String s=”1”;

public MyStaticClass(){

                      s=”2”;

}

public static void main(String[] args){

                     new MyStaticClass();

                      System.out.println(s);

}

static {  s=”3”;

}

}

A.          1

B.           2

C.           3

D.          null

 

Q48.The class declared by

pubic class MyClass implements MyInterface

is a subclass of which of the following?

A.          Component

B.           MyInterface

C.           Object

D.          Not a subclass

 

Q49.A class that doesn’t have an access modifier in its declaration is visible to which of the following?

A.          Objects within the same package

B.           Objects in other packages

C.           Only objects that have been instantiated

D.          All objects within the program

 

Q50.Which of the following methods can a Web browser call in a java applet?

A.          public void start()

B.           public static void main(String args[])

C.           public void init()

D.          public void finish()

 

 answer

1.        B

2.        B

3.        B

4.        A

5.        C

6.        C

7.        AB

8.        ABD

9.        B

10.     AC

11.     CD

12.     ABC

13.     B

14.     B

15.     ABC

16.     AB

17.     A

18.     AD

19.     ABC

20.     BC

21.     C

22.     A/AC

23.     BD

24.     C

25.     AB

26.     B

27.     C

28.     BD

29.     ACD

30.     B

31.     B

32.     B

33.     AB

34.     CD

35.     B

36.     CD

37.     C

38.     B

39.     C

40.     C

41.     A

42.     B

43.     D

44.     D

45.     B

46.     BC

47.     B

48.     C

49.     A

50.     AC

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值