CS115 Flashcards
(63 cards)
What is encapsulation in Java?
Hiding data by making attributes private and exposing them via public getter and setter methods.
What is inheritance in Java?
Allows a subclass to acquire the properties and methods of a superclass using the
‘extends’ keyword
What is polymorphism?
Polymorphism allows different classes to be treated as instances of the same superclass, with
method behavior determined at runtime.
What does the ‘super’ keyword do?
It refers to the superclass of the current object and is used to access superclass methods or
constructors.
What does the ‘this’ keyword refer to?
‘this’ refers to the current instance of the class.
What is method overloading?
Defining multiple methods in the same class with the same name but different parameter lists.
What is method overriding?
Redefining a superclass method in a subclass using the same method signature.
What is the purpose of the ‘static’ keyword?
It defines a method or variable that belongs to the class rather than an instance of the class
What is a constructor in Java?
A special method used to initialize objects; it has the same name as the class and no return
type.
What is the default access modifier in Java?
Package-private (accessible only within the same package).
: What are the operations of a Stack?
push, pop, peek
What are the operations of a Queue?
enqueue (offer), dequeue (poll), peek
What is a HashMap?
A data structure that stores key-value pairs and allows fast lookup using a hash function.
What is a HashSet?
A data structure that stores unique elements and uses hashing for fast access.
What is a binary search tree?
A binary tree where the left child is less than or equal to the node, and the right child is greater.
What is inorder traversal?
Visit left subtree, node, then right subtree.
What is preorder traversal?
Visit node, then left and right subtrees.
What is postorder traversal?
Visit left and right subtrees, then the node.
How do you handle exceptions in Java?
Using try-catch blocks; optionally a finally block for cleanup.
What is the difference between checked and unchecked exceptions?
Checked exceptions are checked at compile time (e.g., IOException); unchecked are checked
at runtime (e.g., NullPointerException).
What is an enum in Java?
A special data type that enables a variable to be a set of predefined constants.
What is an interface in Java?
A contract that specifies method declarations without implementations; classes must
implement them.
How is an abstract class different from an interface?
Abstract classes can have implemented methods and constructors, whereas interfaces cannot
(except default and static methods).
What are generics in Java?
They allow classes and methods to operate on objects of various types while providing
compile-time type safety