comp 202 midterm review

26
COMP 202 Midterm Review Instructors: Emily Sager, Valerie Saunders Duncan

Upload: mohand-khouider

Post on 03-Feb-2016

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 202 Midterm Review

COMP 202 Midterm ReviewInstructors: Emily Sager, Valerie Saunders Duncan

Page 2: COMP 202 Midterm Review

Basic Plan

~ 90 min -- Review of Concepts (focusing on the harder material) ~ 10 min -- break ~ 50 min -- Selected practice problems from practice midterms~ 30 min -- Questions

A few things to note: - These slides will be made available after the review- All example code will be made available after the review

Page 3: COMP 202 Midterm Review

Types

● The following are primitive types:

○ byte, short, long, float (not that important for this course) ○ int, boolean, char, double

● The following are reference types: ○ String, Array, Integer○ Objects are reference types!

■ e.g. a Country class with attributes name, cities

Page 4: COMP 202 Midterm Review

○ int → represents an integer number in memory ○ double → represents a decimal place number in memory

○ boolean → two values: True and False ■ All if statements must evaluate to a boolean value ■ Incorrect:

● if (x = 7) { some code } //x is being assigned value 7● if (y = true) {some code } //this is dangerous, y is being assigned

the value of true! so the statement always evaluates to true! ■ Correct:

● if (x == 7) { some code }

● if (y) { some code }

○ char → represent one character in memory■ e.g. ‘t’ with ASCII value 116 ■ can do comparison operations on chars (e.g. if (‘t’ < ‘y’) { some

code})

More On Primitive Types

Page 5: COMP 202 Midterm Review

○ Example: Which of the following lines will NOT compile?

Example on Casting

public static void main(String[] args){ int myInt = 5;

double myDouble = 5.0; fooDouble(myInt); // Line 1fooInt(myInt); // Line 2 fooDouble(myDouble); //Line 3fooInt(myDouble); //Line 4

}

public static void fooDouble(double x){

System.out.println(x);}

public static void fooInt(int x){

System.out.println(x);}

Page 6: COMP 202 Midterm Review

● Any method that is expecting a double will happily accept an int ● Any method that is expecting an int will NEVER accept a double

Why?

Casting continued

DOubeint

double

5

1 13.7

6.2

Page 7: COMP 202 Midterm Review

● Booleans are either True or False ● Even a statement involving integers ALWAYS evaluates to either true or

false.

Boolean Logic

public static void foo() {

boolean x = 5 < 10; }

Page 8: COMP 202 Midterm Review

Boolean Logic

Suppose m and n are both int variables with positive values. Which of the following boolean expressions are guaranteed to be equal in value to the expression that follows?

(m % n == 0 && n % m == 0)

• I. m == n• II. !(m < n || m > n) • III. !(m < n && m > n)

Page 9: COMP 202 Midterm Review

● Logical equivalency: For the same inputs, each should evaluate to the same boolean value.

● Strategy to solve : either pick values for m and n OR examine properties of the modulo operand.

● m % n == 0 → m is exactly divisible by n. ● n % m == 0 → n is exactly divisible by m. ● for m and n to be exactly divisible by each other they must be equal ● m == n

Boolean Logic

(m % n == 0 && n % m == 0)

• I. m == n• II. !(m < n || m > n) • III. !(m < n && m > n)

Page 10: COMP 202 Midterm Review

● Logical equivalency: For the same inputs, each should evaluate to the same boolean value.

● Strategy to solve : either pick values for m and n OR examine properties of the modulo operand.

● m % n == 0 → m is exactly divisible by n. ● n % m == 0 → n is exactly divisible by m. ● for m and n to be exactly divisible by each other they must be equal ● m == n ● Answer I and II

Boolean Logic

(m % n == 0 && n % m == 0)

• I. m == n• II. !(m < n || m > n) • III. !(m < n && m > n)

Page 11: COMP 202 Midterm Review

● An else statement can only correspond to ONE if statement, the else statement doesn’t know about any other code besides the if statement it corresponds with.

● Example:

Control Flow: If - Else

public static void main(String[] args)int x = -1; if (x < 0) {

System.out.print("-"); } if (x > 0) {

System.out.print("+"); } else {

System.out.print("0"); }

Page 12: COMP 202 Midterm Review

● Pictographically: ○ If- else: The world is split

into 2 options, and you areforced into one of them

If- else if-else : The world is split into 3

Control Flow: If - Else

x > 5 x <= 5

x > 5

0< x <= 5

x <= 0

Page 13: COMP 202 Midterm Review

● Nested if-else example:

Control Flow: If - Else

public static void main(String[] args) { int x = 0; int a = 5; int b = 5;if (a > 0) {

if (b < 0) { x = x + 5;

}else if (a > 5) {

x = x + 4; }else {

x = x + 3; }

else {x = x + 2;

}}

Page 14: COMP 202 Midterm Review

● Basic method structure: ○ visibility staticOrNot returnType name(parameters) { code } ○ public static String foo(int x){does something}

● Think of a method as a Black Box

Methods

does something give the method an int gives you back a String

Page 15: COMP 202 Midterm Review

For Loop

public static void main(String[] args){

for (int i =1; i<25; i*5){

for (int j = 0; j< 10; j++){

System.out.println("x"); }}

}

Page 16: COMP 202 Midterm Review

Loops and Search

● The exam is mostly going to ask you about the number of comparisons a search or sort algorithm does.

● Very important to understand when a loop or a method terminates

Page 17: COMP 202 Midterm Review

Loops and Search

public static int BinarySearchAlgorithm(int [] array, int minIndex, int maxIndex, int target)

{while (maxIndex>=minIndex){

int midpoint = (maxIndex+minIndex)/2;

if (array[midpoint] == target ){

return midpoint; }else if (array[midpoint] < target) //search the upper half of

the array{

minIndex = midpoint + 1; }else //search the lower half of the array{

maxIndex = midpoint - 1; }

}return -1;

}

Page 18: COMP 202 Midterm Review

● Suppose we have an array of size 16, and we are searching for an element that is NOT in the array

● How many iterations of the while loop would occur?

Loops and Binary Search

Page 19: COMP 202 Midterm Review

● Arrays are reference types!!! ●

Arrays

Page 20: COMP 202 Midterm Review

● Variables only exist inside their block of code { } ● What does the following code print?

Scope and Methods

public static void main(String [] args){

int x = 5; int y = 10; foo(x, y);foo(x, y);

}

private static int foo(int x, int y) {

x = x + 10; y = x; System.out.println(y);return y;

}

Page 21: COMP 202 Midterm Review

● Variables only exist inside their block of code { } ● This is even true for reference types! ● What does this code print?

Scope and Methods (Arrays)

public static void resize(int[] y) {

y = new int[y.length * 2];System.out.print(y.length + " ");

}

public static void main(String[] args) {

int[] x = {1,2};resize(x);System.out.println(x.length);

}

Page 22: COMP 202 Midterm Review

Scope and Methods

Xmain 1, 2

Xresize

After resize performs the following line of code x = new int[x.length * 2];

Xmain

Xresize

1, 2

Before any line of resize occurs, the variables look like this:

0, 0, 0, 0

Page 23: COMP 202 Midterm Review

● Variables must be declared in the scope they will be used. ● What (if anything) is wrong with the following code?

Scope and Loops

public static void foo() {

for (int i = 0; i < 10; i++){

int y = 7; }System.out.println(y);

}

Page 24: COMP 202 Midterm Review

● Variables must be declared in the scope they will be used. ● What (if anything) is wrong with the following code?

Scope and Loops

public static void foo() {

int y = 5; for (int i = 0; i < 10; i++){

int y = 7; }System.out.println(y);

}

Page 25: COMP 202 Midterm Review

● Variables must be declared in the scope they will be used. ● What (if anything) is wrong with the following code?

Scope and Loops

public static void foo() {

int y = 5; for (int i = 0; i < 10; i++){

int y = 7; }int y = 9; System.out.println(y);

}