Week 2 Flashcards

1
Q

Array

A

a contiguous block of memory storing a group of sequentially stored elements of the same type.

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

What is int []

A

int array

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

int array declaration

A

int []

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

int [] count;

What is this set to if not initialized?

A

null.

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

When an array is declared, you can resize as often as you’d like. T/F?

A

False. Once an array is declared it cannot be resized.

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

How do you declare a double array?

A

double []

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

what is the difference between length and length()

A

length is for an array.
length() is for string objects.

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

What is a Package?

A

A method to encapsulate a group of classes or sub packages, and interfaces.

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

Why do we use packages?

A

prevent naming conflicts.

location and usage of classes, interfaces, enums, and annotations easier.

Packages can be considered as data encapsulation (or data-hiding)

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

explain package college.staff.cse

A

There are 3 directories total in this package.

cse is inside staff which is inside college.

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

how are packages written?

A

In the reverse way you would specify a domain name.

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

How can I pull in other classes?

A

Import statement after our package declaration.

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

What is the Scanner class used for?

A

to read user input from the command line.

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

What is an import statement?

A

An import statement improves the readability of the program by taking a class or all classes visible for a program under a package, using just a single statement.

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

Explain the following:

import package1[.package2].(*);

A

The top level package is package 1.
The subordiante-level package is package 2, under package 1.
the (*) is to import all classes from package 1 into package 2.

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

Explain the following:

import java.util.ArrayList;

A

This is an import statement that takes an Array list from the package util which is the subpackage of the package java.

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

Where is the Scanner class found?

A

At the java.util package.

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

What class is used to get user input? And where can you find the package to import it?

A

Scanner class.
java.util

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

What does the following code do?

java.util.Scanner

A

Imports the scanner class.

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

What is the following code? Explain each part.

public static void main(String[] args) {
}

A

The main method.

public = access modifier.

static means the method can be accessed straight from the class, we don’t have to instantiate an object to have a reference and use it.

void = The data type. this method doesn’t return a value.

main = the name of the method. This is the identifier java looks for when executing a Java program.

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

Explain the following code, define each part:

Scanner myObj = new Scanner(System.in); 
System.out.println("Enter username");
A

This creates a scanner object so the user can input their username.

Scanner is the class.
myObj is the object name.
new is telling java to create a new object.
Scanner is instantiating the object as a Scanner.
System.in is allowing the user to input from device.

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

Explain the following code, define each part:

String userName = myObj.nextLine();
System.out.println(“Username is: “ + userName);

A

Read user input.

String class is creating a string object called userName which is being set to equal the myObj user input. Then it is printing out the userName.

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

The three steps when creating an object from a class.

A

Declaration (creating the name, the variable part.).
Instantiation (‘new’ keyword which is used to create an object.)
Initialization (following ‘new’ keyword is a call to a constructor. This call initializes the new object)

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

What is a java constructor?

A

a special method that is used to initialize objects.

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

When is a constructor called?

A

When an object of a class is created.

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

explain keyword ‘this’

A

used to access methods of the current class.

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

What is Data Abstraction?

A

A process in which only the essential details are displayed to the user.

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

A car is viewed as a car rather than its individual components. What is this example referring to?

A

Abstraction

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

In java, abstraction is achieved by ______ and _______.

A

Interfaces.

abstract classes.

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

What is an abstract class?

A

a class that is declared with an abstract keyword.

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

what is the keyword for an abstract class?

A

abstract

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

What is an abstract method?

A

a method that is declared without implementation.

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

What does making a class ‘abstract’ do? 2 things.

A

Allows you to use abstract methods.

Prevents you from creating instances of that abstract type directly.

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

What is an interface?

A

an abstract type used to specify the behavior of a class. (blueprint of a class)

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

What does an interface contain?

A

Static constants and abstract methods.

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

Java interface can have a method body. T/F?

A

False.

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

If a class implements an interface and doesn not provide method bodies for all functions specified in the interface, then the class must be ___________.

A

declared abstract.

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

How to declare an interface?

A

use ‘interface’ keyword

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

what keyword can let us generate multiple inheritances in the case of class?

A

interface.

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

Delimiter define.

A

sequence of one or more characters for specifying the boundary between separate, independent regions in text, math, etc.

Ex. comma

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

What is garbage collection in Java?

A

the process by which Java programs perform automatic memory management. The garbage collector finds unused objects and deletes them to free up memory.

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

Another way to say in-use object in Java.

A

Referenced object.

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

Where is the garbage collection implementation in Java?

A

In the JVM (Java Virtual Machine)

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

Two types of garbage collection in Java? Describe each.

A

Minor/Incremental - when unreachable objects in the young gen heap memory are removed.

Major/Full - Objects that survived minor are copied into the old gen or perm gen heap are removed. Major/Full is less frequent.

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

List all 7 non-access modifiers.

A

static. final. abstract. synchronized. transient. volatile. native.

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

What are non-access modifiers used for in Java?

A

They are used to provide information to the JVM about the behavior of classes, methods, variables, constructors, etc.

48
Q

Static keyword. What’s it used for? What can you use static with in Java?

A

Used to share the same variable or method of a given class.

Can be used with Blocks, Variables, Methods, and Classes.

49
Q

What happens when a variable is declared static?

A

a single copy of the variable is created and shared among all objects at the class level.

50
Q

Used to share the same variable or method of a given class.

Can be used with Blocks, Variables, Methods, and Classes.

A

Static keyword. What’s it used for? What can you use static with in Java?

51
Q

a contiguous block of memory storing a group of sequentially stored elements of the same type.

A

Array

52
Q

What happens when a member is declared static?

A

it can be accessed before any objects of its class are created, and without reference to any object.

53
Q

What happens if a reference variable is declared final?

A

the reference variable can no longer be reassigned to refer to a different object.

54
Q

What can still change if a reference variable is declared final ?

A

The data within the object can be changed.

55
Q

What is a static method? What is the most common example of a static method?

A

It is a method that belongs to a class rather than an instance of a class. The main method is the most common static method.

56
Q

Explain the following code and answer…

What is the class?

What access modifiers are used?

What non access modifiers are used?

A

The main method calls the method ‘sayHelloWorld’… It then jumps down to sayHelloWorld and runs the code in the method body.

The class is ‘Methods’

Access modifier used is ‘public’

non access modifiers used are static.

57
Q

How would I call a method called ‘HelloWorld’ in Java?

A

HelloWorld();

58
Q

Explain the following code:

static void sayHello(String name) {

System.out.println(“Hello, “ + name);

A

The method ‘sayHello’ is passing through a parameter of (String name) meaning that the program will expect data to be passed with the method, in this case, a String called ‘name’.

59
Q

What are the restrictions on a static method?

A

They can only directly call other static methods.

They can only directly access static data.

They cannot refer to ‘this’ or ‘super’ in any way.

60
Q

What is ‘extends’ keyword used for in Java?

A

used to indicate that the class which is being defined is derived from the base class using inheritance.

61
Q

What is the ‘implements’ keyword used for in Java? Why is this useful?

A

Used to implement an interface.

interfaces can achieve multiple inheritances.

62
Q

used to indicate that the class which is being defined is derived from the base class using inheritance.

A

What is ‘extends’ keyword used for in Java?

63
Q

What is the abstract non-access modifier?

A

A class that is declared as abtract to indicate that this class can not be instantiated.

64
Q

Define Object class

A

This is the parent class to all classes in Java by default.

65
Q

Upcasting define.

A

casting a subtype to a super type in an upward direction to the inheritance tree.

66
Q

Difference between implicit and explicit casting

A

Implicit is casting done by the compiler without cast syntax.

Explicit is casting done by the programmer with cast syntax.

67
Q

Downcasting define.

A

When subclass type refers to the object of the parent class.

68
Q

two rules to perform class type casting.

A

Classes must be IS-A relationship.

An object must have a property of the class in which it’s going to cast.

69
Q

How to upcast a class called child to a class called parent?

A

Parent p = new Child()

70
Q

Class car extends vehicles. How would you downcast?

A

Car c = (Car) v

71
Q

What do wrapper classes do?

A

They convert primitive data types into objects.

72
Q

What is autoboxing?

A

Automatic conversion of primitive types to the object of their corresponding wrapper class.

73
Q
A
74
Q

casting a subtype to a super type in an upward direction to the inheritance tree.

A

Upcasting define.

75
Q

When subclass type refers to the object of the parent class.

A

Downcasting define.

76
Q

They convert primitive data types into objects.

A

What do wrapper classes do?

77
Q

Automatic conversion of primitive types to the object of their corresponding wrapper class.

A

What is autoboxing?

78
Q

If a class has multiple methods having the same name but different parameters, its is known as…

A

Overloading

79
Q

What is it called if the subclass has the same method as declared in the parent class in Java?

A

method overriding.

80
Q

What are the 3 rules of method overriding?

A

Method must have same name as the parent class.

Method must have the same parameter as the parent class.

There must be an IS-A relationship (inheritance)

81
Q

If an int value is so much larger than a byte value? Why would I ever use a byte value in my Java program?

A

Because if you use a smaller primitive type, you can save memory.

82
Q

Casting

A

treat or covnert number from one type to another.

83
Q

Is the String a type or a class?

A

class.

84
Q

What is the ‘!’ operator?

A

NOT or Logical Complement Operator.

85
Q

In method overloading, how do you make the method signature unique?

A

By chaning the number of parameters.

86
Q
A
87
Q

What is the root class from which every class extends?

A

The Object Class.

88
Q

Where are the strings stored?

A

In the string pool in the heap.

89
Q

Explain stack vs heap.

A

heap is where objects are stored in memory. stack is where local variable references are kept.

90
Q

What are annotations?

A

A type of syntactic metadata added to the code, read by the compiler - use @ syntax.

91
Q

What is a POJO?

What is a bean?

A
  • plain old Java object. Any Java object that you create.
  • a POJO that has private data members, public getters/setters, and overrides .hashcode(), .equals(), and .toString() methods
92
Q

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

A

Strings are immutable. Both StringBuilder and StringBuffer are mutable. Furthermore, StringBuffer is sychronized while StringBuilder is not.

93
Q

What is the difference between static and final variables?

A

static variables are shared by all the instances of objects and it has only single copy.

final variable is a constant and it cannot be changed.

94
Q

What are the default values for all data types in Java?

A

Objects - null

int, short, byte, long, float, double - 0

boolean - false

char - ‘u0000’ (null character)

95
Q

Is java pass-by-value or pass-by-reference?

A

strictly pass by value. Even when object references are passed as arguments, it is the value of the reference that is passed.

96
Q

How to pass multiple values with a single parameter into a method?

A

use varargs.

97
Q

What methods are available in the Object class?

A

.clone

.hashcode

.equals

.toString

98
Q

How would you clone an object?

A

First, tag the class with the Cloneable marker interface.

Next, invoke clone(). The clone method is declared in java.lang.Object and does a shallow copy.

99
Q

What is the difference between an abstract class and an interface?

A

An abstract class can have both concrete and abstract methods whereas an interfface must have only abstract methods.

Interface methods are implicitly public and abstract, interface variables are implicitly public, static, and final, but these do not apply in abstract classes.

100
Q

What are the implicit modifiers for interface variables?

A

public static final .

101
Q

What is the difference between method overloading and overriding?

A

Method overriding - in a subclass when one declares an identical method from the superclass, this method overrides the one in the superclass.

Method overloading - within the same class when one declares more than method with the same name but different signature (parameters)

102
Q

Can you overload / override a main method? static method? a private method? a default method? a protected method?

A

Main method - overload, cannot override b/c is static method.

Static method - overload, cannot override b/c belongs to class (not inherited).

Private method - overload, cannot override b/c doesn’t get inherited.

Default method - both.

Protected method - both (override if inherited).

103
Q

What is the difference between an ArrayList and a List?

A

An ArrayList is a class.

A list is an interface.

104
Q

Difference between extends and implements?

A

Extends is for classes.

Implements is for implementing interfaces.

105
Q

What are enumerations (enums)?

A

A special Java type that defines a collection of constants.

106
Q

What are the implicit modifiers for interface variables/methods?

A

methods - public abstract;

variables - public static final.

107
Q

First line of constructor?

A

The compiler will insert super() as the first line - it cannot be used anywhere else in constructor except for the first line.

108
Q

What is the difference between final, .finalize(), and finally?

A

final class cannot be subclassed and it prevents other programmers from subclassing.

finalize() method is used just before an object is destroyed and called just prior to garbage collection.

finally, a key wor dused ine xception handling, creates a block of cod ethat will eb executed after a try/catch block has completed and before the code following the try/catch bllock.

109
Q

throw vs throws vs throwable?

A

throw - keywword that will actually “throw” an exception in code.

throws - keyword in method signature after params that declare which exception the method might throw.

throwable - the root interface of exceptions, allow a class to be “thrown”

110
Q

Do you need a catch block? Can have more than 1? Order of them?

A

Catch block is not necessary - try/finally will compile. You can have more than one catch block, but the order must be from most narrow exception to most broad/general.

111
Q

What is base class of all exceptions? What interface do they all implement?

A

The base class is Exception, which implements the Throwable interface.

112
Q

List the checked and unchecked exceptions.

A

Checked - IOException. ClassNotFoundException. InterruptedException.

Unchecked - ArithmeticException. ClassCastException. IndexOutOfBoundsException. NullPointerException.

113
Q

What is JUnit?

A

A Java unit testing framework for testing code - use it for TDD.

114
Q

What is TDD?

A

Test Driven Development - write unit tests before application code, then write code to make tests pass. Repeat this process until functionality is complete.

115
Q

What are the annotations in JUnit? Order of Execution?

A

BeforeClass, AfterClass,Before, After, Test, Ignore

116
Q

Give an example of a test case?

A

Adding two numbers, check that the method returns the sum

117
Q
A