Final practice

studied byStudied by 37 people
5.0(2)
get a hint
hint

What is a Class?

1 / 105

Studying Progress

0%
New cards
106
Still learning
0
Almost done
0
Mastered
0
106 Terms
1
New cards

What is a Class?

A blueprint that defines an objects variables and methods.

New cards
2
New cards

What do classes contain?

instance variables to store data and method definitions that perform actions

New cards
3
New cards

Each class, instance variable, and method can be defined as what?

Public or Private

New cards
4
New cards

What does it mean for an instance variable, or method to be defined as public?

They can be used or accessed anywhere by name outside it's class

New cards
5
New cards

What does it mean for an instance variable, or method to be defined as private?

They cannot be accessed by name outside it's class

New cards
6
New cards

Can an instance variable that is defined as private be accessed or used within the definition of methods in its class?

Yes

New cards
7
New cards

Can a private method definition be invoked outside it's class.

no, but it can be called within the definitions of methods in its class

New cards
8
New cards

What is a Instance Variable?

Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.

New cards
9
New cards

What should instance variables be defined as?

Private

New cards
10
New cards

Why should instance variables be defined as private?

To restrict access to read only, and keep the original information of it the same

New cards
11
New cards

What are accessor methods, and what are they used for?

Accessor methods, or getter methods, are methods that let you print or use the values for your private instance variables indirectly. They are return type methods than contain no input parameters and only return the value of the instance variable

New cards
12
New cards

What are mutator methods, and what are they used for?

setter methods that are used to change the values of private instance variables outside of the class. They are void methods that contain an input parameter at which the method body assigns the argument value to the instance variable

New cards
13
New cards

What is the algorithm and implementation for Bogo sort?

a continual random shuffling of the data set until it is sorted correctly. This sorting technique is highly inefficient and not recommended.

New cards
14
New cards

What is the algorithm and implementation for Bubble sort?

comparing two adjacent elements in a data set and swapping their values based upon the sorted output they wish to achieve. Once a pair of adjacent elements are sorted, you move on to the next pair and do not go back to the previous pair as it is already sorted. You continue to compare each of the adjacent elements until the entire data set is sorted.

New cards
15
New cards

What is the algorithm and implementation for Selection Sort?

starts at the beginning index and starts at the beginning index and compares that element to the next minimum or maximum value it can find in the entire data set. If it is found, then that minimum or maximum value is swapped with the current element and now their two indices store the swapped values. This continues until the entire data set is sorted.

New cards
16
New cards

What is the algorithm and implementation for Insertion Sort?

starts at the second index and compares it to the previous indices and swaps their values accordingly. Essentially, Insertion Sort continues to move to the next index and checks the previous indices values and swaps accordingly making it more effective then the previous other sorting mechanisms.

New cards
17
New cards

Why are sorting algorithms important in computer science?

Sorting algorithms are important in computer science because they allow any data set to be organized in a specific order making it easier to read, understand, and access certain elements from.

New cards
18
New cards

What does the keyword "this" do?

it is used to refer to the specific instance variable or object for the compiler to not get confused with a same named parameter.

New cards
19
New cards

What is a local variable?

Variables defined inside methods, constructors or blocks. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.

New cards
20
New cards

Arguments in a method invocation must match the formal parameters in the method heading with respect to what?

Their number, order, and data types

New cards
21
New cards

Methods can have parameters of what

A primitive type and/or of a class type

New cards
22
New cards

how are primitive data types stored

store the variable value on the same memory in the stack as where their variable has been assigned to

New cards
23
New cards

how are class type variables stored

Class type variables only store the references to the memory location of the value that is being assigned to

New cards
24
New cards

What is the arithmetic operator?

operators that perform mathematical calculations such as +, -, *, /, %

New cards
25
New cards

What are the unary operators?

operators that need only one operand to perform any operation like increment, decrement, negation, etc. It consists of various arithmetic, logical and other operators that operate on a single operand

New cards
26
New cards

what's the difference between how primitive type variables are stored compared to class type variables

primitive type variables directly store their values while class types store the reference of their values

New cards
27
New cards

What are access modifiers?

Access modifiers specify how instance variables and methods can be accessed outside of the class definition

New cards
28
New cards

Can the body of a void method contain the keyword return

The body of a void method can The body of a void method can contain the keyword return that cannot be followed by a returning expression afterwards. Essentially the only use for a return statement in a void method is to stop the execution of the method

New cards
29
New cards

List two access modifiers that were covered this semester

  • Public access modifier lets a method or instance variable freely be accessed directly by an object of a class outside of the parent class.

  • Private access modifier restricts a method or instance variable from being accessed directly by an object of a class outside of the parent class. However you can still freely use private instance variables and private methods within the parent class.

New cards
30
New cards

What are the advantages and disadvantages of linear search?

it can be used to search elements of an unsorted array easily. A disadvantage of linear search is that it is inefficient and takes a lot of time to search through an array for a specific element

New cards
31
New cards

Relational Operators

Used to compare two values. Operators include

==,
New cards
32
New cards

logic operators

Used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular considerations

AND(&&), OR(||), NOT(!)

New cards
33
New cards

ternary operator

? :

Java ternary operator is the only conditional operator that takes three operands. It's a one-liner replacement for the if-then-else statement and is used a lot in Java programming

New cards
34
New cards

What is a block?

a list of statements surrounded by braces

New cards
35
New cards

scope of a variable

The part of a program where a variable is visible and can be used by the code.

New cards
36
New cards

What does an object of a class consist of?

  • A state represented by attributes,

  • A behavior represented by methods,

  • A identity represented by a unique name

New cards
37
New cards

reference variable

stores the memory address of an object

New cards
38
New cards

What are the reverence types?

Classes, Interface, Arrays, Enumerations, and annotations

New cards
39
New cards

What is an object?

an instance of a class

New cards
40
New cards

What value will a reference variable store if no object is passes to a reference variable?

Null

New cards
41
New cards

What does UML stand for?

Universal Modeling Language

New cards
42
New cards

Why do programmer use UMLs?

It portrays a diagram of behaviors and structure for a program, especially used in object oriented programming (OOP). It saves time because it's easy to visualize complex structures of code.

New cards
43
New cards

What is a Jar file?

a JavaARchive. A jar file is a group of Java classes and supporting files combined into a single file compressed with ZIP format, and a given.jar file extension. It can be used to compress multiple java files in one file that is easier to download and takes less time to execute.

New cards
44
New cards

What command would you use to run a class file in a jar?

java -cp file_name.jar class_file_name

New cards
45
New cards

What does the keyword 'Static' mean?

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

New cards
46
New cards

What command would you use to create a jar?

jar cvf file_name.jar file(s)

New cards
47
New cards

What are static blocks?

A block of code that gets executed exactly once, when the class is first loaded.

New cards
48
New cards

What are static variables

variables that do not require an object to be created to call them outside of the class. They can be called simply by the class_name.method. these methods can use other static variables and methods. They cannot directly use non-static variables and methods in their body. But rather need an object to be created for that class to use non-static fields

New cards
49
New cards

What are static methods

a behavior that can be called anywhere in the class and outside the its class without invoking its class object.

New cards
50
New cards

What are static classes

allow you to use its members without instantiating it. they can be accessed directly using its class name as opposed to an object variable. it can only contain static members and they are inherently sealed. These are also called nested static classes.

New cards
51
New cards

Why are instance variables typically used in equals methods for a class, but the class variables are not used?

a class variable (static variable) is shared everywhere, so the there would be no point in comparing the class variable among each other since they all have the same values.

New cards
52
New cards

what is a method

a coordinated sequence of instructions that will be carried out when invoked

New cards
53
New cards

What do objects contain

All methods and attributes from that specific class

New cards
54
New cards

what are what are constructors

Special methods that have the same name as the class. They are used when we initialize a class. The have the same name as the class.

New cards
55
New cards

What are overloaded constructors

other constructors that can require a different number of or different type of parameters and serves for different purposes.

New cards
56
New cards

what is a reference

A memory location that is used to store objects information and other class type variables

New cards
57
New cards

what is an array

a homogenous structure that can be used to store multiple variables in a single collection. All variables stored must be of the same data type and are stored in valid indices in the array

New cards
58
New cards

what is a two dimensional array

An array of single dimensional arrays.It can have infinitely many rows and columns. The length of a two dimensional array gives the total number of rows in the array.

New cards
59
New cards

what is a three dimensional array

an array of two dimensional arrays. It can have infinitely many 2D arrays within itself all with varying numbers of rows and columns.

New cards
60
New cards

what command would you use to compile a java file

javac file_name.java

c:> javac pack.java

New cards
61
New cards

what command would you use to run a java file

java file_name

c:> java pack

New cards
62
New cards

What is encapsulation?

The process of creating a class in a way that the user and other programmers using the class do not know the whole functionality required in building and implementing the specific class but rather know how to use the intended class

New cards
63
New cards

Why is encapsulation important?

it simplifies the job of the programmer who uses encapsulated software to write more software

New cards
64
New cards

What are the 4 attributes of a well-encapsulated class?

  • Declare all instance variables in a class as private.

  • Make any helping methods private

Write comments within the class. - definition to describe implementation details

  • Place a comment before each public method heading that fully specifies how to use the method

New cards
65
New cards

what is a single dimensional array

a type of array that has only 1 row but can have infinitely many columns. The length the array defines the total number of columns in the array. Valid indices range from 0 to length-1.

New cards
66
New cards

How would you declare and initialize a two dimensional integer array that has two rows and three columns, without the use of the "new" operator?

int [ ] [ ] numbers = {{1,2,3},{4,5,6}};

New cards
67
New cards

How would you declare and initialize a two dimensional array that has two rows and three columns, using the "new" operator?

int [ ][ ] numbers;

numbers = new int [2][3];

New cards
68
New cards

What is a ragged array?

a multidimensional array that does not have the same number of columns per row, i.e. not a rectangular table in memory

New cards
69
New cards

what is a partially filled array

an array in which all of its elements are not being used and those elements are not holding the values the programmer assigned a valid to but rather hold the default values set by the compiler

New cards
70
New cards

how would you write a ragged array of strings with two rows, where the first row has three elements and the second row has two elements, using the "new" operator?

String [ ][ ] bruh = new String [2][ ];

bruh[0] = new String [3];

bruh[1] = new String [2];

New cards
71
New cards

What kind of array is this:

int [ ] numbers = new int [5];

numbers[0] = 2;

numbers[1] = 3;

numbers[2] = 5;

A partially filled array, since the last two indices are currently holding no values assigned by the user and thus are defaulted by the compile

New cards
72
New cards

what kind of array is this:

String [ ][ ] car = new String [3][ ];

car [0] = new String [5];

car [1] = new String [2];

car [3] = new String [1];

a ragged array, since each row has different numbers of columns

New cards
73
New cards

what is a rectangular array?

A multidimensional array that has the same number of columns for each of the rows

New cards
74
New cards

what kind of array is this:

int [][] test = new int [4][5];

a rectangular array, since each row will have the same number of columns

New cards
75
New cards

Which variables are assigned first in a java class?

Instance variables

New cards
76
New cards

What is a class variable?

It is basically a static variable that can be declared anywhere at class level with static. Across different objects, these variables can have only one value. These variables are not tied to any particular object of the class, therefore, can share across all objects of the class.

New cards
77
New cards

What kind of variable is this:

class Taxes

{

static int count;

}

a class variable

New cards
78
New cards

class Taxes

{

int count;

//or

private int count;

}

a instance variable

New cards
79
New cards

Are all instance variables private or public by default?

private

New cards
80
New cards
New cards
81
New cards

what is a cloned object?

is essentially a deep copy of an object implies a different object (referencing a different memory address) from another object, but the different object has the same values as the other object

New cards
82
New cards

what kind of object is wassup?

String [ ] yo = {"hi","hi","hi"};

String [ ] wassup = {"hi","hi","hi"};

a cloned object

New cards
83
New cards

what is a shallow copy

is when two objects refer to the same memory location. It essentially gives two different names for one object

New cards
84
New cards

what kind of object is bob?

int [ ] numbers = {1,2,3};

int [ ] bob = {4,5,6,7};

bob = numbers;

bob[0] = 3;

a shallow copy

New cards
85
New cards

how do you clone an array or object in java?

int [ ] numbers = {1,2,3};

int [ ] bob = new int [numbers.length];

for(int i = 0; i < bob.length; i++)

{

bob[i] = numbers[i];

}

New cards
86
New cards

How do static and non-static variables differ?

Static variables belong to a specific class and not to specific objects like instance variables. This implies that all objects for a class share the same copy of the static variable. A change in value you make to a static variable will be reflected across all objects rather than the specific object.

New cards
87
New cards

what are non-static methods

a behavior that require an object to be created to invoke them outside of the class. These methods can directly use both non-static and static variables and methods within their class without requiring the need to create an object of that class

New cards
88
New cards

What is a package?

a collection of classes in a single folder. It used to better organize files to related folders to be potentially be imported or used directly by a class all or any specific file in the package

New cards
89
New cards

Implement the method:

public static boolean contains( char element, char [ ] array) { }

using a for loop that doesn't use a colon, " : ", operator

public static boolean contains(char element, char [] array)

{

for(int i = 0; i < array.length; i++)

{

if(element == array[i])

{

return true;

}

}

return false;

}

New cards
90
New cards

Implement the method:

public static boolean contains( char element, char [ ] array) { }

using a for-each loop that uses a colon, ":", operator

public static boolean contains(char element, char [] array)

{

for(char character: array )

{

if(element == character)

{

return true;

}

}

return false;

}

New cards
91
New cards

Implement the method:

public static boolean contains( char element, char [ ] array) { }

using a while loop

public static boolean contains(char element, char [] array)

{

for(char character: array )

{

if(element == character)

{

return true;

}

}

return false;

}

New cards
92
New cards

How are set methods different from constructors?

Constructors are meant to solely initialize all instance variables. Constructors are different from set methods in that they initialize values to all instance variables at the same time as they create objects of that class. Also constructors cannot be invoked frequently like set methods. They can only be invoked at the creation of another object at which the instance variables are set initial values. They change the values of all instance variables throughout the program. Use a set method, when you want to change a specific instance variable value frequently throughout the program as they can be called as many times as you like without creating any object each specific time.

New cards
93
New cards

Implement the method:

public static boolean contains( char element, char [ ] array) { }

using a while loop

public static boolean contains(char element, char [] array)

{

int index = 0;

while(index < array.length)

{

if(element == array[i])

{

return true;

}

index++;

}

return false;

}

New cards
94
New cards

How are set methods similar to constructors

They both can contain parameters

New cards
95
New cards

What is a class path

The location(s) in which Java looks for class files. Can include the current "working directory" from which you ran javac/ java, others folders, URLs

New cards
96
New cards

what does the "new" operator do?

it is used to create objects and arrays and allocates their memory on the heap

New cards
97
New cards

what does the "assert" operator do?

it states the current program logic and whether it is correct or not. If the assert returns true, then the program logic is correct. It must be turned on the ide you are using

New cards
98
New cards

what does the "continue" operator do?

a special statement that skips the rest of the body of the loop when encountered and move on to the next iteration of the loop

New cards
99
New cards

what does the "return" operator do?

You can print or store the return value from the method when that method is called upon

New cards
100
New cards

What are the advantages and disadvantages of binary search?

it is much more effective than linear search as it takes log base 2 as compared to the standard number of attempts that linear search takes to search a data collection . A disadvantage is that binary search can only be used for sorted data rather than available for both unsorted and sorted like linear search.

New cards

Explore top notes

note Note
studied byStudied by 7 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 12 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 24 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 11 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 53 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 14 people
Updated ... ago
4.7 Stars(3)
note Note
studied byStudied by 7275 people
Updated ... ago
4.9 Stars(88)

Explore top flashcards

flashcards Flashcard75 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard96 terms
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard55 terms
studied byStudied by 16 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard179 terms
studied byStudied by 4 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard207 terms
studied byStudied by 11 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard69 terms
studied byStudied by 1 person
Updated ... ago
5.0 Stars(1)
flashcards Flashcard46 terms
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard464 terms
studied byStudied by 33458 people
Updated ... ago
4.1 Stars(392)