Name

advertisement
Name:_______________________
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.
a.
b.
c.
d.
An attribute that is shared by all objects of the class is coded using ________.
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();
}
a.
b.
a static method
an instance method
1
c.
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.
b.
c.
d.
The variable t is not initialized and therefore causes errors.
The variable t is private and therefore cannot be accessed in the main method.
Since t is an instance variable, it cannot appear in the static main method.
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]);
}
}
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.
2
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 count = args.length;
int count = args.length - 1;
int count = 0; while (args[count] != null) count ++;
int 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.
14.
When you implement a method that is defined in a superclass, you __________ the original
method.
a. overload
b. override
c. copy
d. call
15.
a.
What modifier should you use on a variable so that it can only be referenced inside its defining
class.
public
3
b.
c.
d.
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 default constructor of B is invoked"
"The default constructor of A is invoked"
"The default constructor of B is invoked"
"The default constructor of A is 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();
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
}
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);
}
}
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.”
5
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
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.
6
2.
(10 pts) Write a Java applet to add two numbers from text fields, and displays the result in a noneditable 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
8
Download