Tải bản đầy đủ (.doc) (153 trang)

Ôn thi tốt nghiệp JAVA and UML

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (685.55 KB, 153 trang )

Ôn thi t t nghi p: JAVA + UMLố ệ
( *** IN OOP (USING JAVA) SUBJECT *** )
Question 1–b
A class Car and its subclass Yugo both have a method run() which was written by
the programmer as part of the class definition. If junker refers to an object of type
Yugo, what will the following code do?
junker.show();
a) The compiler will complain that run() has been defined twice
b)The show() method defined in Yugo will be called.
b) The show() method defined in Car will be called.
c) Overloading will be used to pick which run() is called
Question 2–e
A class design requires that a particular member variable must be accessible for
direct access by any subclasses of this class, but otherwise not by classes which are
not members of the same package. What should be done to achieve this?
a) The variable should have no special access modifier
b) The variable should be marked private and an accessor method provided
c) The variable should be marked private
d) The variable should be marked public
e)The variable should be marked protected
Question 3–d
A Frame's background color is set to Color.Yellow, and a Button's background
color is set to Color.Blue. Suppose the Button is added to a Panel, and the Panel is
added to the Frame. What background color will be used with the Panel?
a) Color.Green
b) Color.Blue
c) Color.White
d) Color.Yellow
Question 4–b
A Java exception is an instance of __________.
a) Exception


b) Throwable.
c) RuntimeException
d) Error
e) NumberFormatException
Question 5–d
A linked list that stores int values would be comprised(Bao gom) of a group of
Nodes. We might define Node by
a) class Node
{
int next;
}
b) class Node
{
int[ ] data;
- 1 -
Ôn thi t t nghi p: JAVA + UMLố ệ
Node next;
}
c) class Node
{
Node next;
}
d) class Node
{
int data;
Node next;
}
e) class Node
{
int data;

}
Question 6–d
A statement for a constructor that invokes the no-args, default constructor in its
superclass is:
a) this.constructor();
b) descendent classes can not call constructors of their superclass.
c) This
d) super();
Question 7–c
A traversal of the binary search tree in figure is given as:
12, 27, 25, 28, 20, 38, 45, 42, 35, 30
Determine one which of the following traversals has been performed
a) Reverse PostOrder Traversal
b) InOrder Traversal
- 2 -
Ôn thi t t nghi p: JAVA + UMLố ệ
c) PostOrder Traversal
d) Reverse PreOrder Traversal
e) PreOrder Traversal
Question 8–b
A vector can be described as _______.
a) A fixed length data structure
b) A dynamic array
c) A special type queue
d) A special type of stack
Question 9–d
After execution of the code fragment below, what are the values of the variables x,
a, and b?
1. int x, a = 6, b = 7;
2. x = a++ + b++;

a) x = 13, a = 6, b = 7
b) x = 15, a = 6, b = 7
c) x = 15, a = 7, b = 8
d) x = 13, a = 7, b = 8
Question 10–c
An aggregation relationship is usually represented as __________ in
___________.
a) a data field/the aggregated class
b) a method/the aggregated class
c) a data field/the aggregating class
d) a method/the aggregating class
Question 11–de
An instance of _________ are checked exceptions.
a) Throwable.
b) NumberFormatException
c) Error
d) RuntimeException
e) Exception
Question 12–e
An instance of _________ describes programming errors, such as bad casting,
accessing an out-of-bounds array, and numeric errors
a) Error
b) NumberFormatException
c) Throwable.
d) Exception
e) RuntimeException
Question 13–e
An instance of _________ describes the errors caused by your program and
external circumstances. These errors can be caught and handled by your program.
a) Error

- 3 -
Ôn thi t t nghi p: JAVA + UMLố ệ
b) RuntimeException
c) Throwable.
d) NumberFormatException
e) Exception
Question 14–c
Analyze the following code:
Circle c = new Circle (5);
Cylinder c = cy;
a) The code is fine.
b) The code has a runtime error.
c) The code has a syntax error.
Question 15–a
Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}
a) The program will compile, but you cannot create an object of Circle with a
specified radius. The object will always have radius 0.
b) The program has a compilation error because it does not have a main
method.
c) The program has a compilation error because you cannot assign radius to
radius.
d) The program does not compile because Circle does not have a default
constructor.
Question 16–d

Analyze the following code:
class Test {
public static void main(String[] args)
throws MyException {
System.out.println("Welcome to Java");
}
}
class MyException extends Error {
}
a) You cannot declare an exception in the main method.
b) The program has a compilation error.
c) You declared an exception in the main method, but you did not throw it.
d) You should not declare a class that extends Error, because Error raises a
fatal error that terminates the program.
Question 17–c
Analyze the following code:
- 4 -
Ôn thi t t nghi p: JAVA + UMLố ệ
class Test {
public static void main(String[] args) {
try {
Integer.parseInt(5.6); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
}
catch (Exception ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");

}
}
}
a) The program displays RuntimeException.
b) The program displays NumberFormatException.
c) The program has a compilation error.
d) The program displays NumberFormatException followed by
RuntimeException.
Question 18–c
Analyze the following code:
class Test {
public static void main(String[] args) {
try {
int zero = 0;
int y = 2/zero;
try {
Integer.parseInt(5.6); // Causes NumberFormatExcepiton
}
catch(Exception e) {
}
}
catch(RuntimeException e) {
System.out.println(e);
}
}
}
a) None of the other answers
b) The program has a compilation error because Exception appears before
RuntimeException.
c) A good programming practice is to avoid nesting try-catch blocks, because

nesting makes programs difficult to read. You can rewrite the program using
only one try-catch block.
- 5 -
Ôn thi t t nghi p: JAVA + UMLố ệ
d) A try-catch block cannot be embedded inside another try-catch block.
Question 19–c
Analyze the following code:
class WhatHappens implements Runnable {
public static void main(String[] args) {
Thread t = new Thread(this);
t.start();
}
public void run() {
System.out.println("hi");
}
}
a) This program compiles and the word "hi' appears in the standard output,
once
b) This program compiles and the word "hi" appears continuously in the
standard output until the user hits control-c to stop the program
c) This program does not compile
d) This program compiles but nothing appears in the standard output
Question 20–a
Analyze the following code:
Cylinder cy = new Cylinder(1, 1);
Circle c = cy;
a) The code is fine.
b) The code has a runtime error.
c) The code has a syntax error.
Question 21–e

Analyze the following code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends A {
public static void main(String[] args) {
A frame = new Test();
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
JButton jbtOK = new JButton("OK");
public Test() {
getContentPane().add(jbtOK);
jbtOK.addActionListener(this);
- 6 -
Ôn thi t t nghi p: JAVA + UMLố ệ
}
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
if (e.getSource() == jbtOK)
System.out.println("OK button is clicked");
}
}
class A extends JFrame implements ActionListener {
JButton jbtCancel = new JButton("Cancel");
public A() {

getContentPane().setLayout(new FlowLayout());
getContentPane().add(jbtCancel);
jbtCancel.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtCancel)
System.out.println("Cancel button is clicked");
}
}
a) When you click the OK button the message "OK button is clicked" is
displayed.
b) When you click the Cancel button the message "Cancel button is clicked" is
displayed.
c) The program displays Cancel button on the left of the OK button.
d) If the super.actionPerformed(e) statement in the actionPerformed method in
the Test class is omitted, no message is displayed if you click the Cancel
button.
e) All of the other answers
Question 22–d
Analyze the following code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test1 extends JFrame {
public Test1() {
getContentPane().add(new MyCanvas());
}
public static void main(String[] args) {
JFrame frame = new Test1();
frame.setSize(300, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
- 7 -
Ôn thi t t nghi p: JAVA + UMLố ệ
class MyCanvas extends JPanel {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(message, 20, 20);
}
}
a) The program has a syntax error because new Test1() is assigned to frame.
b) The program would display Welcome to Java! if you replace new
MyCanvas() by new MyCanvas("Welcome to Java!").
c) The program runs fine and displays nothing since you have not set a string
value.
d) The program has a NullPointerException since message is null when
g.drawString(message, 20, 20) is executed.
Question 23–d
Analyze the following code:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class Test extends JFrame {
public Test() {

Border border = new TitledBorder("My button");
JButton jbt1 = new JButton("OK");
JButton jbt2 = new JButton("Cancel");
jbt1.setBorder(border);
jbt2.setBorder(border);
getContentPane().add(jbt1, BorderLayout.NORTH);
getContentPane().add(jbt2, BorderLayout.SOUTH);
}
public static void main(String[] args) {
JFrame frame = new Test();
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
a) The program has a syntax error because you assign new TitledBorder("My
button") to a variable of the Border type.
b) The program has a runtime error because you cannot set a border on a
button.
c) Two buttons displayed, but only one button has the border.
- 8 -
Ôn thi t t nghi p: JAVA + UMLố ệ
d) Two buttons displayed with the same border.
Question 24–d
Analyze the following code.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
Component c = new JButton("OK");

JFrame frame = new JFrame("My Frame");
frame.add(c);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
a) You can only add c to a container because c's type is Component.
b) You cannot create a JFrame using new JFrame("My Frame").
c) You cannot assign a JButton to a variable of java.awt.Component.
d) You cannot add a Swing component directly to a JFrame.
e) Instead, you have to add it to a JFrame's contentPane using
frame.getContentPane().add(c).
Question 25–e
Analyze the following code.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.getContentPane().add(new MyDrawing("Welcome to Java!"));
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setVisible(true);
}
}
class MyDrawing extends JPanel {
String message;
public MyDrawing(String message) {
this.message = message;

}
public void paintcomponent(Graphics g) {
super.paintComponent(g);
g.drawString(message, 20 ,20);
}
}
- 9 -
Ôn thi t t nghi p: JAVA + UMLố ệ
a) The program has a runtime error because the paintcomponent should be
spelled as paintComponent.
b) The program has a syntax error because the paintcomponent should be
spelled as paintComponent.
c) The program runs fine and displays Welcome to Java!
d) It is a runtime to invoke the setVisible(true) twice.
e) The program runs, but it does not display the message.
Question 26–b
Analyze the following code.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.getContentPane().add(new JButton("OK"));
frame.getContentPane().add(new JButton("Cancel"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
a) Both button OK and button Cancel are displayed and button OK is displayed

on the left side of button OK.
b) Only button Cancel is displayed.
c) Both button OK and button Cancel are displayed and button OK is displayed
on the right side of button OK.
d) Only button OK is displayed.
Question 27–b
Analyze the following code:
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
public Test() {
getContentPane().setLayout(new FlowLayout());
getContentPane().add(new JButton("Java"));
getContentPane().add(new JButton("Java"));
getContentPane().add(new JButton("Java"));
getContentPane().add(new JButton("Java"));
}
public static void main(String[] args) {
// Create a frame and set its properties
JFrame frame = new Test();
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- 10 -
Ôn thi t t nghi p: JAVA + UMLố ệ
frame.setVisible(true);
}
}
a) One button is displayed with the text "Java".
b) Four buttons are displayed with the same text "Java".
c) Three buttons are displayed with the same text "Java".

d) Two buttons are displayed with the same text "Java".
Question 28–b
Analyze the following code:
import javax.swing.*;
public class Test extends JFrame {
private JButton jbtOK = new JButton("OK");
public static void main(String[] args) {
// Create a frame and set its properties
JFrame frame = new Test();
frame.setTitle("Logic Error");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Test() {
jbtOK.setToolTipText("This is a button");
getContentPane().add(new JButton("OK"));
}
}
a) The tool tip text is displayed when you move the mouse on the button.
b) The tool tip text will be displayed if you replace add(new JButton("OK"))
with add(jbtOK).
c) The tool tip text will be displayed if you swap the two lines in the Test
constructor.
d) The tool tip text will be displayed if you replace add(new JButton("OK"))
with add(jbtOK = new JButton("OK")).
Question 29–bc
Analyze the following code:
import java.util.StringTokenizer;
public class A extends StringTokenizer {

}
a) The program would compile fine if you add the following constructor into
A:
A(String s) { }
b) The program has a compilation error because the default constructor of A
invokes the default constructor of StringTokenizer, but StringTokenizer
does not have a default constructor.
- 11 -
Ôn thi t t nghi p: JAVA + UMLố ệ
c) The program would compile fine if you add the following constructor into
A:
A(String s) { super(s); }
d) The program has a compilation error because A does not have a default
constructor.
Question 30–c
Analyze the following code.
Number[] numberArray = new Integer[2];
numberArray[0] = new Double(1.5);
a) Since each element of numbeArray is of the Number type, you cannot
assign an Integer object to it.
b) You cannot use Number as a data type since it is an abstract class.
c) At runtime, new Integer[2] is assigned to numberArray. This makes each
element of numberArray an Integer object. So you cannot assign a Double
object to it.
d) Since each element of numbeArray is of the Number type, you cannot
assign a Double object to it.
Question 31–a
Analyze the following code.
public class Test {
int x;

public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.x);
}
}
a) The program has a syntax error because Test does not have a default
constructor.
b) The program has a syntax error because x has not been initialized.
c) The program has a syntax error because you cannot create an object from
the class that defines the object.
d) The program has a syntax error because System.out.println method cannot
be invoked from the constructor.
Question 32–c
Analyze the following code.
public class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args) {
- 12 -
Ôn thi t t nghi p: JAVA + UMLố ệ
Test test = null;
System.out.println(test.x);
}
}
a) The program has a syntax error because Test does not have a default

constructor.
b) The program has a syntax error because you cannot create an object from
the class that defines the object.
c) The program has a runtime NullPointerException because test is null while
executing test.x.
d) The program has a syntax error because test is not initialized.
e) The program has a syntax error because x has not been initialized.
Question 33–d
Analyze the following code:
public class Test {
int x;
{ x++; }
}
a) The program cannot be compiled, because the statement x++ must be placed
inside a method or a constructor.
b) When you construct an instance of Test, the value of x becomes 0;
c) You cannot construct an instance of Test, because it does not have a
constructor.
d) When you construct an instance of Test, the value of x becomes 1;
Question 34–a
Analyze the following code:
public class Test {
int x;
static { x++; }
}
a) The program cannot be compiled, because x is non-static, but is used in a
static initialization block.
b) The program cannot be compiled, because the statement x++ must be placed
inside a method or a constructor.
c) When you construct an instance of Test, the value of x becomes 0;

d) When you construct an instance of Test, the value of x becomes 1;
Question 35–a
Analyze the following code:
public class Test {
public static void main(String[] args) {
double radius;
final double PI= 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
- 13 -
Ôn thi t t nghi p: JAVA + UMLố ệ
}
}
a) The program has syntax errors because the variable radius is not initialized.
b) The program compiles and runs fine.
c) The program has a syntax error because a constant PI is defined inside a
method.
d) The program has no syntax errors but will get a runtime error because radius
is not initialized.
Question 36–bde
Analyze the following code:
public class Test {
public static void main(String[] args) {
HastSet set1 = new HashSet();
set1.add("red");
Set set2 = set1.clone();
}
}
a) The program will be fine if set1.clone() is replaced by (Set)set1.clone()
b) The program will be fine if set1.clone() is replaced by (Set)(set1.clone())

c) Line 5 is wrong because a HashSet object cannot be cloned.
d) The program will be fine if set1.clone() is replaced by (HashSet)
(set1.clone())
e) Line 5 has a syntax error because set1.clone() returns an Object.
f) You have to cast it to Set in order to compile it.
Question 37–b
Analyze the following code:
public class Test {
public static void main(String args[]) {
NClass nc = new NClass();
nc.t = nc.t++;
}
}
class NClass {
int t;
private NClass() {
}
}
a) The program compiles and runs fine.
b) The program has a compilation error because the NClass class has a private
constructor.
c) The program compiles, but has a runtime error because t has no initial value.
d) The program does not compile because the parameter list of the main
method is wrong.
Question 38–b
- 14 -
Ôn thi t t nghi p: JAVA + UMLố ệ
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)));
}
}
a) The program compiles and runs fine.
b) The program has a syntax error because x does not have the compareTo
method.
c) The program has a syntax error because an Integer instance cannot be
assigned to a Number variable.
d) 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();
}
}
a) The program compiles and runs and displays nothing.
b) The program has a compilation error because t is defined in both the main()
method and the constructor Test().
c) The program compiles fine, but it does not run because you cannot use the
keyword this in the constructor.
d) 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();
}
}
a) Line 5 is wrong because the decalred type for set1 is Set and the clone
method is protected in an instance of Set.
b) The program will be fine if set1.clone() is replaced by (HashSet)
((HashSet)set1).clone()
- 15 -
Ôn thi t t nghi p: JAVA + UMLố ệ
c) The program will be fine if set1.clone() is replaced by (LinkedHashSet)
((HashSet)set1).clone()
d) The program will be fine if set1.clone() is replaced by (HashSet)set1.clone()
e) 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));
a) If the SQL SELECT statement returns no result, resultSet is null.
b) 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.
c) resultSet.getString(1) returns the firstName field in the result set.
d) 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;
}
}
a) This method writes the number 0 to the standard output
b) the number 4 to the standard output
c) This code compiles
d) the number 3 to the standard output
e) This code does not compile
f) 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);
}
- 16 -
Ôn thi t t nghi p: JAVA + UMLố ệ
}
a) Since x is private, it cannot be accessed from an object foo.

b) 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.
c) 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.
d) 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);
}
}
a) Class Second compiles, but class First does not
b) Class First compiles, but class Second does not
c) Neither class compiles
d) Both classes compile, and if method() is invoked, it writes 3 to the standard
output
e) 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");
a) this code will compile and write the word "true" in the standard output
b) this line of code will not compile
c) 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:
- 17 -
Ôn thi t t nghi p: JAVA + UMLố ệ
Box aBox = new Box(10, 40);
Box bBox = new Box(10, 40);
What is the result of evaluating (aBox == bBox) and why?
a) true, because aBox and bBox refers to the same Box object
b) not a valid boolean expression
c) true, because aBox has the same height and width with bBox
d) false, because aBox and bBox refer to different Box objects. The ==
operator will only compare references to two objects and will
e) 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?
a) B is both the super class and subclass

b) B is the super class, A is the subclass
c) A is both the super class and subclass
d) 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)?
a) f.setSize(100,100);
b) f.setPreferredSize(100,100);
c) f.setPreferredSize(new Dimension(100,100));
d) f.setSize(new Dimension(100,100));
Question 50–a
Assume methodA is defined as follows:
public void methodA (int [] vals, JLabel display){
- 18 -
Ôn thi t t nghi p: JAVA + UMLố ệ

// 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.
a) methodA( data, lbl);
b) methodA( int [] data, lbl);
c) methodA(data[], lbl);
d) 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?
a) After thread A is notified, or after two seconds.
b) After the lock on B is released, or after two
c) Two seconds after thread A is notified.
d) 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;
}
a) 7
b) 3
c) –2
d) 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;
}
a) 1
b) 2
c) 0
d) -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;
- 19 -
Ôn thi t t nghi p: JAVA + UMLố ệ
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.}
a) Line 7
b) Line 8
c) Line 6
Question 56–c
_________ checks whether the JCheckBox jchk is selected.
a) jchk.select()
b) jchk.getSelected()
c) jchk.isSelected().
d) jchk.selected()
Question 57–c
Choose the 1 item below that is not typically listed as a major component of object
oriented languages:
a) Polymorphism
b) Inheritance
c) Abstraction
d) 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;

- 20 -
Ôn thi t t nghi p: JAVA + UMLố ệ
5:
6: public Point3D(int x, int y, int z) {
7: super(x,y);
8: this.z = z;
9: }
a) constructor method
b) method to extend Point3D
c) new method called Point3D
Question 59–abd
Choose three. Which of the following are true of a method?
a) Can return a class of objects
b) Require parentheses after the name
c) Defined using brackets [ ]
d) May or may not return a value when handled
e) Cannot take arguments
Question 60–abc
Choose three. Which of the following are true of constructors?
a) They are a special type of method.
b) They are used to create an object within a class or object.
c) They are used to set up variables needed for a new object.
d) They are used to create subclasses.
e) They cannot take strings as arguments.
Question 61–bc
Choose two. Which of the following are valid reasons for creating an inheritance
hierarchy?
a) Java will do it for you.
b) It makes it easier to create new programs later.
c) It actually reduces the amount of coding you have to do.

d) It's easy to do.
Question 62–bc
Choose two. Which of the following types of collections may have duplicate
elements?
a) Map
b) List
c) Collection
d) Set
Question 63–ab
Choose two. Which statements are true when casting objects in Java?
a) The source object and destination object must be related by inheritance.
b) A subclass object can be used in place of a superclass object and vice versa.
c) Any two Java objects may be used, regardless of inheritance.
d) A subclass object must be used in place of a superclass object.
Question 64–a
class A {
- 21 -
Ôn thi t t nghi p: JAVA + UMLố ệ
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?
a) Prints: 1,3,2
b) Runtime Exception
c) Prints: 2,4,2
d) Prints: 2,2,2
e) Prints: 2,4,1
f) 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();
- 22 -
Ôn thi t t nghi p: JAVA + UMLố ệ
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.length; i++) {
gc[i].draw();
}
// gc[1].setSize(10);
}
}
The datatype of gc[1] is:
a) Paddle
b) Ball
c) Object
d) 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 {
- 23 -
Ôn thi t t nghi p: JAVA + UMLố ệ
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.length; i++) {
gc[i].draw();
}
// gc[1].setSize(10);
}
}

What is the first line of output from this program?
a) <nothing>
b) Draw from Base
c) Draw from Ball
d) 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?
a) Prints: 1,1,1,0,0,1
- 24 -
Ôn thi t t nghi p: JAVA + UMLố ệ
b) Compiler Error
c) Prints: 0,1,0,0,0,0
d) Prints: 0,0,1,0,0,1
e) Prints: 0,1,0,0,0,1
f) 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?
a) Compiler Error
b) Prints: 0,0,1,0,0,1
c) Prints: 0,1,1,0,0,1
d) Prints: 1,1,1,0,0,1
Question 69–abc
Clicking a JComboBox object generates __________ events.
a) ItemEvent
b) MouseEvent
c) ActionEvent
d) WindowEvent
Question 70–ac
Clicking a JRadioButton generates _____________ events.
- 25 -

×