Test 2 Flashcards

1
Q

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.

A

Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

A

RuntimeException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If an exception occurs in a try-catch block, the code in the finally clause is ________.

A

executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The FileWriter, FileReader, FileInputStream, FileOutputStream, and RandomAccessFile classes can be used to process external files. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Closing an OutputStream ensures that the data in the buffer are sent to the destination. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A) interface A { void print();}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A) abstract class A { abstract void unfinished(); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Polymorphism means ________.

A

that a variable of supertype can refer to a subtype object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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 { 
}
A

b is an instance of A followed by b is an instance of C.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which class do you use to write data into a text file?

A

PrintWriter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which method can be used to read a whole line from the file?

A

nextLine

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Assume Calendar calendar = new GregorianCalendar(). ________ returns the week of the year.

A

calendar.get(Calendar.WEEK_OF_YEAR)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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
A

c4 instanceof C2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

It is a compilation error if two methods differ only in return type in the same class. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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)));
}
}
A

false true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Every object is an instance of the Object class. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Inheritance means ________.

A

that a class can extend another class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

The UML uses ________ before a member name to indicate that the member is protected (protected modifier).

A

hashtag symbol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A) abstract class A { abstract void unfinished(); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the best suitable relationship between Employee and Faculty?

A

Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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.
26
You can always successfully cast a subclass to a superclass. (T/F)
True
27
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
28
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();}
29
A final class can be extended. (T/F)
False
30
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)
31
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
32
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();
33
A instance of a subclass is also an instance of its superclass. (T/F)
True
34
Encapsulation means ________.
that data fields should be declared private
35
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
36
______ are not human readable, but more efficient.
Binary files
37
_____ are human readable, but less efficient.
Text files
38
______ does not specify any top-level folder, so the path is relative to the current directory.
relative path
39
______ complete pathname to a file starting at the root directory.
absolute path
40
_____ used to write to a file.
PrintWriter
41
____ used to read a file.
Scanner
42
IOException is an unchecked exception. (T/F)
False
43
Files must be closed before terminating the program to ensure that data is not lost. (T/F)
True
44
When doing file I/O, we have to handle IOException. (T/F)
True
45
_____ store data as characters.
Text files
46
_____ store data as numbers.
Binary files
47
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
48
The Exception class and the Error class are subclasses of the ____________ class.
Throwable
49
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
50
A throw statement is used to begin exception propagation. (T/F)
True
51
A(n)______ is an object that defines an erroneous situation from which the program usually cannot recover.
error
52
Which of the following exceptions are unchecked? A)NoSuchMethodException B)ClassNotFoundException C)RuntimeException D)IllegalAccessException
C)RuntimeException
53
All of the exceptions that you will handle are instances of classes that extend the ________ class.
Exception
54
When you write a method that throws a checked exception, you must have a ________ clause in the method header.
Throws
55
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
56
A(n) ___________ can be used to find the exact line where an exception was thrown during program execution.
call-stack trace
57
If an exception is not caught, a program will _______________
terminate abnormally
58
In a catch statement, what does the following code do? | System.out.println(e.getMessage());
It prints the error message for an exception.
59
Unchecked exceptions must be caught or propagated, or a program will not compile. (T/F)
False
60
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
61
If a method does not handle a possible checked exception, what must the method have?
a throws clause in its header
62
A(n) _______contains one or more statements that are executed and can potentially throw an exception.
try block
63
Every line of a(n) __________________ is executed no matter what exceptions are thrown.
finally block
64
A finally clause is always required in a try-catch block. (T/F)
False
65
In Java there are two categories of exceptions which are
unchecked and checked.
66
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
67
A throw statement is used to begin exception propagation. (T/F)
True
68
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.
69
A(n) __________ is an object that defines an erroneous situation from which the program usually cannot recover.
error
70
What method is part of the Exception class and can be used to give information about a thrown exception?
printStackTrace
71
InputStream and OutputStream are abstract classes. (T/F)
True
72
Closing an OutputStream ensures that the data in the buffer are sent to the destination. (T/F)
True
73
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"))
74
__________ Classes are a representation (idea) of something of interest or generic concept and can be extended but not directly instantiated.
Abstract
75
The UML uses _____ font to represent abstract class names and abstract methods.
italicized
76
A Java ___________ is a collection of constants and abstract methods and methods have public visibility by default.
Interface
77
In a UML diagram how is an interface represented?
<< interface >> | Name of interface
78
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. ```
79
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.
80
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. ```
81
Inheritance involves a superclass and a subclass therefore the relationship of these classes can be thought of as ______.
parent and child classes.
82
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
83
Are superclass's constructor inherited?
No - they are invoked explicitly or implicitly
84
``` 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
85
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
86
What is this an example of? ``` class B { public void p (double i); } ``` ``` class A extends B { public void p (int i); } ```
overloading
87
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
88
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
89
- The ____ class cannot be extended. - The ____ variable is a constant. - The ____ method cannot be overridden by its subclasses.
final
90
The _____ method compares the | contents of two objects.
equals()
91
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)
92
- 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
93
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<>();
94
An _____ occurs during the execution of a program that disrupts the normal flow of instructions
exception
95
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
96
An ______ is a section of code that gracefully responds to exceptions.
exception handler
97
The process of intercepting and responding to exceptions is called _______.
exception handling
98
The ________ deals with unhandled exceptions and prints an error message and crashes the program.
default exception handler
99
RuntimeException, Error and their subclasses are known as _______ exceptions.
unchecked
100
When an exception occurs, we say it was _____ or raised.
thrown
101
When an exception is dealt with, we say it is _____ or caught
handled
102
To handle an exception, you use a ____ statement.
try
103
– one or more statements that are executed, and – can potentially throw an exception Is known as...?
a try block
104
After the try block, a ______ clause appears.
catch
105
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.)
106
Each exception object has a method named _____ that can be used to retrieve the default error message for the exception.
getMessage
107
The try statement may have an optional _____ clause. If present, the _____ clause must appear after all of the catch clauses.
finally
108
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
109
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
110
The ______ is an internal list of all the methods that are currently executing.
call stack
111
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
112
______ : 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
113
______ : a stream that provides input to a program
Input stream | System.in is an input stream
114
______ : a stream that accepts output from a program
Output stream | System.out is an output stream
115
_______ : 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
116
_____: 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
117
To read a text file we use _______.
readFromFile()
118
To write to a text file we use _______.
writeToFile()
119
To read from a binary file we use ______.
readFromFileBinary()
120
To write to a binary file we use ______.
writeToFileBinary()
121
A Java ______ is a collection of constants and abstract methods
interface
122
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
123
The _____ class represents files as objects. The class is defined in the java.io package.
File
124
______ : does not specify any top-level folder, so the path is relative to the current directory:
relative path
125
_______ : The complete pathname to a file starting at the root directory /:
absolute path