COMP SCI EXAM 1

studied byStudied by 4 people
5.0(1)
Get a hint
Hint

Object

1 / 174

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

175 Terms

1

Object

a software bundle of related state and behavior often used to model real life behavior.

have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such as wagging their tail, barking, eating. It is an instance of a class

New cards
2

Class

a template/blueprint that describes the behavior/state that the object of its type supports.

New cards
3

Method

basically a behavior. It is a chunk of code that can be called upon or invoked to perform a set of actions. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed

New cards
4

Instance variable

Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables. HAs separate copies created for every object.

New cards
5

UML

Notation used to depict objects and to illustrate OO concepts

New cards
6

interface

a contract between objects on how to communicate with each other

New cards
7

Encapsulation

objects encapsulate expertise, attributes and actions that it needs to carry out its role

New cards
8

Information hiding

objects can be designed to hide certain info and implementation details from other objects

New cards
9

Generality

objects are designed as general as possible so it can be applied to multiple similar problems

New cards
10

extensibility

designed to potentially be extended to perform a certain task

New cards
11

Literal value

A value expressed as itself, they are constant. EX: int number = 20;

New cards
12

Case sensitivity

Java is sensitive, which means that Hello and hello are different

New cards
13

Class names

1) First letter should be upper case

2) If several words are present, each inner word’s first letter should be uppercase

EX: public class MyFirstJava

New cards
14

Method names

1) Starts with lower case letter

2)if several words, then each inner word’s first letter needs to be uppercase

EX: public void myMethodName(parameters) {

}

New cards
15

Program file name

1) NEEDS TO MATCH class name , it won’t compile otherwise

2) add .java to the end

3) If public isnt included in the class name then the file can have a different name

New cards
16

main method

public static void main (String[] args) {

}

New cards
17

Identifiers

Names used for classes, variables, and methods

New cards
18

Rules of identifiers

  1. All should begin with a letter (A-Z), currency characters ($), or an underscore(_)

  2. after the first character, there can be any combination of characters

  3. A key word cannot be used

  4. CASE SENSITIVE

  5. EX legal: age, $salary, _value

  6. EX illegal: 123abc, -salary

New cards
19

modifiers

modifies classes and methods

New cards
20

Acess modifiers

default, public, protected, private

New cards
21

Non access modifiers

final, abstract, strictfp

New cards
22

Final

keyword used to define a constant value or method/classes. Its a non-access modifier applicable only to a variable, a method or a class. Once declared the variable can’t be changed when using this keyword.

New cards
23

Extends

Keyword used to inherit the properties of a class

New cards
24

package

Set of classes and interfaces grouped together

New cards
25

static

access modifier and keyword used to create variables that will exist independently of any instances created for the class. The method can be called without using an individual object. Belongs to the class rather than an instance. It is only applicable for methods and variables . Only ONE object which is shared by every object of the class.

New cards
26

Inheritence

Allows you to reuse fields and methods of an exisitng class w/o having to rewrite the code. Classes can be derived from classes. Basically if you need to create a new class and you already have a class that has some of the code you require, then it is possible to derive your new class from the already existing code

New cards
27

Superclass

existing class

New cards
28

Subclass

class made from superclass

New cards
29

Types of variables

local, class (Static), instance(non-static)

New cards
30

Variables

named storage units that the program can manipulate. Each has a specific type, which determines the size and layout of the variables memory

Declare: int a,b,c;

Initialize: a = 2, b = 4, c =5

New cards
31
<p>Local</p>

Local

declared in methods. CANNOT have access modifiers

New cards
32

Instance variables

Declared in a class but outside of a method. Created when an object is created with the use of the keyword new and are destroyed when the object is destroyed

They hold values that may be referenced by one or more methods

They can have access modifiers and be declared in the class level before or after use

They have default values

They can be accessed directly by calling the variable name inside the class

Within static methods(if they are given accessibility) they should be called using the full name

Default values:

numbers - 0 , booleans - false, objects - null

New cards
33

Class/Static variables

labeled with the static keyword in a class but outside of a method

There is only ONE copy of each variable per class, regardless of how many objects are created from it.

Created when the program starts and destroyed when the program stops

New cards
34

Data types

primitive and non primitive

New cards
35

primitive data types

byte

short

char

int

long

float

double

boolean

New cards
36

byte

8 bit

New cards
37

short

16 bit

New cards
38

char

16 bit

New cards
39

int

32 bit

New cards
40

long

64 bit

New cards
41

float 32 bit

New cards
42

double

64 bit

New cards
43

Boolean

true/false

New cards
44

Non primitive

Complex data types that store collections of values in various formats rather than just a stingle value

String

Array

Classes

New cards
45

Array

Structures that store multiple values of the same type in a single variable

declaration

int [] arrayName; → ont can be replaced with other data types

arrayName = new int[Array length];

New cards
46

setting and getting an element of an array

int[] numbers = new int [5];

numbers[0] = 10; //setting first element

int firstElement = numbers[0]; //getting first element

New cards
47

Getting length of an array

int length = arrayName.length;

New cards
48

Numeric promotion

converting one type to another type

New cards
49

Type promotion

the smaller type is converted to the larger type before the operand is performed

The smallest value taken is an int. Byte/short will become an int

New cards
50

implicit type conversion

allowed in cases where no information will be lost (narrower to wider) → small to larger, done automatically by Java

New cards
51

Cast operator

Must be used when converting a wider type into a narrower type. This is explicit casting. doing this manually/explicitly, making a larger type become a smaller type

New cards
52

preincrement

++k The operation is done before the operans value is used

EX: k = k + 1; j = k; OUTPUT k = 1, j = 1

New cards
53

postincrement

k++ the operation is done after the operand value is used

EX: j = k; k + 1; OUTPUT k = 1, j = 0;

New cards
54

predecrement (same as preincrement)

-- k

k = k -1; j = k; OUTPUT k = -1, j = -1

New cards
55

Postdecrement (same as postincrement)

k- -

j = k; k - 1; OUTPUT k = -1, j = 0;

New cards
56

Public

An access modifier used for attributes, methods, and constructors, making them accessible from any class

New cards
57

private

access modifier used for attributes, methods, and constructors, making them only accessible within the declared class

New cards
58

Formatting numbers

%n→ names a new line, %07 makes seven spaces where any extra space is a 0. The comma , is used like a decimal

New cards
59

Method overloading

technique used in OOP where multiple methods can have the same name but they need different parameters

New cards
60

Unary vs Binary operations

Unary operation is between one operand like increment/decrement

Binary operation is between two operands, +, - , *, /, and, or

New cards
61

Representation

the objects/characteristics

New cards
62

action

methods/behvaior

New cards
63

class constant

A field that is declared with the static and final keywords, They cannot be changed

New cards
64

class variable

a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist

New cards
65

instance variable

each class has its own copy

New cards
66

Helper method

USed to perform particular repetitive tasks common across multiple classes. A function that is used to assist the main funcion

New cards
67

class method

methods declared within a class, perform specific operations

New cards
68

operator overloading

allows operators to be redefined and used in a way where they have a different meaning based on their operands

New cards
69

method overloading

having two or more methods in a class with the same name but different parameters/arguments

New cards
70

method call

block of code that only runs when it is called

New cards
71

Type conversion/type casting

convert one data to another, this has to be done MANUALLY

New cards
72

Type promotion

Dont naturally, smaller → bigger type

New cards
73

modularity

dividing a program into smaller parts

New cards
74

float is the same as

long

New cards
75

byte is the same as

short

New cards
76

Class Hierarchy

tree like

has one root class, called Object , which is superclass of any class

  • Each class below the Object class is a subclass (child) of its parent class and can have its own subclasses.

  • Subclasses inherit the characteristics (fields and methods) of their parent classes, allowing them to reuse code and create specialized classes based on the more general ones.

New cards
77

Three types of comments in java

Single-Line Comments for brief notes, Multi-Line Comments for detailed explanations or disabling code, and Documentation Comments for generating JavaDoc.

// single

/* multiline */

/** javadoc

*@Author

*@Param

*@return

*/

New cards
78

Consider the following code

public class MainClass {

public static void main(String[] args) {

System.out.println(args[2]+","+args[0]); S

ystem.out.println(args[2] + ", " + args[0] + " "+args[1]);

System.out.println(args[0]); } //end of main method

}

Name of the file - > MainClass

How to compile and run in CD

  1. javac filename,java

  2. java filename

output if it takes the arguments Alan M.Turin

  1. Turin, Alan

  2. Turin, Alan M

  3. Alan

What does the code do

Reads the index of the argument and prints them whith different delimeters like , and space shown by ““

New cards
79

Math package

java.lang.Math

all methods in the math class are static so you can call them directly like

Math.max(24, 9 , 3, 22);

Using static import in the header class will allow you to invoke the methods and use the fields without writing the class name

New cards
80
<p>While loops</p>

While loops

Used when you dont know how many times youre going to loop through.

Will only enter the body if the condition is met

New cards
81

for loops

used when you do know how many loops you want to do.

for(initialization; continuation statement; update)

New cards
82
<p>do - While</p>

do - While

will always enter the body of the loop and then will check the condition

New cards
83

infinite loop

will run continuously

for(;;){

}

while(true){

}

New cards
84
<p>break</p>

break

terminates any loop

New cards
85
<p>continue</p>

continue

skips the current iteration and continues with the next one

New cards
86

Ternary operator

Works like an if else statement but is more compact

variable = expression ? statement1 : statement2

str = (num > 0,0) ? “Positive” : “Negative”;

New cards
87

Create Array

data_type[] variableName = new data_type[Length];

or

data_type[] variableName = {Comma seperated literal values}

New cards
88

Elements of an array

anArray [i-1]

New cards
89

Elements of anArray are indexed from

0 to (# of elements - 1)

New cards
90

Limitations of arrays

only one type

Fixed size

New cards
91

Off- by -One error

When you are off by one index.

It will create an ArrayOutofBoundsException

New cards
92

value semantics

behavoir where values are copied when assigned, passed as parameters or returned

FOR primitive types

New cards
93

reference semantics

Behvaior where variables actually store the address of an object in memory

FOR objects

New cards
94

Aliasing

having more than one reference to the same object

New cards
95

Traversing an array

int[] data = {2,4,6,8,10};

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

System.out.println(data[i]);

}

New cards
96

Array manipulation

java.util.Arrays

copyFrom, indexFrom, indexTo-exclusive

New cards
97

Two arrays are equivalent in values if they have the same

length and same sequence of values

New cards
98

Test if two arrays are equal

.equals

New cards
99

Which of the following would return a random number from 1 to 5 inclusive? - Choose all that apply.

A. (int) (Math.random() 5 )

B. myRandom.nextInt(5) + 1

C. (int) (Math.random() 6 )

D. (int) (Math.random() 5 ) + 1

E. (int)Math.random()5+1

B & D

New cards
100

String

String myStr = “NEW STRING”;

String variableName = new String(“My new String”);

The string class is immutable

String methods return new String objects

New cards

Explore top notes

note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 12 people
... ago
5.0(1)
note Note
studied byStudied by 9 people
... ago
5.0(1)
note Note
studied byStudied by 120 people
... ago
5.0(1)
note Note
studied byStudied by 15 people
... ago
5.0(1)
note Note
studied byStudied by 95 people
... ago
5.0(1)
note Note
studied byStudied by 69 people
... ago
5.0(2)

Explore top flashcards

flashcards Flashcard (44)
studied byStudied by 6 people
... ago
4.0(1)
flashcards Flashcard (54)
studied byStudied by 2 people
... ago
5.0(1)
flashcards Flashcard (144)
studied byStudied by 16 people
... ago
5.0(1)
flashcards Flashcard (32)
studied byStudied by 21 people
... ago
4.0(1)
flashcards Flashcard (25)
studied byStudied by 3 people
... ago
5.0(1)
flashcards Flashcard (64)
studied byStudied by 242 people
... ago
5.0(1)
flashcards Flashcard (41)
studied byStudied by 1768 people
... ago
4.9(14)
flashcards Flashcard (57)
studied byStudied by 12 people
... ago
5.0(1)
robot