( *** IN OOP (USING JAVA) SUBJECT *** ) Question 1

kitewormsSoftware and s/w Development

Nov 3, 2013 (4 years and 9 days ago)

152 views

Ôn thi tốt nghiệp: JAVA + UML





-

1

-

( *** 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

Ques
tion 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) clas
s Node


{


int next;


}

b) class Node


{


int[ ] data;


Node next;

Ôn thi tốt nghiệp: JAVA + UML





-

2

-


}

c) class Node


{


Node next;


}

d) class Node


{


int data;


Node n
ext;


}

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 thei
r 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)

In
Order Traversal

c)

PostOrder Traversal

Ôn thi tốt nghiệp: JAVA + UML





-

3

-

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 e
xecution 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 relationshi
p 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)

Throwab
le.

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)

Run
timeException

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

b)

RuntimeException

c)

Throwable.

Ôn thi tốt nghiệp: JAVA + UML





-

4

-

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[] arg
s)


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:


class Test {


public static void main(String[] args) {


try {

Ôn thi tốt nghiệp: JAVA + UML





-

5

-



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("R
untimeException");


}


}


}

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 fo
llowing 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 block
s, because
nesting makes programs difficult to read. You can rewrite the program using
only one try
-
catch block.

d)

A try
-
catch block cannot be embedded inside another try
-
catch block.

Question 19

c

Analyze the following code:


class WhatHappens implements R
unnable {

Ôn thi tốt nghiệp: JAVA + UML





-

6

-


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 compile
s 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:


Cyli
nder 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.*;


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


}


public void actionPerformed(ActionEvent e) {


super.actionPerformed(e);



if (e.getSource() == jbtOK)


System.out.println("OK button is clicked");

Ôn thi tốt nghiệp: JAVA + UML





-

7

-


}


}


class A extends JFrame implements ActionListener {


JButton jbtCancel = new JButton("Cancel");


public A() {


getContentPane().setLayout(new FlowL
ayout());


getContentPane().add(jbtCancel);


jbtCancel.addActionListener(this);


}


public void actionPerformed(ActionEvent e) {


if (e.getSource() == jbtCancel)


System.out.println("Cancel button is clicked");


}


}

a)

Whe
n 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.s
wing.*;


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


}


}


class MyCanvas extends JPanel {


private String message;


public void setMessage(String message) {


this.message = message;


}


public void paintComponent(Graphics g) {

Ôn thi tốt nghiệp: JAVA + UML





-

8

-


super.pai
ntComponent(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 r
uns 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.se
tDefaultCloseOperation(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 bo
rder on a
button.

c)

Two buttons displayed, but only one button has the border.

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(St
ring[] args) {

Ôn thi tốt nghiệp: JAVA + UML





-

9

-


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 usi
ng
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;


publ
ic MyDrawing(String message) {


this.message = message;


}


public void paintcomponent(Graphics g) {


super.paintComponent(g);


g.drawString(message, 20 ,20);


}


}

a)

The program has a runtime error because the paintcomponent should be
spel
led 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 no
t display the message.

Question 26

b

Ôn thi tốt nghiệp: JAVA + UML





-

10

-

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 JBu
tton("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:


i
mport 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"));


getConte
ntPane().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.setDefa
ultCloseOperation(JFrame.EXIT_ON_CLOSE);


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 display
ed with the same text "Java".

Question 28

b

Analyze the following code:

Ôn thi tốt nghiệp: JAVA + UML





-

11

-


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 i
s 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 d
isplayed 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 StringTokeni
zer, but StringTokenizer
does not have a default constructor.

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.

Que
stion 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.

Ôn thi tốt nghiệp: JAVA + UML





-

12

-

b)

You cannot use Number as a data typ
e 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.o
ut.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 clas
s 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) {


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 cr
eate 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.

Ôn thi tốt nghiệp: JAVA + UML





-

13

-

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;


stati
c { 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 o
f 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);


}


}

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 consta
nt 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 set
1 = new HashSet();

Ôn thi tốt nghiệp: JAVA + UML





-

14

-


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 compi
lation 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

Analyze the fo
llowing 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 prog
ram 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.

Ôn thi tốt nghiệp: JAVA + UML





-

15

-

d)

The program has a syntax error because intValue is an abstract method in
Number.

Questi
on 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 progr
am 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()

c)

The program will be fine if set1.clone() is replaced by
(LinkedHashSet)((HashSet)set1).clone()

d)

The prog
ram 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 r
esultSet does
not point to a row. You must use resultSet.next() to move the cursor to the
Ôn thi tốt nghiệp: JAVA + UML





-

16

-

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


}


}

a)

Since x is private, it cannot be accessed from an object foo.

b)

Since x is an instance variab
le, 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 c
ode 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;


}

Ôn thi tốt nghiệp: JAVA + UML





-

17

-


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:


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 dif
ferent 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

Ôn thi tốt nghiệp: JAVA + UML





-

18

-

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 cl
ass 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){


// do something


....


}


Which one of the follo
wing 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.

Ôn thi tốt nghiệp: JAVA + UML





-

19

-

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;


window.add(theButton);

b) JButton theButton = new JButton("ok");

c) JLabel theLabel = new JLabel("ok");

d)
JButton theButton = new JButto
n("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(Str
ing[] 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);

Ôn thi tốt nghiệp: JAVA + UML





-

20

-


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 ob
ject
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;


5:


6: publi
c 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 cl
ass 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.

Ôn thi tốt nghiệp: JAVA + UML





-

21

-

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 re
asons 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 coll
ections 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 {


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

Ôn thi tốt nghiệp: JAVA + UML





-

22

-


}


}

What is the result of attempting to compile and run the above program?

a)

Prints: 1,3,2

b)

Runtime Excepti
on

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


System.out.println("Draw from Paddle");


}


public void setSize(int iNewSize) {


this.iSize = iNewSiz
e;


}


}


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

Ôn thi tốt nghiệp: JAVA + UML





-

23

-

d)

GameComponent

Question 66

c

class GameComponent { // a game component


public void draw() {


System.out.println("Draw from Base");


}


}


class Ball extends Gam
eComponent {


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 iNewSi
ze) {


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


}


}

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;

Ôn thi tốt nghiệp: JAVA + UML





-

24

-


a = b = c = d = f = g = 0;


x = 3;


try {


try {


switch (x) {


ca
se 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

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


}

Ôn thi tốt nghiệp: JAVA + UML





-

25

-


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.

a)

ActionEvent

b)

ContainerEvent

c)

ItemEvent

d)

ComponentEvent

Question 71

d

Clicking the closing button on the upper
-
right corner of a frame generates a(n)
__________ event.

a)

ItemEvent

b)

ContainerEvent

c)

ComponentEvent

d)

WindowEvent

e)

MouseMotionEvent

Quest
ion 72

d

Consider the following:


class A extends Integer{


int x = 0;


}

Select valid statement.

a)

The code will not compile because class A has no methods or constructor.

b)

The code will compile correctly.

c)

The code will compile correctly, but will throw
an ArithmeticException at
runtime.

d)

The code will not compile because Integer is final and cannot be subclassed.

Question 73

d

Ôn thi tốt nghiệp: JAVA + UML





-

26

-

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 a
t line 6?

a)

-
1.0

b)

0.0

c)

11.3

d)

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


impleme
nts ItemListener{


......


public void actionPerformed(ActionEvent event){


....


}


public void itemStateChanged(Actio
nEvent event){

b
) public class Demo extends JFrame implements ItemListener,


ActionListener{


......


public void actionPerformed(ActionEvent event){


....



}


public void itemStateChanged(ItemEvent event){


...

Ôn thi tốt nghiệp: JAVA + UML





-

27

-


}


}

c) public class Demo extends JFrame implements ActionListe
ner{


......


public void actionPerformed(ActionEvent event){


....


}


public void itemStateChanged(ItemEvent event){


...


}


}

d) public class Demo extends JFrame implements ItemListener,


ActionListener{


......


public voi
d 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?

a)

i = 0 j = 2

b)

i = 0 j = 1

c)

i = 1 j = 2

d)

i = 1 j = 0

e)

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

Ôn thi tốt nghiệp: JAVA + UML





-

28

-


7. }


8. }

Which lines would be part of the output?

a)

i = 1 j = 1

b)

i = 1 j = 0

c)

i = 0 j = 2

d)

i = 0 j = 1

e)

i = 0 j = 0

Question 77

cde

Consider this class:


1. public cl
ass 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?

a)

public float aMethod(float c, float d) { }

b)

public float aMethod(float a, float b) { }

c)

public float

aMethod(float a, float b, int c) throws Exception { }

d)

public int aMethod(int a, int b) { }

e)

private float aMethod(int a, int b, int c) { }

Question 78

c

__________ describe interactions among objects by depicting the time
-
ordering of
method invocations.

a)

St
atechart diagrams

b)

Flowchart diagrams

c)

Sequence diagrams

d)

Class diagrams

Question 79

d

__________ describe the flow of control of an object.

a)

Sequence diagrams

b)

Flowchart diagrams

c)

Class diagrams

d)

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;

Ôn thi tốt nghiệp: JAVA + UML





-

29

-


public void sample() {


System.out.println(" Sample");


}


public Tes
t4() {


(new Inner()).test();


}

public static void main(String args []) {


new Test4();

}


}

What is the result:

a)

Program produces no output but terminates correctly.

b)

Program does not terminate.

c)

Prints out "Sample"

d)

The program will not compile

Questi
on 81

ae

Examine the following switch block:


char mychar = c;


switch (mychar) {


default:


case 'a': System.out.println("a"); break;


case b: System.out.println("b"); break;


}

Which of the following questions are definitely true?

a)

When this code r
uns, the letter "a" is written to the standard output.

b)

When this code runs, nothing is writ en to the standard output.

c)

This switch block is illegal, because the default statement must come last.

d)

This switch block is illegal, because only integers can be us
ed in the switch
statement.

e)

This switch block is fine

Question 82

c

Examine the statements below. Which of the statements correctly describes the
hierarchy of MenuItem and CheckboxMenuItem classes?

1. The CheckboxMenuItem class extends the MenuItem class t
o support a menu
item that may be checked or unchecked.

2. The MenuItem class extends the CheckboxMenuItem class to support a menu
item that may be checked or unchecked.

a)

2

b)

Neither

c)

1

d)

1 and 2

Question 83

d

For THIS question, consider this code fragment:


int

x = 10;


try {

Ôn thi tốt nghiệp: JAVA + UML





-

30

-


int x = 5;


x = foo();


x++;


} catch (Exception e) {}


System.out.println(“x = “ + x);

For the above, assume that 1) it compiles and runs, and 2) that method foo()
throws an Exception during execution.

What is the output?

a)

x = 5

b)

x = 6

c)

Whatever the return value of foo() is.

d)

x = 10

Question 84

a

garbage collection?


2. 1. import java.util.*;


3. 2. public class Question {


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


5. 4. Vector v1 = new Vector(); a


6. 5. Vector v2 = new Vecto
r();


7. 6. v1.add("This");


8. 7. v1.add(v2);


9. 8. String s = (String) v1.elementAt(0);


10. 9. v1 = v2;


11. 10. v2 = v1;


12. 11. v1.add(s);


13. 12. }


13.}YÙ

a)

Line 9

b)

Line 8

c)

Line 6

d)

Line 5

Question 85

a

Given:


1. class Bar{}


1. class Test{


2. Bar do
Bar(){


3. Bar b=new Bar();


4. return b;


5. }


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


7. Test t=new test();


8. Bar newBar=t.doBar();


9. System.out.println("newBar");


10. newBar=new Bar();


11. System.out.println("finishing");

Ôn thi tốt nghiệp: JAVA + UML





-

31

-


12. }


13. }


At wh
at point is the Bar object, created on line 3, eligible for garbage collection?

a)

After line 10.

b)

After line 8.

c)

After line 11, when main() completes.

d)

After line4, when doBar() completes.

Question 86

a

Given:


1 class Bool {


2 static boolean b;


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


4 int x=0;


5 if (b ) {


6 x=1;


7 }


8 else if (b = false) {


9 x=2;


10 }


11 else if (b) {


12 x=3;


13 }


14 else {

15

x=4;

16 }

17 System.out.println("x = "

+ x);


18 }


19 }

What is the result?

a)

x = 4

b)

Compilation fails

c)

x = 2

d)

x = 1

e)

x = 3

f)

x = 0

Question 87

c

Given:


1. class Test{


2. private Demo d;


3. void start(){


4. d=new Demo();


5. this.takeDemo(d);


6. }


7.


8. void takeDemo(Demo demo){

Ôn thi tốt nghiệp: JAVA + UML





-

32

-


9. demo=nu
ll;


10. demo=new Demo();


11. }


12. }

When is the Demo object created on line 4, eligible for garbage collection?

a)

When the takeDemo() method completes.

b)

After line 9.

c)

When the instance running this code is made eligible for garbage collection
.

d)

After the s
tart() method completes.

e)

After line 5.

Question 88

c

Given:


1. import java.awt.*;


2.


3. public class Birthdays extends Frame {


4. Birthdays() {


5. super("Birthday Reminder");


6. String lblsP1[] = {"Name:", "Birthday:", "Address:"};


7. String butnsP2[] = {"Add", "Save", "Exit"};


8. Panel panelTop = new Panel();


9. Panel panelBot = new Panel();


10. panelTop.setLayout(new GridLayout(3,2,3,3));


11. for(int x = 0; x < lblsP1.length; x++) {


12. panelTop
.add(new Label(lblsP1[x]));


13. panelTop.add(new TextField());


14. }


15. for(int y = 0; y < butnsP2.length; y++) {


16. panelBot.add(new Button(butnsP2[y]));


17. }


18. add(panelTop, BorderLayout.NORTH);


19. add(
panelBot, BorderLayout.SOUTH);


20. }


21. }

Which main method should you add to the Birthdays class to allow the program to
compile and run with all defined fields properly displayed?

a) public static void main(String args[]) { Frame f = new Frame(
);


f.setVisible(true);}

b) public static void main(String args[]) { Frame f = Birthdays.new


Frame(); f.pack(); f.visible = true; }

c)
public static void main(String args[]) { Birthdays b = new


Birthdays(); b.pack(); b.setV
isible(true); }

d) public static void main(String args[]) { Frame.visible = true; }

Question 89

c

Given:


1. package foo;

Ôn thi tốt nghiệp: JAVA + UML





-

33

-


2. import java.util.Vector;


3. private class My Vector extends Vector {


4. int i = 1;


. 5. public My Vector() {


6. i = 2; } }


7. public class MyNewVector extends My Vector {


8. public MyNewVector () {14. i = 4; }


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


10. My Vector v = new MyNewVector (); } }


What is the result?

a)

Compilation fails because of an error at line 14.

b)

Compi
lation fails because of an error at line 17.

c)

Compilation fails because of an error at line 5.

d)

Compilation fails because of an error at line 6.

e)

Compilation succeeds.

Question 90

a

Given:


1. public class X{


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


3. try
{


4. badMethod();


5. System.out.print("A");


6. }


7. catch(Exception ex){


8. System.out.print("B");


9. }


10. finally{


11. System.out.print("C");


12. }


13. Sytstem.out.print("D");


14. }


15. public static void badMethod(){


16. throw new RuntimeEx
ception();


17. }


18. }

What is the result?

a)

BCD

b)

Compilation fails.

c)

BC

d)

ABC

e)

AB

Question 91

c

Given:


1. public interface Test{


2. int frood=42;

Ôn thi tốt nghiệp: JAVA + UML





-

34

-


3. }


4. class Testlmpl implements Test{


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


6. System.out.printIn(++fr
ood);


7. }


8. }

What is the result?

a)

43

b)

42

c)

Compilation fails.

d)

0

e)

1

f)

An exception is thrown at runtime.

Question 92

c

Given:


10. public Object m(){


11. Object o=new Float(3.14F);


12. Object[]oa=new Object[1];


13. ao[0]=o;


14. o=null;


15. oa[0]=null;


1
6. return o;


17. }

When is the Float object, created in line 11, eligible for garbage collection?

a)

Just after line 14.

b)

Just after line 13.

c)

Just after line 15.

d)

Just after line 16(that is, as the method returns).

Question 93

d

Given:


11. Float f=new Float("
12");


12. switch(f){


13. case 12:System.out.println("Twelve");


14. case 0:System.out.println("Zero");


15. default: System.out.println("Default");


16. }

What is the result?

a)

Zero

b)

Twelve


Zero


Default

c)

Twelve

d)

Compilation fails

e)

Default

Que
stion 94

b

Ôn thi tốt nghiệp: JAVA + UML





-

35

-

Given:


11. for (int i=0;i<3;i++){


12. switch(i){


13. case 0:break;


14. case 1:System.out.print("one");


15. case 2:System.out.print("two");


16. case 3:System.out.print("three");


17. }


18. }


19. System.out.println("done");

What is the res
ult?

a)

Done

b)

One two three two three done

c)

Compilation fails.

d)

One two done

Question 95

a

Given:


11. public class Test{


12. public void foo(){


13. assert false;


14. assert false;


15. }


16. public void bar(){


17. while(true){


18. assert false;


19. }


20
. assert false;


21. }


22. }

What causes compilation to fail?

a)

Line 20

b)

Line 14

c)

Line 18

d)

Line 13

Question 96

d

Given:


11. public void foo(boolean a, boolean b){


12. if(a){


13. System.out.println("A");


14. }else if(a&b){


15. System.out.println("A&B");


1
6. }else{


17. if( !b ){


18. System.out.println("notB");


19. }else{

Ôn thi tốt nghiệp: JAVA + UML





-

36

-


20. System.out.println("ELSE");


21. }


22. }


23. }

What is correct?

a)

If a is true and b is true then the output is "A&B".

b)

If a is true and be is false then the output is "notB".

c)

If a i
s false and b is false then the output is "ELSE".

d)

If a is false and be is true then the output is "ELSE".

Question 97

d

Given:


11. try{


12. int x=0;


13. int y=5/x;


14. }catch(Exception c){


15. System.out.println("Exception");


16. }catch(ArithmeticExc
eption ac){


17. System.out.println("Arithmetic Exception");


18. }


19. System.out.println("finished");

What is the result?

a)

finished

b)

Arithmetic Exception

c)

Exception

d)

Compilation fails.

Question 98

c

Given:


20. public float getSalary(Employee c){


21. asser
t validEmployee(c);


22. float sal=lookupSalary(c);


23. assert (sal>0);


24. return sal;


25. }


26. private int getAge(Employee c){


27. assert validEmployee(c);


28. int age=lookupAge(c);


29. assert (age>0);


30. return age;


31. }

Which line is a viol
ation of appropriate use of the assertion mechanism?

a)

Line 29

b)

Line 27

c)

Line 21

d)

Line 23

Question 99

e

Ôn thi tốt nghiệp: JAVA + UML





-

37

-

Given a string constructed by calling s = new String("xyzzy"), which of the calls
listed below modify the string?

a)

s.trim();

b)

s.concat(s);

c)

s.replace(`z', `a');

d)

s.substring(3);

e)

None of the others

Question 100

b

Given the declaration Circle x = new Circle(), which of the following statement is
most accurate.

a)

x contains an int value.

b)

x contains a reference to a Circle object.

c)

You can assign an int value to x.

d)

x con
tains an object of the Circle type.

Question 101

d

Given the following,


1.class Foo {


2. class Bar{ }


3.}


4.class Test {


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


6. Foo f = new Foo();


7. // Insert code here


8. }


9.}

which statement, inserted
at line 5, creates an instance of Bar?

a)

Foo.Bar b = new f.Bar();

b)

Foo.Bar b = new Foo.Bar();

c)

Bar b = f.new Bar();

d)

Foo.Bar b = f.new Bar();

e)

Bar b = new f.Bar();

Question 102

a

Given the following,


1. class MyThread extends Thread {


2.


3. public static void

main(String [] args) {


4. MyThread t = new MyThread();


5. t.run();


6. }


7.


8. public void run() {


9. for(int i=1;i<3;++i) {


10. System.out.print(i + "..");


11. }


12. }

Ôn thi tốt nghiệp: JAVA + UML





-

38

-


13. }

what is the result?

a)

1..2..

b)

This code will not compile due to line 5.

c)

An

exception is thrown at runtime.

d)

This code will not compile due to line 4.

e)

1..2..3..

Question 103

b

Given the following,


1. class MyThread extends Thread {


2.


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


4. MyThread t = new MyThread();


5. Thread x = n
ew Thread(t);


6. x.start();


7. }


8.


9. public void run() {


10. for(int i=0;i<3;++i) {


11. System.out.print(i + "..");


12. }


13. }


14. }

what is the result of this code?

a)

1..2..3..

b)

0..1..2..

c)

0..1..2..3..

d)

Compilation fails.

e)

An exception occurs at run
time.

Question 104

c

Given the following,


1. class Test {


2.


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


4. printAll(args);


5. }


6.


7. public static void printAll(String[] lines) {


8. for(int i=0;i<lines.length;i++){


9. System.out.println(lines[i
]);


10. Thread.currentThread().sleep(1000);


11. }


12. }


13. }

Ôn thi tốt nghiệp: JAVA + UML





-

39

-

the static method Thread.currentThread() returns a reference to the currently
executing Thread object. What is the result of this code?

a)

Each String in the array lines will output, with no pa
use in between because
this method is not executed in a Thread.

b)

Each String in the array lines will output, with a 1
-
second pause.

c)

This code will not compile.

d)

Each String in the array lines will output, and there is no guarantee there
will be a pause becau
se currentThread() may not retrieve this thread.

Question 105

d

Given the following,


1. class X2 {


2. public X2 x;


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


4. X2 x2 = new X2();


5. X2 x3 = new X2();


6. x2.x = x3;


7. x3.x = x2;


8. x2 = new X2();


9. x3 = x2;


10. doComplexStuff();


11. }


12. }

after line 9 runs, how many objects are eligible for garbage collection?

a)

3

b)

1

c)

4

d)

2

e)

0

f)

5

Question 106

e

Given the following,


1. public class HorseTest {


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


3. class

Horse {


4. public String name;


5. public Horse(String s) {


6. name = s;


7. }


8. }


9. Object obj = new Horse("Zippo");


10. Horse h = (Horse) obj;


11. System.out.println(h.name);


12. }


13. }

what is the result?

Ôn thi tốt nghiệp: JAVA + UML





-

40

-

a)

An exception occurs at runtime at li
ne 10.

b)

Compilation fails because of an error on line 11.

c)

Compilation fails because of an error on line 9.

d)

Compilation fails because of an error on line 3.

e)

Zippo

f)

Compilation fails because of an error on line 10.

Question 107

b

Given the following,


1. publi
c class HorseTest {


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


3. class Horse {


4. public String name;


5. public Horse(String s) {


6. name = s;


7. }


8. }


9. Object obj = new Horse("Zippo");


10. System.out.println(obj.name);


11. }


12. }


what
is the result?

a)

An exception occurs at runtime at line 10.

b)

Compilation fails because of an error on line 10.

c)

Compilation fails because of an error on line 9.

d)

Compilation fails because of an error on line 3.

e)

Zippo.

Question 108

b

Given the following,


1. pub
lic class MyRunnable implements Runnable {


2. public void run() {


3. // some code here


4. }


5. }

which of these will create and start this thread?

a)

new Thread(MyRunnable).run();

b)

new Thread(new MyRunnable()).start();

c)

new MyRunnable().start();

d)

new Runnabl
e(MyRunnable).start();

Question 109

b

Given the following,


1. public class Test {


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


3. final Foo f = new Foo();


4. Thread t = new Thread(new Runnable() {


5. public void run() {

Ôn thi tốt nghiệp: JAVA + UML





-

41

-


6. f.doStuff();


7. }


8. });


9. Thread g = new Thread() {


10. public void run() {


11. f.doStuff();


12. }


13. };


14. t.start();


15. g.start();


16. }


17. }


1. class Foo {


2. int x = 5;


3. public void doStuff() {


4. if (x < 10) {


5. // nothing to do


6. try {


7. wait();


8. } catch(InterruptedException ex) { }


9. } else {


10. System.out.println("x is " + x++);


11. if (x >= 10) {


12. notify();


13. }


14. }


15. }


16. }

what is the result?

a)

The code will not compile because of an error on line 7 of class Foo.

b)

An excepti
on occurs at runtime.

c)

The code will not compile because of an error on line 12 of class Foo.

d)

x is 5


x is 6