Deck 10: Thinking in Objects

Full screen (f)
exit full mode
Question
When you implement a method that is defined in a superclass, you __________ the original method.

A) overload
B) override
C) copy
D) call
Use Space or
up arrow
down arrow
to flip the card.
Question
Design and create GUI applications
Write a Java applet to add two numbers from text fields, and displays the result in a non-editable text field. Enable your applet to run standalone with a main method. A sample run of the applet is shown in the following figure. Design and create GUI applications Write a Java applet to add two numbers from text fields, and displays the result in a non-editable text field. Enable your applet to run standalone with a main method. A sample run of the applet is shown in the following figure.  <div style=padding-top: 35px>
Question
Show the output of running the class Test in the following code lines: interface A {
Void print();
}
Class C {}
Class B extends C implements A {
Public void print() { }
}
Class Test {
Public static void main(String[] args) {
B b = new B();
If (b instanceof A)
System.out.println("b is an instance of A");
If (b instanceof C)
System.out.println("b is an instance of C");
}
}

A) Nothing.
B) b is an instance of A.
C) b is an instance of C.
D) b is an instance of A followed by b is an instance of C.
Question
_________ describes the state of an object.

A) data fields
B) methods
C) constructors
D) none of the above
Question
Design and use interfaces
Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal size. The area can be computed using the following formula: Design and use interfaces Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal size. The area can be computed using the following formula:   Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable.<div style=padding-top: 35px> Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable.
Question
Suppose s is a string with the value "java". What will be assigned to x if you execute the following code? char x = s.charAt(4);

A) 'a'
B) 'v'
C) Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.
D) None of the above.
Question
How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2

A) args[0]
B) args[1]
C) args[2]
D) args[3]
Question
If a class named Student has no constructors defined explicitly, the following constructor is implicitly provided.

A) public Student()
B) protected Student()
C) private Student()
D) Student()
Question
What would be the result of attempting to compile and run the following code? public class Test {
Static int x;
Public static void main(String[] args){
System.out.println("Value is " + x);
}
}

A) The output "Value is 0" is printed.
B) An "illegal array declaration syntax" compiler error occurs.
C) A "possible reference before assignment" compiler error occurs.
D) A runtime error occurs, because x is not initialized.
Question
Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() {
XMethod();
}

A) a static method
B) an instance method
C) a static method or an instance method
Question
What is the printout for the following code? class Test {
Public static void main(String[] args) {
Int[] x = new int[3];
System.out.println("x[0] is "+x[0]);
}
}

A) The program has a syntax error because the size of the array wasn't specified when declaring the array.
B) The program has a runtime error because the array elements are not initialized.
C) The program runs fine and displays x[0] is 0.
D) None of the above.
Question
Text I/O
Write a program that will count the number of characters (excluding control characters '\r' and '\n'), words, and lines, in a file. Words are separated by spaces, tabs, carriage return, or line-feed characters. The file name should be passed as a command-line argument, as shown in the following sample run. Text I/O Write a program that will count the number of characters (excluding control characters '\r' and '\n'), words, and lines, in a file. Words are separated by spaces, tabs, carriage return, or line-feed characters. The file name should be passed as a command-line argument, as shown in the following sample run.  <div style=padding-top: 35px>
Question
An attribute that is shared by all objects of the class is coded using ________.

A) an instance variable
B) a static variable
C) an instance method
D) a static method
Question
If a class named Student has a constructor Student(String name) defined explicitly, the following constructor is implicitly provided.

A) public Student()
B) protected Student()
C) private Student()
D) Student()
E) None
Question
Analyze the following code: public class Test {
Private int t;
Public static void main(String[] args) {
Test test = new Test();
System.out.println(test.t);
}
}

A) The variable t is not initialized and therefore causes errors.
B) The variable t is private and therefore cannot be accessed in the main method.
C) Since t is an instance variable, it cannot appear in the static main method.
D) The program compiles and runs fine.
Question
Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class. public static void main(String[] args) {
XMethod();
}

A) a static method
B) an instance method
C) a static or an instance method
Question
Write a program that converts US dollars into Canadian dollars, as shown in the following figure. The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One dollar is 1.5 Canadian dollars. Write a program that converts US dollars into Canadian dollars, as shown in the following figure. The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One dollar is 1.5 Canadian dollars.    <div style=padding-top: 35px> Write a program that converts US dollars into Canadian dollars, as shown in the following figure. The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One dollar is 1.5 Canadian dollars.    <div style=padding-top: 35px>
Question
Design and implement classes.
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person's name.
Draw the UML diagram for the classes. Write the code for the Student class only.
Question
What modifier should you use on a variable so that it can only be referenced inside its defining class.

A) public
B) private
C) protected
D) Use the default modifier.
Question
Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

A) int count = args.length;
B) int count = args.length - 1;
C) int count = 0; while (args[count] != null) count ++;
D) int count=0; while (!(args[count].equals(""))) count ++;
Question
Analyze the following program. class Test {
Public static void main(String[] args) {
Try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
Int i = 0;
Int y = 2 / i;
System.out.println("Welcome to Java");
}
Catch (Exception ex) {
System.out.println(ex);
}
}
}

A) An exception is raised due to Integer.parseInt(s);
B) An exception is raised due to 2 / i;
C) The program has a compilation error.
D) The program compiles and runs without exceptions.
Question
To append data to an existing file, use _____________ to construct a FileOutputStream for file out.dat.

A) new FileOutputStream("out.dat")
B) new FileOutputStream("out.dat", false)
C) new FileOutputStream("out.dat", true)
D) new FileOutputStream(true, "out.dat")
Question
What is displayed on the console when running the following program? class Test {
Public static void main(String[] args) {
Try {
System.out.println("Welcome to Java");
Int i = 0;
Int y = 2/i;
System.out.println("Welcome to Java");
}
Catch (RuntimeException ex) {
System.out.println("Welcome to Java");
}
Finally {
System.out.println("End of the block");
}
}
}

A) The program displays Welcome to Java three times followed by End of the block.
B) The program displays Welcome to Java two times followed by End of the block.
C) The program displays Welcome to Java three times.
D) The program displays Welcome to Java two times.
Question
After the following program is finished, how many bytes are written to the file t.dat? import java.io.*;
Public class Test {
Public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
New FileOutputStream("t.dat"));
Output.writeShort(1234);
Output.writeShort(5678);
Output.close();
}
}

A) 2 bytes.
B) 4 bytes.
C) 8 bytes.
D) 16 bytes.
Question
What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B() {
System.out.println(
"The default constructor of B is invoked");
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
a. none
b. "The default constructor of B is invoked"
c. "The default constructor of A is invoked" followed by "The default constructor of B is invoked"
d. "The default constructor of A is invoked"
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/25
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 10: Thinking in Objects
1
When you implement a method that is defined in a superclass, you __________ the original method.

A) overload
B) override
C) copy
D) call
B
2
Design and create GUI applications
Write a Java applet to add two numbers from text fields, and displays the result in a non-editable text field. Enable your applet to run standalone with a main method. A sample run of the applet is shown in the following figure. Design and create GUI applications Write a Java applet to add two numbers from text fields, and displays the result in a non-editable text field. Enable your applet to run standalone with a main method. A sample run of the applet is shown in the following figure.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
// Text fields for Number 1, Number 2, and Result
private JTextField jtfNum1, jtfNum2, jtfResult;
// Buttons "Add"
private JButton jbtAdd;
// Main method
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Test");
// Create an instance of the applet
Test applet = new Test();
// Add the applet instance to the frame
frame.add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
// Default Constructor
public Test() {
// Panel p1 to hold text fields and labels
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(new JLabel("Number 1"));
p1.add(jtfNum1 = new JTextField(3));
p1.add(new JLabel("Number 2"));
p1.add(jtfNum2 = new JTextField(3));
p1.add(new JLabel("Result"));
p1.add(jtfResult = new JTextField(8));
jtfResult.setEditable(false);
jtfResult.setHorizontalAlignment(SwingConstants.RIGHT);
// Panel p2 to hold buttons
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jbtAdd = new JButton("Add"));
// Set mnemonic keys
jbtAdd.setMnemonic('A');
// Add panels to the frame
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
// Register listeners
jbtAdd.addActionListener(new Listener());
}
class Listener implements ActionListener {
// Handle ActionEvent from buttons
public void actionPerformed(ActionEvent e) {
// Obtain Number 1 and Number 2
int num1 = Integer.parseInt(jtfNum1.getText().trim());
int num2 = Integer.parseInt(jtfNum2.getText().trim());
int result = num1 + num2;
// Set result in jtfResult
jtfResult.setText(String.valueOf(result));
}
}
}
3
Show the output of running the class Test in the following code lines: interface A {
Void print();
}
Class C {}
Class B extends C implements A {
Public void print() { }
}
Class Test {
Public static void main(String[] args) {
B b = new B();
If (b instanceof A)
System.out.println("b is an instance of A");
If (b instanceof C)
System.out.println("b is an instance of C");
}
}

A) Nothing.
B) b is an instance of A.
C) b is an instance of C.
D) b is an instance of A followed by b is an instance of C.
D
4
_________ describes the state of an object.

A) data fields
B) methods
C) constructors
D) none of the above
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
5
Design and use interfaces
Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal size. The area can be computed using the following formula: Design and use interfaces Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal size. The area can be computed using the following formula:   Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable. Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
6
Suppose s is a string with the value "java". What will be assigned to x if you execute the following code? char x = s.charAt(4);

A) 'a'
B) 'v'
C) Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.
D) None of the above.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
7
How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2

A) args[0]
B) args[1]
C) args[2]
D) args[3]
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
8
If a class named Student has no constructors defined explicitly, the following constructor is implicitly provided.

A) public Student()
B) protected Student()
C) private Student()
D) Student()
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
9
What would be the result of attempting to compile and run the following code? public class Test {
Static int x;
Public static void main(String[] args){
System.out.println("Value is " + x);
}
}

A) The output "Value is 0" is printed.
B) An "illegal array declaration syntax" compiler error occurs.
C) A "possible reference before assignment" compiler error occurs.
D) A runtime error occurs, because x is not initialized.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
10
Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() {
XMethod();
}

A) a static method
B) an instance method
C) a static method or an instance method
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
11
What is the printout for the following code? class Test {
Public static void main(String[] args) {
Int[] x = new int[3];
System.out.println("x[0] is "+x[0]);
}
}

A) The program has a syntax error because the size of the array wasn't specified when declaring the array.
B) The program has a runtime error because the array elements are not initialized.
C) The program runs fine and displays x[0] is 0.
D) None of the above.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
12
Text I/O
Write a program that will count the number of characters (excluding control characters '\r' and '\n'), words, and lines, in a file. Words are separated by spaces, tabs, carriage return, or line-feed characters. The file name should be passed as a command-line argument, as shown in the following sample run. Text I/O Write a program that will count the number of characters (excluding control characters '\r' and '\n'), words, and lines, in a file. Words are separated by spaces, tabs, carriage return, or line-feed characters. The file name should be passed as a command-line argument, as shown in the following sample run.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
13
An attribute that is shared by all objects of the class is coded using ________.

A) an instance variable
B) a static variable
C) an instance method
D) a static method
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
14
If a class named Student has a constructor Student(String name) defined explicitly, the following constructor is implicitly provided.

A) public Student()
B) protected Student()
C) private Student()
D) Student()
E) None
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
15
Analyze the following code: public class Test {
Private int t;
Public static void main(String[] args) {
Test test = new Test();
System.out.println(test.t);
}
}

A) The variable t is not initialized and therefore causes errors.
B) The variable t is private and therefore cannot be accessed in the main method.
C) Since t is an instance variable, it cannot appear in the static main method.
D) The program compiles and runs fine.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
16
Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class. public static void main(String[] args) {
XMethod();
}

A) a static method
B) an instance method
C) a static or an instance method
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
17
Write a program that converts US dollars into Canadian dollars, as shown in the following figure. The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One dollar is 1.5 Canadian dollars. Write a program that converts US dollars into Canadian dollars, as shown in the following figure. The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One dollar is 1.5 Canadian dollars.    Write a program that converts US dollars into Canadian dollars, as shown in the following figure. The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars when clicking the Convert button. One dollar is 1.5 Canadian dollars.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
18
Design and implement classes.
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person's name.
Draw the UML diagram for the classes. Write the code for the Student class only.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
19
What modifier should you use on a variable so that it can only be referenced inside its defining class.

A) public
B) private
C) protected
D) Use the default modifier.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
20
Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

A) int count = args.length;
B) int count = args.length - 1;
C) int count = 0; while (args[count] != null) count ++;
D) int count=0; while (!(args[count].equals(""))) count ++;
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
21
Analyze the following program. class Test {
Public static void main(String[] args) {
Try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
Int i = 0;
Int y = 2 / i;
System.out.println("Welcome to Java");
}
Catch (Exception ex) {
System.out.println(ex);
}
}
}

A) An exception is raised due to Integer.parseInt(s);
B) An exception is raised due to 2 / i;
C) The program has a compilation error.
D) The program compiles and runs without exceptions.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
22
To append data to an existing file, use _____________ to construct a FileOutputStream for file out.dat.

A) new FileOutputStream("out.dat")
B) new FileOutputStream("out.dat", false)
C) new FileOutputStream("out.dat", true)
D) new FileOutputStream(true, "out.dat")
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
23
What is displayed on the console when running the following program? class Test {
Public static void main(String[] args) {
Try {
System.out.println("Welcome to Java");
Int i = 0;
Int y = 2/i;
System.out.println("Welcome to Java");
}
Catch (RuntimeException ex) {
System.out.println("Welcome to Java");
}
Finally {
System.out.println("End of the block");
}
}
}

A) The program displays Welcome to Java three times followed by End of the block.
B) The program displays Welcome to Java two times followed by End of the block.
C) The program displays Welcome to Java three times.
D) The program displays Welcome to Java two times.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
24
After the following program is finished, how many bytes are written to the file t.dat? import java.io.*;
Public class Test {
Public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
New FileOutputStream("t.dat"));
Output.writeShort(1234);
Output.writeShort(5678);
Output.close();
}
}

A) 2 bytes.
B) 4 bytes.
C) 8 bytes.
D) 16 bytes.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
25
What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B() {
System.out.println(
"The default constructor of B is invoked");
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
a. none
b. "The default constructor of B is invoked"
c. "The default constructor of A is invoked" followed by "The default constructor of B is invoked"
d. "The default constructor of A is invoked"
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 25 flashcards in this deck.