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
A(n)______ is an object that defines an erroneous situation from which the program usually cannot recover.
error
Which of the following exceptions are unchecked?
A)NoSuchMethodException
B)ClassNotFoundException
C)RuntimeException
D)IllegalAccessException
C)RuntimeException
All of the exceptions that you will handle are instances of classes that extend the ________ class.
Exception
When you write a method that throws a checked exception, you must have a ________ clause in the method header.
Throws
When using the throw statement, if you don’t pass a message to the exception object’s constructor, the exception will have a __________ message.
null
A(n) ___________ can be used to find the exact line where an exception was thrown during program execution.
call-stack trace
If an exception is not caught, a program will _______________
terminate abnormally
In a catch statement, what does the following code do?
System.out.println(e.getMessage());
It prints the error message for an exception.
Unchecked exceptions must be caught or propagated, or a program will not compile. (T/F)
False
When accessing an element of an array, if the index is outside of the range of the indexes of the array, an exception is thrown. (T/F)
True
If a method does not handle a possible checked exception, what must the method have?
a throws clause in its header
A(n) _______contains one or more statements that are executed and can potentially throw an exception.
try block
Every line of a(n) __________________ is executed no matter what exceptions are thrown.
finally block
A finally clause is always required in a try-catch block. (T/F)
False
In Java there are two categories of exceptions which are
unchecked and checked.
When an exception is thrown by code in its try block, the JVM begins searching the try statement for a ______ clause that can handle it and passes control of the program to the first ______ clause that can handle the exception.
catch
A throw statement is used to begin exception propagation. (T/F)
True
The catch clause
A) follows the try clause.
B)starts with the word catch followed by a parameter list in parentheses containing an ExceptionType parameter variable.
C)contains code to gracefully handle the exception type listed in the parameter list.
D)The catch clause does all of these.
D)The catch clause does all of these.
A(n) __________ is an object that defines an erroneous situation from which the program usually cannot recover.
error
What method is part of the Exception class and can be used to give information about a thrown exception?
printStackTrace
InputStream and OutputStream are abstract classes. (T/F)
True
Closing an OutputStream ensures that the data in the buffer are sent to the destination. (T/F)
True
Which method can be used to create an input object for file temp.txt?
A) new Scanner(temp.txt)
B)new Scanner(new File(“temp.txt”))
C)new Scanner(File(“temp.txt”))
D)new Scanner(“temp.txt”)
B)new Scanner(new File(“temp.txt”))
__________ Classes are a representation (idea) of something of interest or generic concept and can be extended but not directly instantiated.
Abstract
The UML uses _____ font to represent abstract class names and abstract methods.
italicized
A Java ___________ is a collection of constants and abstract methods and methods have public visibility by default.
Interface
In a UML diagram how is an interface represented?
«_space;interface»_space;
Name of interface
What is a Cloneable Interface?
Marker Interface: An empty interface. A marker interface does not contain constants or methods. It is used to denote that a class possesses certain desirable properties. A class that implements the Cloneable interface is marked cloneable, and its objects can be cloned using the clone() method defined in the Object class.
Interfaces vs. Abstract Classes
- In an interface, the data must be constants; an abstract class can have all types of data.
- Each method in an interface has only a signature without implementation; an abstract class can have concrete methods.
Inheritance is to create an ______ relationship among classes.
"is a" Ex: - A grasshopper “is a” insect. - A poodle “is a” dog. - A car “is a” vehicle.
Inheritance involves a superclass and a subclass therefore the relationship of these classes can be thought of as ______.
parent and child classes.
Through inheritance which of the following marked members of the superclass cannot be accessed from the subclass:
A) private
B) public
C) protected
A) private
Are superclass’s constructor inherited?
No - they are invoked explicitly or implicitly
The keyword \_\_\_\_ refers to the superclass of the class in which \_\_\_\_\_ appears. This keyword can be used in two ways: • To call a superclass constructor • To call a superclass method
super
Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as ______.
method overriding
What is this an example of?
class B { public void p (double i); }
class A extends B { public void p (int i); }
overloading
What is this an example of?
class B { public void p (double i); }
class A extends B { public void p (double i); }
What is this an example of?
overriding
The _______ modifier can be applied on data and methods in a class. A ______ data or a ______ method in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in a different package.
protected
- The ____ class cannot be extended.
- The ____ variable is a constant.
- The ____ method cannot be
overridden by its subclasses.
final
The _____ method compares the
contents of two objects.
equals()
The ____ comparison operator is used for comparing two primitive data type values
The ____ comparison operator is for determining whether two objects have the same references.
== (double equal signs)
- It refers to something having many forms
- allows you to code to an interface that reduces coupling
- one of the important features of Object-Oriented Programming
Polymorphism
ArrayList is known as a _____ class.
generic
- You can specify a concrete type to replace E when creating an ArrayList.
Ex:
ArrayList cities = new ArrayList();
ArrayList cities = new ArrayList<>();
An _____ occurs during the execution of a program that disrupts the normal flow of instructions
exception
Why use exceptions?
- Compilation cannot find all errors
- To separate error handling code from regular code
- To separate error detection, reporting, and handling
- To group and differentiate error types
An ______ is a section of code that gracefully responds to exceptions.
exception handler
The process of intercepting and responding to exceptions is called _______.
exception handling
The ________ deals with unhandled exceptions and prints an error message and crashes the program.
default exception handler
RuntimeException, Error and their subclasses are known as _______ exceptions.
unchecked
When an exception occurs, we say it was _____ or raised.
thrown
When an exception is dealt with, we say it is _____ or caught
handled
To handle an exception, you use a ____ statement.
try
– one or more statements that are executed, and
– can potentially throw an exception
Is known as…?
a try block
After the try block, a ______ clause appears.
catch
The code that immediately follows the catch clause is known as a ______ (the curly braces are required).
catch block - (this code is executed if the try block throws an exception.)
Each exception object has a method named _____ that can be used to retrieve the
default error message for the exception.
getMessage
The try statement may have an optional _____ clause. If present, the _____ clause must appear after all of the catch clauses.
finally
If a method chooses not to catch an exception, then the method must declare that it can throw it.
Every method must state the types of checked exceptions it might throw.
This is known as ______.
declaring exceptions
If we evaluate a state of the program as abnormal, we create an instance of an appropriate exception type and throw it. This is known as ________.
throwing an exception
The ______ is an internal list of all the methods that are currently executing.
call stack
A ______ is a list of all the methods in the call stack.
• It indicates:
– the method that was executing when an exception occurred and
– all of the methods that were called in order to execute that method.
stack trace
______ : an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.)
Stream
it acts as a buffer between the data source and destination
______ : a stream that provides input to a program
Input stream
System.in is an input stream
______ : a stream that accepts output from a program
Output stream
System.out is an output stream
_______ : each byte is read/written from/to disk as soon as possible
– “little”delay for each byte
– A disk operation per byte - higher overhead
Not buffered
_____: reading/writing in “chunks” – Some delay for some bytes
• Assume16-byte _____
• Reading: access the first 4 bytes, need to wait for all 16 bytes are read from disk to memory
• Writing: save the first 4 bytes, need to wait for all 16 bytes before writing from memory to disk
- A disk operation per a _____ of bytes - lower overhead
Buffered / buffer
To read a text file we use _______.
readFromFile()
To write to a text file we use _______.
writeToFile()
To read from a binary file we use ______.
readFromFileBinary()
To write to a binary file we use ______.
writeToFileBinary()
A Java ______ is a collection of constants and abstract methods
interface
Object ________ is where an object can be represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.
serialization
The _____ class represents files as objects. The class is defined in the java.io package.
File
______ : does not specify any top-level folder, so the path is relative to the current directory:
relative path
_______ : The complete pathname to a file starting at the root directory /:
absolute path