Question | Answer |
What keyword is used when calling a parent constructor in inheritance? | Super |
Explain the difference between linear and binary searching. | Linear goes through each element one at a time while binary divides the search pool by two until the desired item is reached. |
Why does polymorphism work? | Because method binding occurs at run time. |
Why do we program to interfaces? | Reduces programming errors
Communicates design
Supports obstruction and encapsulation
Increases development, integration, and system testing |
What is the difference between a class and an object? | A class is a blueprint. |
What keyword is used to instantiate an object? | New |
Why doesn’t Java support multiple inheritance? | Multiple inheritance complicates the design and creates problems during casting, constructor chaining, etc. |
Can we override static method in Java inside a subclass? | No, we can’t override a static method in java but it’s not a compile time error to declare exactly the same method in a sub-class, that’s called method hiding in Java |
Does the following code compile? | Interface Ifruit {
Public string TYPE = “Apple”}
Class Fruit implements Ifruit {}
Public class application {
Public static void main (String [] args) {
System.out.print (Fruit.TYPE);
}
}
Yes |
What are the operations of a stack? | Push – adds an element to the end of the stack
Pop – removes an element from the front of the stack
Peek – returns a reference to the element at the front of the stack
isEmpty – returns true if the stack empty, returns false |
What is the difference between a full and complete tree? | Full – if all leaves of the tree are at the same height and every non-leaf node has exactly n children
Complete - if it is full, or full to the next to last level will all leaves at the bottom level on the left side of the tree |
How do you enable your Java class to use the API stack, queue, and LinkedList class? | Import java.util.stack;
Import java.util.Queue;
Import java.util.LinkedList; |
Why can Java be ran on any machine? | Byte code is OS independent |
What happens when one class is assigned to another? | The pointer is copied |
When does the garbage collector clear things from ram? | When there are no pointers to it |
In what ways might a thrown exception be handled? | It can be ignored
You can use a try-catch statement
Handled higher in the method-calling hierarchy |
What is the order of the search in a binary tree? | O(logn) |
What is the pseudocode of the preorder? | Visit node
Left child
Traverse right child |
What is the characteristic behavior of a stack? | List in First out (LIFO) |
Describe the comparable interface. | The Comparable interface contains a single method called compareTo, which should return an integer that is less than zero, equal to zero, or greater than zero if the executing object is <, =, or < the object to which it is being compared |
What is an off-by-one error? How does it relate to arrays? | An off-by-one error occurs when a program’s logic exceeds the boundary of an array by one. These errors include forgetting to process a boundary element, as well as attempting to process a non-existent element. |
What is the characteristic behavior of a queue? | First in First out (FIFO) |
What make your variables private? | Protect your variable from being manipulated by an unauthorized third party |
Why is the stack trace useful to programmers but not end user? | To the programmer the stack trace gives him an idea of what broke the program but to the user it is just a meaningless of words |
Explain the relationship between a parent and a child class | Is-a relationship |
What is the use of abstract class? | Generic concept in a class hierarchy |
What is the use if the protected modifier? | Maintain some level of encapsulation while using inheritance |
What is the name of the node that has no children? | A leaf |
What is overloading? | Having multiple methods with the same name |
Are exceptions considered objects? | Yes, they are objects that represent unusual or invalid processing |
How is it possible to declare dogs as a Queue and construct it as LinkedList in the following? Private static Queue<Dog> dogs = new LinkedList(); | Polymorphism via Interfaces: an interface reference variable can refer to any object of any class that implements that interface. By declaring dogs as type Queue,
we restrict method calls to only those defined for the Queue interface. |
Which uses LIFO, and which uses FIFO? Queue or stack? And what does each mean? | Queue uses FIFO (First in First out). In a queue objects are added to the rear, removed from the front.
Stack uses LIFO (Last in First out). In a stack objects are added at the top, removed from the top. |
Why do we use static methods? Why do we use generic methods? | We use static methods because it’s a utility method and no instance (object) is necessary. We use generic methods because the method can apply to many types. |
What are pointers? | Addresses in memory |
Why do we provide a buffer for IO? | It is slow compared to computations |
Why can a child be constructed from a parent’s constructor? | Inheritance, the child is the parent |