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
Q

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?

A

The variable should be marked protected.

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

You can always successfully cast a subclass to a superclass. (T/F)

A

True

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

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

Person Person

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

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() { }; }

A

C) interface A { void print();}

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

A final class can be extended. (T/F)

A

False

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

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)

A

C) public boolean equals(Object o)

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

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

Program 1 displays false and Program 2 displays true

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

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();

A

D) public abstract void method();

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

A instance of a subclass is also an instance of its superclass. (T/F)

A

True

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

Encapsulation means ________.

A

that data fields should be declared private

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

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
A

True

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

______ are not human readable, but more efficient.

A

Binary files

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

_____ are human readable, but less efficient.

A

Text files

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

______ does not specify any top-level folder, so the path is relative to the current directory.

A

relative path

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

______ complete pathname to a file starting at the root directory.

A

absolute path

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

_____ used to write to a file.

A

PrintWriter

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

____ used to read a file.

A

Scanner

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

IOException is an unchecked exception. (T/F)

A

False

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

Files must be closed before terminating the program to ensure that data is not lost. (T/F)

A

True

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

When doing file I/O, we have to handle IOException. (T/F)

A

True

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

_____ store data as characters.

A

Text files

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

_____ store data as numbers.

A

Binary files

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

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

A) checked exceptions

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

The Exception class and the Error class are subclasses of the ____________ class.

A

Throwable

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

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.

A

critical

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

A throw statement is used to begin exception propagation. (T/F)

A

True

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

A(n)______ is an object that defines an erroneous situation from which the program usually cannot recover.

A

error

52
Q

Which of the following exceptions are unchecked?

A)NoSuchMethodException
B)ClassNotFoundException
C)RuntimeException
D)IllegalAccessException

A

C)RuntimeException

53
Q

All of the exceptions that you will handle are instances of classes that extend the ________ class.

A

Exception

54
Q

When you write a method that throws a checked exception, you must have a ________ clause in the method header.

A

Throws

55
Q

When using the throw statement, if you don’t pass a message to the exception object’s constructor, the exception will have a __________ message.

A

null

56
Q

A(n) ___________ can be used to find the exact line where an exception was thrown during program execution.

A

call-stack trace

57
Q

If an exception is not caught, a program will _______________

A

terminate abnormally

58
Q

In a catch statement, what does the following code do?

System.out.println(e.getMessage());

A

It prints the error message for an exception.

59
Q

Unchecked exceptions must be caught or propagated, or a program will not compile. (T/F)

A

False

60
Q

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)

A

True

61
Q

If a method does not handle a possible checked exception, what must the method have?

A

a throws clause in its header

62
Q

A(n) _______contains one or more statements that are executed and can potentially throw an exception.

A

try block

63
Q

Every line of a(n) __________________ is executed no matter what exceptions are thrown.

A

finally block

64
Q

A finally clause is always required in a try-catch block. (T/F)

A

False

65
Q

In Java there are two categories of exceptions which are

A

unchecked and checked.

66
Q

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.

A

catch

67
Q

A throw statement is used to begin exception propagation. (T/F)

A

True

68
Q

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.

A

D)The catch clause does all of these.

69
Q

A(n) __________ is an object that defines an erroneous situation from which the program usually cannot recover.

A

error

70
Q

What method is part of the Exception class and can be used to give information about a thrown exception?

A

printStackTrace

71
Q

InputStream and OutputStream are abstract classes. (T/F)

A

True

72
Q

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

A

True

73
Q

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”)

A

B)new Scanner(new File(“temp.txt”))

74
Q

__________ Classes are a representation (idea) of something of interest or generic concept and can be extended but not directly instantiated.

A

Abstract

75
Q

The UML uses _____ font to represent abstract class names and abstract methods.

A

italicized

76
Q

A Java ___________ is a collection of constants and abstract methods and methods have public visibility by default.

A

Interface

77
Q

In a UML diagram how is an interface represented?

A

&laquo_space;interface&raquo_space;

Name of interface

78
Q

What is a Cloneable Interface?

A
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
Q

Interfaces vs. Abstract Classes

A
  • 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
Q

Inheritance is to create an ______ relationship among classes.

A
"is a"
Ex: 
- A grasshopper “is a” insect. 
- A poodle “is a” dog.
- A car “is a” vehicle.
81
Q

Inheritance involves a superclass and a subclass therefore the relationship of these classes can be thought of as ______.

A

parent and child classes.

82
Q

Through inheritance which of the following marked members of the superclass cannot be accessed from the subclass:
A) private
B) public
C) protected

A

A) private

83
Q

Are superclass’s constructor inherited?

A

No - they are invoked explicitly or implicitly

84
Q
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
A

super

85
Q

Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as ______.

A

method overriding

86
Q

What is this an example of?

class B { 
public void p (double i);
}
class A extends B {
public void p (int i);
}
A

overloading

87
Q

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?

A

overriding

88
Q

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.

A

protected

89
Q
  • The ____ class cannot be extended.
  • The ____ variable is a constant.
  • The ____ method cannot be
    overridden by its subclasses.
A

final

90
Q

The _____ method compares the

contents of two objects.

A

equals()

91
Q

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.

A

== (double equal signs)

92
Q
  • 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
A

Polymorphism

93
Q

ArrayList is known as a _____ class.

A

generic
- You can specify a concrete type to replace E when creating an ArrayList.
Ex:

ArrayList cities = new ArrayList();

ArrayList cities = new ArrayList<>();

94
Q

An _____ occurs during the execution of a program that disrupts the normal flow of instructions

A

exception

95
Q

Why use exceptions?

A
  • 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
Q

An ______ is a section of code that gracefully responds to exceptions.

A

exception handler

97
Q

The process of intercepting and responding to exceptions is called _______.

A

exception handling

98
Q

The ________ deals with unhandled exceptions and prints an error message and crashes the program.

A

default exception handler

99
Q

RuntimeException, Error and their subclasses are known as _______ exceptions.

A

unchecked

100
Q

When an exception occurs, we say it was _____ or raised.

A

thrown

101
Q

When an exception is dealt with, we say it is _____ or caught

A

handled

102
Q

To handle an exception, you use a ____ statement.

A

try

103
Q

– one or more statements that are executed, and
– can potentially throw an exception

Is known as…?

A

a try block

104
Q

After the try block, a ______ clause appears.

A

catch

105
Q

The code that immediately follows the catch clause is known as a ______ (the curly braces are required).

A

catch block - (this code is executed if the try block throws an exception.)

106
Q

Each exception object has a method named _____ that can be used to retrieve the
default error message for the exception.

A

getMessage

107
Q

The try statement may have an optional _____ clause. If present, the _____ clause must appear after all of the catch clauses.

A

finally

108
Q

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 ______.

A

declaring exceptions

109
Q

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 ________.

A

throwing an exception

110
Q

The ______ is an internal list of all the methods that are currently executing.

A

call stack

111
Q

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.

A

stack trace

112
Q

______ : an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.)

A

Stream

it acts as a buffer between the data source and destination

113
Q

______ : a stream that provides input to a program

A

Input stream

System.in is an input stream

114
Q

______ : a stream that accepts output from a program

A

Output stream

System.out is an output stream

115
Q

_______ : each byte is read/written from/to disk as soon as possible
– “little”delay for each byte
– A disk operation per byte - higher overhead

A

Not buffered

116
Q

_____: 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

A

Buffered / buffer

117
Q

To read a text file we use _______.

A

readFromFile()

118
Q

To write to a text file we use _______.

A

writeToFile()

119
Q

To read from a binary file we use ______.

A

readFromFileBinary()

120
Q

To write to a binary file we use ______.

A

writeToFileBinary()

121
Q

A Java ______ is a collection of constants and abstract methods

A

interface

122
Q

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.

A

serialization

123
Q

The _____ class represents files as objects. The class is defined in the java.io package.

A

File

124
Q

______ : does not specify any top-level folder, so the path is relative to the current directory:

A

relative path

125
Q

_______ : The complete pathname to a file starting at the root directory /:

A

absolute path