Skillstorm Intro to Coding - OOP & Java Fundamentals Flashcards

1
Q

What are classes in Java?

A

They are blueprints used to define objects

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

What are a few examples of classes?

A
  1. Real-world objects
  2. Application components
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the members of a Class in Java?

A

=> Variables (State)

=> Methods (Behavior)

=> Constructors (Initialization)

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

Given the following code, identify the class body, constructor, method body, and variable.

public class HelloVehicle { 
 int speed; 
 void accelerate() {}

HelloVehicle() {
speed = 0;
}
}

A
public class HelloVehicle { // class body
 int speed; // variable
 void accelerate() {} //method body

HelloVehicle() {
speed = 0;
} // constructor
}

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

Given a basic HelloWorld class, what’s the best way to write the signature of the main method in Java?

public class HelloWorld {

}

A

public class HelloWorld {

public static void main(String[] args) {
System.out.println(“Hello World”);
}

}

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

Which is the correct signature of the main method?

  1. public static void main(String[] args)
  2. static public void main(String[] args)
A

Both 1. and 2. are correct!!!

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

Label the separate code blocks correctly with following labels: Class, Constructor, Method, and Variable

  1. ____________
    public class HelloVehicle{
    2._____________

int speed;

3._____________

void accelerate() {}

4._____________

HelloVehicle(){

speed = 0;

}

}

A
  1. Class

public class HelloVehicle {

  1. Variable

int speed;

  1. Method

void accelerate() {}

  1. Constructor

HelloVehicle(){

speed = 0;

}

}

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

Java is strongly-typed, in other words, you have to state what data type each variable is going to be? True or False

A

True

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

What are the four data types in Java and their sub-types?

A
  1. Numeric (whole)
    1. byte: -128 to 127
    2. short: -32,768 to 32,767
    3. int: -2^31 to 2^31
    4. long: -2^64 to 2^64
  2. Numeric (decimal)
    1. float: single precision
    2. double: double precision
  3. Text
    1. char: single character
  4. True/False
    1. boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are non-primitive data types with an example?

A

Non-Primitive Data Types

  1. Any class you want
  2. Create your own types

For Example,

Vehicle Class as a variable to be used with an Engine Class

String

  1. Multiple characters together

You could string characters together to get a word such as HELLO.

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

In Java arrays, you store multiple values in a _____ _____ variable

A

single reference

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

Are Java arrays continuous?

A

Yes, they are a sequential block of memory

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

How do you access the elements of a Java array?

A

By their index

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

Though values can be different in each index of a Java array, can they also be of a different data type?

A

No. They have to be the same data type but can be different values

For Example:

If the data type is char, index 0 can have raj, index 1 can have hudek and so on, but index 3 could not have 42 because that is a numeric data type which is different than the original char data type we started out with in index 0 and index 1.

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

What do constructors initialize?

A

An object’s state

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

Calling a constructor by its new keyword does what?

A

It creates an object at runtime

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

Does Java provide a default constructor?

A

Yes, BUT only if you haven’t provided your own

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

Can you define many constructors and if so, what would that be called?

A

Yes, and it is known as constructor overloading

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

When a class contains a constructor, and you have more than one keyword, what happens when you call the second keyword?

A

The constructor is invoked to create a brand new object with its own state.

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

If you call the second reference variable (keyword) in a constructor, will the method from the class that acted on the first reference variable work here as well?

A

No

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

Why can’t you write two constructors that have the same number and type of arguments for the same class?

A

Because the compiler would not be able to tell them apart!

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

What happens if you don’t provide any constructors for your class?

A

The compiler automatically provides a no-argument, default constructor for any class without constructors.

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

How do you have variables that are common to all objects?

A

By using a static modifier

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

What does it mean that a static modifier is called a static field or class variable?

A

It means they are associated with the class, rather than any with any object.

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

If any object can change the value of a class variable, can class variables also be manipulated without creating an instance of the class?

A

Yes

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

How are class variables referenced?

A

By referencing the class name itself.

For example:

public class Bicycle {

private int cadence;

private int gear;

private int speed;

// add an instance variable for the object ID

private int id;

// add a class variable for the number of Bicycle objects instantiated

private static int numberOfBicycles = 0;

}

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

What can you use to access static fields?

A

static methods.

For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:

public static int getNumberOfBicycles() {

return numberOfBicycles;

}

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

What can instance methods access directly?

A
  1. instance variables
  2. instance methods
  3. class variables
  4. class methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What can class methods access directly?

What can class methods not access directly and what must they use to access these things indirectly?

BONUS: Why can’t class methods use the “this” keyword?

A

Can access directly => Class variables and class variables.

Cannot access directly => Instance variables or instance methods

What must they use => Object reference

BONUS => Because there is no instance for “this” to refer to.

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

What are the only required elements of a method declaration?

A
  1. Return type
  2. Name
  3. A pair of parentheses ()
  4. A body between braces {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

More generally, what are the six components of method declarations in order?

A
  1. Modifiers—such as public, private, and others you will learn about later.
  2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
  3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
  4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
  5. An exception list—to be discussed later.
  6. The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What two components of a method declaration comprise the method signature?

A
  1. The method’s name
  2. The parameter types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is proper naming convention for methods?

A

By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc.

In multi-word names, the first letter of each of the second and following words should be capitalized.

For example:

run

runFast

getFinalData

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

Though it’s typical for a method to have a unique name within its class, when could they have the same name as other methods (with an example)?

A

Method overloading or methods with different method signatures. In other words methods within a class can have the same name if they have different parameter lists.

For example:

public class DataArtist {

public void draw(String s) {

}

public void draw(int i) {

}

public void draw(double f) {

} public void draw(int i, double f) {

}

}

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

Give examples of static methods that are created without instances of a class (without an object) using the Math class and the static modifier.

A
  1. Math.random();
  2. Math.pow(2, 31);
  3. Math.tan(90);

Where Math is the class and random, pow, and tan are the static modifiers.

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

Match the value to the correct variable declaration for Class Vehicle {}:

Values => 1. false, 2. ‘D’, 3. “Toyota”, 4. 123456789123456789L, 5. 100.1234567890123f, 6. 20000000000

int speed =

char fuel =

boolean running =

long serial =

float fuelRemaining =

String make =

A

int speed = 2000000000

char fuel = ‘D’

boolean running = false

long serial = 123456789123456789L

float fuelRemaining = 100.1234567890123f

String make = “Toyota”

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

Complete the code to call the go method and store the returned value:

public class Methods {

public static void main(String[] args) {

Methods example = new Methods();

int returned = ____________;

}

public ___ go () {

return 510;

}

}

A

public class Methods {

public static void main(String[] args) {

Methods example = new Methods();

int returned = example.go();

}

public int go () {

return 510;

}

}

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

Create an object of the Pizza class

A

Pizza {

pizza = new Pizza();

}

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

Complete the code to create an int array of size 3 and store a value in the first position:

_______ array = ______ ;

_______ = 120;

A

int[] array = new int[3];

array[0] = 120;

40
Q

The Java programming language is a high-level language that can be characterized by which buzzwords?

A
  • Simple
  • Object oriented
  • Distributed
  • Multithreaded
  • Dynamic
  • Architecture neutral
  • Portable
  • High performance
  • Robust
  • Secure
41
Q
  1. What is all source code of the Java programming language written in?
  2. What type of files are those source files compiled into?
  3. What does the compiling?
  4. If the .class file does not contain code that is native to your processor, what kind of code does it contain?
  5. Which type of “machine” uses this kind of code?
  6. What helps run your application with an instance of the Java Virtual Machine?
A
  1. Plain text files ending with the .java extension
  2. .class files
  3. Javac compiler
  4. bytecodes
  5. JVM (Java Virtual Machine)
  6. java launcher tool
42
Q

What is the Java Platform?

A

A platform is the hardware or software environment in which a program runs like Microsoft Windows, Linux, Solaris OS, and Mac OS.

43
Q

What two components does the Java platform have?

A
  1. The Java Virtual Machine
  2. The Java Application Programming Interface (API)
44
Q

What is an API?

A

The API is a large collection of ready-made software components that provide many useful capabilities and are grouped into libraries of related classes and interfaces.

45
Q

What compiles Java code and what executes Java code?

A

Java Development Kit (JDK) and Java Virtual Machine (JVM) respectively.

46
Q
  1. When is a private Java Virtual Machine stack created?
  2. What does a Java Virtual Machine stack store?
  3. What does the JVM stack hold?

. 4. What does the JVM stack play a part in?

  1. Does the memory for a JVM stack need to be contiguous?
  2. Are JVM stacks fixed or dynamic?
A
  1. The same time as the thread
  2. frames
  3. variables and partial results
  4. method invocation and return
  5. No
  6. Can be both
47
Q
  1. If the computation in a thread requires a larger Java Virtual Machine stack than is permitted, what happens?
  2. If Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, what error is thrown?
A
  1. Java Virtual Machine throws a StackOverflowError
  2. OutOfMemoryError
48
Q
  1. What is a JVM heap?
  2. Is it shared among all Java Virtual Machines?
  3. Is the JVM heap fixed, expandable, or contractible?
  4. Does the JVM heap memory need to be contiguous?
  5. If a computation requires more heap than can be made available by the automatic storage management system, what error is thrown?
A
  1. The run-time data area from which memory for all class instances and arrays is allocated.
  2. Yes
  3. All of the above
  4. No
  5. OutOfMemoryError
49
Q

What can a JVM stack contain?

A
  • Methods calls
  • Variables declared in methods
  • Reference variables
  • Cleaned up as method completes
50
Q

What does a Heap contain or hold?

A
  • Objects
  • Instance variables
  • JVM cleans up objects not in use
51
Q

What is encapsulation for?

A
  1. Wrapping related state/behavior in a single unit
  2. Protecting class members from undesirable Access
52
Q

Why do programmers bundle groups of related types into packages?

A
  1. To make types easier to find and use
  2. To avoid naming conflicts
  3. To control access
53
Q

What does “types” in encapsulation refer to?

A
  1. classes
  2. interfaces
  3. enumerations
  4. annotation types
54
Q

Why should you bundle classes and interface?

A
  • You and other programmers can easily determine that these types are related
  • You and other programmers know where to find types that can provide graphics-related functions
  • The names of your types won’t conflict with the type names in other packages because the package creates a new namespace
  • You can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package
55
Q
  1. Packages are folders that bundle _____ classes.
  2. Typically “reverse” domain name => _____.example.project
  3. Packages provide a fully-qualified _____ name which helps avoid naming _____
  4. Packages are a _____ structure
  5. The first line of a class must be the _____ statement
  6. You can _____ classes into another class
A
  1. related
  2. com.example.project
  3. class, collisions
  4. hierarchical
  5. package
  6. import
56
Q
  1. What do access level modifiers determine?
  2. How many levels of access control?
  3. What is top-level access control?
  4. What is member-level access control?
A
  1. The determine whether other classes can use a particular field or invoke a particular method
  2. Two
  3. public, or package-private (no explicit modifier)
  4. public, private, protected, or package-private (no explicit modifier)
57
Q

Fill in Modifiers:

Access Levels

Modifier Class Package Subclass World

_____ Y Y Y Y

_____ Y Y Y N

_____ Y Y N N

______ Y N N N

A

Access Levels

Modifier Class Package Subclass World

public Y Y Y Y

protected Y Y Y N

no modifier Y Y N N

private Y N N N

58
Q

Access Modifiers cont.

  1. What do access modifiers do?
  2. What can most access modifiers be applied to?
  3. Three default modes of access modification?
  4. What are some common terms for having no modifier?
  5. What does public access modifier do?
  6. What does protected access modifier do?
  7. What does no modifier do?
  8. What does private access modifier do?
A
  1. Restrict visibility to class members
  2. Classes, Methods, Variables, Constructors
  3. Public, Protected, and Private
  4. Default and/or Package Private
  5. Accessible anywhere in the application
  6. Accessible within the package and any subclasses
  7. Accessible only within the package
  8. Accessible only within the class
59
Q
  1. What does Variable Scope affect?
  2. What are the four Variable Scopes in Java?
A
  1. Visibility of variable in a class and Lifespan of the variable
  2. Class Variables (static), Instance Variables, Local Variables, and Block-scope Variables
60
Q

Class Variables

What are three characteristics of Class Variables?

A
  1. Variables are declared with the static modifier
  2. Declared in the class body
  3. Variables initialized when the class loads (No instance is needed)
  4. All instances share Class Variables

Example:

class Person {

st

61
Q

Instance Variables

What are characteristics of Instance Variables?

A
  1. Each new object has its own
  2. Declared in the class body
  3. Objects are cleaned up when no longer referenced
  4. Marked for garbage collection when the object is

class Person {

static Planet planet; <======== This is the Class Variable

int age; <================= This is the Instance Variable

}

62
Q

Local Variables

What are characteristics of Local Variables?

A
  1. Declared in the method body
  2. Declared as a method parameter
  3. Marked for garbage collection when the method ends
  4. Available anywhere in the method

public void code(Computer pc) { <========The declaration is also part of the Local Variable

IDE ide = new IDE(); <======= This is the Local Variable

}

63
Q

Block-Scope

What are characteristics of Block-Scope Variables?

A
  1. Declared within a block of code (if statements, loops, try-catch blocks etc.)
  2. Marked for garbage collection after the block ends
  3. Not accessible in the method, only that block

Example:

public void jump(boolean b) {

if(b){

int height = 2; <==== Can’t be used anywhere else inside the jump method

}

64
Q

Label the four different scopes on this code:

class Person {
 // \_\_\_\_\_\_\_\_
 int age;
 // \_\_\_\_\_\_\_\_
 static String planet;
 // \_\_\_\_\_\_\_\_
 public void move(int distance) {
 {
 // \_\_\_\_\_\_\_\_\_\_
 int x = 10;
 distance = 10;
 }
 x = 9;
 distance = 10;
 }
}
A
class Person {
 // instance variable
 int age;
 // class variable
 static String planet;
 // local variable
 public void move(int distance) {
 {
 // block-scope variable
 int x = 10;
 distance = 10;
 }
 x = 9;
 distance = 10;
 }
}
65
Q

What is Inheritance?

A

Passing down state and behavior from one class to another

66
Q

What are four characteristics of Inheritance and an example of how inheritance looks?

A
  1. Has Super-class / sub-class relationship
  2. It is a parent-child relationship
  3. It facilitates code reusability
  4. It allows for extensibility to change existing classes

Example:

class Cucumber {}

class Pickle extends Cucumber {}

67
Q
  1. How many superclasses can an Object have?
  2. How many superclasses can a class have?
  3. If there is no explicit superclass, what does a class become the subclass of?
A
  1. None
  2. Only one direct superclass (single inheritance)
  3. Every class is then implicitly a subclass of Object
68
Q

What is a descended class?

A

Classes can be derived from classes that are derived from classes that are derived from classes and so on, and ultimately derived from the topmost class, Object. The class that is descended from all the classes in the inheritance chain stretching back to the object is the descended class.

69
Q

What does a subclass inherit from its superclass?

A

All the members which include the fields, methods, and nested classes.

70
Q

What does a subclass not inherit from its superclass?

A

Constructors are not members, and therefore not inherited by subclasses, but the constructor of the superclass can be invoked from the sublcass

71
Q

Are all classes in the java platform descendants of Object?

A

Yes

72
Q

True or False:

  1. The inherited fields can not be used directly, unlike all other fields…
  2. Though not recommended, you can declare a field in the subclass with the same name as the one in the superclass, thus hiding it…
  3. You can declare new fields in the subclass that are not in the superclass…
  4. The inherited methods need to be modified before they can be used…
  5. You can not write a new instance method in the subclass that has the same signature as the one in the superclass…
  6. You can write a new static method in the subclass that has the same signature as the one in the superclass…
  7. You can not declare new methods in the subclass that are not in the superclass…
  8. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super…
A
  1. False
  2. True
  3. True
  4. False, they can be used directly
  5. False (new method will override the method in the superclass)
  6. True (this hides method in the superclass)
  7. False, you can declare new methods in the subclass that are not in the superclass
  8. True
73
Q

If your method overrides one of its superclass’s methods, how can you invoke the overridden method?

A

By using the keyword super

Example:

public class Superclass {

public void printMethod() {

System.out.println(“Printed in Superclass.”);

}

}

Here is a subclass, called Subclass, that overrides printMethod():

public class Subclass extends Superclass {

// overrides printMethod in Superclass

public void printMethod() {

super.printMethod(); <==== PAY ATTENTION TO THIS!!!

System.out.println(“Printed in Subclass”);

}

public static void main(String[] args) {

Subclass s = new Subclass();

s.printMethod();

}

}

74
Q

What keyword do you use to invoke a superclass’s constructor?

A

super(); or super(parameter list);

75
Q

What is constructor chaining?

A

If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, there will be a whole chain of constructors call, all the way back to the constructor of Object.

76
Q

How can you refer to any member of the current object from within an instance method or a constructor?

A

By using the “this” keyword.

77
Q

Use the this keyword to call another constructor in the same class below:

public class Rectangle {

private int x, y;

private int width, height;

A

public Rectangle() {

this(0, 0, 1, 1);

}

public Rectangle(int width, int height) {

this(0, 0, width, height);

} public Rectangle(int x, int y, int width, int height) {

this. x = x;
this. y = y;
this. width = width;
this. height = height;

}

}

78
Q

Variable Shadowing

  • Variables can be shadowed
  • Parent defines variable x and so does Child
  • Child variable x shadows the Parent variable
  • Variables can be distinguished super and this
  • Local variables can shadow instance variables

If you have a Parent with int x = 10;

and

A Child with with int x = 5;

  1. How do you print Parent variable?
  2. How do you print the Child variable?
A
  1. super.x
  2. this.x
79
Q

Constructor Chaining

  1. What does every class in Java inherit from?
  2. What is implied if you don’t define a superclass?
  3. What are the two options the first line of a constructor must be a call to?
  4. Does calling a constructor cause a chain of constructors to fire?
A
  1. java.lang.Object
  2. extends Object is implied
  3. super() or this()
  4. Yes
80
Q

Fill in the Blank:

class Example {

public static void main(String[] args) {

Example ex = new Example (“Java”);

}

public Example () {

System.out.println(“Hello World”);

}

public Example(String word) {

WHAT GOES HERE TO PRINT “Hello World” TO THE SCREEN?

}

}

A

this();

81
Q

Complete the code to inherit the state and behavior from the Cucumber class into the Pickle class.

class Pickle ______ Cucumber {

}

A

extends

82
Q

Complete the code to store the value of the parameter into the instance variable.

class Child extends Parent {

int x = 5;

public void setX(int x) {

________ <=== Fill in the correct code here

}

}

A

this.x = x;

83
Q

Abstraction:

  • Building partial or incomplete classes (shapes, for example, are hard to define)
  • Create a foundation for other classes (but we can define the area the shape will take up, for example, double area ())
  • Generalizations group shared functionality into a parent
  • Specializations extend them to further define behavior
  1. Abstraction is defined as what?
  2. What do we use to do abstraction in Java?
A
  1. Creating classes without knowing how they’re to be implemented
  2. Abstract classes and interfaces
84
Q
  1. Can an abstract class include abstract methods?
  2. Can an abstract class be instantiated?
  3. Can an abstract class be subclassed?
  4. How is an abstract method declared?
  5. If a class includes abstract methods, what must be done with the class?
  6. When an abstract class is subclassed, does anything have to be done with the class?
  7. If a method in an interface is not declared or static, then what are they classified as?
  8. Is the abstract modifier used with interface methods?
A
  1. It can include or not include abstract methods
  2. Abstract classes cannot be instantiated
  3. Abstract classes can be subclassed
  4. Abstract methods are declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double detaX double deltaY);
  5. When a class includes abstract methods, then the class itself must be declared abstract, as in: public abstract class GraphicObject {}
  6. No, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
  7. Then they are implicitly abstract
  8. No, abstract modifiers are not used with interface methods.
85
Q

When do you use abstract classes versus?

When do you use interfaces?

A

Abstract Classes:

  • You want to share code among several closely related classes.
  • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
  • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

Interfaces:

  • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
  • You want to take advantage of multiple inheritance of type.
86
Q

True or False:

  1. Abstract classes are not partially built classes…
  2. Methods defined abstract do need a method body…
  3. May have zero to many abstract methods…
  4. Class does not have to be declared abstract to allow abstract methods…
  5. Abstract classes cannot be instantiated…
  6. Abstract classes cannot extend other abstract classes…
A
  1. False (they are partially built classes)
  2. False (they do not need a method body)
  3. True
  4. False (they must be declared)
  5. True
  6. False (they can extend other abstract classes)
87
Q

Interfaces

  1. Are interfaces purely 100% abstract?
  2. What do interfaces define that classes can follow?
  3. How do APIs utilize interfaces?
  4. Interface methods are implicitly what?
  5. Can classes implement more than one interface?
  6. Abstract class defines what versus what an interface defines?
A
  1. Yes they are
  2. They define a Contract
  3. They establish a Behavior for an API
  4. public abstract
  5. They can implement many interfaces
  6. Abstract class defines => What it is, while Interface defines => What it does.
88
Q

What does an interface declaration consist of including an example?

A
  • modifiers
  • the keyword interface
  • the interface name
  • a comma-separated list of parent interfaces (if any)
  • and the interface body

For example:

public interface GroupedInterface extends Interface1, Interface2, Interface3 {

double E = 2.718282;

void doSomething (int i, double x);

int doSomethingElse(String s);

}

89
Q

Interface Body

  1. What can the interface body contain?
  2. What is an abstract method within an interface followed by?
  3. What is an abstract method within an interface not followed by?
  4. Default methods are defined by what?
  5. Static methods are defined by what?
  6. Are all abstract, default and static methods in an interface implicitly public? Which means you can omit what?
  7. Can interfaces contain constant declarations?
  8. Constant values defined in an interface are implicitly what? Which means you can omit what?
A
  1. abstract methods, default methods, and static methods
  2. semicolon
  3. braces (an abstract method does not contain an implementation)
  4. default modifier
  5. static keyword
  6. Yes. You can omit the public modifier
  7. Yes
  8. public, static, and final. You can omit these modifiers.
90
Q

Complete the code so that the class compiles

interface Drawable {

public void draw();

}

class Rectangle ______ Drawable {

public void draw() {}

}

A

implements

91
Q

Complete the code so that the class compiles

______ class Shape {

abstract double area();

}

A

abstract

92
Q

Polymorphism:

  1. Is it the ability for objects to be used strictly as one type?
  2. Does polymorphism allow for covariance and virtual method invocation?
  3. Do covariants have to pass the HAS-A check?
A
  1. False. It is the ability for objects to be used flexibly
  2. Yes or True
  3. No or False. Covariant assignment must pass the IS-A check
93
Q

Method Overriding:

  • If your method signature in the child class is the same as the signature in the parent class, is that method overriding?

class Animal

void speak() {}

class Dog

void speak() {}

  • Method overriding stops us from changing behavior in a specialized class?
  • Will the right method be called, no matter the reference type and why?
  • Overriden method doesn’t take precedence over superclass method?
A
  1. Yes
  2. False. The point of method overriding is allowing us to change behavior in a specialized class
  3. Yes, because of Virtual Method Invocation
  4. False. Method overriding does take precedence over the superclass method, but not that only non-static methods can be overriden.
94
Q

In method overriding, if you don’t have the same method name and parameter, is it still method overriding?

A

No. If you have the same method name, but a different parameter, that is method overloading.

95
Q

Fill in the correct code for Abstraction:

abstract class Furniture{}

class Chair extends Furniture{}

public class Store {

public static void main(String[] args) {

_____ chair = new _____

}

}

A

Chair

Chair();

96
Q

Complete the code so that it compiles and prints Meow Bark Meow

public class Kennel {

public static void main(String[] args) {

pets[0] = ____

pets[1] = ____

pets[2] = ____

for (Animal a : pets) {

a.speak();

}

}

}

abstract class Animal{

abstract void speak();

}

class Dog _____ _____{

void speak() {

System.out.printIn(“Bark”);

}

}

class Cat _____ _____ {

void speak() {

System.out.printIn(“Meow”);

}

}

A

new Cat();

new Dog();

new Cat();

extends Animal

extends Animal

97
Q

Complete the code so that the application prints “p”:

public class Override {

public static void main(String[] args) {

Poppable obj = ____ ____

obj.pop();

}

}

class Poppable {

public void pop() {

System.out.printIn(“Pop”);

}

}

class Balloon extends Poppable {

public void pop() {

System.out.printIn(“POP!”);

}

}

class Bubble extends Poppable {

public void pop() {

System.out.printIn(“p”);

}

}

A

new Bubble();