Question 38–b
Analyze the following code.
public class Test {
public static void main(String[] args) {
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println(x.compareTo(new Integer(4)));
}
}
-
The program compiles and runs fine.
-
The program has a syntax error because x does not have the compareTo method.
-
The program has a syntax error because an Integer instance cannot be assigned to a Number variable.
-
The program has a syntax error because intValue is an abstract method in Number.
Question 39–bcd
Analyze the following code:
public class Test {
public static void main(String[] args) {
Set set1 = new HashSet();
set1.add("red");
Set set2 = set1.clone();
}
}
-
The program compiles and runs and displays nothing.
-
The program has a compilation error because t is defined in both the main() method and the constructor Test().
-
The program compiles fine, but it does not run because you cannot use the keyword this in the constructor.
-
The program compiles and runs and displays test.
Question 40–abe
Analyze the following code:
public class Test {
public static void main(String[] args) {
Set set1 = new HashSet();
set1.add("red");
Set set2 = set1.clone();
}
}
-
Line 5 is wrong because the decalred type for set1 is Set and the clone method is protected in an instance of Set.
-
The program will be fine if set1.clone() is replaced by (HashSet)((HashSet)set1).clone()
-
The program will be fine if set1.clone() is replaced by (LinkedHashSet)((HashSet)set1).clone()
-
The program will be fine if set1.clone() is replaced by (HashSet)set1.clone()
-
The program will be fine if set1.clone() is replaced by (Set)((HashSet)set1).clone()
Question 41–bc
Analyze the following code:
ResultSet resultSet = statement.executeQuery
("select firstName, mi, lastName from Student where lastName " + " = 'Smith'");
System.out.println(resultSet.getString(1));
-
If the SQL SELECT statement returns no result, resultSet is null.
-
The program will have a runtime error, because the cursor in resultSet does not point to a row. You must use resultSet.next() to move the cursor to the first row in the result set. Subsequently, resultSet.next() moves the cursor to the next row in the result set.
-
resultSet.getString(1) returns the firstName field in the result set.
-
resultSet.getString(1) returns the mi field in the result set.
Question 42–e
Analyze the following code.
void looper() {
int x = 0;
one:
while (x<10) {
two:
System.out.println(++x);
if(x>3)
break two;
}
}
-
This method writes the number 0 to the standard output
-
the number 4 to the standard output
-
This code compiles
-
the number 3 to the standard output
-
This code does not compile
-
the numbers 1 and 2 to the standard output
Question 43–b
Analyze the following code and choose the best answer:
public class Foo {
private int x;
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo.x);
}
}
-
Since x is private, it cannot be accessed from an object foo.
-
Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.
-
Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code.
-
You cannot create a self-referenced object; that is, foo is created inside the class Foo.
Question 44–d
Analyze the following two classes.
class First {
static int a = 3;
}
final class Second extends First {
void method(){
System.out.println(a);
}
}
-
Class Second compiles, but class First does not
-
Class First compiles, but class Second does not
-
Neither class compiles
-
Both classes compile, and if method() is invoked, it writes 3 to the standard output
-
Both classes compile, but if method() is invoked, it throws an exception
Question 45–b
Analyze this line of code:
if(5 7 > 0 & 5|2) System.out.println("true");
-
this code will compile and write the word "true" in the standard output
-
this line of code will not compile
-
this code will compile but nothing will appear in the standard output
Question 46–d
Assume Box is a class with two property variables:
class Box {
private int width;
private int height;
public Box(int w, int h){
width = w;
height = h;
} // end constructor
}//end Box class
aBox and bBox are two reference variables to Box objects with the same height and width:
Box aBox = new Box(10, 40);
Box bBox = new Box(10, 40);
What is the result of evaluating (aBox == bBox) and why?
-
true, because aBox and bBox refers to the same Box object
-
not a valid boolean expression
-
true, because aBox has the same height and width with bBox
-
false, because aBox and bBox refer to different Box objects. The == operator will only compare references to two objects and will
-
be true if they refer to the exact same object.
Question 47-b
Assume class A extends class B, that is, class A is defined as:
class A extends B {
....
}// end of class A
Which class is the super class and which one is the subclass?
-
B is both the super class and subclass
-
B is the super class, A is the subclass
-
A is both the super class and subclass
-
A is the super class, B is the subclass
Question 48–a
Assume class MyFrame is defined as follows:
class MyFrame extends JFrame implements ActionListener{
...
}
Which of the following statements is true about the relationship of MyFrame, JFrame and ActionListener?
a) A MyFrame object IS-A JFrame object, and it also IS-A
ActionListener object.
b) A MyFrame object HAS-A JFrame object, and it also IS-A
ActionListener object.
c) A MyFrame object IS-A JFrame object, and it also HAS-A
ActionListener object
d) A MyFrame object HAS-A JFrame object, and it also HAS-A
ActionListener object.
Question 49–c
Assume f is a reference to a JPanel object. How do you set the size of the JPanel object f refers to a square of (100,100)?
-
f.setSize(100,100);
-
f.setPreferredSize(100,100);
-
f.setPreferredSize(new Dimension(100,100));
-
f.setSize(new Dimension(100,100));
Question 50–a
Assume methodA is defined as follows:
public void methodA (int [] vals, JLabel display){
// do something
....
}
Which one of the following is a correct way to call methodA?
Assume data is a non-empty reference to an array object, and lbl is a non-empty reference to a JLabel object.
-
methodA( data, lbl);
-
methodA( int [] data, lbl);
-
methodA(data[], lbl);
-
methodA( data[10], lbl);
Question 51–a
Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?
-
After thread A is notified, or after two seconds.
-
After the lock on B is released, or after two
-
Two seconds after thread A is notified.
-
Two seconds after lock B is released.
Question 52–d
Assume x = 5; y = 3; What's the value of y after this while loop?
while(x > 0){
y += 1;
x -=1;
}
-
7
-
3
-
–2
-
8
Question 53–b
Assume x = 5; y = 3; What's the value of y after this while loop?
while( x > 2 & y > 2) {
x -= 1;
y -= 1;
}
-
1
-
2
-
0
-
-1
Question 54–d
Assume window is a Container object to the applet's visible region on the screen. Which of the following code cause a button displayed?
a) JButton theButton;
window.add(theButton);
b) JButton theButton = new JButton("ok");
c) JLabel theLabel = new JLabel("ok");
d) JButton theButton = new JButton("ok");
window.add(theButton);
Question 55–c
At which line in the following code is the Vector object, created in line 4, first subject to garbage collection?
1. import java.util.*;
2. public class Question {
3. public static void main(String[] args) {
4. Vector v1 = new Vector();
5. Vector v2 = new Vector();
6. v1 = null;
7. Vector v3 = v1;.
8. v1 = v2;
9. v1.add("This");
10. v1.add(v2);
11. String s = (String) v1.elementAt(0);
12. v1 = v2;
13. v2 = v1;
14. v1.add(s);
15. }
16.}
-
Line 7
-
Line 8
-
Line 6
Question 56–c
_________ checks whether the JCheckBox jchk is selected.
-
jchk.select()
-
jchk.getSelected()
-
jchk.isSelected().
-
jchk.selected()
Question 57–c
Choose the 1 item below that is not typically listed as a major component of object oriented languages:
-
Polymorphism
-
Inheritance
-
Abstraction
-
Encapsulation
Question 58–a
Choose the best answer. What is created by lines 6-9 of the Point3D subclass?
3: public class Point3D extends Point {
4: public int z;
5:
6: public Point3D(int x, int y, int z) {
7: super(x,y);
8: this.z = z;
9: }
-
constructor method
-
method to extend Point3D
-
new method called Point3D
Question 59–abd
Choose three. Which of the following are true of a method?
-
Can return a class of objects
-
Require parentheses after the name
-
Defined using brackets [ ]
-
May or may not return a value when handled
-
Cannot take arguments
Question 60–abc
Choose three. Which of the following are true of constructors?
-
They are a special type of method.
-
They are used to create an object within a class or object.
-
They are used to set up variables needed for a new object.
-
They are used to create subclasses.
-
They cannot take strings as arguments.
Question 61–bc
Choose two. Which of the following are valid reasons for creating an inheritance hierarchy?
-
Java will do it for you.
-
It makes it easier to create new programs later.
-
It actually reduces the amount of coding you have to do.
-
It's easy to do.
Question 62–bc
Choose two. Which of the following types of collections may have duplicate elements?
-
Map
-
List
-
Collection
-
Set
Question 63–ab
Choose two. Which statements are true when casting objects in Java?
-
The source object and destination object must be related by inheritance.
-
A subclass object can be used in place of a superclass object and vice versa.
-
Any two Java objects may be used, regardless of inheritance.
-
A subclass object must be used in place of a superclass object.
Question 64–a
class A {
public static void main (String args[]) {
int h = 0, i = 0, j = 0, k = 0;
label1:
for (;;) {
h++;
label2:
do {
i++;
k = h + i + j;
switch (k) {
default: break label1;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
} while (++j<5);
}
System.out.println(h + "," + i + "," + j);
}
}
What is the result of attempting to compile and run the above program?
-
Prints: 1,3,2
-
Runtime Exception
-
Prints: 2,4,2
-
Prints: 2,2,2
-
Prints: 2,4,1
-
Prints: 1,2,3
Question 65–d
class GameComponent { // a game component
public void draw() {
System.out.println("Draw from Base");
}
}
class Ball extends GameComponent {
public void draw() {
System.out.println("Draw from Ball");
}
}
class Paddle extends GameComponent {
int iSize=5;
public void draw() {
super.draw();
System.out.println("Draw from Paddle");
}
public void setSize(int iNewSize) {
this.iSize = iNewSize;
}
}
public class Main {
public static void main(String [] args) {
GameComponent [] gc = new GameComponent [2];
gc[0] = new Ball();
gc[1] = new Paddle();
for (int i=0; i
gc[i].draw();
}
// gc[1].setSize(10);
}
}
The datatype of gc[1] is:
-
Paddle
-
Ball
-
Object
-
GameComponent
Question 66–c
class GameComponent { // a game component
public void draw() {
System.out.println("Draw from Base");
}
}
class Ball extends GameComponent {
public void draw() {
System.out.println("Draw from Ball");
}
}
class Paddle extends GameComponent {
int iSize=5;
public void draw() {
super.draw();
System.out.println("Draw from Paddle");
}
public void setSize(int iNewSize) {
this.iSize = iNewSize;
}
}
public class Main {
public static void main(String [] args) {
GameComponent [] gc = new GameComponent [2];
gc[0] = new Ball();
gc[1] = new Paddle();
for (int i=0; i
gc[i].draw();
}
// gc[1].setSize(10);
}
}
What is the first line of output from this program?
-
-
Draw from Base
-
Draw from Ball
-
Draw from Paddle
Question 67–f
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 3;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
}
a++;
}
catch (Level2Exception e) {b++;}
finally{c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}
}
What is the result of attempting to compile and run the program?
-
Prints: 1,1,1,0,0,1
-
Compiler Error
-
Prints: 0,1,0,0,0,0
-
Prints: 0,0,1,0,0,1
-
Prints: 0,1,0,0,0,1
-
Prints: 0,1,1,0,0,1
Question 68–c
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 3;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
}
a++;
}
finally{c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}
}
What is the result of attempting to compile and run the program?
-
Compiler Error
-
Prints: 0,0,1,0,0,1
-
Prints: 0,1,1,0,0,1
-
Prints: 1,1,1,0,0,1
Question 69–abc
Clicking a JComboBox object generates __________ events.
-
ItemEvent
-
MouseEvent
-
ActionEvent
-
WindowEvent
Question 70–ac
Clicking a JRadioButton generates _____________ events.
-
ActionEvent
-
ContainerEvent
-
ItemEvent
-
ComponentEvent
Question 71–d
Clicking the closing button on the upper-right corner of a frame generates a(n) __________ event.
-
ItemEvent
-
ContainerEvent
-
ComponentEvent
-
WindowEvent
-
MouseMotionEvent
Question 72–d
Consider the following:
class A extends Integer{
int x = 0;
}
Select valid statement.
-
The code will not compile because class A has no methods or constructor.
-
The code will compile correctly.
-
The code will compile correctly, but will throw an ArithmeticException at runtime.
-
The code will not compile because Integer is final and cannot be subclassed.
Question 73–d
Consider the following application:
1. class Q7 {
2. public static void main(String args[]) {
3. double d = 12.3;
4. Decrementer dec = new Decrementer();
5. dec.decrement(d);
6. System.out.println(d);
7. }
8. }
9.
10. class Decrementer {
11. public void decrement(double decMe) {
12. decMe = decMe - 1.0;
13, }
14. }
What value is printed out at line 6?
-
-1.0
-
0.0
-
11.3
-
12.3
Question 74–b
Consider the following class defintion:
public class Demo extends JFrame implements ActionListener{
......
public void actionPerformed(ActionEvent event){
....
}
}
If a JComboBox is used in a class, how should the class be modified to respond to clicks on the JComboBox?
a) public class Demo extends JFrame implements ActionListener
implements ItemListener{
......
public void actionPerformed(ActionEvent event){
....
}
public void itemStateChanged(ActionEvent event){
b) public class Demo extends JFrame implements ItemListener,
ActionListener{
......
public void actionPerformed(ActionEvent event){
....
}
public void itemStateChanged(ItemEvent event){
...
}
}
c) public class Demo extends JFrame implements ActionListener{
......
public void actionPerformed(ActionEvent event){
....
}
public void itemStateChanged(ItemEvent event){
...
}
}
d) public class Demo extends JFrame implements ItemListener,
ActionListener{
......
public void actionPerformed(ActionEvent event){
....
public void itemStateChanged(ItemEvent event){
...
}
}
}
Question 75–abcd
Consider the following code:
1. for (int i = 0; i < 2; i++) {
2. for (int j = 0; j < 3; j++) {
3. if (i == j) {
4. continue;
5. }
6. System.out.println("i = " + i + " j = " + j);
7. }
8. }
Which lines would be part of the output?
-
i = 0 j = 2
-
i = 0 j = 1
-
i = 1 j = 2
-
i = 1 j = 0
-
i = 1 j = 1
Question 76–b
Consider the following code:
1. outer: for (int i = 0; i < 2; i++) {
2. for (int j = 0; j < 3; j++) {
3. if (i == j) {
4. continue outer;
5. }
6. System.out.println("i = " + i + " j = " + j);
7. }
8. }
Which lines would be part of the output?
-
i = 1 j = 1
-
i = 1 j = 0
-
i = 0 j = 2
-
i = 0 j = 1
-
i = 0 j = 0
Question 77–cde
Consider this class:
1. public class Test1 {
2. public float aMethod(float a, float b) {
3. }
4.
5. }
Which of the following methods would be legal if added (individually) at line 4?
-
public float aMethod(float c, float d) { }
-
public float aMethod(float a, float b) { }
-
public float aMethod(float a, float b, int c) throws Exception { }
-
public int aMethod(int a, int b) { }
-
private float aMethod(int a, int b, int c) { }
Question 78–c
__________ describe interactions among objects by depicting the time-ordering of method invocations.
-
Statechart diagrams
-
Flowchart diagrams
-
Sequence diagrams
-
Class diagrams
Question 79–d
__________ describe the flow of control of an object.
-
Sequence diagrams
-
Flowchart diagrams
-
Class diagrams
-
Statechart diagrams
Question 80–c
Examine the following code which includes an inner class:
public final class Test4 implements A {
class Inner {
void test() {
if (Test4.this.flag); {
sample();
}
}
}
private boolean flag = false;
public void sample() {
System.out.println(" Sample");
}
public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}
What is the result:
-
Program produces no output but terminates correctly.
-
Program does not terminate.
-
Prints out "Sample"
-
The program will not compile
Chia sẻ với bạn bè của bạn: |