Core Java (Revature)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is an abstract class?

An abstract class is a class that may not be instantiated. In order to create an object of the abstract class' datatype you must create a child class of it then create an instance of that class. A class MUST be abstract if it has even a single abstract method; but it can have a combination of concrete and abstract methods. We may only EXTENDS a single abstract class, just like any other class.

What is runtime polymorphism? What is compile time polymorphism?

Compile time polymorphism is method overloading. It is compile time because the compiler can tell which method you're calling before the code is ever run; and if you're calling an overloaded version of the method that doesn't exist the compiler can give you an error. This is possible because it only needs to check the method signatures, which all exist even before the code is run. Runtime polymorphism is method overloading AND type casting. It is runtime because the compiler cannot definitely say what type of object will be in the heap when the operation triggers; it has to wait until a heap exists to check a heap. The heap doesn't exist until the code is run.

What does the keyword "continue" do?

Continue tells java to cease the logic of the current code block THEN go back to the top of the loop's code block. Java will then begin processing the logic as if it has reached a new iteration of the loop.

What is covariance in java?

Covariance is when you override a method and alter the method signature in very specific ways. There are covariant return types, covariant access modifiers, and covariant throws declarations.

Describe the hierarchy of exception handling in java.

Throwable (class) is at the top; any object that is Throwable can be thrown using the "throw" or "throws" keywords. Throwable has two children class. Exception (class) is one of the children of Throwable. Exception is "checked". Error (class) is the other child of Throwable. Error is "unchecked". RuntimeException (class) is the child of Exception. RuntimeException is "unchecked".

How can we create an array?

We can create arrays in two ways: ● int[] arrayOne = {15, 88, 99}; ● String[] arrayTwo = new String[200]; Each element starts at the datatypes default value

May I have a constructor inside of an interface? May I have a constructor inside of an abstract class?

You may NOT have a constructor inside of an interface. You may have a constructor inside of an abstract class. There is no multiple inheritance inside of java so you can't have constructors inside of interfaces because you can implement many of them BUT you will only ever extends one class so a single constructor will provide a path back to the object class once (clean and simple).

What is a method?

A modularized block of code; created by putting a series of statements between two curly braces. This special block of code will be given a name so that it can be referred to later. The method signature in java is: [any modifiers] [the return data type] [the method name] ( [the parameter list] ) { // my logic }

What is a wrapper class?

A wrapper class is a reference type (class) version of a primitive data type. They add functionality to an otherwise "boring" primitive type. Think of a Christmas present being wrapped inside of wrapping paper; in that way, there is a version of the primitive data type its wrapper class BUT the wrapper class has added functionality (methods, etc) to the primitive (much like how the Christmas wrapping paper adds decoration). ● int Integer ● char Character ● boolean Boolean ● double Double ● float Float ● byte Byte ● short Short ● Long Long

What is inheritance?

An OOP concept that allows one class to inherit the properties of another (variables, methods, and "maiden name"). It allows code to be reused. In java, we achieve this using the "extends" and "implements" keywords.

What is polymorphism?

An OOP concept that allows one task to be performed in different ways. Variables and methods to take multiple forms. The parent child relationship is a good example. In java, we achieve this using method overloading, method overwriting, and casting.

What is abstraction?

An OOP concept that displays what something is and what it does but not how it does it. Showing purpose but hiding implementation. In java, we the abstract classes and interfaces to achieve abstraction.

What is encapsulation?

An OOP concept that is data hiding. Restricts direct access to data. In java, we achieve this by creating public getters & setters that access private variables."

What is an abstract method?

An abstract method is a method that will have NO implementation so it can NOT be apart of an instance. This method MUST be implemented by the first concrete child (or grandchild, etc) that the class has.

When is an error thrown?

An error can be runtime or compile time. Syntax errors are compile time errors. StackOverflowError and OutOfMemoryError are runtime errors.

What is an object?

An instance of a class. An object has state (variables) and behavior (methods). States are things like "size", "weight", "height", "color", "length", etc. Behaviors are things like "bounce", "makeNoise", "changeColor", etc. Think of an object like the food dish created from a recipe. The dish is an INSTANCE of the recipe.

What is an interface?

An interface is a contract given to a class; it tells the class that it must implement certain methods. By default, methods inside of an interface are public and abstract (unless the "default" or "static" keywords are used). By default, variables inside of an interface are public, static, and final. We may IMPLEMENTS as many interfaces are we'd like.

What does the keyword "break" do?

Break tells java to cease the logic of the current code block. Java will then begin processing the logic just after the code block's ending curly brace. Break may be used in if statements, while loops, etc.

Comparable vs Comparator?

Comparable defines the natural sort order for an object. Comparator defines an unnatural sort order for an object. Comparable is a functional interface that has the "compareTo(Object o)" method in it; you implement it in the object you want to be be sortable. Comparator is a functional interface that has the "compare(Object o1, Object o2)" method in it; you implement it in its own class separate from the object you want to be sortable. Both methods simply return a negative, positive, or zero values to help the sorting algorithms determine order when sorting.

What is the difference between up and down casting?

Down-casting turns the reference variable of an object into a reference to a child, or grandchild, or great grandchild, or etc. Up-casting turns the reference variable off an object into a reference to a parent, or grandparent, or etc.

What is ducking in java?

Ducking is when you use the "throws" declaration on a method signature to duck the responsibility of handling an exception. You may have MULTIPLE exceptions ducked in the throws declaration.

Describe the hierarchy of the collection API in java.

Iterable (interface) -> Collection (interface) -> List (interface) OR Set (interface) OR Queue (interface) Map (interface) is by itself; it's not TECHNICALLY a collection but when we talk about Maps when we talk about the Collection API.

What is the JDK?

JDK - Java Development Kit JDK is [JRE + development tools]. Dev tools include: the compiler + debugger + javadocs + etc. The compiler turns source code (.java files) into bytecode (.class files).

What is the JVM?

JVM - Java Virtual Machine The JVM allows java bytecode (.class files) to be executed on your machine (binary). Loads, verifies, and executes code. The JVM is abstract in nature.

What is java?

Java is a programming language and computing platform. Java is secure and reliable. From laptops to data centers, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

What is the difference between overloading and overriding?

Overloading is when you create multiple methods with the EXACT same name BUT different implementations. The parameter list in each method is slightly different, so that there is a distinct version of the method that is called each time. To overload the method you must change the parameter list of the new signature in one of the following ways: change the data types, change the number of parameter, OR change the order of the parameter datatypes. This happens within a single class and has nothing to do with inheritance. Overriding is when you create a NEW implementation for an inherited method. To override just make sure the create a method with the same name and parameter list as a method in the parent class; if you've done that then the parent method's logic will be altered to the child logic (assuming you've create an object of the child class). Overriding is only able to happen if we have inheritance.

What is method overloading?

Overloading is when you have multiple methods with the same name, but different parameter lists. There are three ways to change the parameter list: ● change the data types ● change the number of parameters ● change the order of the datatypes in the parameter list This concept does not need inheritance to exist; it happens all within the same class.

What is reflection in java?

Reflection allows one to view an object or primitive during runtime you may also modify the object's structure during runtime.

What is shadowing?

Shadowing is when a parent class and a child class have declare a variable with the same exact name. The parent's version of the variable still exists BUT it can now only be accessed using the "super" keyword. The child's version of the variable can be accessed with or without the "this" keyword. If a parent method has the same name as a child method then the parent method will be overridden NOT shadowed. Keep this in mind. Shadowing is for variables, Overriding is for method.

What is short circuiting?

Short circuiting deals with && and || operators. While computing a true/false condition, if the short circuiting operators determine that the result of the condition is apparent even without computing the rest of the statement then it will stop short and proceed with the answer is already knows. For example, if the left side of && is already false then it is impossible for the statement to be true; so java will not compute the right side. Another example, if the left side of || is true then java will stop computer because the condition will be true regardless of the right side's results.

Conceptually, what is abstract vs concrete? (Not just in java, but in general)

Something that is concrete can be touched, held, etc. Something that is abstract is purely conceptual, it's an idea not something that physically exists.

What are the scopes of a variable in java?

Static , Instance, Method, and Block. ● Static (aka class) The variable/method belongs to the class itself. The class has one copy of the variable that all instances share. ● Instance (aka object) Each object has its own copy of each variable/method and each object. So changing one object's variables doesn't change another object's variables. ● Method The variable only exists in the method; outside of the method it stops existing. ● Block The variable only exists within a block of code (denoted by curly braces { } ). Outside of the block the variable stops existing.

What is the difference between String, StringBuilder, and StringBuffer?

String is a class implemented using an immutable array of chars. StringBuilder and StringBuffer are mutable (they CAN be changed). Unlike StringBuilder, StringBuffer is thread safe.

What are the try, catch, and finally blocks used for?

The "try" block is used to attempt code that MAY throw an exception. The try block will be followed by 1 or more "catch" blocks of code; the catch blocks will work like "if" and "else if" statements which trigger depending on which exception is thrown. The "finally" block follow the series of catch blocks; it will trigger regardless of whether or not an exception is thrown.

What is the collection API in java?

The collection API is a series of pre-built data structures inside of java. Each data structure houses a group of java object and provides functionality to organize and manipulate the group of objects.

What is the finalize() method?

The finalize() method is a method that can be overridden in each class; the garbage collector calls right before it deletes an object. Each object may override the finalize() method to give custom functionality to handle any last minute bookkeeping just before it is deleted. (This method is deprecated as of now)

What is the difference between "super" and "this"?

The keyword "this" is used to refer to the CURRENT object/instance. Example "this.myVariable" would refer to an instance variable called "myVariable". Using the "this" keyword from inside a constructor with parenthesis (ex: "this()") will call a different constructor from the current constructor. The keyword "super" is used to refer to the current object/instance's PARENT. Example "super.parentVariable" would refer to an inherited variable called "parentVariable". Using the "super" keyword from inside a constructor with parenthesis (ex: "super()") will call a parent's constructor from the current constructor.

What does the keyword "new" do in java?

The new keyword is a Java operator that creates the object; it is followed by a call to an object's constructor. Example: MyObject myObj = new MyObject();

What is an abstract variable?

There are no abstract variables in java; it doesn't make sense to since you can't override a variable to begin with AND all variables are already passed down to children classes.

What is type casting?

Type casting is when you tell java to change one data type of another. For exampe, you could tell java too change an int to a double: int myInt = 8; double myDoub = (double) myInt;

What does the "final" keyword do in java?

Used on a variable, the final keyword prevents a variable from ever changing its value after it has been declared; this means that you MUST give a final variable a value during declaration. Used on a method, the final keyword means that the method can NOT be overridden. Used on a class, the final keyword means that class can NOT be extended from.

How can I provide an implementation for a method inside of an interface?

Using the "default" keyword OR the "static" keyword.

What is var args in java?

Var args means variable arguments. It's when you create a method that is able to to handle a variable number of arguments. You would use the "..." operator behind the FINAL parameter's data type. For example: static void methodOne(boolean b, int... k) { //my logic }

What are the naming conventions in java?

Variable names: camel case. e.g. myFirstName, myLastName Class names: title case. e.g. Animal, UserStory, ButtonColors, nouns Interface names: title case. e.g. Runnable, Comparable, adjectives Method names: camel case. e.g. drawRectangle, run, verbs package names: lowercase. e.g. java, lang, sql, util, etc constants: uppercase. e.g. RED, YELLOW, MAX_PRIORITY, etc

How do we access an element in an array and what sort of exception can be thrown?

We can access each element of an array using square brackets [ ]. If you attempt to access an element that doesn't exist then you'll get a "ArrayIndexOutOfBounds" exception.

In what ways can we "handle" an exception?

We can handle an exception in one of two ways: We can use a try-catch-finally block OR we can use a throws declaration in the method signature."

When is the garbage collector eligible to consume an object?

When the object no longer has a single reference variable referring to it.

When does a variable assume its default value?

When the variable is in the static scope, when it's in the instance scope, or when it's declared inside of an array.

What is the JRE?

JRE - Java Runtime Environment JRE is [JVM + the set of necesary libraries]. Minimum requirement to run java code.

What is a function vs a method?

A method is simply a function that is attached to an object. It's truly as simple as that. Java is almost entirely object oriented so it strictly uses methods, not functions.

Is the default constructor and the no args constructor the same thing?

"NO! The no args constructor is a constructor that has an empty parameter list. The default constructor is the constructor is that constructor that the compiler will automatically write for you if you compile the code without having written your own constructor. It happens to be a no args constructor; but just because you have a no args constructor does not mean you have a default constructor. The no args constructor is ONLY consider default if the compiler automatically gave it to you."

What is a bit? What is a byte? What is a nibble?

A bit is a binary value (0 or 1 aka ON or OFF) A nibble is 4 bits (16 different combinations 0s and 1s. 2^4) A byte is 8 bits (256 different combinations of 0s and 1s. 2^8)

What is a class?

A blueprint for an object. A class is abstract in nature, you can not have a physical instance of a class; it simply defines the structure of an object. Think of a class like a recipe to create a food dish; it isn't the food itself...just the instructions to create the food.

What is a String?

A class that is implemented using an array of chars; one could think of it as a wrapper class for an array of chars. A String is immutable meaning it cannot be changed.

What is a constructor?

A constructor is a special method that constructs an instance of the class (aka an object) and initializes the object's state. The name constructor is the same name as the class itself, and it does NOT have a return type. public class Monkey{ public monkey(){ //this is the constructor //my logic } }

What are the four pillars of object oriented programming?

Abstraction, Polymorphism, Inheritance, and Encapsulation. Try to remember APIE.

When is an exception thrown?

EVERY exception is thrown at runtime, never compile time. The compiler CHECKS to see if you've handled an exception as compile time...but if you didn't handle a checked exception then the compiler gives you a syntax error; but the exception was never THROWN in that case.

What is flow control?

Flow control statements break up the normal flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. Types of flow control statements include: ● if, else if, and else blocks ● ternary statements ● switch cases ● while loops ● do while loops ● for loops ● enhanced for loops (aka for each loops) ● try, catch, and finally blocks

What is the difference between "checked" and "unchecked" exceptions?

For a checked exception, the compiler forces you to handle the exception before the program ever begins running ELSE it will create a syntax error. For an unchecked exception, the compiler does NOT force you to handle the exception at all.

What are generics in java?

Generics use angle brackets to create a "placeholder" for a future datatype. You define the datatype when you access the generic class OR generic method.

What is autoboxing? What is unboxing?

If at any point in your program, the compiler is looking for a primitive type and it is given its wrapper class variation then the compiler will automatically turn the wrapper class into the primitive variation. This is called autoboxing. The opposite is also true; if the compiler is looking for a wrapper class and you give it a primitive type then the compiler will automatically turn the primitive into its wrapper class variation. This is called unboxing.

Benefits of abstract class vs benefits of interfaces?

If you want to provide lots of partial implementation then you should be using an abstract class. Also, abstract classes can pass down variables (interfaces cannot) If you don't need to use your ONE slot for extending a class then don't do it and simply use an interface.

What is the difference between initializing and instantiating?

Initializing is when you give an initial value to a variable: char c = 'x'; Instantiating is when you create an object instance. Root word "instance" like when you, ya know, create an INSTANCE of a class (aka an object). Like so: Thread th = new Thread(); If you're saying to yourself "Thread th = new Thread()" looks like it is giving a variable an initial value...then you would be correct. Initializing could POSSIBLY include instantiation; but just because you see initialization doesn't mean something is being instantiated.

What is an array?

It's a series of data entries sequential in memory (of the same data type). An array can have multiple dimensions to them. You can declare with with as many sets of square brackets as you'd like.

Why java?

It's portable. Easy to learn. It has simple syntax based on C. Object-oriented. Automatic memory management. Rich API. Java is FREE. Supported by Oracle Corporation.

How can we force the garbage collector to consume an object?

You can NOT force the garbage collector to come. The garbage collector is responsible for all java programs, not just yours. The garbage collector can't be told to prioritize your project over others else all projects would do that. In short, the garbage collector doesn't think you're special and it doesn't get paid enough to care.

What method do you call to find the size of an array?

You can find the size of an array (number of element) by using: myArray.length; BUT ".length" is NOT a method; it's a property of an array.

What is the first implicit line of any constructor?

super();

What datatypes does a switch case allow?

● int (and Integer, the wrapper class) ● short (and Short) ● byte (and Byte) ● char (and Character) ● String ● enum

What are the access modifiers in java?

● public What has access? The class itself & the current package & any subclasses (children classes) & any other classes you can think of. ● protected What has access? The class itself & the current package & any subclasses (children classes) ● (default) What has access? The class itself & the current package ● private What has access? The class itself ONLY.

Can you name 3 types of constructors?

● the default constructor ● the no args constructor ● args constructor (can have any number of overloading variations)"


Ensembles d'études connexes

Clinical Phonology: Consonants PVM, Distinctive Features, Vowels

View Set

Certified Research Administrator Exam Review

View Set

Chapter 44: Assessment and Management of Patients with Biliary Disorders

View Set

everythings an argument chapter 1

View Set

health assessment in class quizzes

View Set

CIS 3352 - Database Management - Final Study

View Set

chapter 4. social & cultural foundations

View Set

High School Health Quiz 3 unit 1 - Body Essentials QUIZ'S

View Set