- What is the difference between a class and an object?
- Explain the concept of inheritance with an explain of how
this object-oriented programming feature is used in Java.
- You have been hired to develop software for the local
library. Your software needs to keep track of the following
types of materials: Video tapes, audio cassette tapes, adult and
children's fiction and non-fiction books, DVD videos,
dictionaries, encyclopedias, music CDs, newspapers, and
magazines. (1) Draw an inheritance hierarchy of classes that
you would use to represent these types of media. You may add new
classes to the hierarchy as necessary. (2) In which class
might you declare a field called "numOfPages"? (3) In which
class might you declare a field called "runningTime"?
- Find four syntax errors in this code and explain why they
are errors:
class A {
public void foo() { System.out.println("foo"); }
}
class B extends A {
public void bar() { System.out.println("bar"); }
}
public class ExamReview {
public static void main(String[] ) {
A[] a = new A[];
B b = new B();
a[0] = b;
b.foo();
b.bar();
a[0].foo();
a[0].bar();
try { // try dividing by 0
int y = 5;
int x = 0;
System.out.println(y/x);
} catch (ArithmeticException e) {
System.out.println("can't divide " + y + " by 0");
e.printStackTrace();
}
}
}
- Do the following loops execute in exactly the same manner and
produce the same output, assuming
line is a
String variable and in is a BufferedReader
object? Why or why not?
// LOOP VERSION 1
line = in.readLine();
while (line != null) {
System.out.println(line.length());
line = in.readLine();
}
// LOOP VERSION 2
while ( (line = in.readLine()) != null)
System.out.println(line.length());
- (a) What will the output of the following method be when it
is executed? (b) What is a more descriptive/clear name for the
"isp" variable? (c) What would be a better name for this
method?
public static void someMethod() {
final int MAX = 10;
int[] list = new int[MAX];
int count = 0;
list[count++] = 2;
for (int cur = 3; count < MAX; cur++) {
boolean isp = true;
for (int i = 0; i < count; i++) {
if (cur % list[i] == 0)
isp = false;
}
if (isp) list[count++] = cur;
}
for (int i = 0; i < MAX; i++)
System.out.print(list[i] + " ");
}