1302 test 2

  1. What is boxing and unboxing?
    Java’s way of casting types back and forth automatically between primitive and object based types (of wrapper classes) using the valueOf method to box
  2. What is inheritance in OOP?
    Creating new classes based on the definition of other classes. Great for avoiding redundant/tedious reuse. “IS A” relationship
  3. How is inheritance represented in UML?
    Arrowhead points to superclass/parent class/supertype from subclass/child class/subtype
  4. How is inheritance represented in java?
    • public class Subclass extends Superclass {
    • // implement Dog class
    • }
  5. What does the super keyword provide access to?
    Public constructors and methods of a superclass. Can use with any method that’s exists in superclass by matching appropriate method signatures.
  6. Overriding
    • Use @Override before a subclass method that matches a superclass method exactly.
    • For example a toString method that you want to return a different thing from the superclass than the subclass
  7. Constructor chaining
    • Java will execute all default constructors up the extends chain. ie, it will create a default superclass object for each subclass object that’s instantiated.
    • For example- super(); is AUTOMATICALLY done when Dog() is invoked
  8. What role does the object class play in java?
    Memory location? idk it is the parent class of all java classes
  9. What is the difference between overriding and overloading a method?
    • Overriding- same name and method signature,
    • Overloading- same name but different parameter list and return type
  10. What is polymorphism and what is it’s purpose?
    • Allows you to refer to a subclass using its super class type or vice versa (any subclass can be passed through a super class reference variable)....
    • public void adopt(Animal a)—
    • Good for flexibility
  11. What is dynamic binding and how is it accomplished?
    The process through which the Jvm decides at runtime what the actual type of an object is and uses the appropriate implementation. Declared type is dynamically binded with actual type.
  12. Why use instanceof?
    When a dealing with polymorphic objects we can use instanceof with a super class instance to see if it’s an instance of a certain subclass
  13. Difference between declared type and actual type?
    • Declared type is variable type declared, matches at compile time.
    • Actual type is type of variable matched at runtime
  14. Implicit casting and explicit casting examples?
    • Implicit: Object o = new Student(); (can only cast DOWN to subtypes, up wouldn’t make sense to compiler. ie Student o = new Object();)
    • Explicit: Student b = (Student) o; (can only cast UP, tells compiler that supertype is a subtype object. ie object is a student)
  15. When would you need to explicitly cast?
    • To tell compiler that a subtype is an instance of a supertype so we can treat it like its actual type within a general polymorphic collection.
    • Football is an instance of a sport —— Football fb2 = (Football)sp3; —-We do this because not all sports are football
  16. How do we determine object equality in java?
    (Overriding) equals() method inherited from Object class then seeing if the supertype is an instanceof another subtype, then checking if their data members are the same with return dataMember == explicitCast.dataMember
  17. Difference between standard arrays and ArrayList?
    • ArrayList class stores unlimited number of OBJECTS whereas standard arrays are fixed size (can’t primitive types but can hold wrapped primitives)
    • Must import java.util.ArrayList
    • ArrayList cities = new ArrayList<>();
    • Much more useful and flexible
  18. Useful methods for lists?
    • Collections.sort/shuffle/min/max(list);
    • Create ArrayList from existing object array- new ArrayList(Arrays.asList(oldArray));
  19. ArrayLists start with a generic type that need to be replaced with a concrete type upon creation
    That’s how they work
  20. Pop method for ArrayList stack
    • public Object pop() {
    • Object o = list.get(getSize()-1);
    • list.remove(getSize()-1);
    • return o;
    • }
  21. Package keyword and what it does
    • It groups classes
    • package p1;
  22. Protected keyword and what it does
    • Private->default->PROTECTED->public
    • # in UML
    • Grants access to any class in the same package and any subclass regardless of package.
    • Different to default in that default is package private
  23. What keyword(s) allow access from a different package?
    Public
  24. What keyword(s) allow access from a subclass?
    Public, protected
  25. What keyword(s) allow access from a the same package?
    Public, protected, default
  26. What keyword(s) allow access from the same class?
    Public, protected, default, private
  27. Final keyword and what it does
    • Final on variables are constants
    • Final on classes cannot be extended
    • Final on methods cannot be overridden
  28. What is an exception and the benefits of it?
    • Objects created from impossible actions that terminate program execution unless handled.
    • Gives us information about our programs
    • Allows us to preform exception handling
  29. What is a try catch block and how does it work?
    • Exception handling code that can throw an exception.
    • When an exception occurs- throw is called and “calls” catch
    • If exception is thrown, execution goes immediately to catch block- nothing in try block executes
  30. What are the three major exception types?
    • System errors- (Error class) thrown by JVM, very rare (unchecked).
    • Exceptions - (Exception class) thrown by your program, can be caught/handled (checked except for runtimes).
    • Runtime exception - (RuntimeException class) thrown by programming errors (unchecked)
  31. What are the major pieces of exception handling
    Declaring throwing and catching exceptions
  32. Difference in dealing with checked and unchecked exceptions
    • Checked- must be dealt with through try catch or declared in method header, customs? (exception)
    • Unchecked - usually just a logic error (error, RuntimeException)
  33. Different ways to throw exceptions
    • throw new Exception();
    • Exception ex = new Exception();
    • throw ex;
  34. Difference between throws and throw
    throws declares exceptions while throw throws exceptions
  35. Different exceptions
    • ArrayIndexOutOfBoundsException
    • InputMismatchException
    • NullPointerException
    • Exception
  36. What is the finally clause and what does it do?
    Executed regardless of exception after catch clause
  37. When should you throw exceptions?
    • If you want it processed by the caller, create an exception object and throw it
    • If you can handle the exception in the method where it occurs, there is no need to throw it
  38. How do you rethrow exceptions?
    • try {
    • statements;
    • } catch(Exception ex) {
    • perform operations;
    • throw ex;
    • }.
    • Do when throwing to a new method
  39. How do you define custom exceptions?
    Make a new class for it that extends Exception (checked because they extend exception)
  40. What happens when no errors are detected in try catch finally
    All of try and finally are executed, execution starts after try-catch-finally
  41. What happens when there is an exception in try of try-catch-finally?
    Skip rest of try, execute appropriate catch only, execute finally
  42. What happens when exception not caught in try-catch-finally?
    Skip rest of try, skip all catch, execute finally, pass exception to calling method
  43. How do you create file objects
    • new File(“src/a.txt”);
    • Always use relative paths and forward slashes, avoid absolute paths whenever possible (ie “home/me/src/a.txt”)
    • Can’t read or write data from/to a file
  44. What types of operations/manipulations can be done with a file object
    • PrintWriter output = new PrintWriter(file); (sane methods as System.out.*)
    • same thing with scanner.
    • Must close the file with output.close(); for the file to be written
  45. Why and how to use try-with-resources
    • Automatically closes object(s) after try block.
    • try ( new PrintWriter object ) { code }
    • Multiple resources can be declared inside parenthesis.
    • Catch block can be omitted or used to catch exceptions
  46. PrintStackTrace() vs toString vs getMessage
    • Print stack trace shows: name of exception-message of exception- stack trace
    • To string shows: name of exception- message of exception
    • Get message shows: message of exception
  47. What is a checked exception
    Exceptions forced by the compiler used to indicate exceptions out of control of the program
  48. Can java access web resources, and if so, how?
    By creating URL object and a scanner object with new Scanner(url.openStream()); to open an input stream
Author
timmymorin
ID
352574
Card Set
1302 test 2
Description
Updated