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 mainString[]
Trang 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 arenot 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 providedc) 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?
Trang 2Determine one which of the following traversals has been performed
a) Reverse PostOrder Traversal
b) InOrder Traversal
Trang 3c) 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
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
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
Trang 4Analyze 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) {
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:
Trang 5a) The program displays RuntimeException.
b) The program displays NumberFormatException
c) The program has a compilation error
d) The program displays NumberFormatException followed by
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 usingonly one try-catch block
Trang 6d) 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);
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
public class Test extends A {
public static void main(String[] args) {
A frame = new Test();
Trang 7class A extends JFrame implements ActionListener {
JButton jbtCancel = new JButton("Cancel");
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
public static void main(String[] args) {
JFrame frame = new Test1();
Trang 8class MyCanvas extends JPanel {
private String message;
public void setMessage(String message) {
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
Border border = new TitledBorder("My button");
JButton jbt1 = new JButton("OK");
JButton jbt2 = new JButton("Cancel");
public static void main(String[] args) {
JFrame frame = new Test();
Trang 9d) 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");
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);
Trang 10a) 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");
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
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);
Trang 11frame.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();
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"))
Trang 12c) 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();
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 {
Trang 13Test test = null;
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 {
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 {
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);
Trang 14Analyze the following code:
public class Test {
public static void main(String[] args) {
HastSet set1 = new HashSet();
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();
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
Trang 15Analyze 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();
Trang 16c) 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
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);
}
Trang 17}
a) Since x is private, it cannot be accessed from an object foo
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 withoutcreating 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
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:
Trang 18Box 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
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{
Assume methodA is defined as follows:
public void methodA (int [] vals, JLabel display){
Trang 19// 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
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
Trang 20window.add(theButton);
b) JButton theButton = new JButton("ok");
c) JLabel theLabel = new JLabel("ok");
d) JButton theButton = new JButton("ok");
2 public class Question {
3 public static void main(String[] args) {
4 Vector v1 = new Vector();
5 Vector v2 = new Vector();
Trang 21b) 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
Choose two Which statements are true when casting objects in Java?
a) The source object and destination object must be related by inheritance
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 {
Trang 22public static void main (String args[]) {
default: break label1;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
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");
Trang 23System.out.println("Draw from Paddle");
public class Main {
public static void main(String [] args) {
GameComponent [] gc = new GameComponent [2];
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");
Trang 24public static void main(String [] args) {
GameComponent [] gc = new GameComponent [2];
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 {}
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
Trang 25class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
Trang 26Consider 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
Trang 27Question 74–b
Consider the following class defintion:
public class Demo extends JFrame implements ActionListener{
public void itemStateChanged(ActionEvent event){
b) public class Demo extends JFrame implements ItemListener,
Trang 28}
}
Question 75–abcd
Consider the following code:
1 for (int i = 0; i < 2; i++) {
Consider the following code:
1 outer: for (int i = 0; i < 2; i++) {
Consider this class:
1 public class Test1 {
2 public float aMethod(float a, float b) {
Trang 29c) 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) { }
Examine the following code which includes an inner class:
public final class Test4 implements A {
private boolean flag = false;
public void sample() {
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
Question 81–ae
Examine the following switch block:
Trang 30char 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 runs, 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 used in the switch statement
e) This switch block is fine
Trang 314 3 public static void main(String[] args) {
5 4 Vector v1 = new Vector(); a
6 5 Vector v2 = new Vector();
6 public static void main(String args[]){
7 Test t=new test();
c) After line 11, when main() completes
d) After line4, when doBar() completes
Trang 32When is the Demo object created on line 4, eligible for garbage collection?
a) When the takeDemo() method completes
b) After line 9
d) After the start() method completes
Trang 333 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();
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.setVisible(true); }
d) public static void main(String args[]) { Frame.visible = true; }
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) Compilation fails because of an error at line 17
c) Compilation fails because of an error at line 5
Trang 34d) Compilation fails because of an error at line 6.
15 public static void badMethod(){
16 throw new RuntimeException();
4 class Testlmpl implements Test{
5 public static void main(String[]args){
Trang 35f) An exception is thrown at runtime.
b) Just after line 13
c) Just after line 15
d) Just after line 16(that is, as the method returns)
Trang 3611 public class Test{
12 public void foo(){
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 is 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
Trang 38Given 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 contains an object of the Circle type
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();
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
Trang 39Question 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 = new Thread(t);
b) Each String in the array lines will output, with a 1-second pause
c) This code will not compile
Trang 40d) Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
Given the following,
1 public class HorseTest {
2 public static void main (String [] args) {
9 Object obj = new Horse("Zippo");
10 Horse h = (Horse) obj;
11 System.out.println(h.name);
12 }
13 }
what is the result?
a) An exception occurs at runtime at line 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