COMP SCI EXAM 1

5.0(1)
studied byStudied by 4 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/174

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

175 Terms

1
New cards

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

2
New cards

Class

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

3
New cards

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

4
New cards

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.

5
New cards

UML

Notation used to depict objects and to illustrate OO concepts

6
New cards

interface

a contract between objects on how to communicate with each other

7
New cards

Encapsulation

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

8
New cards

Information hiding

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

9
New cards

Generality

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

10
New cards

extensibility

designed to potentially be extended to perform a certain task

11
New cards

Literal value

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

12
New cards

Case sensitivity

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

13
New cards

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

14
New cards

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) {

}

15
New cards

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

16
New cards

main method

public static void main (String[] args) {

}

17
New cards

Identifiers

Names used for classes, variables, and methods

18
New cards

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

19
New cards

modifiers

modifies classes and methods

20
New cards

Acess modifiers

default, public, protected, private

21
New cards

Non access modifiers

final, abstract, strictfp

22
New cards

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.

23
New cards

Extends

Keyword used to inherit the properties of a class

24
New cards

package

Set of classes and interfaces grouped together

25
New cards

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.

26
New cards

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

27
New cards

Superclass

existing class

28
New cards

Subclass

class made from superclass

29
New cards

Types of variables

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

30
New cards

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

31
New cards
<p>Local</p>

Local

declared in methods. CANNOT have access modifiers

32
New cards

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

33
New cards

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

34
New cards

Data types

primitive and non primitive

35
New cards

primitive data types

byte

short

char

int

long

float

double

boolean

36
New cards

byte

8 bit

37
New cards

short

16 bit

38
New cards

char

16 bit

39
New cards

int

32 bit

40
New cards

long

64 bit

41
New cards

float 32 bit

42
New cards

double

64 bit

43
New cards

Boolean

true/false

44
New cards

Non primitive

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

String

Array

Classes

45
New cards

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];

46
New cards

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

47
New cards

Getting length of an array

int length = arrayName.length;

48
New cards

Numeric promotion

converting one type to another type

49
New cards

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

50
New cards

implicit type conversion

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

51
New cards

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

52
New cards

preincrement

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

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

53
New cards

postincrement

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

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

54
New cards

predecrement (same as preincrement)

-- k

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

55
New cards

Postdecrement (same as postincrement)

k- -

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

56
New cards

Public

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

57
New cards

private

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

58
New cards

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

59
New cards

Method overloading

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

60
New cards

Unary vs Binary operations

Unary operation is between one operand like increment/decrement

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

61
New cards

Representation

the objects/characteristics

62
New cards

action

methods/behvaior

63
New cards

class constant

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

64
New cards

class variable

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

65
New cards

instance variable

each class has its own copy

66
New cards

Helper method

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

67
New cards

class method

methods declared within a class, perform specific operations

68
New cards

operator overloading

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

69
New cards

method overloading

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

70
New cards

method call

block of code that only runs when it is called

71
New cards

Type conversion/type casting

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

72
New cards

Type promotion

Dont naturally, smaller → bigger type

73
New cards

modularity

dividing a program into smaller parts

74
New cards

float is the same as

long

75
New cards

byte is the same as

short

76
New cards

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.

77
New cards

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

*/

78
New cards

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 ““

79
New cards

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

80
New cards
<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

81
New cards

for loops

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

for(initialization; continuation statement; update)

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

do - While

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

83
New cards

infinite loop

will run continuously

for(;;){

}

while(true){

}

84
New cards
<p>break</p>

break

terminates any loop

85
New cards
<p>continue</p>

continue

skips the current iteration and continues with the next one

86
New cards

Ternary operator

Works like an if else statement but is more compact

variable = expression ? statement1 : statement2

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

87
New cards

Create Array

data_type[] variableName = new data_type[Length];

or

data_type[] variableName = {Comma seperated literal values}

88
New cards

Elements of an array

anArray [i-1]

89
New cards

Elements of anArray are indexed from

0 to (# of elements - 1)

90
New cards

Limitations of arrays

only one type

Fixed size

91
New cards

Off- by -One error

When you are off by one index.

It will create an ArrayOutofBoundsException

92
New cards

value semantics

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

FOR primitive types

93
New cards

reference semantics

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

FOR objects

94
New cards

Aliasing

having more than one reference to the same object

95
New cards

Traversing an array

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

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

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

}

96
New cards

Array manipulation

java.util.Arrays

copyFrom, indexFrom, indexTo-exclusive

97
New cards

Two arrays are equivalent in values if they have the same

length and same sequence of values

98
New cards

Test if two arrays are equal

.equals

99
New cards

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

100
New cards

String

String myStr = “NEW STRING”;

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

The string class is immutable

String methods return new String objects