pre midterm Flashcards

1
Q

Can you change the return type when overriding a method?

A

Not for primitive types.

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

What is a covariant return type?

A

When the return type of an overriding method is changed to a descendent of the original method’s return type.

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

In Java, a function with code that might throw an exception, and that is not caught in said function, must declare it by using the throws.

A

True

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

If a parent class has a method int func(), a child class can reduce its access modifier (make it more restrictive).

A

False, the child can only make it less restrictive.

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

A child class automatically overrides every method of its parent class so long as they have the same signature.

A

False, not “every” method can be overridden (private, static, final).

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

The super() constructor can always be called in a child constructor as long as it is called first.

A

False, the superclass may not have a default constructor.

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

In Java, a class can both inherit from multiple classes and implement multiple interfaces.

A

False, a class can only inherit from 1 other class.

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

Is this line of code correct? : public class PartTimeEmployee implements Serializable extends Employee { … }

A

False, “extend” must come first.

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

Java allows you to declare a variable of an abstract class.

A

True, you cannot create instances of it but you can declare pointers of it.

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

All types of exceptions must follow the Catch or Declare rule.

A

False, this isn’t true for unchecked exceptions.

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

Abstract classes can have contructors.

A

True.

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

When using PrintWriter, there is no way to append to a file - it is always overriten.

A

False

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

A grand-child class can easily call its grandparent class using super().super()

A

false

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

Order of catch blocks are wirtten is unimportant

A

false - more specific first

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

Super constructor is always called, either explicitily or implicitely.

A

True.

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

A derived class with an overridden method can add exceptions to the list of thrown exceptions.

A

You can only add unchecked exceptions or checked exceptions that are children of the original method’s thrown exceptions. Generally, false.

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

The finally block is executed after an exception is successfully caught.

A

True, but not only after an exception is caught, and not if a catch containing system.exit(0); executes.

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

In java it is possible to throw an exception, catch it, then re-throw the same exception if desired.

A

True, but using another try-catch within the catch block that “re-throws” the exception.

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

In order for polymorphism to work, it is sometimes crucial to create an abstract base class (parent) that has at least one private method.

A

False - nonsense.

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

If a base class has a method such as: public int fun1(), an inherited class cannot reduce accessibility to this method by declaring it as protected.

A

True

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

Assume class B is inherited from class A. The following statement, where a1 and b1 are references to Objects A and B, would result in compilation error:
a1 = (A)b1;

A

False, we’re allowed to up-cast. Even if we were down-casting, we wouldn’t get a compilation error because we are forcing the cast.

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

A constructer of an inherited class has the right to call the this() constructor and the super() constructer, but super() must be called first.

A

False : this() and super() cannot be used in the same constructor under any conditions.

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

Even if a class A is created as an abstract class, the following code will still be valid :
A a1;

A

True

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

What’s the order of access modifiers from most restrictive to least?

A

Private, Default (package), Protected, Public

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

What does the equals() method do when it is not defined by the object calling it?

A

It compares addresses.

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

Which method from the BufferedReader may return -1?

A

read()

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

How do you detect the end of a binary file?

A

When an exception is thrown.

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

When does overriding occur in place of overloading?

A

When the new method has the same number and types of parameters as in the base class.

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

When a subclass overloads a method from the base class, does the subclass still inherit the method of the same name from the baseclass?

A

Yes.

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

A final method cannot be overridden.

A

True.

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

A final class can be used to derive other classes.

A

False.

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

If super is not used, a call to the default
constructor of the base class is always issued.

A

False, a (compilation) error occurs in cases where the base class has no default constructor.

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

A derived type can be use in place of an ancestor type.

A

True.

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

What three things can we change about a method definition to overload a method?

A

Change number of parameters, change type of parameters or change order of parameters to overload.

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

The super constructor must be the first action of a constructor.

A

True.

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

An instance variable can be used as an argument to super.

A

False.

37
Q

An abstract method can be final.

A

False (complier error).

38
Q

An abstract method can be static.

A

False.

39
Q

An abstract method can be private.

A

False.

40
Q

A private attribute is not accessible by a child class.

A

It is not accessible by name, but one can use setters/getters.

41
Q

Does “extends” or “implements” come first in a class definition?

A

extends.

42
Q

What does instanceof do that getClass() does not?

A

Unlike getClass(), instanceof also returns true when the first operand is a descendent of the second operand.

43
Q

What are OOP’s three main mechanisms?

A

Encapsulation, Inheritance, Polymorphism.

44
Q

Polymorphism is the ability to…

A

associate many meanings to a single method name.

45
Q

Polymorphism is made possible by what type of binding?

A

Late / dynamic binding.

46
Q

Static methods can be overridden.

A

True (compiler allows it), but this may not work as intended because static methods bind statically. So no.

47
Q

What is upcasting?

A

When an object of a derived class is assigned to a variable of a base class. Upcasting is allowed.

48
Q

What is downcasting?

A

When an object of a base class is assigned to a variable of a derived class. Downcasting is mostly illegal (you can force it).

49
Q

String S1 = “abc”; String S2 = “abc”;
What does S1 == S2 return?

A

True, since the Java compiler is smart enough to know that their values are the same, and so it saves space to put them in the same location in memory.

50
Q

Abstract classes must have at least 1 abstract method.

A

False.

51
Q

A class with an abstract method must be abstract.

A

True.

52
Q

Can you call super in a class derived from an abstract class?

A

Yes.

53
Q

How do you define a checked exception?

A

By extending “Exception”

54
Q

How do you define an unchecked exception?

A

extend RuntimeException.

55
Q

Can you have zero catch blocks?

A

Yes if you have a finally block (compile wise).

56
Q

Will try block with zero catch blocks crash?

A

Yes if an exception is thrown.

57
Q

An incorrect order of catch blocks is a logic error (most specific isn’t placed first).

A

False, it is a compiler error.

58
Q

Subclasses inherit private methods.

A

They do, but they will never be invoked directly.

59
Q

Subclasses can invoke private inherited methods.

A

Only by calling a public method of the parent class that then calls the private method in question.

60
Q

Subclasses can use private inherited methods within their own method definition.

A

False.

61
Q

Instance variables should be marked with what access modifier?

A

Generally, private.

62
Q

Package access attributes and methods can be accessed by name by other classes in the same package.

A

True.

63
Q

Package access attributes/methods can be accessed by name by descendent classes in other packages.

A

False.

64
Q

Protected access attributes can be accessed by name by unrelated classes in the same package.

A

True.

65
Q

Casting a reference changes the object held in memory.

A

False, it only changes what the compiler thinks of the object.

66
Q

instanceof looks at the variable type, not the type in memory.

A

False.

67
Q

public function(Int… a) { return a; }
is valid code.

A

(Int… a) in the parameters of a method will allow the method user to input any number of arguments, and these arguments will be stored as an array called a.

68
Q

Java’s File object can point to directories as well as files.

A

True.

69
Q

The try block scope connects with the catch block scope.

A

False, the try and catch block scopes are separate.

70
Q

What exception is thrown when the Scanner attempts to read at the end of a file? Is it a checked exception?

A

“NoSuchElementException” is thrown. This is exception is unchecked.

71
Q

Can you use variable of father class A to invoke method unique to child class B if the variable holds an instance of B?

A

No, compiler won’t allow it.

72
Q

the getClass() method returns the declared class, rather than the actual class in memory.

A

False, getClass() returns the actual class in memory.

73
Q

How can you check whether a File object’s file exists?

A

using .exists()

74
Q

How can you check whether a File object’s file is readable?

A

using .canRead()

75
Q

In what way are serializable classes restricted if they wish to have instance variables of a class type?

A

The instance variable classes must also be serializable.

76
Q

What classes can you use to write to a file? (ASCII)

A

PrintWriter and FileOutputStream

77
Q

What’s more restrictive, default access or protected access?

A

Default is more restrictive.

78
Q

What methods are used to write to a file using PrintWriter?

A

print, println and printf.

79
Q

What classes can you use to read from a file? (ASCII)

A

Scanner with FileInputStream
or
BufferedReader with FileReader

80
Q

Scanner’s useDelimiter() method is a void method.

A

False, it returns the calling object.

81
Q

What are the 3 common exceptions thrown by a scanner’s methods.

A

NoSuchElementException (no more tokens), InputMismatchExecption (next token doesn’t match), and IllegalStateException if scanner stream is closed.

82
Q

What are the buffered reader’s 3 methods (besides constructors and close)? What are their return types?

A

read() returns int, readLine returns a string, and skip() returns a long.

83
Q

List the methods in the class File (14).

A

exists(), canRead(), setReadOnly(), canWrite(), delete(), createNewFile(), getName(), getPath(), renameTo(File newname), isFile(), isDirectory(), mkdir(), mkdirs(), length().

84
Q

What can you use to write to a binary file?

A

ObjectOutputStream and FileOutputStream.

85
Q

List the methods in the class ObjectOutputStream.

A

writeInt() (or any primitive type), writeUTF(str), writeObject(obj), close() and flush().

86
Q

What can you use to read from a binary file?

A

ObjectInputStream and FileInputStream.

87
Q

What are the methods of the class ObjectInputStream?

A

readInt() (or any primitive type), readUTF(), readObject(), skipBytes(int) and close().

88
Q

How do you check for end of file using ObjectInputStream?

A

Waiting for EOFException to be thrown.

89
Q

Can you change the contents of a final array.

A

Yes