CS115 Flashcards

(63 cards)

1
Q

What is encapsulation in Java?

A

Hiding data by making attributes private and exposing them via public getter and setter methods.

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

What is inheritance in Java?

A

Allows a subclass to acquire the properties and methods of a superclass using the
‘extends’ keyword

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

What is polymorphism?

A

Polymorphism allows different classes to be treated as instances of the same superclass, with
method behavior determined at runtime.

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

What does the ‘super’ keyword do?

A

It refers to the superclass of the current object and is used to access superclass methods or
constructors.

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

What does the ‘this’ keyword refer to?

A

‘this’ refers to the current instance of the class.

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

What is method overloading?

A

Defining multiple methods in the same class with the same name but different parameter lists.

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

What is method overriding?

A

Redefining a superclass method in a subclass using the same method signature.

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

What is the purpose of the ‘static’ keyword?

A

It defines a method or variable that belongs to the class rather than an instance of the class

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

What is a constructor in Java?

A

A special method used to initialize objects; it has the same name as the class and no return
type.

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

What is the default access modifier in Java?

A

Package-private (accessible only within the same package).

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

: What are the operations of a Stack?

A

push, pop, peek

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

What are the operations of a Queue?

A

enqueue (offer), dequeue (poll), peek

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

What is a HashMap?

A

A data structure that stores key-value pairs and allows fast lookup using a hash function.

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

What is a HashSet?

A

A data structure that stores unique elements and uses hashing for fast access.

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

What is a binary search tree?

A

A binary tree where the left child is less than or equal to the node, and the right child is greater.

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

What is inorder traversal?

A

Visit left subtree, node, then right subtree.

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

What is preorder traversal?

A

Visit node, then left and right subtrees.

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

What is postorder traversal?

A

Visit left and right subtrees, then the node.

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

How do you handle exceptions in Java?

A

Using try-catch blocks; optionally a finally block for cleanup.

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

What is the difference between checked and unchecked exceptions?

A

Checked exceptions are checked at compile time (e.g., IOException); unchecked are checked
at runtime (e.g., NullPointerException).

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

What is an enum in Java?

A

A special data type that enables a variable to be a set of predefined constants.

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

What is an interface in Java?

A

A contract that specifies method declarations without implementations; classes must
implement them.

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

How is an abstract class different from an interface?

A

Abstract classes can have implemented methods and constructors, whereas interfaces cannot
(except default and static methods).

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

What are generics in Java?

A

They allow classes and methods to operate on objects of various types while providing
compile-time type safety

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
How do you define a generic class?
public class ClassName { ... }
25
How should class names be written in Java?
With an initial capital letter, using camel case.
26
How should variables and method names be written?
With an initial lowercase letter, using camel case.
27
How are constants written?
In all uppercase letters, with underscores separating words.
28
Why must hashCode() be overridden when equals() is overridden?
Because equal objects must return the same hash code for correct behavior in hash-based collections.
29
What is a hash collision?
A situation where two different objects have the same hash code.
30
What is the defining property of a max heap?
Each parent node has a value greater than or equal to its children, and the root is the maximum value.
31
What is bucket chaining?
A method to handle hash collisions by storing multiple entries in the same array index using a linked list.
32
: What is a priority queue?
A data structure where elements are served based on priority, not insertion order.
33
: When should you use an abstract class instead of an interface?
Use an abstract class when you want to share code across related classes; interfaces are for unrelated classes with common behaviors.
34
Can a class implement multiple interfaces in Java?
Yes, Java supports multiple interface implementations but only single class inheritance.
35
When is LinkedList better than ArrayList?
LinkedList is better for frequent insertions and deletions; ArrayList is better for fast access by index.
36
: What is the time complexity of inserting at the beginning of a LinkedList?
: O(1), because it only involves changing references.
37
Why is input validation important?
To prevent invalid or malicious input that could cause security vulnerabilities or program crashes.
38
: What is the purpose of version control?
To manage changes to source code over time, especially in collaborative environments.
39
: What is the difference between authentication and authorisation?
Authentication verifies identity; authorisation determines access rights.
40
What is the role of JavaFX in Java?
It is a library for building graphical user interfaces (GUIs) in Java applications.
41
: What is the difference between a full and balanced binary tree?
A full binary tree has every node with 0 or 2 children; a balanced tree has roughly equal height in left and right subtrees
42
: In a graph-based model of a network, what does an edge represent?
A connection or relationship between two nodes (vertices).
43
How do you read from a file in Java using Scanner?
Create a File object, then pass it to Scanner: Scanner input = new Scanner(new File("filename.txt"));
44
How do you write to a file in Java using PrintWriter?
Create a PrintWriter object and use print/println: PrintWriter out = new PrintWriter("output.txt");
45
What must you always do after writing to a file in Java?
Call close() on the PrintWriter to ensure data is saved.
46
What is the base case in recursion?
The condition under which the recursive method stops calling itself
47
What is the recursive case in recursion?
The part of the method that calls itself with a smaller or simpler input.
48
What is synthesis in a recursive method?
Combining results from recursive calls to build the final answer.
49
: What is reference aliasing in Java?
When two variables refer to the same object in memory; changes via one affect the other.
50
What does 'null' mean in Java
It indicates that a reference does not point to any object.
51
What are the core components of a JavaFX application?
Stage, Scene, and Nodes like Button, Label, TextField
52
How do you start a JavaFX application?
Extend Application class and override start(Stage primaryStage) method.
53
What happens if a static method tries to access a non-static variable?
A compilation error occurs. Static methods cannot directly access non-static instance variables.
54
: What is the result of assigning one object reference to another?
Both references point to the same object. Changes via one reference affect the other.
55
What will happen if a recursive method does not have a base case?
It will lead to a StackOverflowError due to infinite recursive calls.
56
What happens if you override equals() but not hashCode()?
Objects that are equal might not behave correctly in hash-based collections like HashMap or HashSet.
57
Why must main() be static in Java?
Because it is the entry point of the program and must be accessible without creating an instance of the class.
58
What does the 'finally' block do in a try-catch-finally structure?
It executes regardless of whether an exception is thrown or caught, typically used for cleanup.
59
Can an abstract class have constructors?
Yes. Abstract classes can have constructors to initialize fields used by subclasses.
60
What happens if you declare a method abstract but don't make the class abstract?
A compilation error occurs. Any class with abstract methods must be declared abstract.
61
How does Java handle duplicate keys in a HashMap?
The new value replaces the existing value associated with that key.
62
What does 'null' mean when used with object references?
It means the reference does not currently point to any object.