Java exams

xiaoxiao2021-03-06  22

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

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 Labled "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 dispatch 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?

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 trysing to compile and rain the folowing code?

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 RESULT OF RUNNING THE FOLLOWING METHOD WITH AN INPUT OF 67?

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 APASE EXTENDS OBJECT IMPLEMENTS RUNNABLE

Class APDERIVED EXTENDS APASE IMPLEMENTS OBSERVER

Given Two Variables Created As Follows:

APBASE ABASE = New APASE ();

APDERIVED ADER = New APDERIVED ();

Which of The Following Java Statements Will Compile and Execute WITHOUT ERROR?

A. Runnable rn = adj;

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.ActionEventsuppose We Have The Following Code to Count events and save theme..

INT evtct = 0;

2. AWTEVENT LASTE;

3. Public void saveEvent (aff) {

4. Laste = evt;

5. EVTCT ;

6.}

Which of the following calls of saveevent Would Run WITHOUT CAUSING AN Exception.

A. Call with an afferevent object reference

B. Call with an ActionEvent Object Reference

C. Call with an offbject object reference

D. Call with Null Value

Q9.WHAT HAPPENS WHEN We attempt to compile and run the folload code?

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, Producting The Output "n = -1".

B. The Program Will Compile and Run, Producting The Output "n = 1".

C. A Runtime ArithmeticException Will Be thrown.

D. The Program Will Compile and Run, Producting The Output "n = 0".

Q10. You are Creating a Toolbase Class Which Will Be Extended by Other Program Contains a Single Abstract Method, CreateTool.

Which of the folloading 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. acthod ();

}

Public void amethod () {

// hERE

}

}

Which of the followingiff inster the compile // 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:

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 folloading are reserved Words in java?

A. IMPLEMENTS

B. Finally

C. Final

D. Sizeof

Q16.Which of this attempts to assign a value to a byte primitive variable is out of the byte by = (byte) 255;

B. BYTE B = (Byte) 128;

C. Byte B = (Byte) -128;

D. Byte B = (Byte) 127;

Q17.given the folowing method in an application:

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 folloading 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 CLASSESS 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?

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 folowing class:

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 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 folload Class Declarations Would Be Appropriate?

A. Class MyApp Extends WINDOW IMPLEMENTS RUNNABLE

B. Public Class MyApp Extends Runnablec. 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 FROM A FILE AND RETURNS A STRING. The Method May Throw AN IException 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 oException

B. Public String Readme (URL U) THROWS EXCEPTION

C. Public String Readme (URL u) throws runtimeException

D. Public String Readme (URL u) throws oException

Q24. What happens when We Try to compile this code and execute the Method with a NEGATIVE INPUT NUMBER?

Public string lookup (int N) {

2. IF (n> keys.length) {

3. String Key = "out of ing";

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?

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' Products Output of 1

B. INPUT OF 'X' Products Output of 0c. Input of 'D' Products 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?

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 rain the Following Code

Class base {

Public void base () {

System.out.println ("Base");

}

}

PUBLIC CLASS in Extends Base {

Public static void main (String Argv []) {

I = new in ();

}

}

A. Compile Time Error Base Is a Keyword

B. Runtime Error Base Has No Valid Constructionor

C. Compilation and no output at runtime

D. Output of Base

Q28.Which of the folloading 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 hope to add methods which overload the method in GenericFruit.

Select All of The Following Which Would Constitute Legal Declarations of Oveloading 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 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 What Multiple Widget Objects in a?

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 WEREH The widgets.

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 all classes in a large project. Which of the folloading 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.

Public int total (int rt)) {

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

B. FOR (i = 0; i

C. FOR (i = 0; 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. 3enchandeVening

Q37.What Happenshen 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 WHE TRY TO Compile and Run this code?

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, Producting "EQ".

C. The Program Will Compile and Run, Producing "Equalseq".

D. The Program Will Compile and Run, Producting "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 thing 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 country;

INT getCount () {return count;}

// constructor and more methods here

}

Suppose That You NEED TO Add A Method TO XBase TO CALL THE GETCOUNT METHOT 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 Beness Beness Be ixecuted:

Saytype (New Typeab ());

A. Object,

B. Object, Typea, Typeab, C. Typeab,

D. Object, Typeab,

Q43.Given a Situation in Which Three Threads Area in 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 folload?

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 forin the Same Package

B. Objects in Other Packages

C. ONLY Objects That Have Been Instantiated

D. All Objects forin 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. B2., B3., ABC13. AB17. A18. AD19. ABC20. A18. AD19. ABC20. BC21. C22. ABC20. BC21. C25. AB26. BD29. ACD30. B31. B32. CD35. B36. CD37. C38. B39. C40. C41. A42. B43. D44. D45. C49. A50. AC

转载请注明原文地址:https://www.9cbs.com/read-44136.html

New Post(0)