Test 2 Flashcards
An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
Error
An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.
RuntimeException
If an exception occurs in a try-catch block, the code in the finally clause is ________.
executed
The FileWriter, FileReader, FileInputStream, FileOutputStream, and RandomAccessFile classes can be used to process external files. (T/F)
True
Closing an OutputStream ensures that the data in the buffer are sent to the destination. (T/F)
True
Which of the following is a correct interface?
Question options:
A) interface A { void print();}
B) abstract interface A { print(); }
C) interface A { void print() { }; }
D) abstract interface A { abstract void print() { };}
A) interface A { void print();}
Which of the following class definitions defines a legal abstract class?
A) abstract class A { abstract void unfinished(); }
B) class A { abstract void unfinished() { } }
C) public class abstract A { abstract void unfinished(); }
D) class A { abstract void unfinished(); }
A) abstract class A { abstract void unfinished(); }
Polymorphism means ________.
that a variable of supertype can refer to a subtype object
Show the output of running the class Test in the following code lines:
interface A { } class C { } class B extends D implements A { } public 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"); } } class D extends C { }
b is an instance of A followed by b is an instance of C.
Which class do you use to write data into a text file?
PrintWriter
Which method can be used to read a whole line from the file?
nextLine
Assume Calendar calendar = new GregorianCalendar(). ________ returns the week of the year.
calendar.get(Calendar.WEEK_OF_YEAR)
Given the following code:
class C1 {} class C2 extends C1 { } class C3 extends C2 { } class C4 extends C1 {}
C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4();
Which of the following expressions evaluates to false? A) c2 instanceof C1 B) c1 instanceof C1 C) c4 instanceof C2 D) c3 instance of C1
c4 instanceof C2
It is a compilation error if two methods differ only in return type in the same class. (T/F)
True
To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. (T/F)
True
Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. (T/F)
True
A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. (T/F)
True
A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. (T/F)
True
What is the output of the following code?
public class Test { public static void main(String[] args) { String s1 = new String("Java"); String s2 = new String("Java"); System.out.print((s1 == s2) + " " + (s1.equals(s2))); } }
false true
Every object is an instance of the Object class. (T/F)
True
Inheritance means ________.
that a class can extend another class
The UML uses ________ before a member name to indicate that the member is protected (protected modifier).
hashtag symbol
Which of the following class definitions defines a legal abstract class?
A) abstract class A { abstract void unfinished(); }
B) public class abstract A { abstract void unfinished(); }
C) class A { abstract void unfinished() { } }
D) class A { abstract void unfinished(); }
A) abstract class A { abstract void unfinished(); }
What is the best suitable relationship between Employee and Faculty?
Inheritance
A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this?
The variable should be marked protected.
You can always successfully cast a subclass to a superclass. (T/F)
True
What is the output of the following code?
public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { private String getInfo() { return "Student"; } } class Person { private String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } }
Person Person
Which of the following is a correct interface?
A) abstract interface A { print(); }
B) abstract interface A { abstract void print() { };}
C) interface A { void print();}
D) interface A { void print() { }; }
C) interface A { void print();}
A final class can be extended. (T/F)
False
Which of the following methods override the equals method in the Object class?
A) public static boolean equals(Object o)
B) public boolean equals(SomeType o)
C) public boolean equals(Object o)
D) public void equals(Object o)
C) public boolean equals(Object o)
Analyze the following code. What is displayed in each program?
// Program 1: public class Test { public static void main(String[] args) { Object circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius;
public boolean equals(Circle circle) {
return this.radius == circle.radius;
}
}
// Program 2: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius;
public boolean equals(Object circle) { return this.radius == ((Circle)circle).radius; } }
Program 1 displays false and Program 2 displays true
Which of the following declares an abstract method in an abstract Java class?
A) public abstract method();
B) public abstract void method() {}
C) public void method() {}
D) public abstract void method();
E) public void abstract Method();
D) public abstract void method();
A instance of a subclass is also an instance of its superclass. (T/F)
True
Encapsulation means ________.
that data fields should be declared private
All of the following can be done using the File class: (T/F)
- check if a file can be read
- delete a file
- create a new file
- check if a file exist
True
______ are not human readable, but more efficient.
Binary files
_____ are human readable, but less efficient.
Text files
______ does not specify any top-level folder, so the path is relative to the current directory.
relative path
______ complete pathname to a file starting at the root directory.
absolute path
_____ used to write to a file.
PrintWriter
____ used to read a file.
Scanner
IOException is an unchecked exception. (T/F)
False
Files must be closed before terminating the program to ensure that data is not lost. (T/F)
True
When doing file I/O, we have to handle IOException. (T/F)
True
_____ store data as characters.
Text files
_____ store data as numbers.
Binary files
Which of the following exception types must always be caught unless they are contained in methods that throw them in the method header?
A) checked exceptions
B) RuntimeException
C) unchecked exceptions
D)IndexOutOfBoundsException
A) checked exceptions
The Exception class and the Error class are subclasses of the ____________ class.
Throwable
Classes that inherit from the Error class are for exceptions that are thrown when a _______ error occurs, and the application program should not try to handle them.
critical
A throw statement is used to begin exception propagation. (T/F)
True