0% found this document useful (0 votes)
240 views26 pages

COMP 202 Midterm Review

The document provides a summary of the COMP 202 midterm review session, which will include: 1. A 90 minute review of key concepts from the course, focusing on harder material. 2. A 10 minute break. 3. 50 minutes of selected practice problems from past midterms. 4. 30 minutes for questions. The slides and example code from the review will be made available after the session. Key topics to be covered include primitive vs reference types, casting, boolean logic, control flow, methods, loops, scope, and algorithms analysis. Past midterms will also be provided as study resources.

Uploaded by

Mohand Khouider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views26 pages

COMP 202 Midterm Review

The document provides a summary of the COMP 202 midterm review session, which will include: 1. A 90 minute review of key concepts from the course, focusing on harder material. 2. A 10 minute break. 3. 50 minutes of selected practice problems from past midterms. 4. 30 minutes for questions. The slides and example code from the review will be made available after the session. Key topics to be covered include primitive vs reference types, casting, boolean logic, control flow, methods, loops, scope, and algorithms analysis. Past midterms will also be provided as study resources.

Uploaded by

Mohand Khouider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

COMP 202 Midterm Review

Instructors: Emily Sager, Valerie Saunders Duncan

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

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

More On Primitive Types


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

Example on Casting
Example: Which of the following lines will NOT compile?
public static void main(String[] args)
{
int myInt = 5;
double myDouble = 5.0;
fooDouble(myInt); // Line 1
fooInt(myInt); // Line 2
fooDouble(myDouble); //Line 3
fooInt(myDouble); //Line 4
}
public static void fooDouble(double x)
{
System.out.println(x);
}
public static void fooInt(int x)
{
System.out.println(x);
}

Casting continued
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?
5
int
DOube

13.7

6.2
double

Boolean Logic
Booleans are either True or False
Even a statement involving integers ALWAYS evaluates to either true or
false.

public static void foo()


{
boolean x = 5 < 10;
}

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)

Boolean Logic
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

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

Boolean Logic
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
(m % n == 0 && n % m == 0)
I. m == n
II. !(m < n || m > n)
III. !(m < n && m > n)

Control Flow: If - Else


An else statement can only correspond to ONE if statement, the else
statement doesnt know about any other code besides the if statement it
corresponds with.
Example:
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");
}

Control Flow: If - Else


Pictographically:
If- else: The world is split
into 2 options, and you are
forced into one of them

x>5

x <= 5

0< x <= 5

If- else if-else :


The world is split into 3

x>5
x <= 0

Control Flow: If - Else


Nested if-else example:
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;
}
}

Methods
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

give the method an int


does something

gives you back a String

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");
}
}

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

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

Loops and Binary Search


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?

Arrays
Arrays are reference types!!!

Scope and Methods


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

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

Scope and Methods (Arrays)


Variables only exist inside their block of code { }
This is even true for reference types!
What does this code print?
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);
}

Scope and Methods

Before any line of resize occurs,


the variables look like this:

Xmain

1, 2

Xresize

After resize performs the following line of


code x = new int[x.length * 2];

Xmain

Xresize

1, 2
0, 0, 0, 0

Scope and Loops


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

public static void foo()


{
for (int i = 0; i < 10; i++)
{
int y = 7;
}
System.out.println(y);
}

Scope and Loops


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

public static void foo()


{
int y = 5;
for (int i = 0; i < 10; i++)
{
int y = 7;
}
System.out.println(y);
}

Scope and Loops


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

public static void foo()


{
int y = 5;
for (int i = 0; i < 10; i++)
{
int y = 7;
}
int y = 9;
System.out.println(y);
}

Old Midterms
http://www.cs.mcgill.ca/~cs202/2013-09/web/midterm/COMP-202--midtermexam--2011-03.pdf

You might also like