Download PracticeExam(Ch8, 9 - SIUE Computer Science

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 Chapters 7-9
50 min (Practice Questions)
CSCI 1302 OO Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
(50 minutes)
Part I. (2 pts each)
1.
Add the static keyword in the place of ? if appropriate.
public class Test {
private int count;
public ? void main(String[] args) {
...
}
public ? int getCount() {
return count;
}
public ? int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}
}
In the following code, radius is private in the Circle class, and myCircle is an object of
the Circle class. Does the following highlighted code cause any problems? Explain why.
2.
public class Circle {
private double radius = 1.0;
/** Find the area of this circle */
double getArea() {
return radius * radius * Math.PI;
}
public static void main(String[] args) {
Circle myCircle = new Circle();
System.out.println("Radius is " + myCircle.radius);
}
}
3.
What is wrong in the following code?
public class Test {
public static void main(String[] args) {
java.util.Date[] dates = new java.util.Date[10];
System.out.println(dates[0]);
System.out.println(dates[0].toString());
}
}
4. Describe the role of the this keyword. What is wrong in the following code?
public class C {
int p;
public C() {
1
System.out.println("C’s no-arg constructor invoked");
this(0);
}
public C(int p) {
p = p;
}
public void setP(int p) {
p = p;
}
}
5. Show the output of the following program when invoked using
java Test I have a dream
public class Test {
public static void main(String[] args) {
System.out.println("Number of strings is " + args.length);
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
What will happen if you attempt to create a Scanner for a nonexistent file? What will
happen if you attempt to create a PrintWriter for an existing file?
6.
Part II:
a. (3 pts) Suppose that the Loan class is given as shown in the following
UML. Write a test program that creates a Loan object with loan amount
$40000, annual interest rate 5.5%, and number of years 15, and displays
the monthly payment and total payment.
2
Loan
-annualInterestRate: double
The annual interest rate of the loan (default: 2.5).
-numberOfYears: int
The number of years for the loan (default: 1)
-loanAmount: double
The loan amount (default: 1000)..
-loanDate: Date
The date this loan was created.
+Loan()
Constructs a default loan object.
+Loan(annualInterestRate: double,
numberOfYears: int,
loanAmount: double)
Constructs a loan with specified interest rate, years, and
loan amount.
+getAnnualInterestRate(): double
Returns the annual interest rate of this loan.
+getNumberOfYears(): int
Returns the number of the years of this loan.
+getLoanAmount(): double
Returns the amount of this loan.
+getLoanDate(): Date
Returns the date of the creation of this loan.
+setAnnualInterestRate(
Sets a new annual interest rate to this loan.
annualInterestRate: double): void
Sets a new number of years to this loan.
+setNumberOfYears(
numberOfYears: int): void
+setLoanAmount(
loanAmount: double): void
Sets a new amount to this loan.
+monthlyPayment(): double
Returns the monthly payment of this loan.
+totalPayment(): double
Returns the total payment of this loan.
b. (5 pts) Write a program that reads data from a text file. In the text
file, each line contains four values (firstname, mi, lastname, and
score). Names are strings and score is a double value. Display data to
the console.
(Geometry: n-sided regular polygon) An n-sided
regular polygon has n sides of the same length and all
angles have the same degree (i.e., the polygon is both
equilateral and equiangular). Design a class named
RegularPolygon that contains:
c. (15 pts)


A private
number of
A private
length of
int data field named n that defines the
sides in the polygon with default value 3.
double data field named side that stores the
the side with default value 1.
3








A private double data field named x that defines the
x-coordinate of the center of the polygon with default
value 0.
A private double data field named y that defines the
y-coordinate of the center of the polygon with default
value 0.
A no-arg constructor that creates a regular polygon
with default values.
A constructor that creates a regular polygon with the
specified number of sides and the length of side, and
centered at (0, 0).
A constructor that creates a regular polygon with the
specified number of sides, the length of side, and xand y-coordinates.
The accessor and mutator methods for all data fields.
The method getPerimter() that returns the perimeter of
the polygon.
The method getArea() that returns the area of the
polygon. The formula for computing the area of a
n  s2
regular polygon is Area 
.

4  tan( )
n
Draw the UML diagram for the class. Implement the class.
Write a test program that creates three
RegularPolygon objects, created using the no-arg
constructor, using RegularPolygon(6, 4), and
using RegularPolygon(10, 4, 5.6, 7.8). For each
object, display its perimeter and area.
4
Part III: Multiple Choice Questions: (1 pts each)
7 quizzes for Chapter 7
1 What code may be filled in the blank without causing syntax or runtime errors:
public class Test {
java.util.Date date;
public static void main(String[] args) {
Test test = new Test();
System.out.println(_________________);
}
}
A. test.date.toString()
B. date.toString()
C. test.date
D. date
2 When invoking a method with an object argument, ___________ is passed.
A. the object is copied, then the reference of the copied object
B. a copy of the object
C. the contents of the object
D. the reference of the object
3 Analyze the following code.
public class Test {
public static void main(String[] args) {
int n = 2;
xMethod(n);
System.out.println("n is " + n);
}
void xMethod(int n) {
n++;
}
}
A. The code has a syntax error because xMethod does not return a value.
B. The code prints n is 3.
C. The code has a syntax error because xMethod is not declared static.
D. The code prints n is 2.
E. The code prints n is 1.
4 How many JFrame objects can you create and how many can you display?
5
A. one
B. three
C. unlimited
D. two
5 Array variable are reference variables.
A. true
B. false
6 You can declare variables of the same name in a method if they are in non-nesting
blocks.
A. false
B. true
7 The order of methods in a class is immaterial.
A. false
B. true
8 quizzes for Chapter 8
8 Which of the following is the correct header of the main method?
A. public static void main(String x[])
B. static void main(String[] args)
C. public static void main(String[] args)
D. public static void main(String[] x)
E. public static void main(String args[])
9 "AbA".compareToIgnoreCase("abC") returns ___________.
A. 1
B. -1
C. 0
D. 2
E. -2
10 The following program displays __________.
public class Test {
public static void main(String[] args) {
String s = "Java";
StringBuffer buffer = new StringBuffer(s);
change(buffer);
System.out.println(buffer);
6
}
private static void change(StringBuffer buffer) {
buffer.append(" and HTML");
}
}
A. Java
B. and HTML
C. nothing is displayed
D. Java and HTML
11 Suppose Character x = new Character('a'), __________________ returns true.
A. x.equals('a')
B. x.equals("a")
C. x.equalsIgnoreCase('A')
D. x.equals(new Character('a'))
E. x.compareToIgnoreCase('A')
12 Which of the following is the correct statement to return a string from an array a of
characters?
A. String.toString(a)
B. new String(a)
C. toString(a)
D. convertToString(a)
13 "unhappy".substring(2) returns "happy".
A. false
B. true
14 "ab".compareTo("aB") is _________.
A. equal to 0
B. less than 0
C. less than or equal to 0
D. greater than 0
15 You cannot append a string to a string buffer if the resulting new string exceeds the
capacity.
A. true
B. false
7
16 Which of the following statements are true about an immutable object?
A. An object type property in an immutable object must also be immutable.
B. An immutable object contains no mutator methods.
C. All properties of an immutable object must be private.
D. All properties of an immutable object must be of primitive types.
E. The contents of an immutable object cannot be modified.
17 Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}
A. The program has a compilation error because you cannot assign radius to radius.
B. The program does not compile because Circle does not have a default constructor.
C. The program will compile, but you cannot create an object of Circle with a specified
radius. The object will always have radius 0.
D. The program has a compilation error because it does not have a main method.
18 Java uses _______ to reference the current object.
A. null
B. this
C. that
D. thisObject
19 A constructor can access ___________.
A. A public instance variable
B. A local variable defined in any method
C. A private instance variable
D. A static variable
Keys:
1. C
2. D
3. C
4. C
5. A
6. B
7. B
8. ACDE
9. E
8
10. D
11. AD
12. B
13. B
14. D
15. B
16. ABCE
17. C
18. B
19. AC
9