Download FinalSemester2(Ch7-1..

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Name:_______________________
Covers chs7-18
CSCI 1302 Introduction to Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
Part I: Multiple Choice Questions: (1 pts each)
1.
a.
b.
c.
d.
_________ describes the state of an object.
attributes
methods
constructors
no of the above
2.
using
a.
b.
c.
d.
An attribute that is shared by all objects of the class is coded
________.
an instance variable
a static variable
an instance method
a static method
3.
If a class named Student has no constructors defined explicitly,
the following constructor is implicitly provided.
a.
b.
c.
d.
public Student()
protected Student()
private Student()
Student()
4.
If a class named Student has a constructor Student(String name)
defined explicitly, the following constructor is implicitly provided.
a.
b.
c.
d.
e.
public Student()
protected Student()
private Student()
Student()
None
5.
Suppose the xMethod() is invoked in the following constructor,
xMethod() is _________.
public MyClass() {
xMethod();
}
a.
b.
c.
a static method
an instance method
None of the above
6.
Suppose the xMethod() is invoked from a main method as follows,
xMethod() is _________.
public static void main(String[] args) {
xMethod();
}
1
a.
b.
c.
a static method
an instance method
None of the above
7.
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.
b.
c.
d.
The output "Value is 0" is printed.
An "illegal array declaration syntax" compiler error occurs.
A "possible reference before assignment" compiler error occurs.
A runtime error occurs, because x is not initialized.
8. 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.
9.
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.
10.
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]);
}
}
2
a.
b.
c.
d.
The program has a syntax error because the size of the array
wasn't specified when declaring the array.
The program has a runtime error because the array elements are
not initialized.
The program runs fine and displays x[0] is 0.
None of the above.
11.
How can you get the word "abc" in the main method from the
following call?
java Test "+" 3 "abc" 2
a.
b.
c.
d.
args[0]
args[1]
args[2]
args[3]
12.
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.
b.
c.
d.
int
int
int
int
count = args.length;
count = args.length - 1;
count = 0; while (args[count] != null) count ++;
count=0; while (!(args[count].equals(""))) count ++;
13.
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.
b.
c.
d.
Nothing.
b is an instance of A.
b is an instance of C.
b is an instance of A followed by b is an instance of
C.
3
14.
a.
b.
c.
d.
15.
When you implement a method that is defined in a superclass, you
__________ the original method.
overload
override
copy
call
a.
b.
c.
d.
What modifier should you use on a variable so that it can only be
referenced inside its defining class.
public
private
protected
Use the default modifier.
16.
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.
b.
c.
d.
none
"The
"The
"The
"The
default
default
default
default
constructor
constructor
constructor
constructor
of
of
of
of
B
A
B
A
is
is
is
is
{
invoked"
invoked"
invoked"
invoked"
Part II: Questions: (1 pt each)
1.
Suppose I1 is an interface, what is wrong in the following code?
public interface I1 implements I2 {
}
2. Suppose A is an abstract class, what is wrong in the following code?
A[] list = new A[10];
list[0] = new A();
4
3. The following code has compilation errors, runtime error, or other
problems, or no problems?
public class A {
public static void main(String args[]) {
int[] a = new int[10];
int[] c = new int[20];
a = c;
}
}
4. What is wrong in the following code?
Component[] list = new JButton[10];
list[0] = new JTextField();
5. Create an instance of GridLayout with 2 rows, 3 columns, horizontal
and vertical gaps of 5 pixels.
6. Create a panel with BorderLayout. Add a button labeled “OK” to the
south side of the panel.
7. Suppose g is an instance of a graphic context on a panel p. Draw two
diagonal lines on this panel.
8. What is wrong in the following code?
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
JButton jbt = new JButton("OK");
jbt.addActionListener(this);
5
}
}
9. Create three radio buttons with text “Red”, “Yellow,” and “Green.”
Group these three buttons.
10.
Create a combo box with items “Red”, “Yellow,” and “Green.”
11.
Can a button fire a WindowEvent? Can a button fire a
ComponentEvent?
12.
Set the color of the component c’s background to red?
13.
Is JApplet a subclass of Applet? Is Applet a subclass of
Container? Is Container a subclass of Component?
14.
Suppose you want to get the value of a parameter named MESSAGE
from the HTML applet tag to the applet, write a statement to
retrieve this value.
15.
What is displayed on the console when the following program is
run?
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 HTML");
}
finally {
System.out.println("The finally clause is executed");
}
System.out.println("End of the block");
}
}
16.
If a method does not declare throwing a RuntimeException, can it
throw a RuntimeException, or a subclass of RuntimeException?
Part III: Complete the following program
6
1. (5 pts) Write a program that passes an unspecified number of
integers as one command-line argument and displays their total.
For example, if you run the program using the following argument,
java Sum
“1 2 3”
The output is
Sum is 6.
2. (10 pts) 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.
7
Key
Part I: Multiple Choice Questions: (1 pts each)
1. a
2. b
3. a
4. e
5. b
6. a
7. a
8. d
9. c
10. c
11. c
12. a
13. d
14. b
15. b
16. c
Part II: Questions: (1 pt each)
1. Suppose I1 is an interface, what is wrong in the following code?
public interface I1 implements I2 {
}
A: change implements to extends
17. Suppose A is an abstract class, what is wrong in the following code?
A[] list = new A[10];
list[0] = new A();
A: Since A is an abstract class, you cannot create an
instance from it directly using the new operator.
18. The following code has compilation errors, runtime error, or other problems, or no problems?
public class A {
public static void main(String args[]) {
int[] a = new int[10];
int[] c = new int[20];
a = c;
}
}
A: It is fine to assign c to a because both have the same
type.
8
19.
What is wrong in the following code?
Component[] list = new JButton[10];
list[0] = new JTextField();
A: list[0] is a JButton. You cannot assign a JTextField to
it. However, it would be fine if you assign new
JTextField[10] to list.
20.
Create an instance of GridLayout with 2 rows, 3 columns, horizontal and vertical gaps of 5 pixels.
A: new GridLayout(2, 3, 5, 5).
21.
Create a panel with BorderLayout. Add a button labeled “OK” to the south side of the panel.
A: new GridLayout(2, 3, 5, 5).
22.
Suppose g is an instance of a graphic context on a panel p. Draw two diagonal lines on this panel.
A: g.drawLine(0, 0, getWidth(), getHeight()); g.drawLine(
getWidth(), 0, getHeight(), 0);
23.
What is wrong in the following code?
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
JButton jbt = new JButton("OK");
jbt.addActionListener(this);
}
}
A: Test is not an instance of ActionListener. So,
9
jbt.addActionListener(this);
is wrong.
24.
Create three radio buttons with text “Red”, “Yellow,” and “Green.” Group these three buttons.
JRadioButton jrbRed = new JRadioButton(“Red”);
JRadioButton jrbYellow = new JRadioButton(“Yellow”);
JRadioButton jrbGreen = new JRadioButton(“Green”);
ButtonGroup btg = new ButtonGroup();
btg.add(jrbRed); btg.add(jrbYellow); btg.add(jrbGreen);
25.
Create a combo box with items “Red”, “Yellow,” and “Green.”
JComboBox jcb = new JComboBox();
jcb.add(“Red”); jcb.add(“Yellow”); jcb.add(“Green”);
26.
Can a button fire a WindowEvent? Can a button fire a ComponentEvent?
A: button cannot fire a WindowEvent. A button can fire a
ComponentEvent.
27. Set the color of the component c’s background to red?
A: c.setBackground(Color.red);
28. Is JApplet a subclass of Applet? Is Applet a subclass of Container? Is Container a subclass of
Component?
A: Yes to all.
29. Suppose you want to get the value of a parameter named MESSAGE from the HTML applet tag to the
applet, write a statement to retrieve this value.
A: getParameter(“MESSAGE”)
30. What is displayed on the console when the following program is run?
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 HTML");
}
10
finally {
System.out.println("The finally clause is executed");
}
System.out.println("End of the block");
}
}
A:
Welcome to Java
The finally clause is executed
End of the block
31. If a method does not declare throwing a RuntimeException, can it throw a RuntimeException, or a
subclass of RuntimeException?
A: Yes
11