java笔试题(en)---200道附加答案

[code="java"]Q1 Which of the following statements are valid, given the following variable declarations: boolean a; boolean b; int c;
1) (a | b)
2)(a || a)
3)(a ^ b) | c
4)(a & c)
5)(a && c)
Q2 Which of the following can be applied to constructors:
1) final
2) static
3) synchronized
4) native
5) None of these.
Q3 Which of the following retain their preferred size (width and height) when added (individually) to the North section of a container with a BorderLayout (assume that no other components or containers are present in the North section).
1) TextArea
2) Button
3) TextField
4) Checkbox
5) None. All of these mentioned components will only retain their preferred height.
Q4 Which of the following are legal names for variables.
1) _int
2) %large
3) $fred
4) Integer
5) 2much
Q5 Which of the following are correct ways to create a font.
1) Font f = new Font("Serif", Font.BOLD, 24);
2) Font f = new Font(Font.SERIF, "Bold", 24);
3) Font f = new Font("Serif", "Bold", 24);
4) Font f = new Font(Font.SERIF, Font.BOLD, 24);
Q6 Select the correct statements regarding the following piece of code.
File f = new File("c:\\large.txt");
1) On execution, a file called "large.txt" will be created on the local harddisk.
2) The code fails to compile on a UNIX machine, because the directory separator is not correct.
3) A file is NOT created on the harddisk when this code is executed.
4) An exception is thrown at runtime if the file "large.txt" already exists.
5) The code fails to compile, since this is not a valid constructor for the File class.
Q7 Which of the following statements are correct regarding the RandomAccessFile class?
1) An IOException is thrown if the specified file doesn't exist when created using the "r" mode.
2) This class has a method which allows a file to be deleted from the harddisk.
3) It is possible to use this class in conjunction with the DataInputStream class.
4) When used with the "rw" mode, the specified file is created on a diskdrive, if it doesn't already exist.
5) There are methods to read and write primatives (eg, readInt(), writeInt(), etc).
Q8 Consider the following piece of code and select the correct statement from the following.
1.String s = new String("abcdefgh");
2.s.replace('d', 'q');
3.System.out.println(s);
1) The code fails to compile, reporting an error at line 2. Strings are immutable, and therefore a replace() method is meaningless.
2) The code compiles correctly, and displays the text "abcqefgh".
3) The code compiles correctly, and displays the text "abcdefgh".
4) The code compiles, but an exception is thrown when line 2 is executed.
5) The code compiles, but an exception is thrown at line 3.
Q9 Which of the following keywords can be applied to the variables or methods of an interface.
1) static
2) private
3) synchronised
4) protected
5) public
Q10 True or False.
Only Frames can contain menu bars or pull-down menus.
1) True
2) False.
Q11 Consider the following piece of code and select the correct statement(s):
1. class A{
2. protected int method(){
3. }
4. }
5.
6. class B extends A{
7. int method(){
8. }
9. }
1) The code fails to compile, because you can't override a method to be more private than its parent.
2) The code fails to compile, because method() is declared as protected, and is therefore not available to any subclass.
3) The code compiles correctly, but throws a NullPointerException at runtime.
4) The code fails to compile. However, it can be made to compile correctly by prefixing line 7 with the access qualifier "public".
5) The code fails to compile. However, it can be made to compile correctly by prefixing line 7 with the access qualifier "protected".
Q12 True or False.
The Throwable class is the superclass of all exceptions in the Java language.
1) True
2) False
Q13 Consider the following piece of code (assume the Graphics context g is defined correctly):
g.setBackground(Color.red);
g.setForeground(Color.white);
g.drawLine(10, 10, 50, 10);
g.setForeground(Color.blue);
g.drawRect(100, 100, 50, 50);
What is displayed when this code is executed.
1) A blue line from (10,10) to (50,10) and a blue rectangle with upper left corner at (100,100).
2) A white line from (10,10) to (50,10) and a blue square with top left corner at (100,100).
3) A white line from (10,10) to (10,50) and a blue square with lower left corner at (100,100).
4) A red line from (10, 10) to (50,10) and a red square with upper left corner at (100,100).
5) Nothing is displayed. You must first issue a repaint() command.
Q14 Consider the following piece of code.
class Test{
public static void main(String [] args){
System.out.println(args[3]);
}
}
When the following is typed at the command line, what is displayed:
java Test Metallica Justice For All
1) All
2) For
3) Justice
4) Nothing.
5) Nothing. An ArrayIndexOutOfBoundsException is thrown
Q15 Consider the following piece of code.
1. String s = "abcd";
2. Integer x = new Integer(3);
3. String s2 = s + 4;
4. s2 = null;
5. s = null;
Following the execution of which line above, is the object referenced by s2 available for garbage collection.
1) Line 5
2) It is not possible to say when an object is available for garbage collection.
3) Line 4
4) The objects are not available until the executing thread is ended.
Q16 What is displayed when the following piece of code is compiled and executed:
class Test{
public static void main(String [] args){
Base b = new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base{
int x = 2;
int method(){
return x;
}
}
class Subclass extends Base{
int x = 3;
int method(){
return x;
}
}
1) Nothing. The code fails to compile because the object b is not created in a valid way.
2) 2
3
3) 2
2
4) 3
3
5) 3
2
Q17 What is displayed when the following is executed:
String s1 = "aaa";
s1.concat("bbb");
System.out.println(s1);
1) The string "aaa".
2) The string "aaabbb".
3) Nothing. concat() is not a valid method in the String class.
4) The string "bbbaaa".
5) The string "bbb".
Q18 True or False.
The following is a valid way to construct a StringBuffer.
StringBuffer sb1 = "abcd";
1) True
2) False
Q19 What is the output of the following piece of code:
1. int x = 6;
2. double d = 7.7;
3.
4. System.out.println((x > d) ? 99.9 : 9);
1) 9
2) 9.0
3) 99.9
4) Nothing. An ArithmeticException is thrown at line 4.
5) 6
Q20 Which of the following can be put in applet tags? (select all the correct answers)
1) CODE
2) WIDTH
3) HEIGHT
4) PARAM
5) ARCHIVE
Q21 What is printed out following the execution of the code below:
1. class Test{
2. static String s;
3. public static void main(String []args){
4. int x = 4;
5. if (x < 4)
6. System.out.println("Val = " + x);
7. else
8. System.out.println(s);
9. }
10. }
Nothing. The code fails to compile because the String s isn't declared correctly.
1) The text "Val = null" is displayed.
2) The string "Val = " is displayed.
3) The text "null" is displayed.
4) A blank line of text is printed.
Q22 True or False.
The StringBuffer class does not have a concat() method.
1) True
2) False
Q23 What is displayed when the following piece of code is executed (assume the graphics context, g, is correctly set up):
g.drawString("abc", 10, 10);
1) The text "abc" with the lower left part of "a" located at x = 10, y = 10.
2) The text "abc" with the upper left part of "a" located at x = 10, y = 10.
3) Nothing. This is not a valid method.
Q24 True or False.
Anonymous classes cannot have constructors.
1) True
2) False
Q25 To reference a JAR from a web page, which of the following keywords are used:
1) jar
2) class
3) zip
4) archive
5) package
Q26
Analyse the following 2 classes and select the correct statements.
class A{
private int x = 0;
static int y = 1;
protected int q = 2;
}
class B extends A{
void method(){
System.out.println(x);
System.out.println(y);
System.out.println(q);
}
}
1) The code fails to compile because the variable x is not available to class B.
2) The code compiles correctly, and the following is displayed: 0 1 2
3) The code fails to compile because you can't subclass a class with private variables.
4) Removing the line "System.out.println(x)" will allow the code to compile correctly.
5) The compiler will complain that the variable x in class B is undefined.
Q27 Which of the following interfaces can be used to manage a collection of elements, with no duplication.
1) List
2) Vector
3) Set
Q28 Which of the following statements are true regarding inner classes.
1) Variables defined inside inner classes cannot be static.
2) Variables defined inside inner classes cannot be static unless the inner class itself is static.
3) Non-static inner classes (which are not defined in a method) have access to all class and instance variables, regardless of the access qualifier of those variables.
4) An inner class can actually be a subclass of the outer class
5) Inner classes can be declared as private. Top level, outer classes cannot.
Q29 Which of the following are valid ways to define an abstract method.
1) abstract void Test();
2) abstract void Test() {}
3) static abstract void Test();
4) final abstract void Test();
5) Methods cannot be defined as abstract, only variables can be abstract.
Q30 Consider the following:
class A extends Integer{
int x = 0;
}
Select all valid statements.
1) The code will compile correctly.
2) The code will not compile because Integer is final and cannot be subclassed.
3) The code will not compile because class A has no methods or constructor.
4) The code will compile correctly, but will throw an ArithmeticException at runtime.
Q31 Consider the following and select the correct statement(s):
interface A{
int x = 0;
A(){
x= 5;
}
A(int s){
x = s;
}
}
1) This is a valid piece of code and it compiles correctly.
2) The default constructor is not required since the compiler will create one for you.
3) The code fails to compile because interfaces cannot have more than 1 constructor.
4) The code fails to compile because an interface cannot have any constructors.
5) The code fails to compile, because a class must have more than 1 character in it's name.
Q32 True or False.
A try block always needs a catch or a finally block (either or both, but not none).
1) True
2) False
Q33 Which of the following are valid ways to declare the main() method which is used to start a Java program.
1) public static void main(String [] args)
2) static public void main(String [] args)
3) public void main(String args [])
4) public static void main(String args[])
5) public static void main(String args)
Q34 Consider the following piece of code:
boolean b = true;
System.out.println(b);
What is displayed when this code is executed?
1) The text "true" is displayed.
2) The text "1" is displayed
3) The code fails to compile, because conversion string conversion in ths System.out.println() method only applies to integers.
4) The code compiles but nothing is displayed upon execution.
5) The code fails to compile. However, changing the first line to "boolean b = TRUE;" will correctly declare a boolean, and the code will compile and display "TRUE".
Q35 Which of the following pieces of code compiles without any errors?
1) StringBuffer sb1 = "abcd";
2) Boolean b = new Boolean("abcd");
3) byte b = 255;
4) int x = 0x1234;
5) float fl = 1.2;
Q36 Which of the following are valid statements regarding the following piece of code?
1. String s1 = "abcd";
2. StringBuffer sb1 = new StringBuffer("abcd");
3. int val = 6;
4. System.out.println(s1 + val);
5. System.out.println(sb1 + val);
1) The text "abcd6" is displayed followed by "abcd6".
2) The code fails to compile because String conversion does not apply to StringBuffer.
3) The code compiles but upon execution, throws a NullPointerException at line 5.
4) The code fails to compile at line 2, because this is not a valid way to create a StringBuffer.
5) The code fails to compile at line 1, because this is not a valid way to create a String.
Q37 True or False.
Abstract methods can be declared as static.
1) True
2) False
Q38 FlowLayout is the default layout manager for which of the following containers:
1) Panel
2) Applet
3) Frame
4) Window
5) Dialog
Q39 In which class are the following methods defined:
- wait()
- notify()
- notifyAll()
1) Thread
2) Runnable
3) Object
4) Event
5) Synchronize
Q40 Which one of the following creates an instance of Vector with an initial capacity of 10, and an incremental capacity of 5.
1) new Vector(10, 5);
2) new Vector(5,10);
3) None. There is no constructor of Vector which provides this feature.
4) Vector is declared as final, and it is therefore not possible to instantiate it.
Q41 True of False.
CheckboxGroup is a subclass of Component.
1) True
2) False
Q42 Which statements(s) below are true about the following piece of code.
class Test implements Runnable{
public static void main(String [] args){
Thread t = new Thread(new Test());
t.start();
}
public void run(int limit){
for (int x = 0; x < limit; x++)
System.out.println(x);
}
}
1) All the numbers up to (but not including) "limit" are printed out.
2) Nothing is displayed. There is no explicit call to the run() method.
3) The code fails to compile because the Runnable interface is not implemented correctly.
4) The code can be made to compile by declaring the class Test to be abstract.
5) The code can be made to compile by removing the words "implements Runnable".
Q43 Consider the following code and select the statement(s) which are true:
1. class Test extends Frame{
2.
3. public static void main(String [] args){
4. Test t = new Test();
5. }
6.
7. Test(){
8. Button b = new Button("Hello");
9. add(b, BorderLayout.SOUTH);
10. }
11.
12. }
1) The code compiles. When executed, nothing is displayed.
2) The code compiles. When executed, a button with the text "Hello" is located at the bottom on the screen. The button is as tall as the text, but is the width of the frame.
3) Adding in the following two lines between lines 9 and 10 will display a frame with a button at the bottom of the frame: setSize(100, 100); setVisible(true);
4) The code fails to compile, because a layout manager was not specified.
5) The code fails to compile because you cannot subclass the Frame class.
Q44 Before which of the following can the keyword "synchronized" be placed, without causing a compile error.
1) class methods
2) instance methods
3) any block of code within a method
4) variables
5) a class
Q45 Consider the following class definitions:
class Base{}
class Subclass1 extends Base{}
class Subclass2 extends Base();
Now consider the following declarations:
Base b = new Base();
Subclass1 s1 = new Subclass1();
Subclass2 s2 = new Subclass2();
Now, consider the following assignment:
s1 = (Subclass1)s2;
Which of the following statements are correct regarding this assignment (select one).
1) The assignment is legal and compiles without an error. No exception is thrown at runtime.
2) The code fails to compile. The compiler complains that the assignment "s1 = (Subclass1)s2" is illegal.
3) The code compiles but ClassCastException is thrown at runtime.
4) The code fails to compile. You cannot subclass a parent class more than once.
Q46 Select all the valid ways of initialising an array.
1) int x[] = {1,2,3};
2) int []x[] = {{1,2,3},{1,2,3}};
3) int x[3] = {1,2,3};
4) int []x = {0,0,0};
5) char c[] = {'a', 'b'};
Q47 What is the valid declaration for the finalize() method.
1) protected void finalize() throws Throwable
2) final finalize()
3) public final finalize()
4) private boolean finalize()
5) private final void finalize() throws Exception
Q48 What is the method used to retrieve a parameter passed into an applet using the PARAM tag.
1) getParam()
2) getParameter()
3) getVariable()
4) getVar()
5) There is no method available. You must use "String [] args" approach.
Q49 You have a button, which is in a panel. The panel is inside a frame. You assign the Frame a 24-point font and a background colour of yellow. You set the panel to have a background colour of red. Which of the following statements are true (select all valid statements).
1) The font size of the button is 24-point.
2) The background colour of the button is the same as that of the frame.
3) The panel has a font size of 8-point.
4) The button inherits the font from the panel.
5) This is not a valid configuration. It is not valid to place a panel into a frame.
Q50 Consider the following piece of code and select the correct statement(s):
public class Test{
final int x = 0;
Test(){
x = 1;
}
final int aMethod(){
return x;
}
}
1) The code fails to compile. The compiler complains because there is a final method ("aMethod") in a non-final class.
2) The code compiles correctly. On execution, an exception is thrown when the Test() constructor is executed.
3) The code fails to compile because an attempt is made to alter the value of a final variable.
4) Removing the "final" keyword from the line "final int x = 0" will allow the code to compile correctly.
5) The code fails to compile because only methods can be declared as final (and therefore "final int x = 0" is not valid).
Q51 What is displayed when the following code fragment is compiled and executed (assume that the enveloping class and method is correctly declared and defined):
StringBuffer sb1 = new StringBuffer("abcd");
StringBuffer sb2 = new StringBuffer("abcd");
String s1 = new String("abcd");
String s2 = "abcd";
System.out.println(s1==s2);
System.out.println(s1=s2);
System.out.println(sb1==sb2);
System.out.println(s1.equals(sb1));
System.out.println(sb1.equals(sb2));
1) The code fails to compile, complaining that the line System.out.println(s1=s2); is illegal.
2) The code fails to compile because the equals() method is not defined for the StringBuffer class.
3) false
true
true
false
false
4) false
abcd
false
false
false
5) false
true
false
false
true
Q52 What is the default layout manager for applets and panels?
1) FlowLayout
2) BorderLayout
3) GridBagLayout
4) GridLayout
5) None of these
Q53 Which of the following statements will compile without an error?
1) Boolean b = new Boolean("abcd");
2) float f = 123;
3) byte b = 127;
4) int x = (int)(1.23);
5) short s = 128;
Q54 True or False.
Menus can be added to containers.
1) True
2) False
Q55 Which of the following statements are true regarding the graphical methods
paint(), repaint() and update().
1) paint() schedules a call to repaint().
2) repaint() schedules a call to update().
3) update() calls paint().
4) update() schedules a call to repaint().
5) repaint() calls paint() directly.
Q56 To which of the following can a menubar component be added?
1) Applet
2) Panel
3) Frame
4) Canvas
Q57 With regard to apply applet by HTML tags, which of the following statements are correct?
1) The CODE, WIDTH and HEIGHT tags are mandatory and the order is insignificant.
2) CODE and CODEBASE are case insensitive, and the .class extension is optional.
3) The PARAM tag is case insensitive.
4) It is possible to download multiple JAR's with the ARCHIVE tag (eg, ARCHIVE = "a.jar, b.jar").
5) The CODE tag is the only mandatory tag.
Q58 Consider the following piece of code and select the correct statements.
1. Object o = new String("abcd");
2. String s = o;
3. System.out.println(s);
4. System.out.println(o);
1) The following is displayed:
abcd
abcd
2) The code fails to compile at line 1.
3) The code fails to compile at line 2
4) The code fails to compile at line 4.
5) The code can be made to compile by changing line 1 to the following:
String o = new String("abcd");
Q59 Which of the following are legal methods for the String class?
1) length()
2) toUpper()
3) toUpperCase()
4) toString()
5) equals()
Q60 What is the output from the following piece of code:
loop1:
for(int i = 0; i < 3; i++){
loop2:
for(int j = 0; j < 3; j++){
if (i == j){
break loop2;
}
System.out.print("i = " + i + " j = " + j + " ");
}
}
1)
i = 1
j = 0
2)
i = 1
j = 0
i = 2
j = 1
3) i = 0
j = 1
i = 0
j = 2
i = 1
j = 0
i = 2
j = 0
i = 2
j = 1
4) i = 1
j = 0
i = 2
j = 0
i = 2
j = 1
5)
i = 1 j = 0 i = 2 j = 0 i = 2 j = 1
Q61 What is displayed when the following piece of code is executed:
loop1:
for(int i = 0; i < 3; i++){
loop2:
for(int j = 0; j < 3; j++){
if (i == j){
continue loop2;
}
System.out.println("i = " + i + " j = " + j);
}
}
1) i = 0 j = 0
i = 0 j = 1
i = 1 j = 0
i = 1 j = 1
i = 2 j = 0
i = 2 j = 1
2) i = 0 j = 0
i = 1 j = 1
i = 2 j = 2
3) i = 0 j = 1
i = 0 j = 2
i = 1 j = 0
i = 1 j = 2
i = 2 j = 0
i = 2 j = 1
4) None of the above.
Q62 Consider the following piece of code, and select the correct statement(s):
long val = 2;
...
...
Switch (val){
default:
System.out.println("Default");
break;
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
case 3:
System.out.println("3");
break;
}
1) The following is displayed:
2
3
2) The following is displayed:
2
3) The code fails to compile because the default case must be the last case in the switch statement.
4) The code fails to compile because the argument for a switch statement cannot be a variable of type long.
5) The following is displayed:
2
3
Default
Q63 Consider the following piece of code, and select the correct statement(s):
public class Test extends Applet{
public void init(){
setLayout(new GridLayout(1,2));
add(new Button("#1"));
add(new Button("#2"));
add(new Button("#3"));
add(new Button("#4"));
}
}
1) The Gridlayout is created with 1 row and 2 columns.
2) Adding the button with the label "#3" will cause an exception to be thrown.
3) Adding the button with label "#3" will cause it to overwrite the button with label "#2".
4) The layout automatically extends to accommodate the additional buttons.
5) Only 2 buttons are displayed, one with label "#1" and one with label "#4".
Q64 Which of the following layout managers will retain the preferred width and height of the contained components.
1) GridLayout
2) GridBagLayout
3) BoxLayout
4) FlowLayout
5)BorderLayout
Q65 You construct a List by calling List(5, false).
Which statements below are correct (assume the layout managers do not modify the List properties).
1) The list supports multiple selection.
2) The list has 5 visible items.
3) A vertical scroll bar will be added automatically if needed.
4) The code fails to compile. The given constructor is not a valid one.
Q66 Which of the following will definitely stop a thread from executing:
1) wait()
2) notify()
3) yield()
4) suspend()
5) sleep()
Q67 Which of the following is the correct method to call to change the layout manager for a container:
1) setLayoutManager()
2) setLayManager()
3) changeLayout()
4) You can't change the layout manager for a container.
5) setLayout()
Q68 Which is the correct way to add a button, referenced by b, to a panel, referenced by p.
1) add(p, b);
2) p.add(b)
3) b.add(p)
4) Buttons can't be added to panels.
5) add(p)
Q69 What is displayed when the following code is compiled and executed:
long val = 2;
switch(val){
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
1) default
2) 2
default
3) The code fails to compile because there are no break statements in the case clauses.
4) The code fails to compile because a long data type is not a valid parameter for a switch statement.
Q70 What is displayed following the execution of the code below:
1. class Test{
2. String s;
3. public static void main(String []args){
4. int x = 4;
5. if (x < 4)
6. System.out.println("Val = " + x);
7. else
8. System.out.println(s);
9. }
10. }
1) Nothing. The code doesn't compile because the variable s wasn't initialised.
2) The string "null" is displayed.
3) The code runs, but a NullPointerException is thrown at line 8.
4) The code compiles. No exception is thrown but nothing is displayed.
5) Nothing. The code doesn't compile because you can't make a static reference to a non-static variable.
Q71 Consider the following piece of code:
class Test{
int x;
String s;
float fl;
boolean [] b = new boolean [5];
public static void main(String []args){
System.out.println(x);
System.out.println(s);
System.out.println(fl);
System.out.println(b[2]);
}
}
What is displayed when this code is executed.
1) 0
null
0.0
false
2) 0
" "
0.0
true
3) Nothing. The code fails to compile because the boolean array is incorrectly declared.
4) Nothing. The code will not compile because the static method cannot access the variables since there is no instance of the Test class.
Q72 What is displayed when the following code is compiled and executed:
String s = "abcd";
String s1 = new String(s);
if (s == s1)
System.out.println("the same");
if (s.equals(s1))
System.out.println("equals");
1) the same
equals
2) equals
3) the same
4) The code compiles, but nothing is displayed upon execution.
5) The code fails to compile.
Q73 What is the legal range for the byte data type:
1) 0 to +255
2) -127 to +128
3) -128 to +127
4) 0 to 65535
5) -32767 to +32768
Q74 What is displayed when the following code is executed:
String s = "abcdef";
System.out.println(s.charAt(4));
1) d
2) e
3) Nothing. An ArrayIndexOutOfBoundsException is thrown
4) The code does not compile. charAt() is not a valid method of the String class.
5) The code does not compile because the string s is not created correctly.
Q75 True or False.
The String class does not have an append() method.
1) True
2) False
Q76 Consider the following code segment.
double d = -11.1;
double d1 = method(d);
System.out.println(d1);
If the output of this code segment is -12.0, then what methods could be called in method() above.
1) floor()
2) ceil()
3) round()
4) abs()
5) min()
Q77 What outputs are possible from invoking Math.random().
1) -0.12
0.56E3
2) 0.12
1.1E1
3) -23.45
0.0
4) 0.356
0.03
5) 1.00
0.99
Q78 In a thread, the wait() method must be called inside which of the following:
1) A while() loop
2) The run() method
3) synchronised code
4) The constructor
5) It doesn't matter where the wait() is called from.
Q79 Consider the following piece of code:
public class Test extends Applet{
public void init(){
setLayout(new BorderLayout());
add("South", new Button("B1"));
add("North", new Button("B2"));
add("South", new Button("B3"));
}
}
Select the correct statements regarding the above.
1) There are 2 buttons displayed in the SOUTH section, B1 and B3.
2) Only B3 is displayed in the SOUTH section.
3) An exception is thrown when an attempt is made to add a second component to SOUTH.
4) Only B1 is displayed in the SOUTH section.
5) You can't add buttons to applets.
Q80 What is displayed when the following piece of code is executed:
1. String val = null;
2. int x = Integer.parseInt(val);
3.
4. System.out.println(x);
1) 0
2) null
3) A NumberFormatException is thrown at line 2.
4) Nothing is displayed
Q81 Consider the following piece of code:
class A{
int x = 0;
A(int w){
x = w;
}
}
class B extends A{
int x = 0;
B(int w){
x = w +1;
}
}
1) The code compiles correctly.
2) The code fails to compile, because both class A and B do not have valid constructors.
3) The code fails to compile because there is no default no-args constructor for class A.
Q82 Given an object myListener (whose class implements the ActionListener interface), which of the following are valid ways to enable myListener to receive all action events from component smallButton?
1) smallButton.add(myListener);
2) smallButton.addListener(myListener);
3) smallButton.addActionListener(myListener);
4) smallButton.addItem(myListener);
Q83 Which of the following are valid listener interfaces?
1) ActionListener
2) MouseMotionListener
3) SystemEventListener
4) MouseClickListener
5) WindowListener
Q84 Which of the following is the correct way to set the foreground colour of a component, c.
1) c.setForeground("red");
2) c.setColor("Color.red");
3) c.Foreground(Color.red);
4) c.setForegroundColor("red");
5) c.setForeground(Color.red);
Q85 Which of the following are valid ways to create a Map collection.
1) Map m = new Map();
2) Map m = new Map(init capacity, increment capacity);
3) Map m = new Map(new Collection());
4)Map is an interface, and cannot be instantiated.
Q86 Select all the correct statements relating to the following piece of code?
1. public class A{
2. abstract int method();
3. void anotherMethod(){
4. }
5.
6. class B extends A{
7. int method(){
8. return 2;
9. }
10. }
1) Changing "extends" to "implements" on line 6 will allow the code to compile correctly.
2) The method() in class A cannot be abstract without the entire class being declared as abstract.
3) Declaring class A to be abstract will allow the code to compile correctly.
4) The class A must have an explicit default constructor in order for it to be subclassed correctly.
5) The code fails to compile, because class B does not implement anotherMethod().
Q87 Which layout manager do the comments below refer to:
All components contained in it have the same width and height. Contained components are stretched to have the same dimensions.
1) FlowLayout
2) BorderLayout
3) GridLayout
Q88 Select all valid statements.
1) When typing text into a TextField, scroll bars will automatically appear when the TextField becomes full.
2) When typing text into a TextArea, scroll bars will appear when text is typed past the boundaries of the TextArea.
3) TextFields can have more than 1 row of text.
4) The class TextArea is a super class of the class TextField.
5) Both TextAreas and TextFields have a method called setEditable(), which can enable or disable editing of the component.
Q89 In which class is the paint() method defined?
1) Object
2) Applet
3) Component
4) Thread
5) Graphics
Q90 Select the statement which most closely describes the user interface of an applet with the following init() method:
public void init(){
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(new Button("Hello"), BorderLayout.EAST);
p.add(new Button("Bye"), BorderLayout.WEST);
add(p);
}
1) Two buttons are displayed. A button with "Hello" is on the right side of the applet. The button with "Bye" is on the left side of the applet. Both buttons extend from the centre of the applet to each side, and are the height of the applet.
2) Two buttons are displayed. A button with "Hello" is on the right side of the applet. The button with "Bye" is on the left side of the applet. Both buttons are only as wide as the text on the button, but are the height of the applet.
3) Two buttons are displayed. A button with "Hello" is on the right side of the applet. The button with "Bye" is on the left side of the applet. Both buttons are only large enough to support the associated button texts.
Q91 What is wrong about the following piece of code (assume the relevant import statements are present):
public class Test{
public static void main (String [] args) throws IOException{
if (args[0] == "hello")
throw new IOException();
}
}
1) Nothing. The code compiles correctly.
2) The code fails to compile. You can't throw an exception from the main() method.
3) The code fails to compile. IOException is a system exception, and cannot be thrown by application code.
Q92 Consider the following piece of code, and select the statements which are true.
class Test{
int x = 5;
static String s = "abcd";
public static void method(){
System.out.println(s + x);
}
}
1) The code compiles and displays "abcd5".
2) The code compiles, but throws an exception at runtime, because variable x isn't declared as static.
3) The code fails to compile because you can't make a static reference to a non-static variable.
4) The code will compile if x is declared to be static.
5) The code will compile by removing the static keyword from the declaration of method().
Q93 Select all legal code fragments from the following.
1) char c = "c";
2) Boolean b = new Boolean("qwerty");
3) String s = "null";
4) int q;
for (int p = 0, q = 0; p < 5; p++){
System.out.println("Val = " + p + q);
}
5) int x = 3;
Float f = new Float(x);
Q94 Which of the following are legal ways to construct a RandomAccessFile?
1) RandomAccessFile("file", "r");
2) RandomAccessFile("r", "file");
3) RandomAccessFile('r', "file");
4) RandomAccessFile("file", 'r');
5) RandomAccessFile("file", "rw");
Q95 Consider the following code and select the most appropriate statements.
1. class Test{
2. public int doubleValue(int a)
3. System.out.println(a);
4. return (int)(a * 2);
5 }
6.
7 public float doubleValue(int a){
8. System.out.println(a);
9. return (float)(a * 2);
10. }
11.}
1) The code compiles since the two doubleVal() methods have different return types.
2) The code doesn't compile because the compiler sees two methods with the same signature.
3) The code can be made to compile by redefining the parameter for doubleVal() on line 7 to be "float a" instead of "int a".
4) The code can be made to compile by replacing the public declaration on line 7 with private.
Q96 What is displayed when the following piece of code is executed:
class Test extends Thread{
public void run(){
System.out.println("1");
yield();
System.out.println(2");
suspend();
System.out.println("3");
resume();
System.out.println("4");
}
public static void main(String []args){
Test t = new Test();
t.start();
}
}
1) 1
2
3
4
2) 1
2
3
3) 1
2
4) Nothing. This is not a valid way to create and start a thread.
5) 1
Q97 Consider the following piece of code, and select the most appropriate statements.
TextField t = new TextField("Hello", 20);
1) The user will be able to edit the string.
2) The field will be 20 characters wide.
3) The text field will be same size on all platforms, since Java is platform independent.
4) When entering text into the field, only 20 characters can be entered.
5) If the font is changed, the size of the textfield will adjust to allow 20 characters to be displayed.
Q98 True of False.
Using the instanceof operator on an interface will cause a runtime exception.
1) True
2) False
Q99 Which of the following are valid ways to create a Button component?
1) new Button();
2) new Button(30, 10); where 30 is the width of the button and 10 is the height
3) new Button("hello");
4) new Button(new String("hello"));
5) new Button(String("hello"));
Q100 Which of the following are NOT valid subclasses of AWTEvent?
1) MouseClickEvent
2) OutputEvent
3) MouseMotionEvent
4) KeyAdapter
5) WindowMinimizeEvent
Q101 Which of the following are valid methods for the Graphics class?
1) drawOval(int x, int y, int width, int height)
2) toString()
3) drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
4) drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
5) fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Q102 Which of the following are valid methods in the Math class?
1) arcTan(double a)
2) atan(double a)
3) sqrt(double a)
4) min(int a, int b)
5) cosine(double a)
Q103 Which of the following will display the string "ica", given:
String s = "Metallica";
1) System.out.println(s.subString(7));
2) System.out.println(s.substring(6));
3) System.out.println(s.subString(6, 8));
4) System.out.println(s.substring(7, 9));
5) None of these.
Q104 Which of the following are defined in the Object class?
1) toString()
2) equals(Object o)
3) public static void main(String [] args)
4) System.out.println()
5) wait()
Q105 Select all valid statements relating to the paint() method defined in the Component class.
1) It takes 4 int types as parameters, representing the x and y co-ordinates, width and height, respectively.
2) It is declared as final, and so it cannot be overridden.
3) It is declared as static.
4) It takes an instance of the Graphics component as a parameter.
5) When overriding the paint() method, you must handle the identification and repair of damaged components.
Q106 Which of the following keywords can be applied to a method?
1) transient
2) volatile
3) private
4) protected
5) final
Q107 Which of the following keywords can be applied to a top-level class (i.e., not an inner class)?
1) private
2) protected
3) transient
4) public
5) final
Q108 Consider the following piece of code, and select all relevant statements:
1. class Test{
2. public static void main(String [] args){
3. aMethod();
4. }
5.
6. static void aMethod(){
7. try{
8. System.out.println("abcd");
9. return;
10. } finally {
11. System.out.println("1234");
12. }
13. }
14.}
1) An exception is thrown at line 9.
2) The code fails to compile. The compiler complains about the return statement at line 9.
3) abcd
1234
4) abcd
5) The code fails to compile, complaining that a 'catch' clause is missing from line 10.
Q109 Which of the following statements are true.
1) The type "int" is a 32-bit signed integer value.
2) The type "short" is a 16-bit unsigned integer value.
3) The type "char" is a 16-bit Unicode character.
4) The type "float" is a 64-bit floating point value.
5) The type "long" is a 64-bit signed integer value.
Q110 Which layout manager is described by the following:
It aligns components vertically and horizontally, without requiring that the components be of the same size. It maintains a dynamic rectangular grid of cells, with each component occupying one or more cells, called its display area.
1) GridLayout
2) FlowLayout
3) GridBagLayout
4) BoxLayout
Q111 True of False.
The StringBuffer class overrides the toString() method to return a String representation of the StringBuffer.
1) True
2) False
Q112 Select the correct statements from the following:
1) The class RandomAccessFile is a subclass of the java.io.File class.
2) The class java.io.FileWriter contains methods to write primitives (eg, writeInt(), writeFloat(), etc)
3) The class java.io.BufferedOutputStream contains methods to write primitives (eg, writeInt(), writeFloat(), etc)
4) The File class defines a method which can be used to delete files and directories from a disk.
5) The RandomAccessFile class defines a method which can be used to delete files and directories from a disk.
Q113 True or False.
A class can implement more than one interface, but can only inherit from a single parent class.
1) True
2) False
Q114 What is the result of compiling and executing the following code.
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
1) The code fails to compile in the main() method.
2) The code fails to compile in the run() method.
3) Only the text "In run" will be displayed.
4) The text "In run" followed by "Leaving run" will be displayed.
5) The code compiles correctly, but nothing is displayed.
Q115 Which of the following will compile without errors.
1) Byte b = new Byte(123);
2) Byte b = new Byte("123");
3) Byte b = new Byte();
b = 123;
4) Byte b = new Byte((int)123.4);
5) Byte b = new Byte(0x123);
Q116 Consider the following piece of code and select the correct statement(s):
1. public class Test{
2. static int x;
3. static public void main(String [] args){
4. x = x + 1;
5. System.out.println("Value is " + x);
6. }
7. }
1) The code fails to compile. The compiler complains that the variable x is used before it is initialised.
2) The code compiles correctly and displays "Value is 1" when executed.
3) The code compiles correctly, but throws a NullPointerException at line 5.
4) The code fails to compile because the 'main' method is incorrectly declared.
Q117 Which of the following are termed "short circuit" logical operators.
1) &
2) &&
3) |
4) ||
5) ^
Q118 Which of the following are valid expressions.
1) Object o = new String("abcd");
2) Boolean b = true;
3) Panel p = new Frame();
4) Applet a = new Panel()
5) Panel p = new Applet()
Q119 Which of the following is a valid way to declare an abstract method which is intended to be public.
1) public abstract method();
2) public abstract void method();
3) public abstract void method(){}
4) public virtual void method();
5) public void method() implements abstract;
Q120 Which of the following are valid ways to define a constructor for class Test.
1) public void Test(){}
2) public Test(){}
3) private Test(){}
4) public static Test(){}
5) final Test(){}
Q121 Which of the following statements are valid for a method which is overriding the following:
protected void method(int x){..}
1) The overriding method must take an int as its only parameter.
2) The overriding method must return a void.
3) The overriding method can be declared as private.
4) The overriding method can return any value it wishes.
Q122 True or False.
An inner class can implement an interface or extend a class.
1) True
2) False
Q123 Which of the following are valid return types for listener methods.
1) boolean
2) void
3) An object of type Event
4) An object of type Object
5) A long value, representing the Object ID.
Q124 Consider the following piece of code and select the correct statements.
1. public class Test{
2. public static void main(String [] args){
3. print();
4. }
5.
6. public static void print(){
7. System.out.println("Test");
8. }
9.
10. public void print(){
11. System.out.println("Another Test");
12 }
13. }
1) The code compiles successfully and displays "Test".
2) Changing the code at line 10 to the following, will allow the code to compile correctly:
public void print(int x){
3) The code fails to compile. The compiler complains about a static reference to a non-static method, print().
4) The code fails to compile. The compiler complains about duplicate methods.
5) Changing the return type on line 10 from 'void' to 'int' will allow the code to compile correctly.
Q125 Consider the following list of tags and attributes of tags and select the ones which can legally be placed between the APPLET and /APPLET delimiters.
1) JAVAC
2) JAR
3) CODE
4) NAME
5) PARAM
Q126 Which variables can an inner class access from the class which encapsulates it.
1) All static variables
2) All final variables
3) All instance variables
4) Only final instance variables
5) Only final static variables
Q127 Using a FlowLayout manager, which of the following is the correct way to add a conponent referenced by the variable "c" to a container:
1) add(c);
2) add("Center", c);
3) c.add();
4) c.set();
5) set(c);
Q128 Assuming there is a class X which implements the ActionListener interface.
Which method should be used to register this with a Button?
1) addListener(new X());
2) addActionListener(new X());
3) addButtonListener(new X());
4) addActionAdapter(new X());
5) (new X()).addActionListener();
Q129 What is the result of compiling and executing the following fragment of code:
boolean flag = false;
if (flag = true)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
1) The code fails to compile at the "if" statement.
2) An exception is thrown at run-time at the "if" statement.
3) The text "true" is displayed.
4) The text "false" is displayed.
5) Nothing is displayed.
Q130 Which of the following is not a subclass of java.awt.Component?
1) Frame
2) Container
3) CheckboxGroup
4) Canvas
5) Applet
Q131 What is the result when the following piece of code is compiled and executed.
1. public class Calc {
2. public static void main (String args []) {
3. int total = 0;
4.
5 . for (int i = 0, j = 10; total < 30; ++i, --j) {
6. System.out.println(" i = " + i + " : j = " + j);
7.
8. total += (i + j);
9.
10. }
11.
12. System.out.println("Total " + total);
13. }
14. }
1) The code fails to compile at line 5, because the variable "j" is not declared correctly.
2) The code fails to compile at line 2, because you can't have a static method in a non-static class.
3) The code compiles correctly, but throws an exception at line 5.
4) The code compiles correctly but throws an exception at line 2.
5) The code compiles correctly and displays the following when executed:
i = 0 : j = 10
i = 1 : j = 9
i = 2 : j = 8
Total 30
Q132 Which of the following will create a TextField capable of displaying 10 characters, with the initial text "hello"?
1) TextField t = new TextField("hello", 10);
2) TextField t = new TextField(10, "hello");
3) TextField t = new TextField();
t.setCols(10);
t.setText("hello");
4) TextField t = new TextField();
t.setFieldSize(10);
t.setText("hello");
5) None of the above.
Q133 What is the effect of adding the sixth element to a Vector created using the following code:
new Vector(5, 10);
1) An IndexOutOfBounds exception is thrown.
2) The vector grows in size to a capacity of 10 elements.
3) The vector grows in size to a capacity of 15 elements.
4) Nothing. The Vector will have grown when the fifth element was added, because Vector elements are zero-based.
5) Nothing. This is not a valid way to create a Vector.
Q134 When is the text "Hi there" displayed?
public class StaticTest {
static {
System.out.println("Hi there");
}
public void print() {
System.out.println("Hello");
}
public static void main(String args []) {
StaticTest st1 = new StaticTest();
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}
}
1) Never.
2) Each time a new object of type StaticTest is created.
3) Once when the class is loaded into the Java virtual machine.
4) Only when the main() method is executed.
Q135 Which of the following are valid ways to assign the value of 5 to a variable of type "int" called testValue?
1) int testValue = 0x5;
2) int testValue = (int)(2.1F + 3.4D);
3) int testValue = (octal)5;
4) int testValue = (0x0A >> 1);
5) int testValue = (0x0A >>> 1);
Q136 Which of the following operations will cause an ActionEvent to be generated.
1) Clicking a checkbox.
2) Selecting an option from a Choice box.
3) Scrolling a scroll bar.
4) Clicking a button.
5) Hitting ENTER when typing in a text field.
Q137 True or False.
All Listener interfaces methods are declared as public and void.
1) True
2) False
Q138 True or False.
In a GridLayout, the width and height of all cells are the same.
1) True
2) False
Q139 True or False.
The BorderLayout manager only allows a single component to be displayed in each sector.
1) True
2) False
Q140 You want to extend the functionality of the String class, and decide that the best approach is to subclass String. Which of the following statements are correct.
1) The approach fails, because the String class is declared as final and cannot be subclassed.
2) The approach succeeds, and it is therefore possible to override the standard functionality provided by String.
3) The approach fails, because the String class is declared as abstract, and therefore it cannot be subclassed.
Q141 Which of the following are valid ways to determine the number of elements in an array defined as follows:
int intArray[] = {1,2,3};
1) intArray.size;
2) intArray.size();
3) intArray.length;
4) intArray.length();
5) intArray.getSize();
Q142 You have a check box within a panel, and the panel is contained within an applet. There are no other components within the applet.
Using "setBackground(Color.white)", you assign the background colour of the applet to white.
Which of the following statements are true.
1) The background colour of the panel is white.
2) The foreground colour of the checkbox is the same as the foreground colour of the applet.
3) It is impossible to determine the background colour of the checkbox from the imformation supplied.
4) You can't put a panel inside an applet.
Q143 Which of the following can be added to a menu?
1) A panel
2) A button
3) A menu
4) A checkbox
5) A menubar
Q144 You construct an application by making the following call:
Frame app = new Frame();
app.setSize(100,100);
Which of the following statements are correct.
1) An empty frame of size 100x100 is displayed, located in the top left corner of the screen.
2) An empty frame of size 100x100 is displayed, located in the centre of the screen.
3) Nothing is displayed. There is no setSize() method in the Frame class.
4) Nothing is displayed. The frame will only be displayed by setting the forground and background colours.
5) Nothing is displayed. A frame is created by default with zero size, and invisible.
Q145 Which of the following are valid methods in the java.awt.Graphics class?
1) drawString()
2) drawOval()
3) fillRect()
4) drawPolyline()
5) fillPolyline()
Q146 Which of the following are valid command line options for the JAR utility:
1) c (create a new or empty archive)
2) m (indicates that first argument is the name of a user-supplied manifest)
3) M (do not create a manifest)
4) z (ZIP the contents of the archive)
5) L (list the archive contents)
Q147 Consider the following code segment and select the correct statement(s):
1. class FinalTest{
2. final int q;
3. final int w = 0;
4.
5. FinalTest(){
6. q = 1;
7. }
8.
9. FinalTest(int x){
10. q = x;
11. }
12. }
1) The code fails to compile because a class cannot have more than 1 constructor.
2) The code fails to compile because the class FinalTest has no constructors.
3) The code fails to compile because the an attempt is made to initialise a final variable at lines 6 and 10.
4) The code compiles correctly without any warnings or errors.
5) The code fails to compile because the final variable q is not initialised correctly at line 2.
Q148 True or False.
It is valid to throw (and re-throw) an exception inside a catch{} clause.
For example:
...
catch (Exception e){
throw e;
}
1) True
2) False
Q149 True or False.
A scrollbar will appear in a TextField if more text is typed than can be displayed within the TextField.
1) True
2) False
Q150 True or False.
It is valid to declare an inherited method as abstract.
1) True
2) False
Q151 Consider the following code segments and select the correct statement:
Segment 1:
1. int a = 3;
2. int b = 0;
3. int c = a/b;
Segment 2:
4. float a = 1.0F;
5. float b = 0.0F;
6. float c = a/b;
1) When executed, both segments will cause an exception to be thrown.
2) When executed, neither segment will cause an exception to be thrown.
3) Only Segment 1 will throw an exception.
4) Only Segment 2 will throw an exception.
Q152 Which of the following classes override the equals() method.
1) String
2) Integer
3) Double
4) Date
5) File
Q153 Which of the following is the default layout manager for a panel.
1) BorderLayout
2) FlowLayout
3) GridBagLayout
4) GridLayout
5) None of the above.
Q154 True or False.
The StringBuffer class inherits from the String class.
1) True
2) False
Q155 True or False.
The class String has a concat() method, while the StringBuffer class has an append() method.
1) True
2) False
Q156 True or False.
The primative wrapper classes Integer, Double and Float all inherit directly from the java.lang.Wrapper class.
1) True
2) False
Q157 Which of the following are valid methods for the Applet class.
1) start()
2) stop()
3) init()
4) destroy()
5) kill()
Q158 True or False.
A label can only display a single line of text.
1) True
2) False
Q159 Which of the following are valid methods for the Math class.
1) random()
2) Random()
3) toDegrees()
4) square()
5) sqr()
Q160 True or False.
JDK 1.2 contains an Arrays class which provides various methods for manipulating arrays (such as searching and sorting).
1) True
2) False
Q161 True or False.
The Class class has no public constructor.
1) True
2) False
Q162 Which of the following statements is true with regard to persistence.
1) "Serializable" is a keyword within the Java language.
2) "Serializable" is an interface, which classes can implement.
3) "Serializable" is a class, which other classes can extend.
4) None of the above.
Q163 Which of the following statements is correct with regard to threads.
1) "Runnable" is a keyword within the Java language, used to identify classes which can exist as separate threads.
2) "Runnable" is an interface, which classes can implement when they wish to execute as a separate thread.
3) "Runnable" is a class, which classes can extend when they wish to execute as a separate thread.
4) None of the above.
5) java does not support threads.
Q164 Select the statements which are valid from the list below.
1) The primative data type "char" is 16-bits wide.
2) Unicode uses 16-bits to represent a character.
3) Unicode uses 32-bits to represent a character.
4) UTF uses 24-bits to represent a character.
5) UTF uses as many bits as are needed to represent a character.
Q165 When a Frame is constructed, it has no size and is not displayed. Which of the following methods can be called to display the Frame.
1) setSize()
2) setBounds()
3) setDisplayable()
4) setVisible()
5) setState()
Q166 Which one of the following is correct (assume A and B are correctly defined).
1) if (A instanceOf B)
2) if (A instanceof B)
3) if (A.instanceOf(B))
4) if (A instanceof(B))
Q167 What is displayed when the following is executed:
double d1 = -0.5;
System.out.println("Ceil d1 = " + Math.ceil(d1));
System.out.println("floor d1 = " + Math.floor(d1));
1) Ceil d1 = -0.0
floor d1 = -1.0
2) Ceil d1 = 0.0
floor d1 = -1.0
3) Ceil d1 = -0.0
floor d1 = -0.0
4) Ceil d1 = 0.0
floor d1 = 0.0
5) Ceil d1 = 0
floor d1 = -1
Q168 Select the layour manager which is described by the following:
"It arranges its constituent components in horizontal rows. It will attempt to fit as many components as possible into the first row, and remaining components will spill over into subsequent rows. Components always appear left to right, in the order they were added to the container."
1) FlowLayout
2) GridLayout
3) CardLayout
4) TransverseFlowLayout
5) GridBagLayout
Q169 True or False
The argument for a "case" statement must be a constant or a constant expression which can be evaluated at compile time.
1) True
2) False
Q170 Select the valid code fragments from the following list:
1) public transient static final int _FRAMEX = 850;
2) this("a", "b");
[assuming a constructor of the type xxx(String, String) exists for this class]
3) private transient static final int _FRAMEX = 850;
4) boolean b = 0;
Q171 True or False.
The File class can represent either a file or a directory within a file system.
1) True
2) False
Q172 True or False.
The argument for a while() statement MUST be a boolean.
1) True
2) False
Q173 True or False.
The right-hand operand of the instanceof operator can be an interface; eg
a instanceof b
where b is an interface.
1) True
2) False
Q174 Consider the following class:
public class Test{
public int secret;
}
Which of the following is the correct way to make the variable 'secret' read-only.

1) Declare 'secret' to be private.
2) Declare the class Test to be private.
3) Declare 'secret' to be private and write a method getSecret() which returns the value of 'secret'.
4) Declare 'secret' to be transient.
5) Declare 'secret' to be static.
Q175 Which one of the following methods of the Math class can be used to find the square root of a number.
1) squareRoot()
2) sqrt()
3) root()
4) sqr()
5) None of the above. You use the pow() method.
Q176 To which layout manager does the following statement refer:
"it preserves the preferred size (width and height) of each component".
1) Flow Layout
2) Border Layout
3) Grid Layout
4) Grid Bag Layout
5) None of the above. All layout managers alter the dimensions of their containing components.
Q177 Consider the following code fragment, and select the correct statements(s):
1. public class Test extends MyBase implements MyInterface{
2. int x = 0;
3.
4. public Test(int inVal) throws Exception{
5. if (inVal != this.x){
6. throw new Exception("Invalid input");
7. }
8. }
9.
10. public static void main(String args[]){
11. Test t = new Test(4);
12. }
13.}
1) The code fails to compile at line 1. It is not valid to both implement an interface and extend from a parent class simultaneously.
2) The code fails to compile at line 4. It is not valid for constructors to throw exceptions.
3) The code fails to compile at line 6, because this is not valid way to throw an exception.
4) The code fails to compile at line 11. The compiler complains that there is an uncaught exception.
5) The code fails to compile at line 5, because this is not a valid way to reference variable x.

Q178 Consider the following real-life relationships, and select the code fragment which most closely represents it.
An employee is a person. An employee has zero or more dependants.
[for the code fragments, assume that all relevant packages are imported, and all relevant class definitions exist].
1) class Employee extends Dependants{
Person p;
}
2) class Person extends Employee{
Vector dependants;
}
3) class Employee extends Person{
Vector dependants;
}
4) abstract class Person extends Dependants{
Employee e;
}
5) class Dependant implements Employee{
Vector person;
}
Q179 Which of the following are valid declarations for a native method.
1) public native void aMethod();
2) private native String aMethod();
3) private native boolean aMethod(int val){};
4) public native int aMethod(String test);
5)private native boolean aMethod(boolean flag, Integer val);
Q180 Which of the following are valid ways to define an octal literal of value 17 (octal).
1) private final int theNumber = 0x17;
2) private final int theNumber = 017;
3) public int theNumber = 017;
4) public int theNumber = (octal)17;
5 )public int THE_NUMBER = 017;
Q181 Consider the following piece of code and select all statements which yield a boolean value of true as a result.
Double d1=new Double(10.0);
Double d2=new Double(10.0);
int x=10;
float f=10.0f;
1) d1 == d2;
2) d1 == x;
3) f == x;
4) d1.equals(d2);
5) None of the above
Q182 Given the following definition:
String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown.
1) if ((s != null) & (s.length()>0))
2) if ((s != null) && (s.length()>0))
3) if ((s != null) | (s.length()>0))
4) if ((s != null) || (s.length()>0))
5) None of the above.
Q183 Which of the following are valid declarations for a 2x2 array of integers.
1) int a[][] = new int[10,10];
2) int a[][] = new int [10][10];
3) int a[10,10] = new int [10][10];
4) int [][]a = new int [10][10];
5) int []a[] = new int [10][10];
Q184 Which values of variable x will show "Message 2".
switch(x)
{
case 1 :
System.out.println("Message 1");
case 2 :
case 3 :
System.out.println("Message 2");
default :
System.out.println("End");
}
1) 1
2) 2
3) 3
4) 4
5) none
Q185 What is the result of the following code :
public class SuperEx {
String r;
String s;
public SuperEx(String a,String b) {
r = a;
s = b;
}
public void aMethod() {
System.out.println("r :" + r);
}
}
public class NewSuper extends SuperEx {
public NewSuper(String a,String b) {
super(a,b);
}
public static void main(String args []) {
SuperEx a = new SuperEx("Hi","Tom");
SuperEx b = new NewSuper("Hi","Bart");
a.aMethod();
b.aMethod();
}
public void aMethod() {
System.out.println("r :" + r + " s:" + s);
}
}
1) The following is displayed:
r:Hi
s:Hi
2) Compiler error at the line "SuperEx b = new NewSuper("Hi","Bart");"
3) The following is displayed:
r:Hi
r:Hi s:Bart
4) The following is displayed
r:Hi s:Tom
r:Hi s:Bart
Q186 You have a class with a certain variable and you don't want that variable to be accessible to ANY other class but your own. Your class must be sub-classable.
Which keyword do you add to the variable.
1) private
2) public
3) transient
4) final
5) abstract
Q187 You get this description of a class :
Employee is a person. For every employee, we keep a vector with the working hours, an integer for the salary and a variable that can be true or false whether or not the employee has a company car.
Indicate which of the following would be used to define the class members.
1) Vector
2) Employee
3) Object
4) boolean
5) int
Q188 Which line of code can be inserted in place of the comments, to perform the initialisation described by the comments:
public class T {
int r;
int s;
T(int x, int y) {
r = x;
s = y;
}
}
class S extends T {
int t;
public S(int x, int y, int z){
// insert here the code
// that would do the correct initialisation
// r= x and s= y
t=z;
}
}
1) T(x, y);
2) this(x, y);
3) super(x, y);
4) super(x, y, z);
5) None
Q189 Suppose a MyException should be thrown if Condition() is true, which statements do you have to insert ?
1: public aMethod {
2:
3: if (Condition) {
4:
5: }
6:
7: }
1) throw new Exception() at line 4
2) throw new MyException() at line 4
3) throw new MyException() at line 6
4) throws new Exception() at line 2
5) throws MyException at line 1
Q190 Suppose a NullPointerException is thrown by test(). Which message is displayed?
void Method(){
try {
test();
System.out.println("Message1");
}
catch (ArrayOutOfBoundsException e) {
System.out.println("Message2");
}
finally {
System.out.println("Message3");
}
System.out.println("Message4");
}
1) Message1
2) Message2
3) Message3
4) Message4
5) None of the above.
Q191 Choose the class that can hold multiple equal objects in an ordered way.
1) Map
2) Collection
3) List
4) Set
5) Vector
Q192 Consider the following variables definitions.
Float f1 = new Float("10F");
Float f2 = new Float("10F");
Double d1 = new Double("10D");
Which of the following yields a boolean value of true.
1) f1 == f2
2) f1.equals(f2)
3) f2.equals(d1)
4) f2.equals(new Float("10"))
Q193 What does the getID() method of AWTEvent tell us.
1) Tells us the x and y coords of the mouse at the event time.
2) Tells us which Object the event originated from.
3) Tells us which special keys were pressed at the event time.
4) Tells us the event type.
5) Tells us the how many pixels were between the mouse pointer and text on a Canvas.
Q194 What is the return type of an Eventlistener ?
1) int
2) NULL
3) void
4) boolean
5) Object
Q195 You create a Long object using the following:
Long val = new Long("11");
Which of the following will return the value of the object as a byte value?
1) byte b = (byte)val;
2) byte b = val.getByte();
3) byte b = (byte)val.getValue();
4) byte b = val.byteValue();
5) None of the above.
Q196 Consider the following.
String s1 = "abc";
String s2 = "def";
String s3 = new String(s2);
s2 = s1;
What is the value of s3 after the final line of code is executed?
1) "abc"
2) "def"
3) null
4) An exception is thrown when "s2 = s1" is executed.
Q197 What tags are mandatory when creating HTML to display an applet
1) name, width, height
2) code, name
3) codebase, height, width
4) code, width, height
5) None of the above
Q198 What is displayed when the following code is executed:
String s1 = " hello world ";
String s2 = s1.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值